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