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
28 int compare_no_case(string const & s, string const & s2);
31 int compare_ascii_no_case(string const & s, string const & s2);
34 int compare_no_case(string const & s, string const & s2, unsigned int len);
38 int compare(char const * a, char const * b)
40 #ifndef CXX_GLOBAL_CSTD
41 return std::strcmp(a, b);
49 int compare(char const * a, char const * b, unsigned int len)
51 #ifndef CXX_GLOBAL_CSTD
52 return std::strncmp(a, b, len);
54 return strncmp(a, b, len);
59 bool isStrInt(string const & str);
61 /// does the string represent an unsigned integer value ?
62 bool isStrUnsignedInt(string const & str);
65 int strToInt(string const & str);
67 /// convert string to an unsigned integer
68 unsigned int strToUnsignedInt(string const & str);
71 bool isStrDbl(string const & str);
74 double strToDbl(string const & str);
77 char lowercase(char c);
80 char uppercase(char c);
83 string const lowercase(string const &);
86 string const uppercase(string const &);
88 /// convert \a T to string
91 string const tostr(T const & t)
95 return ostr.str().c_str();
96 // We need to use the .c_str since we sometimes are using
97 // our own string class and that is not compatible with
98 // basic_string<char>. (of course we don't want this later)
105 string const tostr(bool const & b)
107 return (b ? "true" : "false");
113 string const tostr(string const & s)
118 /// Does the string start with this prefix?
119 bool prefixIs(string const &, char const *);
121 /// Does the string start with this prefix?
122 bool prefixIs(string const &, string const &);
124 /// Does the string end with this char?
125 bool suffixIs(string const &, char);
127 /// Does the string end with this suffix?
128 bool suffixIs(string const &, char const *);
130 /// Does the string end with this suffix?
131 bool suffixIs(string const &, string const &);
134 bool contains(char const * a, string const & b);
137 bool contains(string const & a, char const * b);
140 bool contains(string const & a, string const & b);
143 bool contains(string const & a, char b);
146 bool contains(char const * a, char const * b);
149 bool containsOnly(string const &, char const *);
152 bool containsOnly(string const &, string const &);
155 bool containsOnly(char const *, char const *);
158 bool containsOnly(char const *, string const &);
160 /// Counts how many of character c there is in a
161 string::size_type countChar(string const & a, char c);
163 /** Extracts a token from this string at the nth delim.
164 Doesn't modify the original string. Similar to strtok.
167 "a;bc;d".token(';', 1) == "bc";
168 "a;bc;d".token(';', 2) == "d";
171 string const token(string const & a, char delim, int n);
174 /** Search a token in this string using the delim.
175 Doesn't modify the original string. Returns -1 in case of
179 "a;bc;d".tokenPos(';', "bc") == 1;
180 "a;bc;d".token(';', "d") == 2;
183 int tokenPos(string const & a, char delim, string const & tok);
186 /** Compares a string and a (simple) regular expression
187 The only element allowed is "*" for any string of characters
189 bool regexMatch(string const & a, string const & pattern);
191 /// Substitute all \a oldchar with \a newchar
192 string const subst(string const & a, char oldchar, char newchar);
194 /// Substitutes all instances of \a oldstr with \a newstr
195 string const subst(string const & a,
196 char const * oldstr, string const & newstr);
198 /// substitutes all instances of \a oldstr with \a newstr
199 string const subst(string const & a,
200 string const & oldstr, string const & newstr);
202 /** Strips characters off the end of a string.
204 "abccc".strip('c') = "ab".
207 string const strip(string const & a, char c = ' ');
209 /** Strips characters of the beginning of a string.
211 "cccba".frontstrip('c') = "ba"
214 string const frontStrip(string const & a, char c = ' ');
216 /** Strips characters off the beginning of a string.
218 "ababcdef".frontstrip("ab") = "cdef"
221 string const frontStrip(string const & a, char const * p);
223 /** Splits the string by the first delim.
224 Splits the string by the first appearance of delim.
225 The leading string up to delim is returned in piece (not including
226 delim), while the original string is cut from after the delimiter.
229 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
232 string const split(string const & a, string & piece, char delim);
234 /// Same as split but does not return a piece
235 string const split(string const & a, char delim);
237 /// Same as split but uses the last delim.
238 string const rsplit(string const & a, string & piece, char delim);