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.
28 int compare_no_case(string const & s, string const & s2);
31 int compare_no_case(string const & s, string const & s2, unsigned int len);
35 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);
50 bool isStrInt(string const & str);
53 int strToInt(string const & str);
56 bool isStrDbl(string const & str);
59 double strToDbl(string const & str);
62 char lowercase(char c);
65 char uppercase(char c);
68 string lowercase(string const &);
71 string uppercase(string const &);
73 // convert T to string
76 string tostr(T const & t)
79 std::ostringstream ostr;
81 return ostr.str().c_str();
82 // We need to use the .c_str since we sometimes are using
83 // our own string class and that is not compatible with
84 // basic_string<char>. (of course we don't want this later)
86 // The buf is probably a bit large, but if we want to be safer
87 // we should leave it this big. As compiler/libs gets updated
88 // this part of the code will cease to be used and we loose
90 char buf[2048]; // a bit too large perhaps?
91 ostrstream ostr(buf, sizeof(buf));
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 const 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 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 subst(string const & a, char oldchar, char newchar);
166 /// Substitutes all instances of oldstr with newstr
167 string 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 strip(string const & a, char const c = ' ');
175 /** Strips characters of the beginning of a string.
176 #"cccba".frontstrip('c') = "ba"#. */
177 string frontStrip(string const & a, char const c = ' ');
179 /** Strips characters off the beginning of a string.
180 #"ababcdef".frontstrip("ab") = "cdef"# .*/
181 string 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 split(string const & a, string & piece, char delim);
192 /// Same as split but does not return a piece
193 string split(string const & a, char delim);
195 /// Same as split but uses the last delim.
196 string rsplit(string const & a, string & piece, char delim);