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
45 #include <sys/types.h>
51 ** some useful constants:
54 extern const std::string Whitespaces;
55 extern const std::string LineEndings;
63 bool has_prefix(const std::string& str, const std::string& prefix);
65 bool has_suffix(const std::string& str, const std::string& suffix);
69 ** tool functions(modifying):
72 std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces);
74 std::string chomp_mod(std::string& str, const std::string& what= LineEndings );
76 std::string to_lower_mod(std::string& str);
78 std::string to_upper_mod(std::string& str);
82 ** tool functions (not modifying):
85 std::string trim(const std::string& str, const std::string& charlist = Whitespaces);
87 std::string chomp(const std::string& str, const std::string& what= LineEndings );
89 std::string to_lower(const std::string& str);
91 std::string to_upper(const std::string& str);
94 std::string remove_suffix(const std::string& str, const std::string& suffix);
96 std::string remove_prefix(const std::string& str, const std::string& prefix);
106 const std::string& str,
109 char delimiter = '=');
113 const std::string& str,
114 std::list< std::string >& result,
115 const std::string& delimiter= "\n",
116 bool omit_empty= false,
117 const std::string& trim_list= std::string()
121 const std::string& str,
122 std::vector< std::string >& result,
123 const std::string& delimiter= "\n",
124 bool omit_empty= false,
125 const std::string& trim_list= std::string()
128 std::list< std::string > split_string(
129 const std::string& str,
130 const std::string& delimiter = "\n",
131 bool omit_empty= false,
132 const std::string& trim_list= std::string()
135 struct concatenator {
138 concatenator (const std::string &delim) : delim (delim) { }
140 inline std::string operator() (const std::string &acc, const std::string &elt) const
141 { return acc + delim + elt; }
144 template<typename Iter>
149 const std::string &delimiter = "\n"
152 if (first == last) { return ""; }
154 const std::string &init = *first++;
155 if (first == last) { return init; }
157 return std::accumulate (first, last, init, concatenator (delimiter));
161 * @brief joins a container of strings into a single string.
163 * This funtion is (basically) the reverse operation of @a split_string.
165 * @param parts the container of strings.
166 * @param delimiter the delimiter to insert between the strings.
167 * @return the joined string.
169 template<typename Cont>
170 inline std::string join_string(
172 const std::string& delimiter = "\n"
174 { return join_string (parts.begin (), parts.end (), delimiter); }
176 std::string join_string(
177 const char *const parts [],
178 const std::string& delimiter = "\n"
182 * provide instantiation wrappers for ABI compatibility
184 std::string join_string (const std::list<std::string> &l,
185 const std::string &d = "\n");
187 std::string join_string (const std::vector<std::string> &l,
188 const std::string &d = "\n");
195 std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false);
197 std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error);
201 ** "type conversions":
206 * convert a datatype @a T to a string via string stream.
208 * This will not report trouble in conversion; for example:
209 * string_to<int>("christian")
210 * will return 0 and not throw an error.
211 * Use boost::lexical_cast<T>(string) to get error-checked results.
213 * @param s the string which should be converted to @a T.
214 * @return the value of type T.
219 T string_to(const std::string& s)
221 std::istringstream istr(s);
225 } // eo string_to(const std::string&)
229 * convert a datatype @a T to a string via string stream.
231 * @param s the string which should be converted to @a T.
232 * @param result the resulting value of type @a T.
233 * @return @a true iff the internal string stream was EOF after the conversion.
235 * @attention: does not return if the conversion was successful. So check for empty strings before.
240 bool string_to(const std::string& s, T& result)
242 std::istringstream istr(s);
245 } // eo string_to(const std::string&)
249 * convert string in hexadecimal notation to a datatype @a T
250 * supports strings with and without "0x" notation, e.g. 0xff and FF are both valid
252 * @param s the hex string which should be converted to @a T.
253 * @return the value of type T.
258 T hex_string_to(const std::string& s)
260 std::istringstream istr(s);
262 istr >> std::hex >> result;
264 } // eo string_to(const std::string&)
268 * convert string in hexadecimal notation to a datatype @a T
269 * supports strings with and without "0x" notation, e.g. 0xff and FF are both valid
271 * @param s the hex string which should be converted to @a T.
272 * @param result the resulting value of type @a T.
273 * @return @a true iff the internal string stream was EOF after the conversion.
275 * @attention: does not return if the conversion was successful. So check for empty strings before.
280 bool hex_string_to(const std::string& s, T& result)
282 std::istringstream istr(s);
283 istr >> std::hex >> result;
285 } // eo string_to(const std::string&)
289 * convert a string to another datatype @a T via string stream.
291 * @param v the value (of type @a T) which should be converted to a string.
292 * @return the resulting string.
297 std::string to_string(const T& v)
299 std::ostringstream ostr;
302 } // eo to_string(const T&)
306 * Create a string with types shortened in texts describing C++ types
308 * for example: std::list<some_long_type, std::allocator<some_long_type> >
309 * --> std::list<some_long_type, _alloc_>
311 * and std::basic_string<char, std::char_traits<char>, std::allocator<char> >
314 std::string shorten_stl_types(const std::string &input);
316 std::string base64_encode(const std::string &input, bool one_line=true);
317 std::string base64_decode(const std::string &input, bool one_line=true);
319 } // eo namespace I2n
323 std::string to_lower(const std::string &src);
324 std::string to_upper(const std::string &src);
326 // compatibility: import lower/upper funcs from I2n:
333 UnitBase1000, // SI decimal, composed by multiples of 1000 (KB, MB, etc.)
334 UnitBase1024 // IEC binary, composed by multiples of 1024 (KiB, MiB, etc. )
338 ShortUnitFormat, // B, KB, MB, ...
339 LongUnitFormat // Byte, KByte, MByte, ...
342 std::string nice_unit_format(
344 const UnitFormat format = ShortUnitFormat,
345 const UnitBase base = UnitBase1024
348 std::string nice_unit_format(
350 const UnitFormat format = ShortUnitFormat,
351 const UnitBase base = UnitBase1024
354 bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
355 bool replace_all(std::string &base, const char *ist, const char *soll);
356 bool replace_all(std::string &base, const char *ist, const std::string *soll);
357 bool replace_all(std::string &base, const std::string &ist, const char *soll);
358 bool replace_all(std::string &base, const std::string &ist, const std::string &soll);
360 std::string iso_to_utf8(const std::string& isostring);
361 std::string utf8_to_iso(const std::string& utf8string);
362 std::string utf7imap_to_utf8(const std::string &utf7imapstring);
363 std::string utf8_to_utf7imap(const std::string &utf8string);
365 std::string strip_html_tags(const std::string &input);
366 std::string smart_html_entities(const std::string &input);
367 std::string html_entities(std::string str);
368 std::string html_entities_to_console(std::string str);
370 typedef std::pair<std::string::size_type, std::string::size_type> CommentZone;
371 std::vector<CommentZone> find_html_comments(const std::string &str);
372 void remove_html_comments(std::string &str);
373 void remove_html_comments(std::string &str, const std::vector<CommentZone> &comments);
375 std::string sanitize_for_logging(const std::string &str, const char replace_with='?');
377 std::string escape(const std::string &s);
379 std::string descape(const std::string &s, int startpos, int &endpos);
380 inline std::string descape(const std::string &s)
383 return descape(s,0,endpos);
386 std::string escape_shellarg(const std::string &input);