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