2 * @brief collection of string tools (/ functions).
4 * contains a collection of miscellaneous functions for dealing with strings.
6 * some functions (like trim, lower case, upper case ...) are available in two versions:
7 * - a modifying one (suffix "Mod") which modifies the given string argument in place.
8 * - a non modifying one which take a constant string reference and returns a new string.
12 * (c) Copyright 2007-2008 by Intra2net AG
17 #ifndef __STRINGFUNC_HXX
18 #define __STRINGFUNC_HXX
24 #include <sys/types.h>
30 ** some useful constants:
33 extern const std::string Whitespaces;
34 extern const std::string LineEndings;
42 bool has_prefix(const std::string& str, const std::string& prefix);
44 bool has_suffix(const std::string& str, const std::string& suffix);
48 ** tool functions(modifying):
51 std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces);
53 std::string chomp_mod(std::string& str, const std::string& what= LineEndings );
55 std::string to_lower_mod(std::string& str);
57 std::string to_upper_mod(std::string& str);
61 ** tool functions (not modifying):
64 std::string trim(const std::string& str, const std::string& charlist = Whitespaces);
66 std::string chomp(const std::string& str, const std::string& what= LineEndings );
68 std::string to_lower(const std::string& str);
70 std::string to_upper(const std::string& str);
73 std::string remove_suffix(const std::string& str, const std::string& suffix);
75 std::string remove_prefix(const std::string& str, const std::string& prefix);
85 const std::string& str,
88 char delimiter = '=');
92 const std::string& str,
93 std::list< std::string >& result,
94 const std::string& delimiter= "\n",
95 bool omit_empty= false,
96 const std::string& trim_list= std::string()
99 std::list< std::string > split_string(
100 const std::string& str,
101 const std::string& delimiter = "\n",
102 bool omit_empty= false,
103 const std::string& trim_list= std::string()
107 std::string join_string(
108 const std::list< std::string >& parts,
109 const std::string& delimiter = "\n"
118 std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false);
120 std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error);
125 ** "type conversions":
130 * convert a datatype @a T to a string via string stream.
132 * @param s the string which should be converted to @a T.
133 * @return the value of type T.
138 T string_to(const std::string& s)
140 std::istringstream istr(s);
144 } // eo string_to(const std::string&)
148 * convert a datatype @a T to a string via string stream.
150 * @param s the string which should be converted to @a T.
151 * @param result the resulting value of type @a T.
152 * @return @a true iff the internal string stream was EOF after the conversion.
157 bool string_to(const std::string& s, T& result)
159 std::istringstream istr(s);
162 } // eo string_to(const std::string&)
166 * convert a string to another datatype @a T via string stream.
168 * @param v the value (of type @a T) which should be converted to a string.
169 * @return the resulting string.
174 std::string to_string(const T& v)
176 std::ostringstream ostr;
179 } // eo to_string(const T&)
182 } // eo namespace I2n
186 std::string to_lower(const std::string &src);
187 std::string to_upper(const std::string &src);
189 // compatibility: import lower/upper funcs from I2n:
196 UnitBase1000, // SI decimal, composed by multiples of 1000 (KB, MB, etc.)
197 UnitBase1024 // IEC binary, composed by multiples of 1024 (KiB, MiB, etc. )
201 ShortUnitFormat, // B, KB, MB, ...
202 LongUnitFormat // Byte, KByte, MByte, ...
205 std::string nice_unit_format(
207 const UnitFormat format = LongUnitFormat,
208 const UnitBase base = UnitBase1000
211 bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
212 bool replace_all(std::string &base, const char *ist, const char *soll);
213 bool replace_all(std::string &base, const char *ist, const std::string *soll);
214 bool replace_all(std::string &base, const std::string &ist, const char *soll);
215 bool replace_all(std::string &base, const std::string &ist, const std::string &soll);
217 std::string iso_to_utf8(const std::string& isostring);
218 std::string utf8_to_iso(const std::string& utf8string);
219 std::string utf7imap_to_utf8(const std::string &utf7imapstring);
220 std::string utf8_to_utf7imap(const std::string &utf8string);
222 std::string strip_html_tags(const std::string &input);
223 std::string smart_html_entities(const std::string &input);
224 std::string html_entities(std::string str);
226 std::string escape(const std::string &s);
228 std::string descape(const std::string &s, int startpos, int &endpos);
229 inline std::string descape(const std::string &s)
232 return descape(s,0,endpos);
235 std::string escape_shellarg(const std::string &input);