3 /** String helper functions.
5 This is a collection of string helper functions that works
6 together with string (and later also with STL String. Some of these
7 would certainly benefit from a rewrite/optimization.
8 \author Lars Gullik Bjønnes
9 \author Jean-Marc Lasgouttes
29 int compare_no_case(string const & s, string const & s2);
32 int compare_ascii_no_case(string const & s, string const & s2);
35 int compare_no_case(string const & s, string const & s2, unsigned int len);
39 int compare(char const * a, char const * b)
41 #ifndef CXX_GLOBAL_CSTD
42 return std::strcmp(a, b);
50 int compare(char const * a, char const * b, unsigned int len)
52 #ifndef CXX_GLOBAL_CSTD
53 return std::strncmp(a, b, len);
55 return strncmp(a, b, len);
60 bool isStrInt(string const & str);
62 /// does the string represent an unsigned integer value ?
63 bool isStrUnsignedInt(string const & str);
66 int strToInt(string const & str);
68 /// convert string to an unsigned integer
69 unsigned int strToUnsignedInt(string const & str);
72 bool isStrDbl(string const & str);
75 double strToDbl(string const & str);
78 char lowercase(char c);
81 char uppercase(char c);
84 string const lowercase(string const &);
87 string const uppercase(string const &);
89 /// convert \a T to string
92 string const tostr(T const & t)
96 return ostr.str().c_str();
97 // We need to use the .c_str since we sometimes are using
98 // our own string class and that is not compatible with
99 // basic_string<char>. (of course we don't want this later)
106 string const tostr(bool const & b)
108 return (b ? "true" : "false");
114 string const tostr(string const & s)
119 /// Does the string start with this prefix?
120 bool prefixIs(string const &, char const *);
122 /// Does the string start with this prefix?
123 bool prefixIs(string const &, string const &);
125 /// Does the string end with this char?
126 bool suffixIs(string const &, char);
128 /// Does the string end with this suffix?
129 bool suffixIs(string const &, char const *);
131 /// Does the string end with this suffix?
132 bool suffixIs(string const &, string const &);
135 bool contains(char const * a, string const & b);
138 bool contains(string const & a, char const * b);
141 bool contains(string const & a, string const & b);
144 bool contains(string const & a, char b);
147 bool contains(char const * a, char const * b);
149 /// This should probably we rewritten to be more general.
150 class contains_functor {
152 typedef string first_argument_type;
153 typedef string second_argument_type;
154 typedef bool result_type;
156 bool operator()(string const & haystack, string const & needle) const {
157 return contains(haystack, needle);
163 bool containsOnly(string const &, char const *);
166 bool containsOnly(string const &, string const &);
169 bool containsOnly(char const *, char const *);
172 bool containsOnly(char const *, string const &);
174 /** Extracts a token from this string at the nth delim.
175 Doesn't modify the original string. Similar to strtok.
178 "a;bc;d".token(';', 1) == "bc";
179 "a;bc;d".token(';', 2) == "d";
182 string const token(string const & a, char delim, int n);
185 /** Search a token in this string using the delim.
186 Doesn't modify the original string. Returns -1 in case of
190 "a;bc;d".tokenPos(';', "bc") == 1;
191 "a;bc;d".token(';', "d") == 2;
194 int tokenPos(string const & a, char delim, string const & tok);
197 /** Compares a string and a (simple) regular expression
198 The only element allowed is "*" for any string of characters
200 bool regexMatch(string const & a, string const & pattern);
202 /// Substitute all \a oldchar with \a newchar
203 string const subst(string const & a, char oldchar, char newchar);
205 /// Substitutes all instances of \a oldstr with \a newstr
206 string const subst(string const & a,
207 char const * oldstr, string const & newstr);
209 /// substitutes all instances of \a oldstr with \a newstr
210 string const subst(string const & a,
211 string const & oldstr, string const & newstr);
213 /** Strips characters off the end of a string.
215 "abccc".strip('c') = "ab".
218 string const strip(string const & a, char c = ' ');
220 /** Strips characters of the beginning of a string.
222 "cccba".frontstrip('c') = "ba"
225 string const frontStrip(string const & a, char c = ' ');
227 /** Strips characters off the beginning of a string.
229 "ababcdef".frontstrip("ab") = "cdef"
232 string const frontStrip(string const & a, char const * p);
234 /** Splits the string by the first delim.
235 Splits the string by the first appearance of delim.
236 The leading string up to delim is returned in piece (not including
237 delim), while the original string is cut from after the delimiter.
240 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
243 string const split(string const & a, string & piece, char delim);
245 /// Same as split but does not return a piece
246 string const split(string const & a, char delim);
248 /// Same as split but uses the last delim.
249 string const rsplit(string const & a, string & piece, char delim);
251 /// Escapes non ASCII chars
252 string const escape(string const & lab);
254 /// gives a vector of stringparts which have the delimiter delim
255 std::vector<string> const getVectorFromString(string const & str,
256 string const & delim = ",");
258 // the same vice versa
259 string const getStringFromVector(std::vector<string> const & vec,
260 string const & delim = ",");