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.
13 //#warning verify this please. Lgb
16 size_t lstrlen(T const * t)
18 Assert(t); // we don't want null pointers
21 while(*r != 0) ++r, ++count;
26 //#warning verify this please. Lgb
29 T * lstrchr(T const * t, int c)
31 Assert(t); // we don't want null pointers
32 T * r = const_cast<T*>(t);
34 if (*r == c) return r; else ++r;
43 inline int compare_no_case(string const & s, string const & s2)
46 string::const_iterator p = s.begin();
47 string::const_iterator p2 = s2.begin();
49 while (p != s.end() && p2 != s2.end()) {
50 if (tolower(*p) != tolower(*p2))
51 return (tolower(*p) < tolower(*p2)) ? -1 : 1;
56 return s.size() - s2.size();
61 inline int compare_no_case(string const & s, string const & s2,
64 //#warning verify this func please
65 string::const_iterator p = s.begin();
66 string::const_iterator p2 = s2.begin();
68 while (i < len && p != s.end() && p2 != s2.end()) {
69 if (tolower(*p) != tolower(*p2))
70 return (tolower(*p) < tolower(*p2)) ? -1 : 1;
75 return s.size() - s2.size();
80 inline int compare(char const * a, char const * b)
87 inline int compare(char const * a, char const * b, unsigned int len)
89 return strncmp(a, b, len);
94 bool isStrInt(string const & str);
97 int strToInt(string const & str);
100 string lowercase(string const &);
106 string tostr(unsigned int);
109 string tostr(long l);
112 string tostr(unsigned long l);
115 string tostr(char c);
118 string tostr(void * v);
121 string tostr(bool b);
124 string tostr(double d);
126 /// Does the string start with this prefix?
127 bool prefixIs(string const &, char const *);
129 /// Does the string end with this char?
130 bool suffixIs(string const &, char);
132 /// Does the string end with this suffix?
133 bool suffixIs(string const &, char const *);
136 bool contains(char const * a, string const & b);
139 bool contains(string const & a, char const * b);
142 bool contains(string const & a, string const & b);
145 bool contains(char const * a, char const * b);
147 /// Counts how many of character c there is in a
148 int countChar(string const & a, char const c);
150 /** Extracts a token from this string at the nth delim.
151 Doesn't modify the original string. Similar to strtok.
153 #"a;bc;d".token(';', 1) == "bc";#
154 #"a;bc;d".token(';', 2) == "d";#
156 string token(string const & a, char delim, int n);
159 /** Search a token in this string using the delim.
160 Doesn't modify the original string. Returns -1 in case of
163 #"a;bc;d".tokenPos(';', "bc") == 1;#
164 #"a;bc;d".token(';', "d") == 2;#
166 int tokenPos(string const & a, char delim, string const & tok);
169 /** Compares a string and a (simple) regular expression
170 The only element allowed is "*" for any string of characters
172 bool regexMatch(string const & a, string const & pattern);
174 /// Substitute all "oldchar"s with "newchar"
175 string subst(string const & a, char oldchar, char newchar);
177 /// Substitutes all instances of oldstr with newstr
178 string subst(string const & a,
179 char const * oldstr, string const & newstr);
181 /** Strips characters off the end of a string.
182 #"abccc".strip('c') = "ab".#
184 string strip(string const & a, char const c = ' ');
186 /** Strips characters of the beginning of a string.
187 #"cccba".frontstrip('c') = "ba"#. */
188 string frontStrip(string const & a, char const c = ' ');
190 /** Strips characters off the beginning of a string.
191 #"ababcdef".frontstrip("ab") = "cdef"# .*/
192 string 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 split(string const & a, string & piece, char delim);
203 /// Same as split but does not return a piece
204 string split(string const & a, char delim);
206 /// Same as split but uses the last delim.
207 string rsplit(string const & a, string & piece, char delim);