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.
24 int compare_no_case(string const & s, string const & s2);
27 int compare_ascii_no_case(string const & s, string const & s2);
30 int compare_no_case(string const & s, string const & s2, unsigned int len);
34 int compare(char const * a, char const * b)
36 #ifndef CXX_GLOBAL_CSTD
37 return std::strcmp(a, b);
45 int compare(char const * a, char const * b, unsigned int len)
47 #ifndef CXX_GLOBAL_CSTD
48 return std::strncmp(a, b, len);
50 return strncmp(a, b, len);
55 bool isStrInt(string const & str);
57 /// does the string represent an unsigned integer value ?
58 bool isStrUnsignedInt(string const & str);
61 int strToInt(string const & str);
63 /// convert string to an unsigned integer
64 unsigned int strToUnsignedInt(string const & str);
67 bool isStrDbl(string const & str);
70 double strToDbl(string const & str);
73 char lowercase(char c);
76 char uppercase(char c);
78 /// same as lowercase(), but ignores locale
79 string const ascii_lowercase(string const &);
82 string const lowercase(string const &);
85 string const uppercase(string const &);
87 /// Does the string start with this prefix?
88 bool prefixIs(string const &, string 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 &, string const &);
97 bool contains(string const & a, string const & b);
100 bool contains(string const & a, char b);
102 /// This should probably we rewritten to be more general.
103 class contains_functor {
105 typedef string first_argument_type;
106 typedef string second_argument_type;
107 typedef bool result_type;
109 bool operator()(string const & haystack, string const & needle) const {
110 return contains(haystack, needle);
116 bool containsOnly(string const &, string const &);
118 /** Extracts a token from this string at the nth delim.
119 Doesn't modify the original string. Similar to strtok.
122 token("a;bc;d", ';', 1) == "bc";
123 token("a;bc;d", ';', 2) == "d";
126 string const token(string const & a, char delim, int n);
129 /** Search a token in this string using the delim.
130 Doesn't modify the original string. Returns -1 in case of
134 tokenPos("a;bc;d", ';', "bc") == 1;
135 tokenPos("a;bc;d", ';', "d") == 2;
138 int tokenPos(string const & a, char delim, string const & tok);
141 /** Compares a string and a (simple) regular expression
142 The only element allowed is "*" for any string of characters
144 bool regexMatch(string const & a, string const & pattern);
146 /// Substitute all \a oldchar with \a newchar
147 string const subst(string const & a, char oldchar, char newchar);
149 /// substitutes all instances of \a oldstr with \a newstr
150 string const subst(string const & a,
151 string const & oldstr, string const & newstr);
153 /** Trims characters off the end and beginning of a string.
155 trim("ccabccc", "c") == "ab".
158 string const trim(string const & a, char const * p = " ");
160 /** Trims characters off the end of a string.
162 rtrim("abccc", "c") == "ab".
165 string const rtrim(string const & a, char const * p = " ");
167 /** Trims characters off the beginning of a string.
169 ltrim("ababcdef", "ab") = "cdef"
172 string const ltrim(string const & a, char const * p = " ");
174 /** Splits the string by the first delim.
175 Splits the string by the first appearance of delim.
176 The leading string up to delim is returned in piece (not including
177 delim), while the original string is cut from after the delimiter.
180 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
183 string const split(string const & a, string & piece, char delim);
185 /// Same as split but does not return a piece
186 string const split(string const & a, char delim);
188 /// Same as split but uses the last delim.
189 string const rsplit(string const & a, string & piece, char delim);
191 /// Escapes non ASCII chars
192 string const escape(string const & lab);
194 /// gives a vector of stringparts which have the delimiter delim
195 std::vector<string> const getVectorFromString(string const & str,
196 string const & delim = ",");
198 // the same vice versa
199 string const getStringFromVector(std::vector<string> const & vec,
200 string const & delim = ",");
202 // wrapper around boost::format using one argument %1$s
203 string bformat(string const & fmt, string const & arg1);
204 // arguments %1$s and %2$s
205 string bformat(string const & fmt, string const & arg1, string const & arg2);
206 // arguments %1$s and %2$s and %3$s
207 string bformat(string const & fmt, string const & arg1, string const & arg2,
208 string const & arg3);
209 // arguments %1$s and %2$s and %3$s and %4$s
210 string bformat(string const & fmt, string const & arg1, string const & arg2,
211 string const & arg3, string const & arg4);