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_no_case(string const & s, string const & s2, unsigned int len);
35 int compare(char const * a, char const * b)
37 return std::strcmp(a, b);
42 int compare(char const * a, char const * b, unsigned int len)
44 return std::strncmp(a, b, len);
48 bool isStrInt(string const & str);
50 /// does the string represent an unsigned integer value ?
51 bool isStrUnsignedInt(string const & str);
54 int strToInt(string const & str);
56 /// convert string to an unsigned integer
57 unsigned int strToUnsignedInt(string const & str);
60 bool isStrDbl(string const & str);
63 double strToDbl(string const & str);
66 char lowercase(char c);
69 char uppercase(char c);
72 string const lowercase(string const &);
75 string const uppercase(string const &);
77 /// convert \a T to string
80 string const tostr(T const & t)
84 return ostr.str().c_str();
85 // We need to use the .c_str since we sometimes are using
86 // our own string class and that is not compatible with
87 // basic_string<char>. (of course we don't want this later)
94 string const tostr(bool const & b)
96 return (b ? "true" : "false");
102 string const tostr(string const & s)
107 /// Does the string start with this prefix?
108 bool prefixIs(string const &, char const *);
110 /// Does the string start with this prefix?
111 bool prefixIs(string const &, string const &);
113 /// Does the string end with this char?
114 bool suffixIs(string const &, char);
116 /// Does the string end with this suffix?
117 bool suffixIs(string const &, char const *);
119 /// Does the string end with this suffix?
120 bool suffixIs(string const &, string const &);
123 bool contains(char const * a, string const & b);
126 bool contains(string const & a, char const * b);
129 bool contains(string const & a, string const & b);
132 bool contains(string const & a, char b);
135 bool contains(char const * a, char const * b);
138 bool containsOnly(string const &, char const *);
141 bool containsOnly(string const &, string const &);
144 bool containsOnly(char const *, char const *);
147 bool containsOnly(char const *, string const &);
149 /// Counts how many of character c there is in a
150 string::size_type countChar(string const & a, char c);
152 /** Extracts a token from this string at the nth delim.
153 Doesn't modify the original string. Similar to strtok.
156 "a;bc;d".token(';', 1) == "bc";
157 "a;bc;d".token(';', 2) == "d";
160 string const token(string const & a, char delim, int n);
163 /** Search a token in this string using the delim.
164 Doesn't modify the original string. Returns -1 in case of
168 "a;bc;d".tokenPos(';', "bc") == 1;
169 "a;bc;d".token(';', "d") == 2;
172 int tokenPos(string const & a, char delim, string const & tok);
175 /** Compares a string and a (simple) regular expression
176 The only element allowed is "*" for any string of characters
178 bool regexMatch(string const & a, string const & pattern);
180 /// Substitute all \a oldchar with \a newchar
181 string const subst(string const & a, char oldchar, char newchar);
183 /// Substitutes all instances of \a oldstr with \a newstr
184 string const subst(string const & a,
185 char const * oldstr, string const & newstr);
187 /// substitutes all instances of \a oldstr with \a newstr
188 string const subst(string const & a,
189 string const & oldstr, string const & newstr);
191 /** Strips characters off the end of a string.
193 "abccc".strip('c') = "ab".
196 string const strip(string const & a, char c = ' ');
198 /** Strips characters of the beginning of a string.
200 "cccba".frontstrip('c') = "ba"
203 string const frontStrip(string const & a, char c = ' ');
205 /** Strips characters off the beginning of a string.
207 "ababcdef".frontstrip("ab") = "cdef"
210 string const frontStrip(string const & a, char const * p);
212 /** Splits the string by the first delim.
213 Splits the string by the first appearance of delim.
214 The leading string up to delim is returned in piece (not including
215 delim), while the original string is cut from after the delimiter.
218 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
221 string const split(string const & a, string & piece, char delim);
223 /// Same as split but does not return a piece
224 string const split(string const & a, char delim);
226 /// Same as split but uses the last delim.
227 string const rsplit(string const & a, string & piece, char delim);