3 /** String helper functions.
5 This is a collection of string helper functions that works
6 together with string (and later also with STL String. Some of these
7 would certainly benefit from a rewrite/optimization.
8 \author Lars Gullik Bjønnes
9 \author Jean-Marc Lasgouttes
26 int compare_no_case(string const & s, string const & s2);
29 int compare_ascii_no_case(string const & s, string const & s2);
32 int compare_no_case(string const & s, string const & s2, unsigned int len);
36 int compare(char const * a, char const * b)
38 #ifndef CXX_GLOBAL_CSTD
39 return std::strcmp(a, b);
47 int compare(char const * a, char const * b, unsigned int len)
49 #ifndef CXX_GLOBAL_CSTD
50 return std::strncmp(a, b, len);
52 return strncmp(a, b, len);
57 bool isStrInt(string const & str);
59 /// does the string represent an unsigned integer value ?
60 bool isStrUnsignedInt(string const & str);
63 int strToInt(string const & str);
65 /// convert string to an unsigned integer
66 unsigned int strToUnsignedInt(string const & str);
69 bool isStrDbl(string const & str);
72 double strToDbl(string const & str);
75 char lowercase(char c);
78 char uppercase(char c);
80 /// same as lowercase(), but ignores locale
81 string const ascii_lowercase(string const &);
84 string const lowercase(string const &);
87 string const uppercase(string const &);
89 /// convert \a T to string
92 string const tostr(T const & t)
96 return ostr.str().c_str();
97 // We need to use the .c_str since we sometimes are using
98 // our own string class and that is not compatible with
99 // basic_string<char>. (of course we don't want this later)
106 string const tostr(bool const & b)
108 return (b ? "true" : "false");
114 string const tostr(string const & s)
119 /// Does the string start with this prefix?
120 bool prefixIs(string const &, char const *);
122 /// Does the string start with this prefix?
123 bool prefixIs(string const &, string const &);
125 /// Does the string end with this char?
126 bool suffixIs(string const &, char);
128 /// Does the string end with this suffix?
129 bool suffixIs(string const &, char const *);
131 /// Does the string end with this suffix?
132 bool suffixIs(string const &, string const &);
135 bool contains(string const & a, string const & b);
138 bool contains(string const & a, char b);
140 /// This should probably we rewritten to be more general.
141 class contains_functor {
143 typedef string first_argument_type;
144 typedef string second_argument_type;
145 typedef bool result_type;
147 bool operator()(string const & haystack, string const & needle) const {
148 return contains(haystack, needle);
154 bool containsOnly(string const &, string const &);
156 /** Extracts a token from this string at the nth delim.
157 Doesn't modify the original string. Similar to strtok.
160 token("a;bc;d", ';', 1) == "bc";
161 token("a;bc;d", ';', 2) == "d";
164 string const token(string const & a, char delim, int n);
167 /** Search a token in this string using the delim.
168 Doesn't modify the original string. Returns -1 in case of
172 tokenPos("a;bc;d", ';', "bc") == 1;
173 tokenPos("a;bc;d", ';', "d") == 2;
176 int tokenPos(string const & a, char delim, string const & tok);
179 /** Compares a string and a (simple) regular expression
180 The only element allowed is "*" for any string of characters
182 bool regexMatch(string const & a, string const & pattern);
184 /// Substitute all \a oldchar with \a newchar
185 string const subst(string const & a, char oldchar, char newchar);
187 /// Substitutes all instances of \a oldstr with \a newstr
188 string const subst(string const & a,
189 char const * oldstr, string const & newstr);
191 /// substitutes all instances of \a oldstr with \a newstr
192 string const subst(string const & a,
193 string const & oldstr, string const & newstr);
195 /** Trims characters off the end and beginning of a string.
197 trim("ccabccc", "c") == "ab".
200 string const trim(string const & a, char const * p = " ");
202 /** Trims characters off the end of a string.
204 rtrim("abccc", "c") == "ab".
207 string const rtrim(string const & a, char const * p = " ");
209 /** Trims characters off the beginning of a string.
211 ltrim("ababcdef", "ab") = "cdef"
214 string const ltrim(string const & a, char const * p = " ");
216 /** Splits the string by the first delim.
217 Splits the string by the first appearance of delim.
218 The leading string up to delim is returned in piece (not including
219 delim), while the original string is cut from after the delimiter.
222 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
225 string const split(string const & a, string & piece, char delim);
227 /// Same as split but does not return a piece
228 string const split(string const & a, char delim);
230 /// Same as split but uses the last delim.
231 string const rsplit(string const & a, string & piece, char delim);
233 /// Escapes non ASCII chars
234 string const escape(string const & lab);
236 /// gives a vector of stringparts which have the delimiter delim
237 std::vector<string> const getVectorFromString(string const & str,
238 string const & delim = ",");
240 // the same vice versa
241 string const getStringFromVector(std::vector<string> const & vec,
242 string const & delim = ",");