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.
27 int compare_no_case(string const & s, string const & s2);
30 int compare_ascii_no_case(string const & s, string const & s2);
33 int compare_no_case(string const & s, string const & s2, unsigned int len);
37 int compare(char const * a, char const * b)
39 #ifndef CXX_GLOBAL_CSTD
40 return std::strcmp(a, b);
48 int compare(char const * a, char const * b, unsigned int len)
50 #ifndef CXX_GLOBAL_CSTD
51 return std::strncmp(a, b, len);
53 return strncmp(a, b, len);
58 bool isStrInt(string const & str);
60 /// does the string represent an unsigned integer value ?
61 bool isStrUnsignedInt(string const & str);
64 int strToInt(string const & str);
66 /// convert string to an unsigned integer
67 unsigned int strToUnsignedInt(string const & str);
70 bool isStrDbl(string const & str);
73 double strToDbl(string const & str);
76 char lowercase(char c);
79 char uppercase(char c);
81 /// same as lowercase(), but ignores locale
82 string const ascii_lowercase(string const &);
85 string const lowercase(string const &);
88 string const uppercase(string const &);
90 /// Does the string start with this prefix?
91 bool prefixIs(string const &, string const &);
93 /// Does the string end with this char?
94 bool suffixIs(string const &, char);
96 /// Does the string end with this suffix?
97 bool suffixIs(string const &, string const &);
100 bool contains(string const & a, string const & b);
103 bool contains(string const & a, char b);
105 /// This should probably we rewritten to be more general.
106 class contains_functor {
108 typedef string first_argument_type;
109 typedef string second_argument_type;
110 typedef bool result_type;
112 bool operator()(string const & haystack, string const & needle) const {
113 return contains(haystack, needle);
119 bool containsOnly(string const &, string const &);
121 /** Extracts a token from this string at the nth delim.
122 Doesn't modify the original string. Similar to strtok.
125 token("a;bc;d", ';', 1) == "bc";
126 token("a;bc;d", ';', 2) == "d";
129 string const token(string const & a, char delim, int n);
132 /** Search a token in this string using the delim.
133 Doesn't modify the original string. Returns -1 in case of
137 tokenPos("a;bc;d", ';', "bc") == 1;
138 tokenPos("a;bc;d", ';', "d") == 2;
141 int tokenPos(string const & a, char delim, string const & tok);
144 /** Compares a string and a (simple) regular expression
145 The only element allowed is "*" for any string of characters
147 bool regexMatch(string const & a, string const & pattern);
149 /// Substitute all \a oldchar with \a newchar
150 string const subst(string const & a, char oldchar, char newchar);
152 /// substitutes all instances of \a oldstr with \a newstr
153 string const subst(string const & a,
154 string const & oldstr, string const & newstr);
156 /** Trims characters off the end and beginning of a string.
158 trim("ccabccc", "c") == "ab".
161 string const trim(string const & a, char const * p = " ");
163 /** Trims characters off the end of a string.
165 rtrim("abccc", "c") == "ab".
168 string const rtrim(string const & a, char const * p = " ");
170 /** Trims characters off the beginning of a string.
172 ltrim("ababcdef", "ab") = "cdef"
175 string const ltrim(string const & a, char const * p = " ");
177 /** Splits the string by the first delim.
178 Splits the string by the first appearance of delim.
179 The leading string up to delim is returned in piece (not including
180 delim), while the original string is cut from after the delimiter.
183 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
186 string const split(string const & a, string & piece, char delim);
188 /// Same as split but does not return a piece
189 string const split(string const & a, char delim);
191 /// Same as split but uses the last delim.
192 string const rsplit(string const & a, string & piece, char delim);
194 /// Escapes non ASCII chars
195 string const escape(string const & lab);
197 /// gives a vector of stringparts which have the delimiter delim
198 std::vector<string> const getVectorFromString(string const & str,
199 string const & delim = ",");
201 // the same vice versa
202 string const getStringFromVector(std::vector<string> const & vec,
203 string const & delim = ",");
205 // wrapper around boost::format using one argument %1$s
206 string bformat(string const & fmt, string const & arg1);
207 // arguments %1$s and %2$s
208 string bformat(string const & fmt, string const & arg1, string const & arg2);
209 // arguments %1$s and %2$s and %3$s
210 string bformat(string const & fmt, string const & arg1, string const & arg2,
211 string const & arg3);
212 // arguments %1$s and %2$s and %3$s and %4$s
213 string bformat(string const & fmt, string const & arg1, string const & arg2,
214 string const & arg3, string const & arg4);
216 } // namespace support