overload join_string() for std::set
[libi2ncommon] / src / stringfunc.hxx
CommitLineData
0e23f538
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
6a93d84a
TJ
20/** @file
21 * @brief collection of string tools (/ functions).
22 *
23 * contains a collection of miscellaneous functions for dealing with strings.
24 *
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.
28 * .
29 *
30 *
31 * (c) Copyright 2007-2008 by Intra2net AG
6a93d84a 32 */
e93545dd
GE
33
34#ifndef __STRINGFUNC_HXX
35#define __STRINGFUNC_HXX
36
6a93d84a 37#include <list>
0aaf13b5 38#include <set>
376ec4fa 39#include <vector>
e93545dd 40#include <string>
6a93d84a
TJ
41#include <sstream>
42#include <stdexcept>
b04b4f7e 43#include <sys/types.h>
6a93d84a 44
6ab3bc95
RP
45namespace I2n
46{
6a93d84a
TJ
47
48/*
49** some useful constants:
50*/
51
6ab3bc95
RP
52extern const std::string Whitespaces;
53extern const std::string LineEndings;
6a93d84a
TJ
54
55
56/*
57** predicates:
58*/
59
60
6ab3bc95 61bool has_prefix(const std::string& str, const std::string& prefix);
6a93d84a 62
6ab3bc95 63bool has_suffix(const std::string& str, const std::string& suffix);
6a93d84a
TJ
64
65
66/*
6ab3bc95 67** tool functions(modifying):
6a93d84a
TJ
68*/
69
6ab3bc95 70std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces);
6a93d84a 71
6ab3bc95 72std::string chomp_mod(std::string& str, const std::string& what= LineEndings );
6a93d84a 73
6ab3bc95 74std::string to_lower_mod(std::string& str);
6a93d84a 75
6ab3bc95 76std::string to_upper_mod(std::string& str);
6a93d84a
TJ
77
78
79/*
80** tool functions (not modifying):
81*/
82
6ab3bc95 83std::string trim(const std::string& str, const std::string& charlist = Whitespaces);
6a93d84a 84
6ab3bc95 85std::string chomp(const std::string& str, const std::string& what= LineEndings );
6a93d84a 86
6ab3bc95 87std::string to_lower(const std::string& str);
6a93d84a 88
6ab3bc95 89std::string to_upper(const std::string& str);
6a93d84a
TJ
90
91
6ab3bc95 92std::string remove_suffix(const std::string& str, const std::string& suffix);
6a93d84a 93
6ab3bc95 94std::string remove_prefix(const std::string& str, const std::string& prefix);
6a93d84a
TJ
95
96
97
98/*
99** split and join:
100*/
101
102
6ab3bc95
RP
103bool pair_split(
104 const std::string& str,
105 std::string& key,
106 std::string& value,
107 char delimiter = '=');
6a93d84a
TJ
108
109
6ab3bc95
RP
110void split_string(
111 const std::string& str,
112 std::list< std::string >& result,
113 const std::string& delimiter= "\n",
114 bool omit_empty= false,
115 const std::string& trim_list= std::string()
6a93d84a
TJ
116);
117
338da253
CH
118void split_string(
119 const std::string& str,
120 std::vector< std::string >& result,
121 const std::string& delimiter= "\n",
122 bool omit_empty= false,
123 const std::string& trim_list= std::string()
124);
125
6ab3bc95
RP
126std::list< std::string > split_string(
127 const std::string& str,
128 const std::string& delimiter = "\n",
129 bool omit_empty= false,
130 const std::string& trim_list= std::string()
6a93d84a
TJ
131);
132
133
6ab3bc95
RP
134std::string join_string(
135 const std::list< std::string >& parts,
136 const std::string& delimiter = "\n"
6a93d84a
TJ
137);
138
376ec4fa
CH
139std::string join_string(
140 const std::vector< std::string >& parts,
141 const std::string& delimiter = "\n"
142);
143
4f7a7b9f 144std::string join_string(
0aaf13b5
PG
145 const std::set< std::string >& parts,
146 const std::string& delimiter = "\n"
147);
148
149std::string join_string(
4f7a7b9f
PG
150 const char *const parts [],
151 const std::string& delimiter = "\n"
152);
153
6a93d84a
TJ
154
155/*
156** conversions:
157*/
158
159
6ab3bc95 160std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false);
6a93d84a 161
6ab3bc95 162std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error);
6a93d84a
TJ
163
164
6a93d84a
TJ
165/*
166** "type conversions":
167*/
168
169
170/**
171 * convert a datatype @a T to a string via string stream.
172 *
1fe2e899
CH
173 * This will not report trouble in conversion; for example:
174 * string_to<int>("christian")
175 * will return 0 and not throw an error.
176 * Use boost::lexical_cast<T>(string) to get error-checked results.
177 *
6a93d84a
TJ
178 * @param s the string which should be converted to @a T.
179 * @return the value of type T.
180 */
181template<
6ab3bc95 182class T
6a93d84a 183>
6ab3bc95 184T string_to(const std::string& s)
6a93d84a 185{
6ab3bc95
RP
186 std::istringstream istr(s);
187 T result;
188 istr >> result;
189 return result;
190} // eo string_to(const std::string&)
6a93d84a
TJ
191
192
193/**
194 * convert a datatype @a T to a string via string stream.
195 *
196 * @param s the string which should be converted to @a T.
197 * @param result the resulting value of type @a T.
198 * @return @a true iff the internal string stream was EOF after the conversion.
08f2d184
GE
199 *
200 * @attention: does not return if the conversion was successful. So check for empty strings before.
6a93d84a
TJ
201 */
202template<
6ab3bc95 203class T
6a93d84a 204>
6ab3bc95 205bool string_to(const std::string& s, T& result)
6a93d84a 206{
6ab3bc95
RP
207 std::istringstream istr(s);
208 istr >> result;
209 return istr.eof();
210} // eo string_to(const std::string&)
6a93d84a
TJ
211
212
213/**
3bcc713f
GE
214 * convert string in hexadecimal notation to a datatype @a T
215 * supports strings with and without "0x" notation, e.g. 0xff and FF are both valid
216 *
217 * @param s the hex string which should be converted to @a T.
218 * @return the value of type T.
219 */
220template<
221class T
222>
223T hex_string_to(const std::string& s)
224{
225 std::istringstream istr(s);
226 T result;
227 istr >> std::hex >> result;
228 return result;
229} // eo string_to(const std::string&)
230
231
232/**
233 * convert string in hexadecimal notation to a datatype @a T
234 * supports strings with and without "0x" notation, e.g. 0xff and FF are both valid
235 *
236 * @param s the hex string which should be converted to @a T.
237 * @param result the resulting value of type @a T.
238 * @return @a true iff the internal string stream was EOF after the conversion.
239 *
240 * @attention: does not return if the conversion was successful. So check for empty strings before.
241 */
242template<
243class T
244>
245bool hex_string_to(const std::string& s, T& result)
246{
247 std::istringstream istr(s);
248 istr >> std::hex >> result;
249 return istr.eof();
250} // eo string_to(const std::string&)
251
252
253/**
6a93d84a
TJ
254 * convert a string to another datatype @a T via string stream.
255 *
256 * @param v the value (of type @a T) which should be converted to a string.
257 * @return the resulting string.
258 */
259template<
6ab3bc95 260class T
6a93d84a 261>
6ab3bc95 262std::string to_string(const T& v)
6a93d84a 263{
6ab3bc95
RP
264 std::ostringstream ostr;
265 ostr << v;
266 return ostr.str();
267} // eo to_string(const T&)
268
269
1a0267e5
CH
270/**
271 * Create a string with types shortened in texts describing C++ types
272 *
273 * for example: std::list<some_long_type, std::allocator<some_long_type> >
274 * --> std::list<some_long_type, _alloc_>
275 *
276 * and std::basic_string<char, std::char_traits<char>, std::allocator<char> >
277 * --> std::string
278 */
279std::string shorten_stl_types(const std::string &input);
280
1ebab1e3
TJ
281std::string base64_encode(const std::string &input, bool one_line=true);
282std::string base64_decode(const std::string &input, bool one_line=true);
1a0267e5 283
6ab3bc95
RP
284} // eo namespace I2n
285
6a93d84a 286
e5b21dbb 287#if 0
6ab3bc95
RP
288std::string to_lower(const std::string &src);
289std::string to_upper(const std::string &src);
e5b21dbb
RP
290#else
291// compatibility: import lower/upper funcs from I2n:
292using I2n::to_lower;
293using I2n::to_upper;
294#endif
e93545dd 295
81267544 296
8f296924
GMF
297enum UnitBase {
298 UnitBase1000, // SI decimal, composed by multiples of 1000 (KB, MB, etc.)
299 UnitBase1024 // IEC binary, composed by multiples of 1024 (KiB, MiB, etc. )
81267544
GMF
300};
301
8f296924
GMF
302enum UnitFormat {
303 ShortUnitFormat, // B, KB, MB, ...
304 LongUnitFormat // Byte, KByte, MByte, ...
d1ea9075
GMF
305};
306
81267544
GMF
307std::string nice_unit_format(
308 const int64_t input,
ee037ee3
GMF
309 const UnitFormat format = ShortUnitFormat,
310 const UnitBase base = UnitBase1024
81267544 311);
e93545dd 312
5cd64148
CH
313std::string nice_unit_format(
314 const double input,
315 const UnitFormat format = ShortUnitFormat,
316 const UnitBase base = UnitBase1024
317);
318
e93545dd
GE
319bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
320bool replace_all(std::string &base, const char *ist, const char *soll);
321bool replace_all(std::string &base, const char *ist, const std::string *soll);
322bool replace_all(std::string &base, const std::string &ist, const char *soll);
323bool replace_all(std::string &base, const std::string &ist, const std::string &soll);
324
325std::string iso_to_utf8(const std::string& isostring);
326std::string utf8_to_iso(const std::string& utf8string);
13cc4db1 327std::string utf7imap_to_utf8(const std::string &utf7imapstring);
6a2b6dd1 328std::string utf8_to_utf7imap(const std::string &utf8string);
118e216e
TJ
329
330std::string strip_html_tags(const std::string &input);
331std::string smart_html_entities(const std::string &input);
332std::string html_entities(std::string str);
554f813d 333std::string html_entities_to_console(std::string str);
118e216e 334
3f5c5ccd 335typedef std::pair<std::string::size_type, std::string::size_type> CommentZone;
46dd1321 336std::vector<CommentZone> find_html_comments(const std::string &str);
3f5c5ccd
CH
337void remove_html_comments(std::string &str);
338void remove_html_comments(std::string &str, const std::vector<CommentZone> &comments);
339
b953bf36
GE
340std::string sanitize_for_logging(const std::string &str, const char replace_with='?');
341
47c07fba
GE
342std::string escape(const std::string &s);
343
344std::string descape(const std::string &s, int startpos, int &endpos);
e6da286a 345inline std::string descape(const std::string &s)
47c07fba 346{
6ab3bc95
RP
347 int endpos;
348 return descape(s,0,endpos);
47c07fba
GE
349}
350
351std::string escape_shellarg(const std::string &input);
352
e93545dd 353#endif