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