libi2ncommon: (reinhard) modified stringfunc (and some others) according to our new...
[libi2ncommon] / src / stringfunc.hxx
CommitLineData
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
25namespace I2n
26{
6a93d84a
TJ
27
28/*
29** some useful constants:
30*/
31
6ab3bc95
RP
32extern const std::string Whitespaces;
33extern const std::string LineEndings;
6a93d84a
TJ
34
35
36/*
37** predicates:
38*/
39
40
6ab3bc95 41bool has_prefix(const std::string& str, const std::string& prefix);
6a93d84a 42
6ab3bc95 43bool 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 50std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces);
6a93d84a 51
6ab3bc95 52std::string chomp_mod(std::string& str, const std::string& what= LineEndings );
6a93d84a 53
6ab3bc95 54std::string to_lower_mod(std::string& str);
6a93d84a 55
6ab3bc95 56std::string to_upper_mod(std::string& str);
6a93d84a
TJ
57
58
59/*
60** tool functions (not modifying):
61*/
62
6ab3bc95 63std::string trim(const std::string& str, const std::string& charlist = Whitespaces);
6a93d84a 64
6ab3bc95 65std::string chomp(const std::string& str, const std::string& what= LineEndings );
6a93d84a 66
6ab3bc95 67std::string to_lower(const std::string& str);
6a93d84a 68
6ab3bc95 69std::string to_upper(const std::string& str);
6a93d84a
TJ
70
71
6ab3bc95 72std::string remove_suffix(const std::string& str, const std::string& suffix);
6a93d84a 73
6ab3bc95 74std::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
83bool pair_split(
84 const std::string& str,
85 std::string& key,
86 std::string& value,
87 char delimiter = '=');
6a93d84a
TJ
88
89
6ab3bc95
RP
90void 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
98std::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
106std::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 117std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false);
6a93d84a 118
6ab3bc95 119std::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 */
134template<
6ab3bc95 135class T
6a93d84a 136>
6ab3bc95 137T 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 */
153template<
6ab3bc95 154class T
6a93d84a 155>
6ab3bc95 156bool 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 */
170template<
6ab3bc95 171class T
6a93d84a 172>
6ab3bc95 173std::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
e93545dd 184
6ab3bc95
RP
185std::string to_lower(const std::string &src);
186std::string to_upper(const std::string &src);
e93545dd 187
6ab3bc95 188std::string nice_unit_format(int input);
e93545dd
GE
189
190bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
191bool replace_all(std::string &base, const char *ist, const char *soll);
192bool replace_all(std::string &base, const char *ist, const std::string *soll);
193bool replace_all(std::string &base, const std::string &ist, const char *soll);
194bool replace_all(std::string &base, const std::string &ist, const std::string &soll);
195
196std::string iso_to_utf8(const std::string& isostring);
197std::string utf8_to_iso(const std::string& utf8string);
13cc4db1 198std::string utf7imap_to_utf8(const std::string &utf7imapstring);
6a2b6dd1 199std::string utf8_to_utf7imap(const std::string &utf8string);
118e216e
TJ
200
201std::string strip_html_tags(const std::string &input);
202std::string smart_html_entities(const std::string &input);
203std::string html_entities(std::string str);
204
47c07fba
GE
205std::string escape(const std::string &s);
206
207std::string descape(const std::string &s, int startpos, int &endpos);
e6da286a 208inline std::string descape(const std::string &s)
47c07fba 209{
6ab3bc95
RP
210 int endpos;
211 return descape(s,0,endpos);
47c07fba
GE
212}
213
214std::string escape_shellarg(const std::string &input);
215
e93545dd 216#endif