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