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