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