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 string lowercase(string const &);
55 string uppercase(string const &);
57 /// convert T to string
60 string tostr(T const & t)
63 std::ostringstream ostr;
65 return ostr.str().c_str();
66 // We need to use the .c_str since we sometimes are using
67 // our own string class and that is not compatible with
68 // basic_string<char>. (of course we don't want this later)
70 // The buf is probably a bit large, but if we want to be safer
71 // we should leave it this big. As compiler/libs gets updated
72 // this part of the code will cease to be used and we loose
74 char buf[2048]; // a bit too large perhaps?
75 ostrstream ostr(buf, sizeof(buf));
84 return (b ? "true" : "false");
87 /// Does the string start with this prefix?
88 bool prefixIs(string const &, char const *);
90 /// Does the string end with this char?
91 bool suffixIs(string const &, char);
93 /// Does the string end with this suffix?
94 bool suffixIs(string const &, char const *);
97 bool contains(char const * a, string const & b);
100 bool contains(string const & a, char const * b);
103 bool contains(string const & a, string const & b);
106 bool contains(char const * a, char const * b);
108 /// Counts how many of character c there is in a
109 unsigned int countChar(string const & a, char const c);
111 /** Extracts a token from this string at the nth delim.
112 Doesn't modify the original string. Similar to strtok.
114 #"a;bc;d".token(';', 1) == "bc";#
115 #"a;bc;d".token(';', 2) == "d";#
117 string token(string const & a, char delim, int n);
120 /** Search a token in this string using the delim.
121 Doesn't modify the original string. Returns -1 in case of
124 #"a;bc;d".tokenPos(';', "bc") == 1;#
125 #"a;bc;d".token(';', "d") == 2;#
127 int tokenPos(string const & a, char delim, string const & tok);
130 /** Compares a string and a (simple) regular expression
131 The only element allowed is "*" for any string of characters
133 bool regexMatch(string const & a, string const & pattern);
135 /// Substitute all "oldchar"s with "newchar"
136 string subst(string const & a, char oldchar, char newchar);
138 /// Substitutes all instances of oldstr with newstr
139 string subst(string const & a,
140 char const * oldstr, string const & newstr);
142 /** Strips characters off the end of a string.
143 #"abccc".strip('c') = "ab".#
145 string strip(string const & a, char const c = ' ');
147 /** Strips characters of the beginning of a string.
148 #"cccba".frontstrip('c') = "ba"#. */
149 string frontStrip(string const & a, char const c = ' ');
151 /** Strips characters off the beginning of a string.
152 #"ababcdef".frontstrip("ab") = "cdef"# .*/
153 string frontStrip(string const & a, char const * p);
155 /** Splits the string by the first delim.
156 Splits the string by the first appearance of delim.
157 The leading string up to delim is returned in piece (not including
158 delim), while the original string is cut from after the delimiter.
160 #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
162 string split(string const & a, string & piece, char delim);
164 /// Same as split but does not return a piece
165 string split(string const & a, char delim);
167 /// Same as split but uses the last delim.
168 string rsplit(string const & a, string & piece, char delim);