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.
24 int compare_no_case(string const & s, string const & s2);
27 int compare_no_case(string const & s, string const & s2, unsigned int len);
31 int compare(char const * a, char const * b)
39 int compare(char const * a, char const * b, unsigned int len)
41 return strncmp(a, b, len);
46 bool isStrInt(string const & str);
49 int strToInt(string const & str);
52 bool isStrDbl(string const & str);
55 double strToDbl(string const & str);
58 string lowercase(string const &);
61 string uppercase(string const &);
63 // convert T to string
66 string tostr(T const & t)
69 std::ostringstream ostr;
71 return ostr.str().c_str();
72 // We need to use the .c_str since we sometimes are using
73 // our own string class and that is not compatible with
74 // basic_string<char>. (of course we don't want this later)
76 // The buf is probably a bit large, but if we want to be safer
77 // we should leave it this big. As compiler/libs gets updated
78 // this part of the code will cease to be used and we loose
80 char buf[2048]; // a bit too large perhaps?
81 ostrstream ostr(buf, sizeof(buf));
90 return (b ? "true" : "false");
93 /// Does the string start with this prefix?
94 bool prefixIs(string const &, char const *);
96 /// Does the string end with this char?
97 bool suffixIs(string const &, char);
99 /// Does the string end with this suffix?
100 bool suffixIs(string const &, char const *);
103 bool contains(char const * a, string const & b);
106 bool contains(string const & a, char const * b);
109 bool contains(string const & a, string const & b);
112 bool contains(char const * a, char const * b);
115 bool containsOnly(string const &, char const *);
118 bool containsOnly(string const &, string const &);
121 bool containsOnly(char const *, char const *);
124 bool containsOnly(char const *, string const &);
126 /// Counts how many of character c there is in a
127 unsigned int countChar(string const & a, char const c);
129 /** Extracts a token from this string at the nth delim.
130 Doesn't modify the original string. Similar to strtok.
132 #"a;bc;d".token(';', 1) == "bc";#
133 #"a;bc;d".token(';', 2) == "d";#
135 string token(string const & a, char delim, int n);
138 /** Search a token in this string using the delim.
139 Doesn't modify the original string. Returns -1 in case of
142 #"a;bc;d".tokenPos(';', "bc") == 1;#
143 #"a;bc;d".token(';', "d") == 2;#
145 int tokenPos(string const & a, char delim, string const & tok);
148 /** Compares a string and a (simple) regular expression
149 The only element allowed is "*" for any string of characters
151 bool regexMatch(string const & a, string const & pattern);
153 /// Substitute all "oldchar"s with "newchar"
154 string subst(string const & a, char oldchar, char newchar);
156 /// Substitutes all instances of oldstr with newstr
157 string subst(string const & a,
158 char const * oldstr, string const & newstr);
160 /** Strips characters off the end of a string.
161 #"abccc".strip('c') = "ab".#
163 string strip(string const & a, char const c = ' ');
165 /** Strips characters of the beginning of a string.
166 #"cccba".frontstrip('c') = "ba"#. */
167 string frontStrip(string const & a, char const c = ' ');
169 /** Strips characters off the beginning of a string.
170 #"ababcdef".frontstrip("ab") = "cdef"# .*/
171 string frontStrip(string const & a, char const * p);
173 /** Splits the string by the first delim.
174 Splits the string by the first appearance of delim.
175 The leading string up to delim is returned in piece (not including
176 delim), while the original string is cut from after the delimiter.
178 #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
180 string split(string const & a, string & piece, char delim);
182 /// Same as split but does not return a piece
183 string split(string const & a, char delim);
185 /// Same as split but uses the last delim.
186 string rsplit(string const & a, string & piece, char delim);