]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.h
e6087cf498457c6d2afb992bbc7ead3b5805a595
[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 <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 lyx::docstring const trim(lyx::docstring const & a, char const * p = " ");
160
161 /** Trims characters off the end and beginning of a string.
162     \code
163     trim("ccabccc", "c") == "ab".
164     \endcode
165 */
166 std::string const trim(std::string const & a, char const * p = " ");
167
168 /** Trims characters off the end of a string.
169     \code
170     rtrim("abccc", "c") == "ab".
171     \endcode
172 */
173 std::string const rtrim(std::string const & a, char const * p = " ");
174
175 /** Trims characters off the beginning of a string.
176     \code
177    ltrim("ababcdef", "ab") = "cdef"
178     \endcode
179 */
180 std::string const ltrim(std::string const & a, char const * p = " ");
181
182 /** Splits the string by the first delim.
183     Splits the string by the first appearance of delim.
184     The leading string up to delim is returned in piece (not including
185     delim), while the original string is cut from after the delimiter.
186     Example:
187     \code
188     s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
189     \endcode
190 */
191 std::string const split(std::string const & a, std::string & piece, char delim);
192
193 /// Same as split but does not return a piece
194 std::string const split(std::string const & a, char delim);
195
196 /// Same as split but uses the last delim.
197 std::string const rsplit(std::string const & a, std::string & piece, char delim);
198
199 /// Escapes non ASCII chars
200 std::string const escape(std::string const & lab);
201
202 /// gives a vector of stringparts which have the delimiter delim
203 std::vector<std::string> const getVectorFromString(std::string const & str,
204                                               std::string const & delim = std::string(","));
205
206 // the same vice versa
207 std::string const getStringFromVector(std::vector<std::string> const & vec,
208                                  std::string const & delim = std::string(","));
209
210 /// Search \p search_token in \p str and return the position if it is
211 /// found, else -1. The last item in \p str must be "".
212 int findToken(char const * const str[], std::string const & search_token);
213
214 /// Convert internal line endings to line endings as expected by the OS
215 lyx::docstring const externalLineEnding(lyx::docstring const & str);
216
217 /// Convert line endings in any formnat to internal line endings
218 lyx::docstring const internalLineEnding(lyx::docstring const & str);
219
220
221 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
222
223 #include <boost/format.hpp>
224
225 template<class Arg1>
226 lyx::docstring bformat(lyx::docstring const & fmt, Arg1 arg1)
227 {
228         return (boost::basic_format<char_type>(fmt) % arg1).str();
229 }
230
231
232 template<class Arg1, class Arg2>
233 lyx::docstring bformat(lyx::docstring const & fmt, Arg1 arg1, Arg2 arg2)
234 {
235         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
236 }
237
238
239 template<class Arg1, class Arg2, class Arg3>
240 lyx::docstring bformat(lyx::docstring const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
241 {
242         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
243 }
244
245
246 template<class Arg1, class Arg2, class Arg3, class Arg4>
247 lyx::docstring bformat(lyx::docstring const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
248 {
249         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
250 }
251
252 #else
253
254 template <class Arg1>
255 lyx::docstring bformat(lyx::docstring const & fmt, Arg1);
256
257 template <class Arg1, class Arg2>
258 lyx::docstring bformat(lyx::docstring const & fmt, Arg1, Arg2);
259
260 template <class Arg1, class Arg2, class Arg3>
261 lyx::docstring bformat(lyx::docstring const & fmt, Arg1, Arg2, Arg3);
262
263 template <class Arg1, class Arg2, class Arg3, class Arg4>
264 lyx::docstring bformat(lyx::docstring const & fmt, Arg1, Arg2, Arg3, Arg4);
265
266 #endif
267
268 } // namespace support
269 } // namespace lyx
270
271 #endif