4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS
12 * A collection of string helper functions that works with string.
13 * Some of these would certainly benefit from a rewrite/optimization.
30 int compare_no_case(string const & s, string const & s2);
33 int compare_ascii_no_case(string const & s, string const & s2);
36 int compare_no_case(string const & s, string const & s2, unsigned int len);
40 int compare(char const * a, char const * b)
42 #ifndef CXX_GLOBAL_CSTD
43 return std::strcmp(a, b);
51 int compare(char const * a, char const * b, unsigned int len)
53 #ifndef CXX_GLOBAL_CSTD
54 return std::strncmp(a, b, len);
56 return strncmp(a, b, len);
61 bool isStrInt(string const & str);
63 /// does the string represent an unsigned integer value ?
64 bool isStrUnsignedInt(string const & str);
67 int strToInt(string const & str);
69 /// convert string to an unsigned integer
70 unsigned int strToUnsignedInt(string const & str);
73 bool isStrDbl(string const & str);
76 double strToDbl(string const & str);
79 char lowercase(char c);
82 char uppercase(char c);
84 /// same as lowercase(), but ignores locale
85 string const ascii_lowercase(string const &);
88 string const lowercase(string const &);
91 string const uppercase(string const &);
93 /// convert \a T to string
96 string const tostr(T const & t)
100 return STRCONV(ostr.str());
101 // We need to use the .c_str since we sometimes are using
102 // our own string class and that is not compatible with
103 // basic_string<char>. (of course we don't want this later)
110 string const tostr(bool const & b)
112 return (b ? "true" : "false");
118 string const tostr(string const & s)
123 /// Does the string start with this prefix?
124 bool prefixIs(string const &, char const *);
126 /// Does the string start with this prefix?
127 bool prefixIs(string const &, string const &);
129 /// Does the string end with this char?
130 bool suffixIs(string const &, char);
132 /// Does the string end with this suffix?
133 bool suffixIs(string const &, char const *);
135 /// Does the string end with this suffix?
136 bool suffixIs(string const &, string const &);
139 bool contains(string const & a, string const & b);
142 bool contains(string const & a, char b);
144 /// This should probably we rewritten to be more general.
145 class contains_functor {
147 typedef string first_argument_type;
148 typedef string second_argument_type;
149 typedef bool result_type;
151 bool operator()(string const & haystack, string const & needle) const {
152 return contains(haystack, needle);
158 bool containsOnly(string const &, string const &);
160 /** Extracts a token from this string at the nth delim.
161 Doesn't modify the original string. Similar to strtok.
164 token("a;bc;d", ';', 1) == "bc";
165 token("a;bc;d", ';', 2) == "d";
168 string const token(string const & a, char delim, int n);
171 /** Search a token in this string using the delim.
172 Doesn't modify the original string. Returns -1 in case of
176 tokenPos("a;bc;d", ';', "bc") == 1;
177 tokenPos("a;bc;d", ';', "d") == 2;
180 int tokenPos(string const & a, char delim, string const & tok);
183 /** Compares a string and a (simple) regular expression
184 The only element allowed is "*" for any string of characters
186 bool regexMatch(string const & a, string const & pattern);
188 /// Substitute all \a oldchar with \a newchar
189 string const subst(string const & a, char oldchar, char newchar);
191 /// Substitutes all instances of \a oldstr with \a newstr
192 string const subst(string const & a,
193 char const * oldstr, string const & newstr);
195 /// substitutes all instances of \a oldstr with \a newstr
196 string const subst(string const & a,
197 string const & oldstr, string const & newstr);
199 /** Trims characters off the end and beginning of a string.
201 trim("ccabccc", "c") == "ab".
204 string const trim(string const & a, char const * p = " ");
206 /** Trims characters off the end of a string.
208 rtrim("abccc", "c") == "ab".
211 string const rtrim(string const & a, char const * p = " ");
213 /** Trims characters off the beginning of a string.
215 ltrim("ababcdef", "ab") = "cdef"
218 string const ltrim(string const & a, char const * p = " ");
220 /** Splits the string by the first delim.
221 Splits the string by the first appearance of delim.
222 The leading string up to delim is returned in piece (not including
223 delim), while the original string is cut from after the delimiter.
226 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
229 string const split(string const & a, string & piece, char delim);
231 /// Same as split but does not return a piece
232 string const split(string const & a, char delim);
234 /// Same as split but uses the last delim.
235 string const rsplit(string const & a, string & piece, char delim);
237 /// Escapes non ASCII chars
238 string const escape(string const & lab);
240 /// gives a vector of stringparts which have the delimiter delim
241 std::vector<string> const getVectorFromString(string const & str,
242 string const & delim = ",");
244 // the same vice versa
245 string const getStringFromVector(std::vector<string> const & vec,
246 string const & delim = ",");