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 &, char 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 &, char const *);
99 /// Does the string end with this suffix?
100 bool suffixIs(string const &, string const &);
103 bool contains(string const & a, string const & b);
106 bool contains(string const & a, char b);
108 /// This should probably we rewritten to be more general.
109 class contains_functor {
111 typedef string first_argument_type;
112 typedef string second_argument_type;
113 typedef bool result_type;
115 bool operator()(string const & haystack, string const & needle) const {
116 return contains(haystack, needle);
122 bool containsOnly(string const &, string const &);
124 /** Extracts a token from this string at the nth delim.
125 Doesn't modify the original string. Similar to strtok.
128 token("a;bc;d", ';', 1) == "bc";
129 token("a;bc;d", ';', 2) == "d";
132 string const token(string const & a, char delim, int n);
135 /** Search a token in this string using the delim.
136 Doesn't modify the original string. Returns -1 in case of
140 tokenPos("a;bc;d", ';', "bc") == 1;
141 tokenPos("a;bc;d", ';', "d") == 2;
144 int tokenPos(string const & a, char delim, string const & tok);
147 /** Compares a string and a (simple) regular expression
148 The only element allowed is "*" for any string of characters
150 bool regexMatch(string const & a, string const & pattern);
152 /// Substitute all \a oldchar with \a newchar
153 string const subst(string const & a, char oldchar, char newchar);
155 /// Substitutes all instances of \a oldstr with \a newstr
156 string const subst(string const & a,
157 char const * oldstr, string const & newstr);
159 /// substitutes all instances of \a oldstr with \a newstr
160 string const subst(string const & a,
161 string const & oldstr, string const & newstr);
163 /** Trims characters off the end and beginning of a string.
165 trim("ccabccc", "c") == "ab".
168 string const trim(string const & a, char const * p = " ");
170 /** Trims characters off the end of a string.
172 rtrim("abccc", "c") == "ab".
175 string const rtrim(string const & a, char const * p = " ");
177 /** Trims characters off the beginning of a string.
179 ltrim("ababcdef", "ab") = "cdef"
182 string const ltrim(string const & a, char const * p = " ");
184 /** Splits the string by the first delim.
185 Splits the string by the first appearance of delim.
186 The leading string up to delim is returned in piece (not including
187 delim), while the original string is cut from after the delimiter.
190 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
193 string const split(string const & a, string & piece, char delim);
195 /// Same as split but does not return a piece
196 string const split(string const & a, char delim);
198 /// Same as split but uses the last delim.
199 string const rsplit(string const & a, string & piece, char delim);
201 /// Escapes non ASCII chars
202 string const escape(string const & lab);
204 /// gives a vector of stringparts which have the delimiter delim
205 std::vector<string> const getVectorFromString(string const & str,
206 string const & delim = ",");
208 // the same vice versa
209 string const getStringFromVector(std::vector<string> const & vec,
210 string const & delim = ",");
212 // wrapper around boost::format using one argument %1$s
213 string bformat(char const * fmt, string const & arg1);
214 // arguments %1$s and %2$s
215 string bformat(char const * fmt, string const & arg1, string const & arg2);
216 // arguments %1$s and %2$s and %3$s
217 string bformat(char const * fmt, string const & arg1, string const & arg2,
218 string const & arg3);
219 // arguments %1$s and %2$s and %3$s and %4$s
220 string bformat(char const * fmt, string const & arg1, string const & arg2,
221 string const & arg3, string const & arg4);