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(std::string const & s, std::string const & s2);
30 int compare_ascii_no_case(std::string const & s, std::string const & s2);
33 int compare_no_case(std::string const & s, std::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(std::string const & str);
60 /// does the std::string represent an unsigned integer value ?
61 bool isStrUnsignedInt(std::string const & str);
64 int strToInt(std::string const & str);
66 /// convert string to an unsigned integer
67 unsigned int strToUnsignedInt(std::string const & str);
70 bool isStrDbl(std::string const & str);
73 double strToDbl(std::string const & str);
76 char lowercase(char c);
79 char uppercase(char c);
81 /// same as lowercase(), but ignores locale
82 std::string const ascii_lowercase(std::string const &);
85 std::string const lowercase(std::string const &);
88 std::string const uppercase(std::string const &);
90 /// Does the std::string start with this prefix?
91 bool prefixIs(std::string const &, std::string const &);
93 /// Does the string end with this char?
94 bool suffixIs(std::string const &, char);
96 /// Does the std::string end with this suffix?
97 bool suffixIs(std::string const &, std::string const &);
100 bool contains(std::string const & a, std::string const & b);
103 bool contains(std::string const & a, char b);
105 /// This should probably we rewritten to be more general.
106 struct contains_functor
107 : public std::binary_function<std::string, std::string, bool>
109 bool operator()(std::string const & haystack,
110 std::string const & needle) const
112 return contains(haystack, needle);
118 bool containsOnly(std::string const &, std::string const &);
120 /** Extracts a token from this string at the nth delim.
121 Doesn't modify the original string. Similar to strtok.
124 token("a;bc;d", ';', 1) == "bc";
125 token("a;bc;d", ';', 2) == "d";
128 std::string const token(std::string const & a, char delim, int n);
131 /** Search a token in this string using the delim.
132 Doesn't modify the original string. Returns -1 in case of
136 tokenPos("a;bc;d", ';', "bc") == 1;
137 tokenPos("a;bc;d", ';', "d") == 2;
140 int tokenPos(std::string const & a, char delim, std::string const & tok);
143 /// Substitute all \a oldchar with \a newchar
144 std::string const subst(std::string const & a, char oldchar, char newchar);
146 /// substitutes all instances of \a oldstr with \a newstr
147 std::string const subst(std::string const & a,
148 std::string const & oldstr, std::string const & newstr);
150 /** Trims characters off the end and beginning of a string.
152 trim("ccabccc", "c") == "ab".
155 std::string const trim(std::string const & a, char const * p = " ");
157 /** Trims characters off the end of a string.
159 rtrim("abccc", "c") == "ab".
162 std::string const rtrim(std::string const & a, char const * p = " ");
164 /** Trims characters off the beginning of a string.
166 ltrim("ababcdef", "ab") = "cdef"
169 std::string const ltrim(std::string const & a, char const * p = " ");
171 /** Splits the string by the first delim.
172 Splits the string by the first appearance of delim.
173 The leading string up to delim is returned in piece (not including
174 delim), while the original string is cut from after the delimiter.
177 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
180 std::string const split(std::string const & a, std::string & piece, char delim);
182 /// Same as split but does not return a piece
183 std::string const split(std::string const & a, char delim);
185 /// Same as split but uses the last delim.
186 std::string const rsplit(std::string const & a, std::string & piece, char delim);
188 /// Escapes non ASCII chars
189 std::string const escape(std::string const & lab);
191 /// gives a vector of stringparts which have the delimiter delim
192 std::vector<std::string> const getVectorFromString(std::string const & str,
193 std::string const & delim = std::string(","));
195 // the same vice versa
196 std::string const getStringFromVector(std::vector<std::string> const & vec,
197 std::string const & delim = std::string(","));
199 // wrapper around boost::format using one argument %1$s
200 std::string bformat(std::string const & fmt, std::string const & arg1);
201 // arguments %1$s and %2$s
202 std::string bformat(std::string const & fmt, std::string const & arg1, std::string const & arg2);
203 // arguments %1$d and %2$d
204 std::string bformat(std::string const & fmt, int arg1, int arg2);
205 // arguments %1$s and %2$s and %3$s
206 std::string bformat(std::string const & fmt, std::string const & arg1, std::string const & arg2,
207 std::string const & arg3);
208 // arguments %1$s and %2$s and %3$s and %4$s
209 std::string bformat(std::string const & fmt, std::string const & arg1, std::string const & arg2,
210 std::string const & arg3, std::string const & arg4);
212 } // namespace support