]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.h
add generic helper class for calling functions in gui thread
[lyx.git] / src / support / lstrings.h
1 // -*- C++ -*-
2 /**
3  * \file lstrings.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  *
10  * Full author contact details are available in file CREDITS.
11  *
12  * A collection of string helper functions that works with string.
13  * Some of these would certainly benefit from a rewrite/optimization.
14  */
15
16 #ifndef LSTRINGS_H
17 #define LSTRINGS_H
18
19 #include "support/docstring.h"
20
21 #include <string>
22 #include <vector>
23
24
25 namespace lyx {
26 namespace support {
27
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);
31
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);
34
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);
37
38 ///
39 bool isStrInt(std::string const & str);
40
41 /// does the std::string represent an unsigned integer value ?
42 bool isStrUnsignedInt(std::string const & str);
43
44 ///
45 bool isStrDbl(std::string const & str);
46
47 /// does the string contain a digit?
48 bool hasDigit(docstring const & str);
49
50 bool isHex(docstring const & str);
51
52 int hexToInt(docstring const & str);
53
54 /// is \p str pure ascii?
55 bool isAscii(docstring const & str);
56
57 /// is \p str pure ascii?
58 bool isAscii(std::string const & str);
59
60 /**
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
65  * the ASCII range.
66  */
67 char lowercase(char c);
68
69 /**
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
74  * the ASCII range.
75  */
76 char uppercase(char c);
77
78 /// Changes the case of \p c to lowercase.
79 /// Does not depend on the locale.
80 char_type lowercase(char_type c);
81
82 /// Changes the case of \p c to uppercase.
83 /// Does not depend on the locale.
84 char_type uppercase(char_type c);
85
86 /// Checks if the supplied character is lower-case
87 bool isLowerCase(char_type ch);
88
89 /// Checks if the supplied character is upper-case
90 bool isUpperCase(char_type ch);
91
92 /// same as lowercase(), but ignores locale
93 std::string const ascii_lowercase(std::string const &);
94 docstring const ascii_lowercase(docstring const &);
95
96 /// Changes the case of \p s to lowercase.
97 /// Does not depend on the locale.
98 docstring const lowercase(docstring const & s);
99
100 /// Changes the case of \p s to uppercase.
101 /// Does not depend on the locale.
102 docstring const uppercase(docstring const & s);
103
104 /// Does str start with c?
105 bool prefixIs(docstring const & str, char_type c);
106
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);
110
111 /// Does the string end with this char?
112 bool suffixIs(std::string const &, char);
113 bool suffixIs(docstring const &, char_type);
114
115 /// Does the string end with this suffix?
116 bool suffixIs(std::string const &, std::string const &);
117 bool suffixIs(docstring const &, docstring const &);
118
119 /// Is b contained in a?
120 inline bool contains(std::string const & a, std::string const & b)
121 {
122         return a.find(b) != std::string::npos;
123 }
124
125 inline bool contains(docstring const & a, docstring const & b)
126 {
127         return a.find(b) != docstring::npos;
128 }
129
130 inline bool contains(std::string const & a, char b)
131 {
132         return a.find(b) != std::string::npos;
133 }
134
135 inline bool contains(docstring const & a, char_type b)
136 {
137         return a.find(b) != docstring::npos;
138 }
139
140 ///
141 bool containsOnly(std::string const &, std::string const &);
142
143 /** Extracts a token from this string at the nth delim.
144     Doesn't modify the original string. Similar to strtok.
145     Example:
146     \code
147     token("a;bc;d", ';', 1) == "bc";
148     token("a;bc;d", ';', 2) == "d";
149     \endcode
150 */
151 std::string const token(std::string const & a, char delim, int n);
152
153 docstring const token(docstring const & a, char_type delim, int n);
154
155 /** Search a token in this string using the delim.
156     Doesn't modify the original string. Returns -1 in case of
157     failure.
158     Example:
159     \code
160     tokenPos("a;bc;d", ';', "bc") == 1;
161     tokenPos("a;bc;d", ';', "d") == 2;
162     \endcode
163 */
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);
166
167
168 /// Substitute all \a oldchar with \a newchar
169 std::string const subst(std::string const & a, char oldchar, char newchar);
170
171 /// Substitute all \a oldchar with \a newchar
172 docstring const subst(docstring const & a, char_type oldchar, char_type newchar);
173
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);
177
178 /// substitutes all instances of \a oldstr with \a newstr
179 docstring const subst(docstring const & a,
180                 docstring const & oldstr, docstring const & newstr);
181
182 /// Count all occurences of char \a chr inside \a str
183 int count_char(docstring const & str, docstring::value_type chr);
184
185 /** Trims characters off the end and beginning of a string.
186     \code
187     trim("ccabccc", "c") == "ab".
188     \endcode
189 */
190 docstring const trim(docstring const & a, char const * p = " ");
191
192 /** Trims characters off the end and beginning of a string.
193     \code
194     trim("ccabccc", "c") == "ab".
195     \endcode
196 */
197 std::string const trim(std::string const & a, char const * p = " ");
198
199 /** Trims characters off the end of a string, removing any character
200     in p.
201     \code
202     rtrim("abcde", "dec") == "ab".
203     \endcode
204 */
205 std::string const rtrim(std::string const & a, char const * p = " ");
206 docstring const rtrim(docstring const & a, char const * p = " ");
207
208 /** Trims characters off the beginning of a string.
209     \code
210    ("abbabcdef", "ab") = "cdef"
211     \endcode
212 */
213 std::string const ltrim(std::string const & a, char const * p = " ");
214 docstring const ltrim(docstring const & a, char const * p = " ");
215
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.
222     Examples:
223     \code
224     s1= "a;bc"; s2= ""
225     ret = split(s1, s2, ';') -> ret = "bc", s2 == "a"
226     \endcode
227  */
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);
230
231 /// Same as split but does not return a piece
232 std::string const split(std::string const & a, char delim);
233
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);
237
238 /// Escapes non ASCII chars and other problematic characters that cause
239 /// problems in latex labels.
240 docstring const escape(docstring const & lab);
241
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);
252
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);
259
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);
267
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(","));
273
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);
277
278 template <class Arg1>
279 docstring bformat(docstring const & fmt, Arg1);
280
281 template <class Arg1, class Arg2>
282 docstring bformat(docstring const & fmt, Arg1, Arg2);
283
284 template <class Arg1, class Arg2, class Arg3>
285 docstring bformat(docstring const & fmt, Arg1, Arg2, Arg3);
286
287 template <class Arg1, class Arg2, class Arg3, class Arg4>
288 docstring bformat(docstring const & fmt, Arg1, Arg2, Arg3, Arg4);
289
290
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);
302
303
304 } // namespace support
305 } // namespace lyx
306
307 #endif