3 /** This is a collection of string helper functions that works
4 together with string (and later also with STL String. Some of these
5 would certainly benefit from a rewrite/optimization.
15 //#warning verify this please. Lgb
18 size_t lstrlen(T const * t)
20 Assert(t); // we don't want null pointers
23 while(*r != 0) ++r, ++count;
28 //#warning verify this please. Lgb
31 T * lstrchr(T const * t, int c)
33 Assert(t); // we don't want null pointers
34 T * r = const_cast<T*>(t);
36 if (*r == c) return r; else ++r;
45 inline int compare_no_case(string const & s, string const & s2)
48 string::const_iterator p = s.begin();
49 string::const_iterator p2 = s2.begin();
51 while (p != s.end() && p2 != s2.end()) {
52 if (tolower(*p) != tolower(*p2))
53 return (tolower(*p) < tolower(*p2)) ? -1 : 1;
58 return s.size() - s2.size();
63 inline int compare_no_case(string const & s, string const & s2,
66 //#warning verify this func please
67 string::const_iterator p = s.begin();
68 string::const_iterator p2 = s2.begin();
70 while (i < len && p != s.end() && p2 != s2.end()) {
71 if (tolower(*p) != tolower(*p2))
72 return (tolower(*p) < tolower(*p2)) ? -1 : 1;
77 return s.size() - s2.size();
82 inline int compare(char const * a, char const * b)
89 inline int compare(char const * a, char const * b, unsigned int len)
91 return strncmp(a, b, len);
96 bool isStrInt(string const & str);
99 int strToInt(string const & str);
102 string lowercase(string const &);
105 string uppercase(string const &);
111 string tostr(unsigned int);
114 string tostr(long l);
117 string tostr(unsigned long l);
120 string tostr(char c);
123 string tostr(void * v);
126 string tostr(bool b);
129 string tostr(double d);
131 /// Does the string start with this prefix?
132 bool prefixIs(string const &, char const *);
134 /// Does the string end with this char?
135 bool suffixIs(string const &, char);
137 /// Does the string end with this suffix?
138 bool suffixIs(string const &, char const *);
141 bool contains(char const * a, string const & b);
144 bool contains(string const & a, char const * b);
147 bool contains(string const & a, string const & b);
150 bool contains(char const * a, char const * b);
152 /// Counts how many of character c there is in a
153 int countChar(string const & a, char const c);
155 /** Extracts a token from this string at the nth delim.
156 Doesn't modify the original string. Similar to strtok.
158 #"a;bc;d".token(';', 1) == "bc";#
159 #"a;bc;d".token(';', 2) == "d";#
161 string token(string const & a, char delim, int n);
164 /** Search a token in this string using the delim.
165 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;#
171 int tokenPos(string const & a, char delim, string const & tok);
174 /** Compares a string and a (simple) regular expression
175 The only element allowed is "*" for any string of characters
177 bool regexMatch(string const & a, string const & pattern);
179 /// Substitute all "oldchar"s with "newchar"
180 string subst(string const & a, char oldchar, char newchar);
182 /// Substitutes all instances of oldstr with newstr
183 string subst(string const & a,
184 char const * oldstr, string const & newstr);
186 /** Strips characters off the end of a string.
187 #"abccc".strip('c') = "ab".#
189 string strip(string const & a, char const c = ' ');
191 /** Strips characters of the beginning of a string.
192 #"cccba".frontstrip('c') = "ba"#. */
193 string frontStrip(string const & a, char const c = ' ');
195 /** Strips characters off the beginning of a string.
196 #"ababcdef".frontstrip("ab") = "cdef"# .*/
197 string frontStrip(string const & a, char const * p);
199 /** Splits the string by the first delim.
200 Splits the string by the first appearance of delim.
201 The leading string up to delim is returned in piece (not including
202 delim), while the original string is cut from after the delimiter.
204 #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
206 string split(string const & a, string & piece, char delim);
208 /// Same as split but does not return a piece
209 string split(string const & a, char delim);
211 /// Same as split but uses the last delim.
212 string rsplit(string const & a, string & piece, char delim);