libi2ncommon: (reinhard) modified stringfunc (and some others) according to our new...
[libi2ncommon] / src / stringfunc.hxx
1 /** @file
2  * @brief collection of string tools (/ functions).
3  *
4  * contains a collection of miscellaneous functions for dealing with strings.
5  *
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.
9  * .
10  *
11  *
12  * (c) Copyright 2007-2008 by Intra2net AG
13  *
14  * info@intra2net.com
15  */
16
17 #ifndef __STRINGFUNC_HXX
18 #define __STRINGFUNC_HXX
19
20 #include <list>
21 #include <string>
22 #include <sstream>
23 #include <stdexcept>
24
25 namespace I2n
26 {
27
28 /*
29 ** some useful constants:
30 */
31
32 extern const std::string Whitespaces;
33 extern const std::string LineEndings;
34
35
36 /*
37 ** predicates:
38 */
39
40
41 bool has_prefix(const std::string& str, const std::string& prefix);
42
43 bool has_suffix(const std::string& str, const std::string& suffix);
44
45
46 /*
47 ** tool functions(modifying):
48 */
49
50 std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces);
51
52 std::string chomp_mod(std::string& str, const std::string& what= LineEndings );
53
54 std::string to_lower_mod(std::string& str);
55
56 std::string to_upper_mod(std::string& str);
57
58
59 /*
60 ** tool functions (not modifying):
61 */
62
63 std::string trim(const std::string& str, const std::string& charlist = Whitespaces);
64
65 std::string chomp(const std::string& str, const std::string& what= LineEndings );
66
67 std::string to_lower(const std::string& str);
68
69 std::string to_upper(const std::string& str);
70
71
72 std::string remove_suffix(const std::string& str, const std::string& suffix);
73
74 std::string remove_prefix(const std::string& str, const std::string& prefix);
75
76
77
78 /*
79 ** split and join:
80 */
81
82
83 bool pair_split(
84    const std::string& str,
85    std::string& key,
86    std::string& value,
87    char delimiter = '=');
88
89
90 void split_string(
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()
96 );
97
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()
103 );
104
105
106 std::string join_string(
107    const std::list< std::string >& parts,
108    const std::string& delimiter = "\n"
109 );
110
111
112 /*
113 ** conversions:
114 */
115
116
117 std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false);
118
119 std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error);
120
121
122
123 /*
124 ** "type conversions":
125 */
126
127
128 /**
129  * convert a datatype @a T to a string via string stream.
130  *
131  * @param s the string which should be converted to @a T.
132  * @return the value of type T.
133  */
134 template<
135 class T
136 >
137 T string_to(const std::string& s)
138 {
139    std::istringstream istr(s);
140    T result;
141    istr >> result;
142    return result;
143 } // eo string_to(const std::string&)
144
145
146 /**
147  * convert a datatype @a T to a string via string stream.
148  *
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.
152  */
153 template<
154 class T
155 >
156 bool string_to(const std::string& s, T& result)
157 {
158    std::istringstream istr(s);
159    istr >> result;
160    return istr.eof();
161 } // eo string_to(const std::string&)
162
163
164 /**
165  * convert a string to another datatype @a T via string stream.
166  *
167  * @param v the value (of type @a T) which should be converted to a string.
168  * @return the resulting string.
169  */
170 template<
171 class T
172 >
173 std::string to_string(const T& v)
174 {
175    std::ostringstream ostr;
176    ostr << v;
177    return ostr.str();
178 } // eo to_string(const T&)
179
180
181 } // eo namespace I2n
182
183
184
185 std::string to_lower(const std::string &src);
186 std::string to_upper(const std::string &src);
187
188 std::string nice_unit_format(int input);
189
190 bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
191 bool replace_all(std::string &base, const char *ist, const char *soll);
192 bool replace_all(std::string &base, const char *ist, const std::string *soll);
193 bool replace_all(std::string &base, const std::string &ist, const char *soll);
194 bool replace_all(std::string &base, const std::string &ist, const std::string &soll);
195
196 std::string iso_to_utf8(const std::string& isostring);
197 std::string utf8_to_iso(const std::string& utf8string);
198 std::string utf7imap_to_utf8(const std::string &utf7imapstring);
199 std::string utf8_to_utf7imap(const std::string &utf8string);
200
201 std::string strip_html_tags(const std::string &input);
202 std::string smart_html_entities(const std::string &input);
203 std::string html_entities(std::string str);
204
205 std::string escape(const std::string &s);
206
207 std::string descape(const std::string &s, int startpos, int &endpos);
208 inline std::string descape(const std::string &s)
209 {
210    int endpos;
211    return descape(s,0,endpos);
212 }
213
214 std::string escape_shellarg(const std::string &input);
215
216 #endif