]> git.lyx.org Git - features.git/blob - src/support/lstrings.h
Fix character classification functions by using qt (bugs like 3270 and 1247)
[features.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 <vector>
22
23
24 namespace lyx {
25 namespace support {
26
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);
30
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);
34
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);
37
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);
40
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);
44
45 ///
46 inline
47 int compare(char const * a, char const * b)
48 {
49 #ifndef CXX_GLOBAL_CSTD
50         return std::strcmp(a, b);
51 #else
52         return strcmp(a, b);
53 #endif
54 }
55
56 ///
57 inline
58 int compare(char const * a, char const * b, unsigned int len)
59 {
60 #ifndef CXX_GLOBAL_CSTD
61         return std::strncmp(a, b, len);
62 #else
63         return strncmp(a, b, len);
64 #endif
65 }
66
67 ///
68 bool isStrInt(std::string const & str);
69
70 /// does the std::string represent an unsigned integer value ?
71 bool isStrUnsignedInt(std::string const & str);
72
73 ///
74 bool isStrDbl(std::string const & str);
75
76 bool isHex(lyx::docstring const & str);
77
78 int hexToInt(lyx::docstring const & str);
79
80 /// is \p str pure ascii?
81 bool isAscii(docstring const & str);
82
83 /// Changes the case of \p c to lowercase.
84 /// Caution: Depends on the locale
85 char lowercase(char c);
86
87 /// Changes the case of \p c to uppercase.
88 /// Caution: Depends on the locale
89 char uppercase(char c);
90
91 /// Changes the case of \p c to lowercase.
92 /// Does not depend on the locale.
93 char_type lowercase(char_type c);
94
95 /// Changes the case of \p c to uppercase.
96 /// Does not depend on the locale.
97 char_type uppercase(char_type c);
98
99 /// same as lowercase(), but ignores locale
100 std::string const ascii_lowercase(std::string const &);
101 docstring const ascii_lowercase(docstring const &);
102
103 /// Changes the case of \p s to lowercase.
104 /// Caution: Depends on the locale
105 std::string const lowercase(std::string const & s);
106
107 /// Changes the case of \p s to lowercase.
108 /// Does not depend on the locale.
109 docstring const lowercase(docstring const & s);
110
111 /// Changes the case of \p s to uppercase.
112 /// Caution: Depends on the locale
113 std::string const uppercase(std::string const & s);
114
115 /// Does the string start with this prefix?
116 bool prefixIs(docstring const &, char_type);
117
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 &);
121
122 /// Does the string end with this char?
123 bool suffixIs(std::string const &, char);
124 bool suffixIs(docstring const &, char_type);
125
126 /// Does the std::string end with this suffix?
127 bool suffixIs(std::string const &, std::string const &);
128
129 ///
130 inline bool contains(std::string const & a, std::string const & b)
131 {
132         return a.find(b) != std::string::npos;
133 }
134
135 inline bool contains(docstring const & a, docstring const & b)
136 {
137         return a.find(b) != docstring::npos;
138 }
139
140 inline bool contains(std::string const & a, char b)
141 {
142         return a.find(b) != std::string::npos;
143 }
144
145 inline bool contains(docstring const & a, char_type b)
146 {
147         return a.find(b) != docstring::npos;
148 }
149
150 ///
151 bool containsOnly(std::string const &, std::string const &);
152
153 /** Extracts a token from this string at the nth delim.
154     Doesn't modify the original string. Similar to strtok.
155     Example:
156     \code
157     token("a;bc;d", ';', 1) == "bc";
158     token("a;bc;d", ';', 2) == "d";
159     \endcode
160 */
161 std::string const token(std::string const & a, char delim, int n);
162
163 docstring const token(docstring const & a, char_type delim, int n);
164
165 /** Search a token in this string using the delim.
166     Doesn't modify the original string. Returns -1 in case of
167     failure.
168     Example:
169     \code
170     tokenPos("a;bc;d", ';', "bc") == 1;
171     tokenPos("a;bc;d", ';', "d") == 2;
172     \endcode
173 */
174 int tokenPos(std::string const & a, char delim, std::string const & tok);
175
176
177 /// Substitute all \a oldchar with \a newchar
178 std::string const subst(std::string const & a, char oldchar, char newchar);
179
180 /// Substitute all \a oldchar with \a newchar
181 docstring const subst(docstring const & a, char_type oldchar, char_type newchar);
182
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);
186
187 /// substitutes all instances of \a oldstr with \a newstr
188 docstring const subst(docstring const & a,
189                 docstring const & oldstr, docstring const & newstr);
190
191 /** Trims characters off the end and beginning of a string.
192     \code
193     trim("ccabccc", "c") == "ab".
194     \endcode
195 */
196 docstring const trim(docstring const & a, char const * p = " ");
197
198 /** Trims characters off the end and beginning of a string.
199     \code
200     trim("ccabccc", "c") == "ab".
201     \endcode
202 */
203 std::string const trim(std::string const & a, char const * p = " ");
204
205 /** Trims characters off the end of a string.
206     \code
207     rtrim("abccc", "c") == "ab".
208     \endcode
209 */
210 std::string const rtrim(std::string const & a, char const * p = " ");
211 docstring const rtrim(docstring const & a, char const * p = " ");
212
213 /** Trims characters off the beginning of a string.
214     \code
215    ("ababcdef", "ab") = "cdef"
216     \endcode
217 */
218 std::string const ltrim(std::string const & a, char const * p = " ");
219 docstring const ltrim(docstring const & a, char const * p = " ");
220
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.
225     Example:
226     \code
227     s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
228     \endcode
229 */
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);
232
233 /// Same as split but does not return a piece
234 std::string const split(std::string const & a, char delim);
235
236 /// Same as split but uses the last delim.
237 std::string const rsplit(std::string const & a, std::string & piece, char delim);
238
239 /// Escapes non ASCII chars and other problematic characters that cause
240 /// problems in latex labels.
241 docstring const escape(docstring const & lab);
242
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(","));
248
249 // the same vice versa
250 std::string const getStringFromVector(std::vector<std::string> const & vec,
251                                  std::string const & delim = std::string(","));
252
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);
256
257 /// Convert internal line endings to line endings as expected by the OS
258 docstring const externalLineEnding(docstring const & str);
259
260 /// Convert line endings in any formnat to internal line endings
261 docstring const internalLineEnding(docstring const & str);
262
263
264 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
265
266 #include <boost/format.hpp>
267
268 template<class Arg1>
269 docstring bformat(docstring const & fmt, Arg1 arg1)
270 {
271         return (boost::basic_format<char_type>(fmt) % arg1).str();
272 }
273
274
275 template<class Arg1, class Arg2>
276 docstring bformat(docstring const & fmt, Arg1 arg1, Arg2 arg2)
277 {
278         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
279 }
280
281
282 template<class Arg1, class Arg2, class Arg3>
283 docstring bformat(docstring const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
284 {
285         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
286 }
287
288
289 template<class Arg1, class Arg2, class Arg3, class Arg4>
290 docstring bformat(docstring const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
291 {
292         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
293 }
294
295 #else
296
297 template <class Arg1>
298 docstring bformat(docstring const & fmt, Arg1);
299
300 template <class Arg1, class Arg2>
301 docstring bformat(docstring const & fmt, Arg1, Arg2);
302
303 template <class Arg1, class Arg2, class Arg3>
304 docstring bformat(docstring const & fmt, Arg1, Arg2, Arg3);
305
306 template <class Arg1, class Arg2, class Arg3, class Arg4>
307 docstring bformat(docstring const & fmt, Arg1, Arg2, Arg3, Arg4);
308
309 #endif
310
311 } // namespace support
312 } // namespace lyx
313
314 #endif