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 //#include "LAssert.h"
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);
49 int compare_no_case(string const & s, string const & s2);
52 int compare_no_case(string const & s, string const & s2, unsigned int len);
55 inline int compare(char const * a, char const * b)
62 inline int compare(char const * a, char const * b, unsigned int len)
64 return strncmp(a, b, len);
69 bool isStrInt(string const & str);
72 int strToInt(string const & str);
75 string lowercase(string const &);
78 string uppercase(string const &);
84 string tostr(unsigned int);
90 string tostr(unsigned long l);
96 string tostr(void * v);
102 string tostr(double d);
104 /// Does the string start with this prefix?
105 bool prefixIs(string const &, char const *);
107 /// Does the string end with this char?
108 bool suffixIs(string const &, char);
110 /// Does the string end with this suffix?
111 bool suffixIs(string const &, char const *);
114 bool contains(char const * a, string const & b);
117 bool contains(string const & a, char const * b);
120 bool contains(string const & a, string const & b);
123 bool contains(char const * a, char const * b);
125 /// Counts how many of character c there is in a
126 unsigned int countChar(string const & a, char const c);
128 /** Extracts a token from this string at the nth delim.
129 Doesn't modify the original string. Similar to strtok.
131 #"a;bc;d".token(';', 1) == "bc";#
132 #"a;bc;d".token(';', 2) == "d";#
134 string token(string const & a, char delim, int n);
137 /** Search a token in this string using the delim.
138 Doesn't modify the original string. Returns -1 in case of
141 #"a;bc;d".tokenPos(';', "bc") == 1;#
142 #"a;bc;d".token(';', "d") == 2;#
144 int tokenPos(string const & a, char delim, string const & tok);
147 /** Compares a string and a (simple) regular expression
148 The only element allowed is "*" for any string of characters
150 bool regexMatch(string const & a, string const & pattern);
152 /// Substitute all "oldchar"s with "newchar"
153 string subst(string const & a, char oldchar, char newchar);
155 /// Substitutes all instances of oldstr with newstr
156 string subst(string const & a,
157 char const * oldstr, string const & newstr);
159 /** Strips characters off the end of a string.
160 #"abccc".strip('c') = "ab".#
162 string strip(string const & a, char const c = ' ');
164 /** Strips characters of the beginning of a string.
165 #"cccba".frontstrip('c') = "ba"#. */
166 string frontStrip(string const & a, char const c = ' ');
168 /** Strips characters off the beginning of a string.
169 #"ababcdef".frontstrip("ab") = "cdef"# .*/
170 string frontStrip(string const & a, char const * p);
172 /** Splits the string by the first delim.
173 Splits the string by the first appearance of delim.
174 The leading string up to delim is returned in piece (not including
175 delim), while the original string is cut from after the delimiter.
177 #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
179 string split(string const & a, string & piece, char delim);
181 /// Same as split but does not return a piece
182 string split(string const & a, char delim);
184 /// Same as split but uses the last delim.
185 string rsplit(string const & a, string & piece, char delim);