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