Improved method signature, constification of parameter and type
[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>
b04b4f7e 24#include <sys/types.h>
6a93d84a 25
6ab3bc95
RP
26namespace I2n
27{
6a93d84a
TJ
28
29/*
30** some useful constants:
31*/
32
6ab3bc95
RP
33extern const std::string Whitespaces;
34extern const std::string LineEndings;
6a93d84a
TJ
35
36
37/*
38** predicates:
39*/
40
41
6ab3bc95 42bool has_prefix(const std::string& str, const std::string& prefix);
6a93d84a 43
6ab3bc95 44bool 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 51std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces);
6a93d84a 52
6ab3bc95 53std::string chomp_mod(std::string& str, const std::string& what= LineEndings );
6a93d84a 54
6ab3bc95 55std::string to_lower_mod(std::string& str);
6a93d84a 56
6ab3bc95 57std::string to_upper_mod(std::string& str);
6a93d84a
TJ
58
59
60/*
61** tool functions (not modifying):
62*/
63
6ab3bc95 64std::string trim(const std::string& str, const std::string& charlist = Whitespaces);
6a93d84a 65
6ab3bc95 66std::string chomp(const std::string& str, const std::string& what= LineEndings );
6a93d84a 67
6ab3bc95 68std::string to_lower(const std::string& str);
6a93d84a 69
6ab3bc95 70std::string to_upper(const std::string& str);
6a93d84a
TJ
71
72
6ab3bc95 73std::string remove_suffix(const std::string& str, const std::string& suffix);
6a93d84a 74
6ab3bc95 75std::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
84bool pair_split(
85 const std::string& str,
86 std::string& key,
87 std::string& value,
88 char delimiter = '=');
6a93d84a
TJ
89
90
6ab3bc95
RP
91void 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
99std::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
107std::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 118std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false);
6a93d84a 119
6ab3bc95 120std::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 */
135template<
6ab3bc95 136class T
6a93d84a 137>
6ab3bc95 138T 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 */
154template<
6ab3bc95 155class T
6a93d84a 156>
6ab3bc95 157bool 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 */
171template<
6ab3bc95 172class T
6a93d84a 173>
6ab3bc95 174std::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
186std::string to_lower(const std::string &src);
187std::string to_upper(const std::string &src);
e5b21dbb
RP
188#else
189// compatibility: import lower/upper funcs from I2n:
190using I2n::to_lower;
191using I2n::to_upper;
192#endif
e93545dd 193
b04b4f7e 194std::string nice_unit_format(const int64_t input);
e93545dd
GE
195
196bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
197bool replace_all(std::string &base, const char *ist, const char *soll);
198bool replace_all(std::string &base, const char *ist, const std::string *soll);
199bool replace_all(std::string &base, const std::string &ist, const char *soll);
200bool replace_all(std::string &base, const std::string &ist, const std::string &soll);
201
202std::string iso_to_utf8(const std::string& isostring);
203std::string utf8_to_iso(const std::string& utf8string);
13cc4db1 204std::string utf7imap_to_utf8(const std::string &utf7imapstring);
6a2b6dd1 205std::string utf8_to_utf7imap(const std::string &utf8string);
118e216e
TJ
206
207std::string strip_html_tags(const std::string &input);
208std::string smart_html_entities(const std::string &input);
209std::string html_entities(std::string str);
210
47c07fba
GE
211std::string escape(const std::string &s);
212
213std::string descape(const std::string &s, int startpos, int &endpos);
e6da286a 214inline std::string descape(const std::string &s)
47c07fba 215{
6ab3bc95
RP
216 int endpos;
217 return descape(s,0,endpos);
47c07fba
GE
218}
219
220std::string escape_shellarg(const std::string &input);
221
e93545dd 222#endif