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
29 ** some useful constants:
32 extern const std::string Whitespaces;
33 extern const std::string LineEndings;
41 bool has_prefix(const std::string& str, const std::string& prefix);
43 bool has_suffix(const std::string& str, const std::string& suffix);
47 ** tool functions(modifying):
50 std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces);
52 std::string chomp_mod(std::string& str, const std::string& what= LineEndings );
54 std::string to_lower_mod(std::string& str);
56 std::string to_upper_mod(std::string& str);
60 ** tool functions (not modifying):
63 std::string trim(const std::string& str, const std::string& charlist = Whitespaces);
65 std::string chomp(const std::string& str, const std::string& what= LineEndings );
67 std::string to_lower(const std::string& str);
69 std::string to_upper(const std::string& str);
72 std::string remove_suffix(const std::string& str, const std::string& suffix);
74 std::string remove_prefix(const std::string& str, const std::string& prefix);
84 const std::string& str,
87 char delimiter = '=');
91 const std::string& str,
92 std::list< std::string >& result,
93 const std::string& delimiter= "\n",
94 bool omit_empty= false,
95 const std::string& trim_list= std::string()
98 std::list< std::string > split_string(
99 const std::string& str,
100 const std::string& delimiter = "\n",
101 bool omit_empty= false,
102 const std::string& trim_list= std::string()
106 std::string join_string(
107 const std::list< std::string >& parts,
108 const std::string& delimiter = "\n"
117 std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false);
119 std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error);
124 ** "type conversions":
129 * convert a datatype @a T to a string via string stream.
131 * @param s the string which should be converted to @a T.
132 * @return the value of type T.
137 T string_to(const std::string& s)
139 std::istringstream istr(s);
143 } // eo string_to(const std::string&)
147 * convert a datatype @a T to a string via string stream.
149 * @param s the string which should be converted to @a T.
150 * @param result the resulting value of type @a T.
151 * @return @a true iff the internal string stream was EOF after the conversion.
156 bool string_to(const std::string& s, T& result)
158 std::istringstream istr(s);
161 } // eo string_to(const std::string&)
165 * convert a string to another datatype @a T via string stream.
167 * @param v the value (of type @a T) which should be converted to a string.
168 * @return the resulting string.
173 std::string to_string(const T& v)
175 std::ostringstream ostr;
178 } // eo to_string(const T&)
181 } // eo namespace I2n
185 std::string to_lower(const std::string &src);
186 std::string to_upper(const std::string &src);
188 // compatibility: import lower/upper funcs from I2n:
193 std::string nice_unit_format(long long input);
195 bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
196 bool replace_all(std::string &base, const char *ist, const char *soll);
197 bool replace_all(std::string &base, const char *ist, const std::string *soll);
198 bool replace_all(std::string &base, const std::string &ist, const char *soll);
199 bool replace_all(std::string &base, const std::string &ist, const std::string &soll);
201 std::string iso_to_utf8(const std::string& isostring);
202 std::string utf8_to_iso(const std::string& utf8string);
203 std::string utf7imap_to_utf8(const std::string &utf7imapstring);
204 std::string utf8_to_utf7imap(const std::string &utf8string);
206 std::string strip_html_tags(const std::string &input);
207 std::string smart_html_entities(const std::string &input);
208 std::string html_entities(std::string str);
210 std::string escape(const std::string &s);
212 std::string descape(const std::string &s, int startpos, int &endpos);
213 inline std::string descape(const std::string &s)
216 return descape(s,0,endpos);
219 std::string escape_shellarg(const std::string &input);