Changed the default parameters value from nice_unit_format()
[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 #include <sys/types.h>
25
26 namespace I2n
27 {
28
29 /*
30 ** some useful constants:
31 */
32
33 extern const std::string Whitespaces;
34 extern const std::string LineEndings;
35
36
37 /*
38 ** predicates:
39 */
40
41
42 bool has_prefix(const std::string& str, const std::string& prefix);
43
44 bool has_suffix(const std::string& str, const std::string& suffix);
45
46
47 /*
48 ** tool functions(modifying):
49 */
50
51 std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces);
52
53 std::string chomp_mod(std::string& str, const std::string& what= LineEndings );
54
55 std::string to_lower_mod(std::string& str);
56
57 std::string to_upper_mod(std::string& str);
58
59
60 /*
61 ** tool functions (not modifying):
62 */
63
64 std::string trim(const std::string& str, const std::string& charlist = Whitespaces);
65
66 std::string chomp(const std::string& str, const std::string& what= LineEndings );
67
68 std::string to_lower(const std::string& str);
69
70 std::string to_upper(const std::string& str);
71
72
73 std::string remove_suffix(const std::string& str, const std::string& suffix);
74
75 std::string remove_prefix(const std::string& str, const std::string& prefix);
76
77
78
79 /*
80 ** split and join:
81 */
82
83
84 bool pair_split(
85    const std::string& str,
86    std::string& key,
87    std::string& value,
88    char delimiter = '=');
89
90
91 void split_string(
92    const std::string& str,
93    std::list< std::string >& result,
94    const std::string& delimiter= "\n",
95    bool omit_empty= false,
96    const std::string& trim_list= std::string()
97 );
98
99 std::list< std::string > split_string(
100    const std::string& str,
101    const std::string& delimiter = "\n",
102    bool omit_empty= false,
103    const std::string& trim_list= std::string()
104 );
105
106
107 std::string join_string(
108    const std::list< std::string >& parts,
109    const std::string& delimiter = "\n"
110 );
111
112
113 /*
114 ** conversions:
115 */
116
117
118 std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false);
119
120 std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error);
121
122
123
124 /*
125 ** "type conversions":
126 */
127
128
129 /**
130  * convert a datatype @a T to a string via string stream.
131  *
132  * @param s the string which should be converted to @a T.
133  * @return the value of type T.
134  */
135 template<
136 class T
137 >
138 T string_to(const std::string& s)
139 {
140    std::istringstream istr(s);
141    T result;
142    istr >> result;
143    return result;
144 } // eo string_to(const std::string&)
145
146
147 /**
148  * convert a datatype @a T to a string via string stream.
149  *
150  * @param s the string which should be converted to @a T.
151  * @param result the resulting value of type @a T.
152  * @return @a true iff the internal string stream was EOF after the conversion.
153  */
154 template<
155 class T
156 >
157 bool string_to(const std::string& s, T& result)
158 {
159    std::istringstream istr(s);
160    istr >> result;
161    return istr.eof();
162 } // eo string_to(const std::string&)
163
164
165 /**
166  * convert a string to another datatype @a T via string stream.
167  *
168  * @param v the value (of type @a T) which should be converted to a string.
169  * @return the resulting string.
170  */
171 template<
172 class T
173 >
174 std::string to_string(const T& v)
175 {
176    std::ostringstream ostr;
177    ostr << v;
178    return ostr.str();
179 } // eo to_string(const T&)
180
181
182 } // eo namespace I2n
183
184
185 #if 0
186 std::string to_lower(const std::string &src);
187 std::string to_upper(const std::string &src);
188 #else
189 // compatibility: import lower/upper funcs from I2n:
190 using I2n::to_lower;
191 using I2n::to_upper;
192 #endif
193
194
195 enum UnitBase {
196     UnitBase1000, // SI decimal, composed by multiples of 1000 (KB, MB, etc.)
197     UnitBase1024 // IEC binary, composed by multiples of 1024 (KiB, MiB, etc. )
198 };
199
200 enum UnitFormat {
201     ShortUnitFormat, // B, KB, MB, ...
202     LongUnitFormat // Byte, KByte, MByte, ...
203 };
204
205 std::string nice_unit_format(
206         const int64_t input,
207         const UnitFormat format = ShortUnitFormat,
208         const UnitBase base = UnitBase1024
209 );
210
211 bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
212 bool replace_all(std::string &base, const char *ist, const char *soll);
213 bool replace_all(std::string &base, const char *ist, const std::string *soll);
214 bool replace_all(std::string &base, const std::string &ist, const char *soll);
215 bool replace_all(std::string &base, const std::string &ist, const std::string &soll);
216
217 std::string iso_to_utf8(const std::string& isostring);
218 std::string utf8_to_iso(const std::string& utf8string);
219 std::string utf7imap_to_utf8(const std::string &utf7imapstring);
220 std::string utf8_to_utf7imap(const std::string &utf8string);
221
222 std::string strip_html_tags(const std::string &input);
223 std::string smart_html_entities(const std::string &input);
224 std::string html_entities(std::string str);
225
226 std::string escape(const std::string &s);
227
228 std::string descape(const std::string &s, int startpos, int &endpos);
229 inline std::string descape(const std::string &s)
230 {
231    int endpos;
232    return descape(s,0,endpos);
233 }
234
235 std::string escape_shellarg(const std::string &input);
236
237 #endif