Commit | Line | Data |
---|---|---|
6a93d84a TJ |
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 | |
6ab3bc95 | 13 | * |
6a93d84a TJ |
14 | * info@intra2net.com |
15 | */ | |
e93545dd GE |
16 | |
17 | #ifndef __STRINGFUNC_HXX | |
18 | #define __STRINGFUNC_HXX | |
19 | ||
6a93d84a | 20 | #include <list> |
e93545dd | 21 | #include <string> |
6a93d84a TJ |
22 | #include <sstream> |
23 | #include <stdexcept> | |
b04b4f7e | 24 | #include <sys/types.h> |
6a93d84a | 25 | |
6ab3bc95 RP |
26 | namespace I2n |
27 | { | |
6a93d84a TJ |
28 | |
29 | /* | |
30 | ** some useful constants: | |
31 | */ | |
32 | ||
6ab3bc95 RP |
33 | extern const std::string Whitespaces; |
34 | extern const std::string LineEndings; | |
6a93d84a TJ |
35 | |
36 | ||
37 | /* | |
38 | ** predicates: | |
39 | */ | |
40 | ||
41 | ||
6ab3bc95 | 42 | bool has_prefix(const std::string& str, const std::string& prefix); |
6a93d84a | 43 | |
6ab3bc95 | 44 | bool has_suffix(const std::string& str, const std::string& suffix); |
6a93d84a TJ |
45 | |
46 | ||
47 | /* | |
6ab3bc95 | 48 | ** tool functions(modifying): |
6a93d84a TJ |
49 | */ |
50 | ||
6ab3bc95 | 51 | std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces); |
6a93d84a | 52 | |
6ab3bc95 | 53 | std::string chomp_mod(std::string& str, const std::string& what= LineEndings ); |
6a93d84a | 54 | |
6ab3bc95 | 55 | std::string to_lower_mod(std::string& str); |
6a93d84a | 56 | |
6ab3bc95 | 57 | std::string to_upper_mod(std::string& str); |
6a93d84a TJ |
58 | |
59 | ||
60 | /* | |
61 | ** tool functions (not modifying): | |
62 | */ | |
63 | ||
6ab3bc95 | 64 | std::string trim(const std::string& str, const std::string& charlist = Whitespaces); |
6a93d84a | 65 | |
6ab3bc95 | 66 | std::string chomp(const std::string& str, const std::string& what= LineEndings ); |
6a93d84a | 67 | |
6ab3bc95 | 68 | std::string to_lower(const std::string& str); |
6a93d84a | 69 | |
6ab3bc95 | 70 | std::string to_upper(const std::string& str); |
6a93d84a TJ |
71 | |
72 | ||
6ab3bc95 | 73 | std::string remove_suffix(const std::string& str, const std::string& suffix); |
6a93d84a | 74 | |
6ab3bc95 | 75 | std::string remove_prefix(const std::string& str, const std::string& prefix); |
6a93d84a TJ |
76 | |
77 | ||
78 | ||
79 | /* | |
80 | ** split and join: | |
81 | */ | |
82 | ||
83 | ||
6ab3bc95 RP |
84 | bool pair_split( |
85 | const std::string& str, | |
86 | std::string& key, | |
87 | std::string& value, | |
88 | char delimiter = '='); | |
6a93d84a TJ |
89 | |
90 | ||
6ab3bc95 RP |
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() | |
6a93d84a TJ |
97 | ); |
98 | ||
6ab3bc95 RP |
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() | |
6a93d84a TJ |
104 | ); |
105 | ||
106 | ||
6ab3bc95 RP |
107 | std::string join_string( |
108 | const std::list< std::string >& parts, | |
109 | const std::string& delimiter = "\n" | |
6a93d84a TJ |
110 | ); |
111 | ||
112 | ||
113 | /* | |
114 | ** conversions: | |
115 | */ | |
116 | ||
117 | ||
6ab3bc95 | 118 | std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false); |
6a93d84a | 119 | |
6ab3bc95 | 120 | std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error); |
6a93d84a TJ |
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< | |
6ab3bc95 | 136 | class T |
6a93d84a | 137 | > |
6ab3bc95 | 138 | T string_to(const std::string& s) |
6a93d84a | 139 | { |
6ab3bc95 RP |
140 | std::istringstream istr(s); |
141 | T result; | |
142 | istr >> result; | |
143 | return result; | |
144 | } // eo string_to(const std::string&) | |
6a93d84a TJ |
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< | |
6ab3bc95 | 155 | class T |
6a93d84a | 156 | > |
6ab3bc95 | 157 | bool string_to(const std::string& s, T& result) |
6a93d84a | 158 | { |
6ab3bc95 RP |
159 | std::istringstream istr(s); |
160 | istr >> result; | |
161 | return istr.eof(); | |
162 | } // eo string_to(const std::string&) | |
6a93d84a TJ |
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< | |
6ab3bc95 | 172 | class T |
6a93d84a | 173 | > |
6ab3bc95 | 174 | std::string to_string(const T& v) |
6a93d84a | 175 | { |
6ab3bc95 RP |
176 | std::ostringstream ostr; |
177 | ostr << v; | |
178 | return ostr.str(); | |
179 | } // eo to_string(const T&) | |
180 | ||
181 | ||
182 | } // eo namespace I2n | |
183 | ||
6a93d84a | 184 | |
e5b21dbb | 185 | #if 0 |
6ab3bc95 RP |
186 | std::string to_lower(const std::string &src); |
187 | std::string to_upper(const std::string &src); | |
e5b21dbb RP |
188 | #else |
189 | // compatibility: import lower/upper funcs from I2n: | |
190 | using I2n::to_lower; | |
191 | using I2n::to_upper; | |
192 | #endif | |
e93545dd | 193 | |
81267544 | 194 | |
8f296924 GMF |
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. ) | |
81267544 GMF |
198 | }; |
199 | ||
8f296924 GMF |
200 | enum UnitFormat { |
201 | ShortUnitFormat, // B, KB, MB, ... | |
202 | LongUnitFormat // Byte, KByte, MByte, ... | |
d1ea9075 GMF |
203 | }; |
204 | ||
81267544 GMF |
205 | std::string nice_unit_format( |
206 | const int64_t input, | |
70fc0674 GMF |
207 | const UnitFormat format = LongUnitFormat, |
208 | const UnitBase base = UnitBase1000 | |
81267544 | 209 | ); |
e93545dd GE |
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); | |
13cc4db1 | 219 | std::string utf7imap_to_utf8(const std::string &utf7imapstring); |
6a2b6dd1 | 220 | std::string utf8_to_utf7imap(const std::string &utf8string); |
118e216e TJ |
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 | ||
47c07fba GE |
226 | std::string escape(const std::string &s); |
227 | ||
228 | std::string descape(const std::string &s, int startpos, int &endpos); | |
e6da286a | 229 | inline std::string descape(const std::string &s) |
47c07fba | 230 | { |
6ab3bc95 RP |
231 | int endpos; |
232 | return descape(s,0,endpos); | |
47c07fba GE |
233 | } |
234 | ||
235 | std::string escape_shellarg(const std::string &input); | |
236 | ||
e93545dd | 237 | #endif |