2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 * @brief collection of string tools (/ functions).
23 * contains a collection of miscellaneous functions for dealing with strings.
25 * some functions (like trim, lower case, upper case ...) are available in two versions:
26 * - a modifying one (suffix "Mod") which modifies the given string argument in place.
27 * - a non modifying one which take a constant string reference and returns a new string.
31 * (c) Copyright 2007-2008 by Intra2net AG
34 #ifndef __STRINGFUNC_HXX
35 #define __STRINGFUNC_HXX
42 #include <sys/types.h>
48 ** some useful constants:
51 extern const std::string Whitespaces;
52 extern const std::string LineEndings;
60 bool has_prefix(const std::string& str, const std::string& prefix);
62 bool has_suffix(const std::string& str, const std::string& suffix);
66 ** tool functions(modifying):
69 std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces);
71 std::string chomp_mod(std::string& str, const std::string& what= LineEndings );
73 std::string to_lower_mod(std::string& str);
75 std::string to_upper_mod(std::string& str);
79 ** tool functions (not modifying):
82 std::string trim(const std::string& str, const std::string& charlist = Whitespaces);
84 std::string chomp(const std::string& str, const std::string& what= LineEndings );
86 std::string to_lower(const std::string& str);
88 std::string to_upper(const std::string& str);
91 std::string remove_suffix(const std::string& str, const std::string& suffix);
93 std::string remove_prefix(const std::string& str, const std::string& prefix);
103 const std::string& str,
106 char delimiter = '=');
110 const std::string& str,
111 std::list< std::string >& result,
112 const std::string& delimiter= "\n",
113 bool omit_empty= false,
114 const std::string& trim_list= std::string()
118 const std::string& str,
119 std::vector< std::string >& result,
120 const std::string& delimiter= "\n",
121 bool omit_empty= false,
122 const std::string& trim_list= std::string()
125 std::list< std::string > split_string(
126 const std::string& str,
127 const std::string& delimiter = "\n",
128 bool omit_empty= false,
129 const std::string& trim_list= std::string()
133 std::string join_string(
134 const std::list< std::string >& parts,
135 const std::string& delimiter = "\n"
138 std::string join_string(
139 const std::vector< std::string >& parts,
140 const std::string& delimiter = "\n"
149 std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false);
151 std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error);
155 ** "type conversions":
160 * convert a datatype @a T to a string via string stream.
162 * This will not report trouble in conversion; for example:
163 * string_to<int>("christian")
164 * will return 0 and not throw an error.
165 * Use boost::lexical_cast<T>(string) to get error-checked results.
167 * @param s the string which should be converted to @a T.
168 * @return the value of type T.
173 T string_to(const std::string& s)
175 std::istringstream istr(s);
179 } // eo string_to(const std::string&)
183 * convert a datatype @a T to a string via string stream.
185 * @param s the string which should be converted to @a T.
186 * @param result the resulting value of type @a T.
187 * @return @a true iff the internal string stream was EOF after the conversion.
189 * @attention: does not return if the conversion was successful. So check for empty strings before.
194 bool string_to(const std::string& s, T& result)
196 std::istringstream istr(s);
199 } // eo string_to(const std::string&)
203 * convert string in hexadecimal notation to a datatype @a T
204 * supports strings with and without "0x" notation, e.g. 0xff and FF are both valid
206 * @param s the hex string which should be converted to @a T.
207 * @return the value of type T.
212 T hex_string_to(const std::string& s)
214 std::istringstream istr(s);
216 istr >> std::hex >> result;
218 } // eo string_to(const std::string&)
222 * convert string in hexadecimal notation to a datatype @a T
223 * supports strings with and without "0x" notation, e.g. 0xff and FF are both valid
225 * @param s the hex string which should be converted to @a T.
226 * @param result the resulting value of type @a T.
227 * @return @a true iff the internal string stream was EOF after the conversion.
229 * @attention: does not return if the conversion was successful. So check for empty strings before.
234 bool hex_string_to(const std::string& s, T& result)
236 std::istringstream istr(s);
237 istr >> std::hex >> result;
239 } // eo string_to(const std::string&)
243 * convert a string to another datatype @a T via string stream.
245 * @param v the value (of type @a T) which should be converted to a string.
246 * @return the resulting string.
251 std::string to_string(const T& v)
253 std::ostringstream ostr;
256 } // eo to_string(const T&)
260 * Create a string with types shortened in texts describing C++ types
262 * for example: std::list<some_long_type, std::allocator<some_long_type> >
263 * --> std::list<some_long_type, _alloc_>
265 * and std::basic_string<char, std::char_traits<char>, std::allocator<char> >
268 std::string shorten_stl_types(const std::string &input);
270 std::string base64_encode(const std::string &input, bool one_line=true);
271 std::string base64_decode(const std::string &input, bool one_line=true);
273 } // eo namespace I2n
277 std::string to_lower(const std::string &src);
278 std::string to_upper(const std::string &src);
280 // compatibility: import lower/upper funcs from I2n:
287 UnitBase1000, // SI decimal, composed by multiples of 1000 (KB, MB, etc.)
288 UnitBase1024 // IEC binary, composed by multiples of 1024 (KiB, MiB, etc. )
292 ShortUnitFormat, // B, KB, MB, ...
293 LongUnitFormat // Byte, KByte, MByte, ...
296 std::string nice_unit_format(
298 const UnitFormat format = ShortUnitFormat,
299 const UnitBase base = UnitBase1024
302 std::string nice_unit_format(
304 const UnitFormat format = ShortUnitFormat,
305 const UnitBase base = UnitBase1024
308 bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
309 bool replace_all(std::string &base, const char *ist, const char *soll);
310 bool replace_all(std::string &base, const char *ist, const std::string *soll);
311 bool replace_all(std::string &base, const std::string &ist, const char *soll);
312 bool replace_all(std::string &base, const std::string &ist, const std::string &soll);
314 std::string iso_to_utf8(const std::string& isostring);
315 std::string utf8_to_iso(const std::string& utf8string);
316 std::string utf7imap_to_utf8(const std::string &utf7imapstring);
317 std::string utf8_to_utf7imap(const std::string &utf8string);
319 std::string strip_html_tags(const std::string &input);
320 std::string smart_html_entities(const std::string &input);
321 std::string html_entities(std::string str);
322 std::string html_entities_to_console(std::string str);
324 typedef std::pair<std::string::size_type, std::string::size_type> CommentZone;
325 std::vector<CommentZone> find_html_comments(const std::string &str);
326 void remove_html_comments(std::string &str);
327 void remove_html_comments(std::string &str, const std::vector<CommentZone> &comments);
329 std::string sanitize_for_logging(const std::string &str, const char replace_with='?');
331 std::string escape(const std::string &s);
333 std::string descape(const std::string &s, int startpos, int &endpos);
334 inline std::string descape(const std::string &s)
337 return descape(s,0,endpos);
340 std::string escape_shellarg(const std::string &input);