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.
29 int compare_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)
43 int compare(char const * a, char const * b, unsigned int len)
45 return strncmp(a, b, len);
49 bool isStrInt(string const & str);
52 int strToInt(string const & str);
55 bool isStrDbl(string const & str);
58 double strToDbl(string const & str);
61 char lowercase(char c);
64 char uppercase(char c);
67 string const lowercase(string const &);
70 string const uppercase(string const &);
72 /// convert T to string
75 string const tostr(T const & t)
78 std::ostringstream ostr;
80 return ostr.str().c_str();
81 // We need to use the .c_str since we sometimes are using
82 // our own string class and that is not compatible with
83 // basic_string<char>. (of course we don't want this later)
85 // The buf is probably a bit large, but if we want to be safer
86 // we should leave it this big. As compiler/libs gets updated
87 // this part of the code will cease to be used and we loose
89 char buf[2048]; // a bit too large perhaps?
90 ostrstream ostr(buf, sizeof(buf));
98 string const tostr(bool b)
100 return (b ? "true" : "false");
103 /// Does the string start with this prefix?
104 bool prefixIs(string const &, char const *);
106 /// Does the string end with this char?
107 bool suffixIs(string const &, char);
109 /// Does the string end with this suffix?
110 bool suffixIs(string const &, char const *);
113 bool contains(char const * a, string const & b);
116 bool contains(string const & a, char const * b);
119 bool contains(string const & a, string const & b);
122 bool contains(char const * a, char const * b);
125 bool containsOnly(string const &, char const *);
128 bool containsOnly(string const &, string const &);
131 bool containsOnly(char const *, char const *);
134 bool containsOnly(char const *, string const &);
136 /// Counts how many of character c there is in a
137 unsigned int countChar(string const & a, char c);
139 /** Extracts a token from this string at the nth delim.
140 Doesn't modify the original string. Similar to strtok.
142 #"a;bc;d".token(';', 1) == "bc";#
143 #"a;bc;d".token(';', 2) == "d";#
145 string const token(string const & a, char delim, int n);
148 /** Search a token in this string using the delim.
149 Doesn't modify the original string. Returns -1 in case of
152 #"a;bc;d".tokenPos(';', "bc") == 1;#
153 #"a;bc;d".token(';', "d") == 2;#
155 int tokenPos(string const & a, char delim, string const & tok);
158 /** Compares a string and a (simple) regular expression
159 The only element allowed is "*" for any string of characters
161 bool regexMatch(string const & a, string const & pattern);
163 /// Substitute all "oldchar"s with "newchar"
164 string const subst(string const & a, char oldchar, char newchar);
166 /// Substitutes all instances of oldstr with newstr
167 string const subst(string const & a,
168 char const * oldstr, string const & newstr);
170 /** Strips characters off the end of a string.
171 #"abccc".strip('c') = "ab".#
173 string const strip(string const & a, char c = ' ');
175 /** Strips characters of the beginning of a string.
176 #"cccba".frontstrip('c') = "ba"#. */
177 string const frontStrip(string const & a, char c = ' ');
179 /** Strips characters off the beginning of a string.
180 #"ababcdef".frontstrip("ab") = "cdef"# .*/
181 string const frontStrip(string const & a, char const * p);
183 /** Splits the string by the first delim.
184 Splits the string by the first appearance of delim.
185 The leading string up to delim is returned in piece (not including
186 delim), while the original string is cut from after the delimiter.
188 #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
190 string const split(string const & a, string & piece, char delim);
192 /// Same as split but does not return a piece
193 string const split(string const & a, char delim);
195 /// Same as split but uses the last delim.
196 string const rsplit(string const & a, string & piece, char delim);