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);
39 bool isStrInt(std::string const & str);
41 /// does the std::string represent an unsigned integer value ?
42 bool isStrUnsignedInt(std::string const & str);
45 bool isStrDbl(std::string const & str);
47 /// does the string contain a digit?
48 bool hasDigit(docstring const & str);
50 bool isHex(docstring const & str);
52 int hexToInt(docstring const & str);
54 /// is \p str pure ascii?
55 bool isAscii(docstring const & str);
57 /// is \p str pure ascii?
58 bool isAscii(std::string const & str);
61 * Changes the case of \p c to lowercase.
62 * Don't use this for non-ASCII characters, since it depends on the locale.
63 * This overloaded function is only implemented because the char_type variant
64 * would be used otherwise, and we assert in this function that \p c is in
67 char lowercase(char c);
70 * Changes the case of \p c to uppercase.
71 * Don't use this for non-ASCII characters, since it depends on the locale.
72 * This overloaded function is only implemented because the char_type variant
73 * would be used otherwise, and we assert in this function that \p c is in
76 char uppercase(char c);
78 /// Changes the case of \p c to lowercase.
79 /// Does not depend on the locale.
80 char_type lowercase(char_type c);
82 /// Changes the case of \p c to uppercase.
83 /// Does not depend on the locale.
84 char_type uppercase(char_type c);
86 /// Checks if the supplied character is lower-case
87 bool isLowerCase(char_type ch);
89 /// Checks if the supplied character is upper-case
90 bool isUpperCase(char_type ch);
92 /// same as lowercase(), but ignores locale
93 std::string const ascii_lowercase(std::string const &);
94 docstring const ascii_lowercase(docstring const &);
96 /// Changes the case of \p s to lowercase.
97 /// Does not depend on the locale.
98 docstring const lowercase(docstring const & s);
100 /// Changes the case of \p s to uppercase.
101 /// Does not depend on the locale.
102 docstring const uppercase(docstring const & s);
104 /// Does str start with c?
105 bool prefixIs(docstring const & str, char_type c);
107 /// Does str start with pre?
108 bool prefixIs(std::string const & str, std::string const & pre);
109 bool prefixIs(docstring const & str, docstring const & pre);
111 /// Does the string end with this char?
112 bool suffixIs(std::string const &, char);
113 bool suffixIs(docstring const &, char_type);
115 /// Does the string end with this suffix?
116 bool suffixIs(std::string const &, std::string const &);
117 bool suffixIs(docstring const &, docstring const &);
119 /// Is b contained in a?
120 inline bool contains(std::string const & a, std::string const & b)
122 return a.find(b) != std::string::npos;
125 inline bool contains(docstring const & a, docstring const & b)
127 return a.find(b) != docstring::npos;
130 inline bool contains(std::string const & a, char b)
132 return a.find(b) != std::string::npos;
135 inline bool contains(docstring const & a, char_type b)
137 return a.find(b) != docstring::npos;
141 bool containsOnly(std::string const &, std::string const &);
143 /** Extracts a token from this string at the nth delim.
144 Doesn't modify the original string. Similar to strtok.
147 token("a;bc;d", ';', 1) == "bc";
148 token("a;bc;d", ';', 2) == "d";
151 std::string const token(std::string const & a, char delim, int n);
153 docstring const token(docstring const & a, char_type delim, int n);
155 /** Search a token in this string using the delim.
156 Doesn't modify the original string. Returns -1 in case of
160 tokenPos("a;bc;d", ';', "bc") == 1;
161 tokenPos("a;bc;d", ';', "d") == 2;
164 int tokenPos(std::string const & a, char delim, std::string const & tok);
165 int tokenPos(docstring const & a, char_type delim, docstring const & tok);
168 /// Substitute all \a oldchar with \a newchar
169 std::string const subst(std::string const & a, char oldchar, char newchar);
171 /// Substitute all \a oldchar with \a newchar
172 docstring const subst(docstring const & a, char_type oldchar, char_type newchar);
174 /// substitutes all instances of \a oldstr with \a newstr
175 std::string const subst(std::string const & a,
176 std::string const & oldstr, std::string const & newstr);
178 /// substitutes all instances of \a oldstr with \a newstr
179 docstring const subst(docstring const & a,
180 docstring const & oldstr, docstring const & newstr);
182 /// Count all occurences of char \a chr inside \a str
183 int count_char(docstring const & str, docstring::value_type chr);
185 /** Trims characters off the end and beginning of a string.
187 trim("ccabccc", "c") == "ab".
190 docstring const trim(docstring const & a, char const * p = " ");
192 /** Trims characters off the end and beginning of a string.
194 trim("ccabccc", "c") == "ab".
197 std::string const trim(std::string const & a, char const * p = " ");
199 /** Trims characters off the end of a string, removing any character
202 rtrim("abcde", "dec") == "ab".
205 std::string const rtrim(std::string const & a, char const * p = " ");
206 docstring const rtrim(docstring const & a, char const * p = " ");
208 /** Trims characters off the beginning of a string.
210 ("abbabcdef", "ab") = "cdef"
213 std::string const ltrim(std::string const & a, char const * p = " ");
214 docstring const ltrim(docstring const & a, char const * p = " ");
216 /** Splits the string given in the first argument at the first occurence
217 of the third argument, delim.
218 What precedes delim is returned in the second argument, piece; this
219 will be the whole of the string if no delimiter is found.
220 The return value is what follows delim, if anything. So the return
221 value is the null string if no delimiter is found.
225 ret = split(s1, s2, ';') -> ret = "bc", s2 == "a"
228 std::string const split(std::string const & a, std::string & piece, char delim);
229 docstring const split(docstring const & a, docstring & piece, char_type delim);
231 /// Same as split but does not return a piece
232 std::string const split(std::string const & a, char delim);
234 /// Same as split but uses the last delim.
235 std::string const rsplit(std::string const & a, std::string & piece, char delim);
236 docstring const rsplit(docstring const & a, char_type delim);
238 /// Escapes non ASCII chars and other problematic characters that cause
239 /// problems in latex labels.
240 docstring const escape(docstring const & lab);
242 /// Word-wraps the provided docstring, returning a line-broken string
243 /// of width no wider than width, with the string broken at spaces.
244 /// If the string cannot be broken appropriately, it returns something
245 /// with "..." at the end, again no wider than width.
246 /// We assume here that str does not contain newlines.
247 /// If indent is positive, then the first line is indented that many
248 /// spaces. If it is negative, then successive lines are indented, as
249 /// if the first line were "outdented".
250 docstring wrap(docstring const & str, int const indent = 0,
251 size_t const width = 80);
253 /// Like the preceding, except it is intended to operate on strings
254 /// that may contain embedded newlines.
255 /// \param numlines Don't return more than numlines lines. If numlines
256 /// is 0, we return everything.
257 docstring wrapParas(docstring const & str, int const indent = 0,
258 size_t const width = 80, size_t const maxlines = 10);
260 /// gives a vector of stringparts which have the delimiter delim
261 /// If \p keepempty is true, empty strings will be pushed to the vector as well
262 std::vector<std::string> const getVectorFromString(std::string const & str,
263 std::string const & delim = std::string(","),
264 bool keepempty = false);
265 std::vector<docstring> const getVectorFromString(docstring const & str,
266 docstring const & delim = from_ascii(","), bool keepempty = false);
268 /// the same vice versa
269 std::string const getStringFromVector(std::vector<std::string> const & vec,
270 std::string const & delim = std::string(","));
271 docstring const getStringFromVector(std::vector<docstring> const & vec,
272 docstring const & delim = from_ascii(","));
274 /// Search \p search_token in \p str and return the position if it is
275 /// found, else -1. The last item in \p str must be "".
276 int findToken(char const * const str[], std::string const & search_token);
278 template <class Arg1>
279 docstring bformat(docstring const & fmt, Arg1);
281 template <class Arg1, class Arg2>
282 docstring bformat(docstring const & fmt, Arg1, Arg2);
284 template <class Arg1, class Arg2, class Arg3>
285 docstring bformat(docstring const & fmt, Arg1, Arg2, Arg3);
287 template <class Arg1, class Arg2, class Arg3, class Arg4>
288 docstring bformat(docstring const & fmt, Arg1, Arg2, Arg3, Arg4);
291 template<> docstring bformat(docstring const & fmt, int arg1);
292 template<> docstring bformat(docstring const & fmt, long arg1);
293 template<> docstring bformat(docstring const & fmt, unsigned int arg1);
294 template<> docstring bformat(docstring const & fmt, docstring arg1);
295 template<> docstring bformat(docstring const & fmt, char * arg1);
296 template<> docstring bformat(docstring const & fmt, docstring arg1, docstring arg2);
297 template<> docstring bformat(docstring const & fmt, docstring arg1, int arg2);
298 template<> docstring bformat(docstring const & fmt, char const * arg1, docstring arg2);
299 template<> docstring bformat(docstring const & fmt, int arg1, int arg2);
300 template<> docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3);
301 template<> docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3, docstring arg4);
304 } // namespace support