]> git.lyx.org Git - features.git/blob - src/support/lstrings.h
The 'bformat' introduction
[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 <vector>
20
21 #include "Lsstream.h"
22
23 #include "LString.h"
24
25 ///
26 int compare_no_case(string const & s, string const & s2);
27
28 ///
29 int compare_ascii_no_case(string const & s, string const & s2);
30
31 ///
32 int compare_no_case(string const & s, string const & s2, unsigned int len);
33
34 ///
35 inline
36 int compare(char const * a, char const * b)
37 {
38 #ifndef CXX_GLOBAL_CSTD
39         return std::strcmp(a, b);
40 #else
41         return strcmp(a, b);
42 #endif
43 }
44
45 ///
46 inline
47 int compare(char const * a, char const * b, unsigned int len)
48 {
49 #ifndef CXX_GLOBAL_CSTD
50         return std::strncmp(a, b, len);
51 #else
52         return strncmp(a, b, len);
53 #endif
54 }
55
56 ///
57 bool isStrInt(string const & str);
58
59 /// does the string represent an unsigned integer value ?
60 bool isStrUnsignedInt(string const & str);
61
62 ///
63 int strToInt(string const & str);
64
65 /// convert string to an unsigned integer
66 unsigned int strToUnsignedInt(string const & str);
67
68 ///
69 bool isStrDbl(string const & str);
70
71 ///
72 double strToDbl(string const & str);
73
74 ///
75 char lowercase(char c);
76
77 ///
78 char uppercase(char c);
79
80 /// same as lowercase(), but ignores locale
81 string const ascii_lowercase(string const &);
82
83 ///
84 string const lowercase(string const &);
85
86 ///
87 string const uppercase(string const &);
88
89 /// convert \a T to string
90 template<typename T>
91 inline
92 string const tostr(T const & t)
93 {
94         ostringstream ostr;
95         ostr << t;
96         return STRCONV(ostr.str());
97         // We need to use the .c_str since we sometimes are using
98         // our own string class and that is not compatible with
99         // basic_string<char>. (of course we don't want this later)
100 }
101
102
103 ///
104 template<>
105 inline
106 string const tostr(bool const & b)
107 {
108         return (b ? "true" : "false");
109 }
110
111 ///
112 template<>
113 inline
114 string const tostr(string const & s)
115 {
116         return s;
117 }
118
119 /// Does the string start with this prefix?
120 bool prefixIs(string const &, char const *);
121
122 /// Does the string start with this prefix?
123 bool prefixIs(string const &, string const &);
124
125 /// Does the string end with this char?
126 bool suffixIs(string const &, char);
127
128 /// Does the string end with this suffix?
129 bool suffixIs(string const &, char const *);
130
131 /// Does the string end with this suffix?
132 bool suffixIs(string const &, string const &);
133
134 ///
135 bool contains(string const & a, string const & b);
136
137 ///
138 bool contains(string const & a, char b);
139
140 /// This should probably we rewritten to be more general.
141 class contains_functor {
142 public:
143         typedef string first_argument_type;
144         typedef string second_argument_type;
145         typedef bool result_type;
146
147         bool operator()(string const & haystack, string const & needle) const {
148                 return contains(haystack, needle);
149         }
150 };
151
152
153 ///
154 bool containsOnly(string const &, string const &);
155
156 /** Extracts a token from this string at the nth delim.
157     Doesn't modify the original string. Similar to strtok.
158     Example:
159     \code
160     token("a;bc;d", ';', 1) == "bc";
161     token("a;bc;d", ';', 2) == "d";
162     \endcode
163 */
164 string const token(string const & a, char delim, int n);
165
166
167 /** Search a token in this string using the delim.
168     Doesn't modify the original string. Returns -1 in case of
169     failure.
170     Example:
171     \code
172     tokenPos("a;bc;d", ';', "bc") == 1;
173     tokenPos("a;bc;d", ';', "d") == 2;
174     \endcode
175 */
176 int tokenPos(string const & a, char delim, string const & tok);
177
178
179 /** Compares a string and a (simple) regular expression
180   The only element allowed is "*" for any string of characters
181   */
182 bool regexMatch(string const & a, string const & pattern);
183
184 /// Substitute all \a oldchar with \a newchar
185 string const subst(string const & a, char oldchar, char newchar);
186
187 /// Substitutes all instances of \a oldstr with \a newstr
188 string const subst(string const & a,
189              char const * oldstr, string const & newstr);
190
191 /// substitutes all instances of \a oldstr with \a newstr
192 string const subst(string const & a,
193                    string const & oldstr, string const & newstr);
194
195 /** Trims characters off the end and beginning of a string.
196     \code
197     trim("ccabccc", "c") == "ab".
198     \endcode
199 */
200 string const trim(string const & a, char const * p = " ");
201
202 /** Trims characters off the end of a string.
203     \code
204     rtrim("abccc", "c") == "ab".
205     \endcode
206 */
207 string const rtrim(string const & a, char const * p = " ");
208
209 /** Trims characters off the beginning of a string.
210     \code
211    ltrim("ababcdef", "ab") = "cdef"
212     \endcode
213 */
214 string const ltrim(string const & a, char const * p = " ");
215
216 /** Splits the string by the first delim.
217     Splits the string by the first appearance of delim.
218     The leading string up to delim is returned in piece (not including
219     delim), while the original string is cut from after the delimiter.
220     Example:
221     \code
222     s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
223     \endcode
224 */
225 string const split(string const & a, string & piece, char delim);
226
227 /// Same as split but does not return a piece
228 string const split(string const & a, char delim);
229
230 /// Same as split but uses the last delim.
231 string const rsplit(string const & a, string & piece, char delim);
232
233 /// Escapes non ASCII chars
234 string const escape(string const & lab);
235
236 /// gives a vector of stringparts which have the delimiter delim
237 std::vector<string> const getVectorFromString(string const & str,
238                                               string const & delim = ",");
239
240 // the same vice versa
241 string const getStringFromVector(std::vector<string> const & vec,
242                                  string const & delim = ",");
243
244 // wrapper around boost::format using one argument %1$s
245 string bformat(char const * fmt, string const & arg1);
246 // arguments %1$s and %2$s
247 string bformat(char const * fmt, string const & arg1, string const & arg2);
248 // arguments %1$s and %2$s and %3$s
249 string bformat(char const * fmt, string const & arg1, string const & arg2,
250                string const & arg3);
251 // arguments %1$s and %2$s and %3$s and %4$s
252 string bformat(char const * fmt, string const & arg1, string const & arg2,
253                string const & arg3, string const & arg4);
254
255 #endif