]> git.lyx.org Git - features.git/blob - src/support/lstrings.h
A bunch of conversion to docstring.
[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/types.h"
20
21 #include <vector>
22
23
24 namespace lyx {
25 namespace support {
26
27 ///
28 int compare_no_case(std::string const & s, std::string const & s2);
29 int compare_no_case(lyx::docstring const & s, lyx::docstring const & s2);
30
31 ///
32 int compare_ascii_no_case(std::string const & s, std::string const & s2);
33
34 ///
35 int compare_no_case(std::string const & s, std::string const & s2, unsigned int len);
36
37 ///
38 inline
39 int compare(char const * a, char const * b)
40 {
41 #ifndef CXX_GLOBAL_CSTD
42         return std::strcmp(a, b);
43 #else
44         return strcmp(a, b);
45 #endif
46 }
47
48 ///
49 inline
50 int compare(char const * a, char const * b, unsigned int len)
51 {
52 #ifndef CXX_GLOBAL_CSTD
53         return std::strncmp(a, b, len);
54 #else
55         return strncmp(a, b, len);
56 #endif
57 }
58
59 ///
60 bool isStrInt(std::string const & str);
61
62 /// does the std::string represent an unsigned integer value ?
63 bool isStrUnsignedInt(std::string const & str);
64
65 ///
66 bool isStrDbl(std::string const & str);
67
68 ///
69 char lowercase(char c);
70
71 ///
72 char uppercase(char c);
73
74 /// changes the case only if c is a one-byte char
75 char_type lowercase(char_type c);
76
77 /// changes the case only if c is a one-byte char
78 char_type uppercase(char_type c);
79
80 /// same as lowercase(), but ignores locale
81 std::string const ascii_lowercase(std::string const &);
82
83 ///
84 std::string const lowercase(std::string const &);
85
86 ///
87 std::string const uppercase(std::string const &);
88
89 /// Does the std::string start with this prefix?
90 bool prefixIs(std::string const &, std::string const &);
91
92 /// Does the string end with this char?
93 bool suffixIs(std::string const &, char);
94
95 /// Does the std::string end with this suffix?
96 bool suffixIs(std::string const &, std::string const &);
97
98 ///
99 template <typename B>
100 bool contains(std::string const & a, B b)
101 {
102         return a.find(b) != std::string::npos;
103 }
104
105 template <typename B>
106 bool contains(lyx::docstring const & a, B b)
107 {
108         return a.find(b) != lyx::docstring::npos;
109 }
110
111 ///
112 bool containsOnly(std::string const &, std::string const &);
113
114 /** Extracts a token from this string at the nth delim.
115     Doesn't modify the original string. Similar to strtok.
116     Example:
117     \code
118     token("a;bc;d", ';', 1) == "bc";
119     token("a;bc;d", ';', 2) == "d";
120     \endcode
121 */
122 std::string const token(std::string const & a, char delim, int n);
123
124 lyx::docstring const token(lyx::docstring const & a,
125                            lyx::char_type delim, int n);
126
127 /** Search a token in this string using the delim.
128     Doesn't modify the original string. Returns -1 in case of
129     failure.
130     Example:
131     \code
132     tokenPos("a;bc;d", ';', "bc") == 1;
133     tokenPos("a;bc;d", ';', "d") == 2;
134     \endcode
135 */
136 int tokenPos(std::string const & a, char delim, std::string const & tok);
137
138
139 /// Substitute all \a oldchar with \a newchar
140 std::string const subst(std::string const & a, char oldchar, char newchar);
141
142 /// Substitute all \a oldchar with \a newchar
143 lyx::docstring const subst(docstring const & a,
144                                                    lyx::char_type oldchar, lyx::char_type newchar);
145
146 /// substitutes all instances of \a oldstr with \a newstr
147 std::string const subst(std::string const & a,
148                    std::string const & oldstr, std::string const & newstr);
149
150 /// substitutes all instances of \a oldstr with \a newstr
151 lyx::docstring const subst(lyx::docstring const & a,
152                 lyx::docstring const & oldstr, lyx::docstring const & newstr);
153
154 /** Trims characters off the end and beginning of a string.
155     \code
156     trim("ccabccc", "c") == "ab".
157     \endcode
158 */
159 std::string const trim(std::string const & a, char const * p = " ");
160
161 /** Trims characters off the end of a string.
162     \code
163     rtrim("abccc", "c") == "ab".
164     \endcode
165 */
166 std::string const rtrim(std::string const & a, char const * p = " ");
167
168 /** Trims characters off the beginning of a string.
169     \code
170    ltrim("ababcdef", "ab") = "cdef"
171     \endcode
172 */
173 std::string const ltrim(std::string const & a, char const * p = " ");
174
175 /** Splits the string by the first delim.
176     Splits the string by the first appearance of delim.
177     The leading string up to delim is returned in piece (not including
178     delim), while the original string is cut from after the delimiter.
179     Example:
180     \code
181     s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
182     \endcode
183 */
184 std::string const split(std::string const & a, std::string & piece, char delim);
185
186 /// Same as split but does not return a piece
187 std::string const split(std::string const & a, char delim);
188
189 /// Same as split but uses the last delim.
190 std::string const rsplit(std::string const & a, std::string & piece, char delim);
191
192 /// Escapes non ASCII chars
193 std::string const escape(std::string const & lab);
194
195 /// gives a vector of stringparts which have the delimiter delim
196 std::vector<std::string> const getVectorFromString(std::string const & str,
197                                               std::string const & delim = std::string(","));
198
199 // the same vice versa
200 std::string const getStringFromVector(std::vector<std::string> const & vec,
201                                  std::string const & delim = std::string(","));
202
203 /// Search \p search_token in \p str and return the position if it is
204 /// found, else -1. The last item in \p str must be "".
205 int findToken(char const * const str[], std::string const & search_token);
206
207 /// Convert internal line endings to line endings as expected by the OS
208 lyx::docstring const externalLineEnding(lyx::docstring const & str);
209
210 /// Convert line endings in any formnat to internal line endings
211 lyx::docstring const internalLineEnding(lyx::docstring const & str);
212
213
214 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
215
216 #include <boost/format.hpp>
217
218 template<class Arg1>
219 lyx::docstring bformat(lyx::docstring const & fmt, Arg1 arg1)
220 {
221         return (boost::basic_format<char_type>(fmt) % arg1).str();
222 }
223
224
225 template<class Arg1, class Arg2>
226 lyx::docstring bformat(lyx::docstring const & fmt, Arg1 arg1, Arg2 arg2)
227 {
228         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
229 }
230
231
232 template<class Arg1, class Arg2, class Arg3>
233 lyx::docstring bformat(lyx::docstring const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
234 {
235         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
236 }
237
238
239 template<class Arg1, class Arg2, class Arg3, class Arg4>
240 lyx::docstring bformat(lyx::docstring const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
241 {
242         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
243 }
244
245 #else
246
247 template <class Arg1>
248 lyx::docstring bformat(lyx::docstring const & fmt, Arg1);
249
250 template <class Arg1, class Arg2>
251 lyx::docstring bformat(lyx::docstring const & fmt, Arg1, Arg2);
252
253 template <class Arg1, class Arg2, class Arg3>
254 lyx::docstring bformat(lyx::docstring const & fmt, Arg1, Arg2, Arg3);
255
256 template <class Arg1, class Arg2, class Arg3, class Arg4>
257 lyx::docstring bformat(lyx::docstring const & fmt, Arg1, Arg2, Arg3, Arg4);
258
259 #endif
260
261 } // namespace support
262 } // namespace lyx
263
264 #endif