4 This is a collection of string helper functions that works
5 together with string (and later also with STL String. Some of these
6 would certainly benefit from a rewrite/optimization.
25 int compare_no_case(string const & s, string const & s2);
28 int compare_no_case(string const & s, string const & s2, unsigned int len);
32 int compare(char const * a, char const * b)
39 int compare(char const * a, char const * b, unsigned int len)
41 return strncmp(a, b, len);
45 bool isStrInt(string const & str);
47 /// does the string represent an unsigned integer value ?
48 bool isStrUnsignedInt(string const & str);
51 int strToInt(string const & str);
53 /// convert string to an unsigned integer
54 unsigned int strToUnsignedInt(string const & str);
57 bool isStrDbl(string const & str);
60 double strToDbl(string const & str);
63 char lowercase(char c);
66 char uppercase(char c);
69 string const lowercase(string const &);
72 string const uppercase(string const &);
74 /// convert T to string
77 string const tostr(T const & t)
81 return ostr.str().c_str();
82 // We need to use the .c_str since we sometimes are using
83 // our own string class and that is not compatible with
84 // basic_string<char>. (of course we don't want this later)
91 string const tostr(bool const & b)
93 return (b ? "true" : "false");
99 string const tostr(string const & s)
104 /// Does the string start with this prefix?
105 bool prefixIs(string const &, char const *);
107 /// Does the string start with this prefix?
108 bool prefixIs(string const &, string const &);
110 /// Does the string end with this char?
111 bool suffixIs(string const &, char);
113 /// Does the string end with this suffix?
114 bool suffixIs(string const &, char const *);
116 /// Does the string end with this suffix?
117 bool suffixIs(string const &, string const &);
120 bool contains(char const * a, string const & b);
123 bool contains(string const & a, char const * b);
126 bool contains(string const & a, string const & b);
129 bool contains(char const * a, char const * b);
132 bool containsOnly(string const &, char const *);
135 bool containsOnly(string const &, string const &);
138 bool containsOnly(char const *, char const *);
141 bool containsOnly(char const *, string const &);
143 /// Counts how many of character c there is in a
144 string::size_type countChar(string const & a, char c);
146 /** Extracts a token from this string at the nth delim.
147 Doesn't modify the original string. Similar to strtok.
149 #"a;bc;d".token(';', 1) == "bc";#
150 #"a;bc;d".token(';', 2) == "d";#
152 string const token(string const & a, char delim, int n);
155 /** Search a token in this string using the delim.
156 Doesn't modify the original string. Returns -1 in case of
159 #"a;bc;d".tokenPos(';', "bc") == 1;#
160 #"a;bc;d".token(';', "d") == 2;#
162 int tokenPos(string const & a, char delim, string const & tok);
165 /** Compares a string and a (simple) regular expression
166 The only element allowed is "*" for any string of characters
168 bool regexMatch(string const & a, string const & pattern);
170 /// Substitute all "oldchar"s with "newchar"
171 string const subst(string const & a, char oldchar, char newchar);
173 /// Substitutes all instances of oldstr with newstr
174 string const subst(string const & a,
175 char const * oldstr, string const & newstr);
177 /// substitutes all instances ofr oldstr with newstr
178 string const subst(string const & a,
179 string const & oldstr, string const & newstr);
181 /** Strips characters off the end of a string.
182 #"abccc".strip('c') = "ab".#
184 string const strip(string const & a, char c = ' ');
186 /** Strips characters of the beginning of a string.
187 #"cccba".frontstrip('c') = "ba"#. */
188 string const frontStrip(string const & a, char c = ' ');
190 /** Strips characters off the beginning of a string.
191 #"ababcdef".frontstrip("ab") = "cdef"# .*/
192 string const frontStrip(string const & a, char const * p);
194 /** Splits the string by the first delim.
195 Splits the string by the first appearance of delim.
196 The leading string up to delim is returned in piece (not including
197 delim), while the original string is cut from after the delimiter.
199 #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
201 string const split(string const & a, string & piece, char delim);
203 /// Same as split but does not return a piece
204 string const split(string const & a, char delim);
206 /// Same as split but uses the last delim.
207 string const rsplit(string const & a, string & piece, char delim);