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 int compare_no_case(string const & s, string const & s2);
48 int compare_no_case(string const & s, string const & s2, unsigned int len);
51 inline int compare(char const * a, char const * b)
58 inline int compare(char const * a, char const * b, unsigned int len)
60 return strncmp(a, b, len);
65 bool isStrInt(string const & str);
68 int strToInt(string const & str);
71 string lowercase(string const &);
74 string uppercase(string const &);
80 string tostr(unsigned int);
86 string tostr(unsigned long l);
92 string tostr(void * v);
98 string tostr(double d);
100 /// Does the string start with this prefix?
101 bool prefixIs(string const &, char const *);
103 /// Does the string end with this char?
104 bool suffixIs(string const &, char);
106 /// Does the string end with this suffix?
107 bool suffixIs(string const &, char const *);
110 bool contains(char const * a, string const & b);
113 bool contains(string const & a, char const * b);
116 bool contains(string const & a, string const & b);
119 bool contains(char const * a, char const * b);
121 /// Counts how many of character c there is in a
122 unsigned int countChar(string const & a, char const c);
124 /** Extracts a token from this string at the nth delim.
125 Doesn't modify the original string. Similar to strtok.
127 #"a;bc;d".token(';', 1) == "bc";#
128 #"a;bc;d".token(';', 2) == "d";#
130 string token(string const & a, char delim, int n);
133 /** Search a token in this string using the delim.
134 Doesn't modify the original string. Returns -1 in case of
137 #"a;bc;d".tokenPos(';', "bc") == 1;#
138 #"a;bc;d".token(';', "d") == 2;#
140 int tokenPos(string const & a, char delim, string const & tok);
143 /** Compares a string and a (simple) regular expression
144 The only element allowed is "*" for any string of characters
146 bool regexMatch(string const & a, string const & pattern);
148 /// Substitute all "oldchar"s with "newchar"
149 string subst(string const & a, char oldchar, char newchar);
151 /// Substitutes all instances of oldstr with newstr
152 string subst(string const & a,
153 char const * oldstr, string const & newstr);
155 /** Strips characters off the end of a string.
156 #"abccc".strip('c') = "ab".#
158 string strip(string const & a, char const c = ' ');
160 /** Strips characters of the beginning of a string.
161 #"cccba".frontstrip('c') = "ba"#. */
162 string frontStrip(string const & a, char const c = ' ');
164 /** Strips characters off the beginning of a string.
165 #"ababcdef".frontstrip("ab") = "cdef"# .*/
166 string frontStrip(string const & a, char const * p);
168 /** Splits the string by the first delim.
169 Splits the string by the first appearance of delim.
170 The leading string up to delim is returned in piece (not including
171 delim), while the original string is cut from after the delimiter.
173 #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
175 string split(string const & a, string & piece, char delim);
177 /// Same as split but does not return a piece
178 string split(string const & a, char delim);
180 /// Same as split but uses the last delim.
181 string rsplit(string const & a, string & piece, char delim);