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