]> git.lyx.org Git - features.git/blob - src/support/lstrings.h
Fix some conversion problems spotted by MSVC warnings
[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 /// 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 /// substitutes all instances of \a oldstr with \a newstr
134 std::string const subst(std::string const & a,
135                    std::string const & oldstr, std::string const & newstr);
136
137 /** Trims characters off the end and beginning of a string.
138     \code
139     trim("ccabccc", "c") == "ab".
140     \endcode
141 */
142 std::string const trim(std::string const & a, char const * p = " ");
143
144 /** Trims characters off the end of a string.
145     \code
146     rtrim("abccc", "c") == "ab".
147     \endcode
148 */
149 std::string const rtrim(std::string const & a, char const * p = " ");
150
151 /** Trims characters off the beginning of a string.
152     \code
153    ltrim("ababcdef", "ab") = "cdef"
154     \endcode
155 */
156 std::string const ltrim(std::string const & a, char const * p = " ");
157
158 /** Splits the string by the first delim.
159     Splits the string by the first appearance of delim.
160     The leading string up to delim is returned in piece (not including
161     delim), while the original string is cut from after the delimiter.
162     Example:
163     \code
164     s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
165     \endcode
166 */
167 std::string const split(std::string const & a, std::string & piece, char delim);
168
169 /// Same as split but does not return a piece
170 std::string const split(std::string const & a, char delim);
171
172 /// Same as split but uses the last delim.
173 std::string const rsplit(std::string const & a, std::string & piece, char delim);
174
175 /// Escapes non ASCII chars
176 std::string const escape(std::string const & lab);
177
178 /// gives a vector of stringparts which have the delimiter delim
179 std::vector<std::string> const getVectorFromString(std::string const & str,
180                                               std::string const & delim = std::string(","));
181
182 // the same vice versa
183 std::string const getStringFromVector(std::vector<std::string> const & vec,
184                                  std::string const & delim = std::string(","));
185
186 /// Search \p search_token in \p str and return the position if it is
187 /// found, else -1. The last item in \p str must be "".
188 int findToken(char const * const str[], std::string const & search_token);
189
190 /// Convert internal line endings to line endings as expected by the OS
191 std::string const externalLineEnding(std::string const & str);
192
193 /// Convert line endings in any formnat to internal line endings
194 std::string const internalLineEnding(std::string const & str);
195
196
197 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
198
199 #include <boost/format.hpp>
200
201 template<class Arg1>
202 string bformat(string const & fmt, Arg1 arg1)
203 {
204         return (boost::format(fmt) % arg1).str();
205 }
206
207
208 template<class Arg1, class Arg2>
209 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2)
210 {
211         return (boost::format(fmt) % arg1 % arg2).str();
212 }
213
214
215 template<class Arg1, class Arg2, class Arg3>
216 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
217 {
218         return (boost::format(fmt) % arg1 % arg2 % arg3).str();
219 }
220
221
222 template<class Arg1, class Arg2, class Arg3, class Arg4>
223 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
224 {
225         return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4).str();
226 }
227
228 #else
229
230 template <class Arg1>
231 std::string bformat(std::string const & fmt, Arg1);
232
233 template <class Arg1, class Arg2>
234 std::string bformat(std::string const & fmt, Arg1, Arg2);
235
236 template <class Arg1, class Arg2, class Arg3>
237 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3);
238
239 template <class Arg1, class Arg2, class Arg3, class Arg4>
240 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3, Arg4);
241
242 #endif
243
244 } // namespace support
245 } // namespace lyx
246
247 #endif