added function nice_unit_format for double input (calls other after proper round...
[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>
e93545dd 38#include <string>
6a93d84a
TJ
39#include <sstream>
40#include <stdexcept>
b04b4f7e 41#include <sys/types.h>
6a93d84a 42
6ab3bc95
RP
43namespace I2n
44{
6a93d84a
TJ
45
46/*
47** some useful constants:
48*/
49
6ab3bc95
RP
50extern const std::string Whitespaces;
51extern const std::string LineEndings;
6a93d84a
TJ
52
53
54/*
55** predicates:
56*/
57
58
6ab3bc95 59bool has_prefix(const std::string& str, const std::string& prefix);
6a93d84a 60
6ab3bc95 61bool has_suffix(const std::string& str, const std::string& suffix);
6a93d84a
TJ
62
63
64/*
6ab3bc95 65** tool functions(modifying):
6a93d84a
TJ
66*/
67
6ab3bc95 68std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces);
6a93d84a 69
6ab3bc95 70std::string chomp_mod(std::string& str, const std::string& what= LineEndings );
6a93d84a 71
6ab3bc95 72std::string to_lower_mod(std::string& str);
6a93d84a 73
6ab3bc95 74std::string to_upper_mod(std::string& str);
6a93d84a
TJ
75
76
77/*
78** tool functions (not modifying):
79*/
80
6ab3bc95 81std::string trim(const std::string& str, const std::string& charlist = Whitespaces);
6a93d84a 82
6ab3bc95 83std::string chomp(const std::string& str, const std::string& what= LineEndings );
6a93d84a 84
6ab3bc95 85std::string to_lower(const std::string& str);
6a93d84a 86
6ab3bc95 87std::string to_upper(const std::string& str);
6a93d84a
TJ
88
89
6ab3bc95 90std::string remove_suffix(const std::string& str, const std::string& suffix);
6a93d84a 91
6ab3bc95 92std::string remove_prefix(const std::string& str, const std::string& prefix);
6a93d84a
TJ
93
94
95
96/*
97** split and join:
98*/
99
100
6ab3bc95
RP
101bool pair_split(
102 const std::string& str,
103 std::string& key,
104 std::string& value,
105 char delimiter = '=');
6a93d84a
TJ
106
107
6ab3bc95
RP
108void split_string(
109 const std::string& str,
110 std::list< std::string >& result,
111 const std::string& delimiter= "\n",
112 bool omit_empty= false,
113 const std::string& trim_list= std::string()
6a93d84a
TJ
114);
115
6ab3bc95
RP
116std::list< std::string > split_string(
117 const std::string& str,
118 const std::string& delimiter = "\n",
119 bool omit_empty= false,
120 const std::string& trim_list= std::string()
6a93d84a
TJ
121);
122
123
6ab3bc95
RP
124std::string join_string(
125 const std::list< std::string >& parts,
126 const std::string& delimiter = "\n"
6a93d84a
TJ
127);
128
129
130/*
131** conversions:
132*/
133
134
6ab3bc95 135std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false);
6a93d84a 136
6ab3bc95 137std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error);
6a93d84a
TJ
138
139
140
141/*
142** "type conversions":
143*/
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 * @return the value of type T.
151 */
152template<
6ab3bc95 153class T
6a93d84a 154>
6ab3bc95 155T string_to(const std::string& s)
6a93d84a 156{
6ab3bc95
RP
157 std::istringstream istr(s);
158 T result;
159 istr >> result;
160 return result;
161} // eo string_to(const std::string&)
6a93d84a
TJ
162
163
164/**
165 * convert a datatype @a T to a string via string stream.
166 *
167 * @param s the string which should be converted to @a T.
168 * @param result the resulting value of type @a T.
169 * @return @a true iff the internal string stream was EOF after the conversion.
08f2d184
GE
170 *
171 * @attention: does not return if the conversion was successful. So check for empty strings before.
6a93d84a
TJ
172 */
173template<
6ab3bc95 174class T
6a93d84a 175>
6ab3bc95 176bool string_to(const std::string& s, T& result)
6a93d84a 177{
6ab3bc95
RP
178 std::istringstream istr(s);
179 istr >> result;
180 return istr.eof();
181} // eo string_to(const std::string&)
6a93d84a
TJ
182
183
184/**
185 * convert a string to another datatype @a T via string stream.
186 *
187 * @param v the value (of type @a T) which should be converted to a string.
188 * @return the resulting string.
189 */
190template<
6ab3bc95 191class T
6a93d84a 192>
6ab3bc95 193std::string to_string(const T& v)
6a93d84a 194{
6ab3bc95
RP
195 std::ostringstream ostr;
196 ostr << v;
197 return ostr.str();
198} // eo to_string(const T&)
199
200
201} // eo namespace I2n
202
6a93d84a 203
e5b21dbb 204#if 0
6ab3bc95
RP
205std::string to_lower(const std::string &src);
206std::string to_upper(const std::string &src);
e5b21dbb
RP
207#else
208// compatibility: import lower/upper funcs from I2n:
209using I2n::to_lower;
210using I2n::to_upper;
211#endif
e93545dd 212
81267544 213
8f296924
GMF
214enum UnitBase {
215 UnitBase1000, // SI decimal, composed by multiples of 1000 (KB, MB, etc.)
216 UnitBase1024 // IEC binary, composed by multiples of 1024 (KiB, MiB, etc. )
81267544
GMF
217};
218
8f296924
GMF
219enum UnitFormat {
220 ShortUnitFormat, // B, KB, MB, ...
221 LongUnitFormat // Byte, KByte, MByte, ...
d1ea9075
GMF
222};
223
81267544
GMF
224std::string nice_unit_format(
225 const int64_t input,
ee037ee3
GMF
226 const UnitFormat format = ShortUnitFormat,
227 const UnitBase base = UnitBase1024
81267544 228);
e93545dd 229
5cd64148
CH
230std::string nice_unit_format(
231 const double input,
232 const UnitFormat format = ShortUnitFormat,
233 const UnitBase base = UnitBase1024
234);
235
e93545dd
GE
236bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
237bool replace_all(std::string &base, const char *ist, const char *soll);
238bool replace_all(std::string &base, const char *ist, const std::string *soll);
239bool replace_all(std::string &base, const std::string &ist, const char *soll);
240bool replace_all(std::string &base, const std::string &ist, const std::string &soll);
241
242std::string iso_to_utf8(const std::string& isostring);
243std::string utf8_to_iso(const std::string& utf8string);
13cc4db1 244std::string utf7imap_to_utf8(const std::string &utf7imapstring);
6a2b6dd1 245std::string utf8_to_utf7imap(const std::string &utf8string);
118e216e
TJ
246
247std::string strip_html_tags(const std::string &input);
248std::string smart_html_entities(const std::string &input);
249std::string html_entities(std::string str);
554f813d 250std::string html_entities_to_console(std::string str);
118e216e 251
b953bf36
GE
252std::string sanitize_for_logging(const std::string &str, const char replace_with='?');
253
47c07fba
GE
254std::string escape(const std::string &s);
255
256std::string descape(const std::string &s, int startpos, int &endpos);
e6da286a 257inline std::string descape(const std::string &s)
47c07fba 258{
6ab3bc95
RP
259 int endpos;
260 return descape(s,0,endpos);
47c07fba
GE
261}
262
263std::string escape_shellarg(const std::string &input);
264
e93545dd 265#endif