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.
33 int compare_no_case(string const & s, string const & s2);
36 int compare_no_case(string const & s, string const & s2, unsigned int len);
40 int compare(char const * a, char const * b)
47 int compare(char const * a, char const * b, unsigned int len)
49 return strncmp(a, b, len);
53 bool isStrInt(string const & str);
56 int strToInt(string const & str);
59 bool isStrDbl(string const & str);
62 double strToDbl(string const & str);
65 char lowercase(char c);
68 char uppercase(char c);
71 string const lowercase(string const &);
74 string const uppercase(string const &);
76 /// convert T to string
79 string const tostr(T const & t)
82 std::ostringstream ostr;
84 return ostr.str().c_str();
85 // We need to use the .c_str since we sometimes are using
86 // our own string class and that is not compatible with
87 // basic_string<char>. (of course we don't want this later)
89 // The buf is probably a bit large, but if we want to be safer
90 // we should leave it this big. As compiler/libs gets updated
91 // this part of the code will cease to be used and we loose
93 // char buf[2048]; // a bit too large perhaps?
94 // ostrstream ostr(buf, sizeof(buf));
103 string const tostr(bool b)
105 return (b ? "true" : "false");
108 /// Does the string start with this prefix?
109 bool prefixIs(string const &, char const *);
111 /// Does the string end with this char?
112 bool suffixIs(string const &, char);
114 /// Does the string end with this suffix?
115 bool suffixIs(string const &, char const *);
118 bool contains(char const * a, string const & b);
121 bool contains(string const & a, char const * b);
124 bool contains(string const & a, string const & b);
127 bool contains(char const * a, char const * b);
130 bool containsOnly(string const &, char const *);
133 bool containsOnly(string const &, string const &);
136 bool containsOnly(char const *, char const *);
139 bool containsOnly(char const *, string const &);
141 /// Counts how many of character c there is in a
142 unsigned int countChar(string const & a, char c);
144 /** Extracts a token from this string at the nth delim.
145 Doesn't modify the original string. Similar to strtok.
147 #"a;bc;d".token(';', 1) == "bc";#
148 #"a;bc;d".token(';', 2) == "d";#
150 string const token(string const & a, char delim, int n);
153 /** Search a token in this string using the delim.
154 Doesn't modify the original string. Returns -1 in case of
157 #"a;bc;d".tokenPos(';', "bc") == 1;#
158 #"a;bc;d".token(';', "d") == 2;#
160 int tokenPos(string const & a, char delim, string const & tok);
163 /** Compares a string and a (simple) regular expression
164 The only element allowed is "*" for any string of characters
166 bool regexMatch(string const & a, string const & pattern);
168 /// Substitute all "oldchar"s with "newchar"
169 string const subst(string const & a, char oldchar, char newchar);
171 /// Substitutes all instances of oldstr with newstr
172 string const subst(string const & a,
173 char const * oldstr, string const & newstr);
175 /** Strips characters off the end of a string.
176 #"abccc".strip('c') = "ab".#
178 string const strip(string const & a, char c = ' ');
180 /** Strips characters of the beginning of a string.
181 #"cccba".frontstrip('c') = "ba"#. */
182 string const frontStrip(string const & a, char c = ' ');
184 /** Strips characters off the beginning of a string.
185 #"ababcdef".frontstrip("ab") = "cdef"# .*/
186 string const frontStrip(string const & a, char const * p);
188 /** Splits the string by the first delim.
189 Splits the string by the first appearance of delim.
190 The leading string up to delim is returned in piece (not including
191 delim), while the original string is cut from after the delimiter.
193 #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
195 string const split(string const & a, string & piece, char delim);
197 /// Same as split but does not return a piece
198 string const split(string const & a, char delim);
200 /// Same as split but uses the last delim.
201 string const rsplit(string const & a, string & piece, char delim);