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.
16 using std::ostringstream;
25 int compare_no_case(string const & s, string const & s2);
28 int compare_no_case(string const & s, string const & s2, unsigned int len);
32 int compare(char const * a, char const * b)
40 int compare(char const * a, char const * b, unsigned int len)
42 return strncmp(a, b, len);
47 bool isStrInt(string const & str);
50 int strToInt(string const & str);
53 string lowercase(string const &);
56 string uppercase(string const &);
58 /// convert T to string
61 string tostr(T const & t)
66 return ostr.str().c_str();
67 // We need to use the .c_str since we sometimes are using
68 // our own string class and that is not compatible with
69 // basic_string<char>. (of course we don't want this later)
71 // The buf is probably a bit large, but if we want to be safer
72 // we should leave it this big. As compiler/libs gets updated
73 // this part of the code will cease to be used and we loose
75 char buf[2048]; // a bit too large perhaps?
76 ostrstream ostr(buf, sizeof(buf));
85 return (b ? "true" : "false");
88 /// Does the string start with this prefix?
89 bool prefixIs(string const &, char const *);
91 /// Does the string end with this char?
92 bool suffixIs(string const &, char);
94 /// Does the string end with this suffix?
95 bool suffixIs(string const &, char const *);
98 bool contains(char const * a, string const & b);
101 bool contains(string const & a, char const * b);
104 bool contains(string const & a, string const & b);
107 bool contains(char const * a, char const * b);
109 /// Counts how many of character c there is in a
110 unsigned int countChar(string const & a, char const c);
112 /** Extracts a token from this string at the nth delim.
113 Doesn't modify the original string. Similar to strtok.
115 #"a;bc;d".token(';', 1) == "bc";#
116 #"a;bc;d".token(';', 2) == "d";#
118 string token(string const & a, char delim, int n);
121 /** Search a token in this string using the delim.
122 Doesn't modify the original string. Returns -1 in case of
125 #"a;bc;d".tokenPos(';', "bc") == 1;#
126 #"a;bc;d".token(';', "d") == 2;#
128 int tokenPos(string const & a, char delim, string const & tok);
131 /** Compares a string and a (simple) regular expression
132 The only element allowed is "*" for any string of characters
134 bool regexMatch(string const & a, string const & pattern);
136 /// Substitute all "oldchar"s with "newchar"
137 string subst(string const & a, char oldchar, char newchar);
139 /// Substitutes all instances of oldstr with newstr
140 string subst(string const & a,
141 char const * oldstr, string const & newstr);
143 /** Strips characters off the end of a string.
144 #"abccc".strip('c') = "ab".#
146 string strip(string const & a, char const c = ' ');
148 /** Strips characters of the beginning of a string.
149 #"cccba".frontstrip('c') = "ba"#. */
150 string frontStrip(string const & a, char const c = ' ');
152 /** Strips characters off the beginning of a string.
153 #"ababcdef".frontstrip("ab") = "cdef"# .*/
154 string frontStrip(string const & a, char const * p);
156 /** Splits the string by the first delim.
157 Splits the string by the first appearance of delim.
158 The leading string up to delim is returned in piece (not including
159 delim), while the original string is cut from after the delimiter.
161 #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
163 string split(string const & a, string & piece, char delim);
165 /// Same as split but does not return a piece
166 string split(string const & a, char delim);
168 /// Same as split but uses the last delim.
169 string rsplit(string const & a, string & piece, char delim);