author | Michael Krelin <hacker@klever.net> | 2008-02-02 10:50:48 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-02-02 10:50:48 (UTC) |
commit | b7ce9a84f0775eb24f0a27d3816bf57b774a2927 (patch) (unidiff) | |
tree | 77d3bf4830e9d45824bc466c7e1b5279f3ff1111 | |
parent | f29ad6501686e5f43b536258b86d12683c711f02 (diff) | |
download | libopkele-b7ce9a84f0775eb24f0a27d3816bf57b774a2927.zip libopkele-b7ce9a84f0775eb24f0a27d3816bf57b774a2927.tar.gz libopkele-b7ce9a84f0775eb24f0a27d3816bf57b774a2927.tar.bz2 |
added util::attr_escape
escaping string for inclusion into x(ht)ml attributes
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | include/opkele/util.h | 7 | ||||
-rw-r--r-- | lib/util.cc | 19 |
2 files changed, 26 insertions, 0 deletions
diff --git a/include/opkele/util.h b/include/opkele/util.h index 6f3ddf6..719f951 100644 --- a/include/opkele/util.h +++ b/include/opkele/util.h | |||
@@ -102,2 +102,9 @@ namespace opkele { | |||
102 | /** | 102 | /** |
103 | * Make string suitable for using as x(ht)ml attribute. | ||
104 | * @param str string to escape | ||
105 | * @return escaped string | ||
106 | */ | ||
107 | string attr_escape(const string& str); | ||
108 | |||
109 | /** | ||
103 | * Convert number to string | 110 | * Convert number to string |
diff --git a/lib/util.cc b/lib/util.cc index 54d6535..b7bc437 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -163,2 +163,21 @@ namespace opkele { | |||
163 | 163 | ||
164 | string attr_escape(const string& str) { | ||
165 | static const char *unsafechars = "<>&\n\"'"; | ||
166 | string rv; | ||
167 | string::size_type p=0; | ||
168 | while(true) { | ||
169 | string::size_type us = str.find_first_of(unsafechars,p); | ||
170 | if(us==string::npos) { | ||
171 | if(p!=str.length()) | ||
172 | rv.append(str,p,str.length()-p); | ||
173 | return rv; | ||
174 | } | ||
175 | rv.append(str,p,us-p); | ||
176 | rv += "&#"; | ||
177 | rv += long_to_string((long)str[us]); | ||
178 | rv += ';'; | ||
179 | p = us+1; | ||
180 | } | ||
181 | } | ||
182 | |||
164 | string long_to_string(long l) { | 183 | string long_to_string(long l) { |