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"
28 /// Compare \p s and \p s2, ignoring the case.
29 /// Does not depend on the locale.
30 int compare_no_case(docstring const & s, docstring const & s2);
32 /// Compare \p s and \p s2, ignoring the case of ASCII characters only.
33 int compare_ascii_no_case(std::string const & s, std::string const & s2);
35 /// Compare \p s and \p s2, ignoring the case of ASCII characters only.
36 int compare_ascii_no_case(docstring const & s, docstring const & s2);
40 int compare(char const * a, char const * b)
42 #ifndef CXX_GLOBAL_CSTD
43 return std::strcmp(a, b);
51 int compare(char const * a, char const * b, unsigned int len)
53 #ifndef CXX_GLOBAL_CSTD
54 return std::strncmp(a, b, len);
56 return strncmp(a, b, len);
61 bool isStrInt(std::string const & str);
63 /// does the std::string represent an unsigned integer value ?
64 bool isStrUnsignedInt(std::string const & str);
67 bool isStrDbl(std::string const & str);
69 bool isHex(docstring const & str);
71 int hexToInt(docstring const & str);
73 /// is \p str pure ascii?
74 bool isAscii(docstring const & str);
76 /// is \p str pure ascii?
77 bool isAscii(std::string const & str);
80 * Changes the case of \p c to lowercase.
81 * Don't use this for non-ASCII characters, since it depends on the locale.
82 * This overloaded function is only implemented because the char_type variant
83 * would be used otherwise, and we assert in this function that \p c is in
86 char lowercase(char c);
89 * Changes the case of \p c to uppercase.
90 * Don't use this for non-ASCII characters, since it depends on the locale.
91 * This overloaded function is only implemented because the char_type variant
92 * would be used otherwise, and we assert in this function that \p c is in
95 char uppercase(char c);
97 /// Changes the case of \p c to lowercase.
98 /// Does not depend on the locale.
99 char_type lowercase(char_type c);
101 /// Changes the case of \p c to uppercase.
102 /// Does not depend on the locale.
103 char_type uppercase(char_type c);
105 /// same as lowercase(), but ignores locale
106 std::string const ascii_lowercase(std::string const &);
107 docstring const ascii_lowercase(docstring const &);
109 /// Changes the case of \p s to lowercase.
110 /// Does not depend on the locale.
111 docstring const lowercase(docstring const & s);
113 /// Changes the case of \p s to uppercase.
114 /// Does not depend on the locale.
115 docstring const uppercase(docstring const & s);
117 /// Does the string start with this prefix?
118 bool prefixIs(docstring const &, char_type);
120 /// Does the std::string start with this prefix?
121 bool prefixIs(std::string const &, std::string const &);
122 bool prefixIs(docstring const &, docstring const &);
124 /// Does the string end with this char?
125 bool suffixIs(std::string const &, char);
126 bool suffixIs(docstring const &, char_type);
128 /// Does the std::string end with this suffix?
129 bool suffixIs(std::string const &, std::string const &);
132 inline bool contains(std::string const & a, std::string const & b)
134 return a.find(b) != std::string::npos;
137 inline bool contains(docstring const & a, docstring const & b)
139 return a.find(b) != docstring::npos;
142 inline bool contains(std::string const & a, char b)
144 return a.find(b) != std::string::npos;
147 inline bool contains(docstring const & a, char_type b)
149 return a.find(b) != docstring::npos;
153 bool containsOnly(std::string const &, std::string const &);
155 /** Extracts a token from this string at the nth delim.
156 Doesn't modify the original string. Similar to strtok.
159 token("a;bc;d", ';', 1) == "bc";
160 token("a;bc;d", ';', 2) == "d";
163 std::string const token(std::string const & a, char delim, int n);
165 docstring const token(docstring const & a, char_type delim, int n);
167 /** Search a token in this string using the delim.
168 Doesn't modify the original string. Returns -1 in case of
172 tokenPos("a;bc;d", ';', "bc") == 1;
173 tokenPos("a;bc;d", ';', "d") == 2;
176 int tokenPos(std::string const & a, char delim, std::string const & tok);
179 /// Substitute all \a oldchar with \a newchar
180 std::string const subst(std::string const & a, char oldchar, char newchar);
182 /// Substitute all \a oldchar with \a newchar
183 docstring const subst(docstring const & a, char_type oldchar, char_type newchar);
185 /// substitutes all instances of \a oldstr with \a newstr
186 std::string const subst(std::string const & a,
187 std::string const & oldstr, std::string const & newstr);
189 /// substitutes all instances of \a oldstr with \a newstr
190 docstring const subst(docstring const & a,
191 docstring const & oldstr, docstring const & newstr);
193 /** Trims characters off the end and beginning of a string.
195 trim("ccabccc", "c") == "ab".
198 docstring const trim(docstring const & a, char const * p = " ");
200 /** Trims characters off the end and beginning of a string.
202 trim("ccabccc", "c") == "ab".
205 std::string const trim(std::string const & a, char const * p = " ");
207 /** Trims characters off the end of a string.
209 rtrim("abccc", "c") == "ab".
212 std::string const rtrim(std::string const & a, char const * p = " ");
213 docstring const rtrim(docstring const & a, char const * p = " ");
215 /** Trims characters off the beginning of a string.
217 ("ababcdef", "ab") = "cdef"
220 std::string const ltrim(std::string const & a, char const * p = " ");
221 docstring const ltrim(docstring const & a, char const * p = " ");
223 /** Splits the string given in the first argument at the first occurence
224 of the third argumnent, delim.
225 What precedes delim is returned in the second argument, piece; this
226 will be the whole of the string if no delimiter is found.
227 The return value is what follows delim, if anything. So the return
228 value is the null string if no delimiter is found.
232 ret = split(s1, s2, ';') -> ret = "bc", s2 == "a"
235 std::string const split(std::string const & a, std::string & piece, char delim);
236 docstring const split(docstring const & a, docstring & piece, char_type delim);
238 /// Same as split but does not return a piece
239 std::string const split(std::string const & a, char delim);
241 /// Same as split but uses the last delim.
242 std::string const rsplit(std::string const & a, std::string & piece, char delim);
244 /// Escapes non ASCII chars and other problematic characters that cause
245 /// problems in latex labels.
246 docstring const escape(docstring const & lab);
248 /// gives a vector of stringparts which have the delimiter delim
249 std::vector<std::string> const getVectorFromString(std::string const & str,
250 std::string const & delim = std::string(","));
251 std::vector<docstring> const getVectorFromString(docstring const & str,
252 docstring const & delim = from_ascii(","));
254 // the same vice versa
255 std::string const getStringFromVector(std::vector<std::string> const & vec,
256 std::string const & delim = std::string(","));
258 /// Search \p search_token in \p str and return the position if it is
259 /// found, else -1. The last item in \p str must be "".
260 int findToken(char const * const str[], std::string const & search_token);
262 /// Convert internal line endings to line endings as expected by the OS
263 docstring const externalLineEnding(docstring const & str);
265 /// Convert line endings in any formnat to internal line endings
266 docstring const internalLineEnding(docstring const & str);
269 template <class Arg1>
270 docstring bformat(docstring const & fmt, Arg1);
272 template <class Arg1, class Arg2>
273 docstring bformat(docstring const & fmt, Arg1, Arg2);
275 template <class Arg1, class Arg2, class Arg3>
276 docstring bformat(docstring const & fmt, Arg1, Arg2, Arg3);
278 template <class Arg1, class Arg2, class Arg3, class Arg4>
279 docstring bformat(docstring const & fmt, Arg1, Arg2, Arg3, Arg4);
282 } // namespace support