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.
19 #include "support/docstring.h"
27 /// Compare \p s and \p s2, ignoring the case.
28 /// Caution: Depends on the locale
29 int compare_no_case(std::string const & s, std::string const & s2);
31 /// Compare \p s and \p s2, ignoring the case.
32 /// Does not depend on the locale.
33 int compare_no_case(docstring const & s, docstring const & s2);
35 /// Compare \p s and \p s2, ignoring the case of ASCII characters only.
36 int compare_ascii_no_case(std::string const & s, std::string const & s2);
38 /// Compare \p s and \p s2, ignoring the case of ASCII characters only.
39 int compare_ascii_no_case(docstring const & s, docstring const & s2);
41 /// Compare the first \p len characters of \p s and \p s2, ignoring the case.
42 /// Caution: Depends on the locale
43 int compare_no_case(std::string const & s, std::string const & s2, unsigned int len);
47 int compare(char const * a, char const * b)
49 #ifndef CXX_GLOBAL_CSTD
50 return std::strcmp(a, b);
58 int compare(char const * a, char const * b, unsigned int len)
60 #ifndef CXX_GLOBAL_CSTD
61 return std::strncmp(a, b, len);
63 return strncmp(a, b, len);
68 bool isStrInt(std::string const & str);
70 /// does the std::string represent an unsigned integer value ?
71 bool isStrUnsignedInt(std::string const & str);
74 bool isStrDbl(std::string const & str);
76 bool isHex(lyx::docstring const & str);
78 int hexToInt(lyx::docstring const & str);
80 /// is \p str pure ascii?
81 bool isAscii(docstring const & str);
83 /// Changes the case of \p c to lowercase.
84 /// Caution: Depends on the locale
85 char lowercase(char c);
87 /// Changes the case of \p c to uppercase.
88 /// Caution: Depends on the locale
89 char uppercase(char c);
91 /// Changes the case of \p c to lowercase.
92 /// Does not depend on the locale.
93 char_type lowercase(char_type c);
95 /// Changes the case of \p c to uppercase.
96 /// Does not depend on the locale.
97 char_type uppercase(char_type c);
99 /// same as lowercase(), but ignores locale
100 std::string const ascii_lowercase(std::string const &);
101 docstring const ascii_lowercase(docstring const &);
103 /// Changes the case of \p s to lowercase.
104 /// Caution: Depends on the locale
105 std::string const lowercase(std::string const & s);
107 /// Changes the case of \p s to lowercase.
108 /// Does not depend on the locale.
109 docstring const lowercase(docstring const & s);
111 /// Changes the case of \p s to uppercase.
112 /// Caution: Depends on the locale
113 std::string const uppercase(std::string const & s);
115 /// Does the string start with this prefix?
116 bool prefixIs(docstring const &, char_type);
118 /// Does the std::string start with this prefix?
119 bool prefixIs(std::string const &, std::string const &);
120 bool prefixIs(docstring const &, docstring const &);
122 /// Does the string end with this char?
123 bool suffixIs(std::string const &, char);
124 bool suffixIs(docstring const &, char_type);
126 /// Does the std::string end with this suffix?
127 bool suffixIs(std::string const &, std::string const &);
130 inline bool contains(std::string const & a, std::string const & b)
132 return a.find(b) != std::string::npos;
135 inline bool contains(docstring const & a, docstring const & b)
137 return a.find(b) != docstring::npos;
140 inline bool contains(std::string const & a, char b)
142 return a.find(b) != std::string::npos;
145 inline bool contains(docstring const & a, char_type b)
147 return a.find(b) != docstring::npos;
151 bool containsOnly(std::string const &, std::string const &);
153 /** Extracts a token from this string at the nth delim.
154 Doesn't modify the original string. Similar to strtok.
157 token("a;bc;d", ';', 1) == "bc";
158 token("a;bc;d", ';', 2) == "d";
161 std::string const token(std::string const & a, char delim, int n);
163 docstring const token(docstring const & a, char_type delim, int n);
165 /** Search a token in this string using the delim.
166 Doesn't modify the original string. Returns -1 in case of
170 tokenPos("a;bc;d", ';', "bc") == 1;
171 tokenPos("a;bc;d", ';', "d") == 2;
174 int tokenPos(std::string const & a, char delim, std::string const & tok);
177 /// Substitute all \a oldchar with \a newchar
178 std::string const subst(std::string const & a, char oldchar, char newchar);
180 /// Substitute all \a oldchar with \a newchar
181 docstring const subst(docstring const & a, char_type oldchar, char_type newchar);
183 /// substitutes all instances of \a oldstr with \a newstr
184 std::string const subst(std::string const & a,
185 std::string const & oldstr, std::string const & newstr);
187 /// substitutes all instances of \a oldstr with \a newstr
188 docstring const subst(docstring const & a,
189 docstring const & oldstr, docstring const & newstr);
191 /** Trims characters off the end and beginning of a string.
193 trim("ccabccc", "c") == "ab".
196 docstring const trim(docstring const & a, char const * p = " ");
198 /** Trims characters off the end and beginning of a string.
200 trim("ccabccc", "c") == "ab".
203 std::string const trim(std::string const & a, char const * p = " ");
205 /** Trims characters off the end of a string.
207 rtrim("abccc", "c") == "ab".
210 std::string const rtrim(std::string const & a, char const * p = " ");
211 docstring const rtrim(docstring const & a, char const * p = " ");
213 /** Trims characters off the beginning of a string.
215 ("ababcdef", "ab") = "cdef"
218 std::string const ltrim(std::string const & a, char const * p = " ");
219 docstring const ltrim(docstring const & a, char const * p = " ");
221 /** Splits the string by the first delim.
222 Splits the string by the first appearance of delim.
223 The leading string up to delim is returned in piece (not including
224 delim), while the original string is cut from after the delimiter.
227 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
230 std::string const split(std::string const & a, std::string & piece, char delim);
231 docstring const split(docstring const & a, docstring & piece, char_type delim);
233 /// Same as split but does not return a piece
234 std::string const split(std::string const & a, char delim);
236 /// Same as split but uses the last delim.
237 std::string const rsplit(std::string const & a, std::string & piece, char delim);
239 /// Escapes non ASCII chars and other problematic characters that cause
240 /// problems in latex labels.
241 docstring const escape(docstring const & lab);
243 /// gives a vector of stringparts which have the delimiter delim
244 std::vector<std::string> const getVectorFromString(std::string const & str,
245 std::string const & delim = std::string(","));
246 std::vector<docstring> const getVectorFromString(docstring const & str,
247 docstring const & delim = from_ascii(","));
249 // the same vice versa
250 std::string const getStringFromVector(std::vector<std::string> const & vec,
251 std::string const & delim = std::string(","));
253 /// Search \p search_token in \p str and return the position if it is
254 /// found, else -1. The last item in \p str must be "".
255 int findToken(char const * const str[], std::string const & search_token);
257 /// Convert internal line endings to line endings as expected by the OS
258 docstring const externalLineEnding(docstring const & str);
260 /// Convert line endings in any formnat to internal line endings
261 docstring const internalLineEnding(docstring const & str);
264 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
266 #include <boost/format.hpp>
269 docstring bformat(docstring const & fmt, Arg1 arg1)
271 return (boost::basic_format<char_type>(fmt) % arg1).str();
275 template<class Arg1, class Arg2>
276 docstring bformat(docstring const & fmt, Arg1 arg1, Arg2 arg2)
278 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
282 template<class Arg1, class Arg2, class Arg3>
283 docstring bformat(docstring const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
285 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
289 template<class Arg1, class Arg2, class Arg3, class Arg4>
290 docstring bformat(docstring const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
292 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
297 template <class Arg1>
298 docstring bformat(docstring const & fmt, Arg1);
300 template <class Arg1, class Arg2>
301 docstring bformat(docstring const & fmt, Arg1, Arg2);
303 template <class Arg1, class Arg2, class Arg3>
304 docstring bformat(docstring const & fmt, Arg1, Arg2, Arg3);
306 template <class Arg1, class Arg2, class Arg3, class Arg4>
307 docstring bformat(docstring const & fmt, Arg1, Arg2, Arg3, Arg4);
311 } // namespace support