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"
29 /// Compare \p s and \p s2, ignoring the case.
30 /// Does not depend on the locale.
31 int compare_no_case(docstring const & s, docstring const & s2);
33 /// Compare \p s and \p s2, ignoring the case of ASCII characters only.
34 int compare_ascii_no_case(std::string const & s, std::string const & s2);
36 /// Compare \p s and \p s2, ignoring the case of ASCII characters only.
37 int compare_ascii_no_case(docstring const & s, docstring const & s2);
41 int compare(char const * a, char const * b)
43 #ifndef CXX_GLOBAL_CSTD
44 return std::strcmp(a, b);
52 int compare(char const * a, char const * b, unsigned int len)
54 #ifndef CXX_GLOBAL_CSTD
55 return std::strncmp(a, b, len);
57 return strncmp(a, b, len);
62 bool isStrInt(std::string const & str);
64 /// does the std::string represent an unsigned integer value ?
65 bool isStrUnsignedInt(std::string const & str);
68 bool isStrDbl(std::string const & str);
70 bool isHex(docstring const & str);
72 int hexToInt(docstring const & str);
74 /// is \p str pure ascii?
75 bool isAscii(docstring const & str);
77 /// is \p str pure ascii?
78 bool isAscii(std::string const & str);
81 * Changes the case of \p c to lowercase.
82 * Don't use this for non-ASCII characters, since it depends on the locale.
83 * This overloaded function is only implemented because the char_type variant
84 * would be used otherwise, and we assert in this function that \p c is in
87 char lowercase(char c);
90 * Changes the case of \p c to uppercase.
91 * Don't use this for non-ASCII characters, since it depends on the locale.
92 * This overloaded function is only implemented because the char_type variant
93 * would be used otherwise, and we assert in this function that \p c is in
96 char uppercase(char c);
98 /// Changes the case of \p c to lowercase.
99 /// Does not depend on the locale.
100 char_type lowercase(char_type c);
102 /// Changes the case of \p c to uppercase.
103 /// Does not depend on the locale.
104 char_type uppercase(char_type c);
106 /// same as lowercase(), but ignores locale
107 std::string const ascii_lowercase(std::string const &);
108 docstring const ascii_lowercase(docstring const &);
110 /// Changes the case of \p s to lowercase.
111 /// Does not depend on the locale.
112 docstring const lowercase(docstring const & s);
114 /// Changes the case of \p s to uppercase.
115 /// Does not depend on the locale.
116 docstring const uppercase(docstring const & s);
118 /// Does the string start with this prefix?
119 bool prefixIs(docstring const &, char_type);
121 /// Does the std::string start with this prefix?
122 bool prefixIs(std::string const &, std::string const &);
123 bool prefixIs(docstring const &, docstring const &);
125 /// Does the string end with this char?
126 bool suffixIs(std::string const &, char);
127 bool suffixIs(docstring const &, char_type);
129 /// Does the std::string end with this suffix?
130 bool suffixIs(std::string const &, std::string const &);
133 inline bool contains(std::string const & a, std::string const & b)
135 return a.find(b) != std::string::npos;
138 inline bool contains(docstring const & a, docstring const & b)
140 return a.find(b) != docstring::npos;
143 inline bool contains(std::string const & a, char b)
145 return a.find(b) != std::string::npos;
148 inline bool contains(docstring const & a, char_type b)
150 return a.find(b) != docstring::npos;
154 bool containsOnly(std::string const &, std::string const &);
156 /** Extracts a token from this string at the nth delim.
157 Doesn't modify the original string. Similar to strtok.
160 token("a;bc;d", ';', 1) == "bc";
161 token("a;bc;d", ';', 2) == "d";
164 std::string const token(std::string const & a, char delim, int n);
166 docstring const token(docstring const & a, char_type delim, int n);
168 /** Search a token in this string using the delim.
169 Doesn't modify the original string. Returns -1 in case of
173 tokenPos("a;bc;d", ';', "bc") == 1;
174 tokenPos("a;bc;d", ';', "d") == 2;
177 int tokenPos(std::string const & a, char delim, std::string const & tok);
180 /// Substitute all \a oldchar with \a newchar
181 std::string const subst(std::string const & a, char oldchar, char newchar);
183 /// Substitute all \a oldchar with \a newchar
184 docstring const subst(docstring const & a, char_type oldchar, char_type newchar);
186 /// substitutes all instances of \a oldstr with \a newstr
187 std::string const subst(std::string const & a,
188 std::string const & oldstr, std::string const & newstr);
190 /// substitutes all instances of \a oldstr with \a newstr
191 docstring const subst(docstring const & a,
192 docstring const & oldstr, docstring const & newstr);
194 /** Trims characters off the end and beginning of a string.
196 trim("ccabccc", "c") == "ab".
199 docstring const trim(docstring const & a, char const * p = " ");
201 /** Trims characters off the end and beginning of a string.
203 trim("ccabccc", "c") == "ab".
206 std::string const trim(std::string const & a, char const * p = " ");
208 /** Trims characters off the end of a string.
210 rtrim("abccc", "c") == "ab".
213 std::string const rtrim(std::string const & a, char const * p = " ");
214 docstring const rtrim(docstring const & a, char const * p = " ");
216 /** Trims characters off the beginning of a string.
218 ("ababcdef", "ab") = "cdef"
221 std::string const ltrim(std::string const & a, char const * p = " ");
222 docstring const ltrim(docstring const & a, char const * p = " ");
224 /** Splits the string given in the first argument at the first occurence
225 of the third argumnent, delim.
226 What precedes delim is returned in the second argument, piece; this
227 will be the whole of the string if no delimiter is found.
228 The return value is what follows delim, if anything. So the return
229 value is the null string if no delimiter is found.
233 ret = split(s1, s2, ';') -> ret = "bc", s2 == "a"
236 std::string const split(std::string const & a, std::string & piece, char delim);
237 docstring const split(docstring const & a, docstring & piece, char_type delim);
239 /// Same as split but does not return a piece
240 std::string const split(std::string const & a, char delim);
242 /// Same as split but uses the last delim.
243 std::string const rsplit(std::string const & a, std::string & piece, char delim);
245 /// Escapes non ASCII chars and other problematic characters that cause
246 /// problems in latex labels.
247 docstring const escape(docstring const & lab);
249 /// gives a vector of stringparts which have the delimiter delim
250 std::vector<std::string> const getVectorFromString(std::string const & str,
251 std::string const & delim = std::string(","));
252 std::vector<docstring> const getVectorFromString(docstring const & str,
253 docstring const & delim = from_ascii(","));
255 // the same vice versa
256 std::string const getStringFromVector(std::vector<std::string> const & vec,
257 std::string const & delim = std::string(","));
259 /// Search \p search_token in \p str and return the position if it is
260 /// found, else -1. The last item in \p str must be "".
261 int findToken(char const * const str[], std::string const & search_token);
263 /// Convert internal line endings to line endings as expected by the OS
264 docstring const externalLineEnding(docstring const & str);
266 /// Convert line endings in any formnat to internal line endings
267 docstring const internalLineEnding(docstring const & str);
270 template <class Arg1>
271 docstring bformat(docstring const & fmt, Arg1);
273 template <class Arg1, class Arg2>
274 docstring bformat(docstring const & fmt, Arg1, Arg2);
276 template <class Arg1, class Arg2, class Arg3>
277 docstring bformat(docstring const & fmt, Arg1, Arg2, Arg3);
279 template <class Arg1, class Arg2, class Arg3, class Arg4>
280 docstring bformat(docstring const & fmt, Arg1, Arg2, Arg3, Arg4);
283 } // namespace support