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);
83 /// same as lowercase(), but ignores locale
84 string const ascii_lowercase(string const &);
87 string const lowercase(string const &);
90 string const uppercase(string const &);
92 /// convert \a T to string
95 string const tostr(T const & t)
99 return ostr.str().c_str();
100 // We need to use the .c_str since we sometimes are using
101 // our own string class and that is not compatible with
102 // basic_string<char>. (of course we don't want this later)
109 string const tostr(bool const & b)
111 return (b ? "true" : "false");
117 string const tostr(string const & s)
122 /// Does the string start with this prefix?
123 bool prefixIs(string const &, char const *);
125 /// Does the string start with this prefix?
126 bool prefixIs(string const &, string const &);
128 /// Does the string end with this char?
129 bool suffixIs(string const &, char);
131 /// Does the string end with this suffix?
132 bool suffixIs(string const &, char const *);
134 /// Does the string end with this suffix?
135 bool suffixIs(string const &, string const &);
138 bool contains(char const * a, string const & b);
141 bool contains(string const & a, char const * b);
144 bool contains(string const & a, string const & b);
147 bool contains(string const & a, char b);
150 bool contains(char const * a, char const * b);
152 /// This should probably we rewritten to be more general.
153 class contains_functor {
155 typedef string first_argument_type;
156 typedef string second_argument_type;
157 typedef bool result_type;
159 bool operator()(string const & haystack, string const & needle) const {
160 return contains(haystack, needle);
166 bool containsOnly(string const &, char const *);
169 bool containsOnly(string const &, string const &);
172 bool containsOnly(char const *, char const *);
175 bool containsOnly(char const *, string const &);
177 /** Extracts a token from this string at the nth delim.
178 Doesn't modify the original string. Similar to strtok.
181 "a;bc;d".token(';', 1) == "bc";
182 "a;bc;d".token(';', 2) == "d";
185 string const token(string const & a, char delim, int n);
188 /** Search a token in this string using the delim.
189 Doesn't modify the original string. Returns -1 in case of
193 "a;bc;d".tokenPos(';', "bc") == 1;
194 "a;bc;d".token(';', "d") == 2;
197 int tokenPos(string const & a, char delim, string const & tok);
200 /** Compares a string and a (simple) regular expression
201 The only element allowed is "*" for any string of characters
203 bool regexMatch(string const & a, string const & pattern);
205 /// Substitute all \a oldchar with \a newchar
206 string const subst(string const & a, char oldchar, char newchar);
208 /// Substitutes all instances of \a oldstr with \a newstr
209 string const subst(string const & a,
210 char const * oldstr, string const & newstr);
212 /// substitutes all instances of \a oldstr with \a newstr
213 string const subst(string const & a,
214 string const & oldstr, string const & newstr);
216 /** Strips characters off the end of a string.
218 "abccc".strip('c') = "ab".
221 string const strip(string const & a, char c = ' ');
223 /** Strips characters of the beginning of a string.
225 "cccba".frontstrip('c') = "ba"
228 string const frontStrip(string const & a, char c = ' ');
230 /** Strips characters off the beginning of a string.
232 "ababcdef".frontstrip("ab") = "cdef"
235 string const frontStrip(string const & a, char const * p);
237 /** Splits the string by the first delim.
238 Splits the string by the first appearance of delim.
239 The leading string up to delim is returned in piece (not including
240 delim), while the original string is cut from after the delimiter.
243 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
246 string const split(string const & a, string & piece, char delim);
248 /// Same as split but does not return a piece
249 string const split(string const & a, char delim);
251 /// Same as split but uses the last delim.
252 string const rsplit(string const & a, string & piece, char delim);
254 /// Escapes non ASCII chars
255 string const escape(string const & lab);
257 /// gives a vector of stringparts which have the delimiter delim
258 std::vector<string> const getVectorFromString(string const & str,
259 string const & delim = ",");
261 // the same vice versa
262 string const getStringFromVector(std::vector<string> const & vec,
263 string const & delim = ",");