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