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
41 #include <sys/types.h>
47 ** some useful constants:
50 extern const std::string Whitespaces;
51 extern const std::string LineEndings;
59 bool has_prefix(const std::string& str, const std::string& prefix);
61 bool has_suffix(const std::string& str, const std::string& suffix);
65 ** tool functions(modifying):
68 std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces);
70 std::string chomp_mod(std::string& str, const std::string& what= LineEndings );
72 std::string to_lower_mod(std::string& str);
74 std::string to_upper_mod(std::string& str);
78 ** tool functions (not modifying):
81 std::string trim(const std::string& str, const std::string& charlist = Whitespaces);
83 std::string chomp(const std::string& str, const std::string& what= LineEndings );
85 std::string to_lower(const std::string& str);
87 std::string to_upper(const std::string& str);
90 std::string remove_suffix(const std::string& str, const std::string& suffix);
92 std::string remove_prefix(const std::string& str, const std::string& prefix);
102 const std::string& str,
105 char delimiter = '=');
109 const std::string& str,
110 std::list< std::string >& result,
111 const std::string& delimiter= "\n",
112 bool omit_empty= false,
113 const std::string& trim_list= std::string()
116 std::list< std::string > split_string(
117 const std::string& str,
118 const std::string& delimiter = "\n",
119 bool omit_empty= false,
120 const std::string& trim_list= std::string()
124 std::string join_string(
125 const std::list< std::string >& parts,
126 const std::string& delimiter = "\n"
135 std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false);
137 std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error);
141 ** "type conversions":
146 * convert a datatype @a T to a string via string stream.
148 * @param s the string which should be converted to @a T.
149 * @return the value of type T.
154 T string_to(const std::string& s)
156 std::istringstream istr(s);
160 } // eo string_to(const std::string&)
164 * convert a datatype @a T to a string via string stream.
166 * @param s the string which should be converted to @a T.
167 * @param result the resulting value of type @a T.
168 * @return @a true iff the internal string stream was EOF after the conversion.
170 * @attention: does not return if the conversion was successful. So check for empty strings before.
175 bool string_to(const std::string& s, T& result)
177 std::istringstream istr(s);
180 } // eo string_to(const std::string&)
184 * convert string in hexadecimal notation to a datatype @a T
185 * supports strings with and without "0x" notation, e.g. 0xff and FF are both valid
187 * @param s the hex string which should be converted to @a T.
188 * @return the value of type T.
193 T hex_string_to(const std::string& s)
195 std::istringstream istr(s);
197 istr >> std::hex >> result;
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 * @param result the resulting value of type @a T.
208 * @return @a true iff the internal string stream was EOF after the conversion.
210 * @attention: does not return if the conversion was successful. So check for empty strings before.
215 bool hex_string_to(const std::string& s, T& result)
217 std::istringstream istr(s);
218 istr >> std::hex >> result;
220 } // eo string_to(const std::string&)
224 * convert a string to another datatype @a T via string stream.
226 * @param v the value (of type @a T) which should be converted to a string.
227 * @return the resulting string.
232 std::string to_string(const T& v)
234 std::ostringstream ostr;
237 } // eo to_string(const T&)
240 } // eo namespace I2n
244 std::string to_lower(const std::string &src);
245 std::string to_upper(const std::string &src);
247 // compatibility: import lower/upper funcs from I2n:
254 UnitBase1000, // SI decimal, composed by multiples of 1000 (KB, MB, etc.)
255 UnitBase1024 // IEC binary, composed by multiples of 1024 (KiB, MiB, etc. )
259 ShortUnitFormat, // B, KB, MB, ...
260 LongUnitFormat // Byte, KByte, MByte, ...
263 std::string nice_unit_format(
265 const UnitFormat format = ShortUnitFormat,
266 const UnitBase base = UnitBase1024
269 std::string nice_unit_format(
271 const UnitFormat format = ShortUnitFormat,
272 const UnitBase base = UnitBase1024
275 bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
276 bool replace_all(std::string &base, const char *ist, const char *soll);
277 bool replace_all(std::string &base, const char *ist, const std::string *soll);
278 bool replace_all(std::string &base, const std::string &ist, const char *soll);
279 bool replace_all(std::string &base, const std::string &ist, const std::string &soll);
281 std::string iso_to_utf8(const std::string& isostring);
282 std::string utf8_to_iso(const std::string& utf8string);
283 std::string utf7imap_to_utf8(const std::string &utf7imapstring);
284 std::string utf8_to_utf7imap(const std::string &utf8string);
286 std::string strip_html_tags(const std::string &input);
287 std::string smart_html_entities(const std::string &input);
288 std::string html_entities(std::string str);
289 std::string html_entities_to_console(std::string str);
291 std::string sanitize_for_logging(const std::string &str, const char replace_with='?');
293 std::string escape(const std::string &s);
295 std::string descape(const std::string &s, int startpos, int &endpos);
296 inline std::string descape(const std::string &s)
299 return descape(s,0,endpos);
302 std::string escape_shellarg(const std::string &input);