]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.h
Undo /does/ seem to still work properly :)
[lyx.git] / src / support / lstrings.h
1 // -*- C++ -*-
2
3 /** String helper functions.
4     \file lstrings.h
5     This is a collection of string helper functions that works
6     together with string (and later also with STL String. Some of these
7     would certainly benefit from a rewrite/optimization.
8     \author Lars Gullik Bjønnes
9     \author Jean-Marc Lasgouttes
10 */
11
12 #ifndef LSTRINGS_H
13 #define LSTRINGS_H
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 //#include <cstring>
20 //#include <cctype>
21 //#include <cctype>
22 #include <vector>
23
24 #include "Lsstream.h"
25
26 #include "LString.h"
27
28 ///
29 int compare_no_case(string const & s, string const & s2);
30
31 ///
32 int compare_ascii_no_case(string const & s, string const & s2);
33
34 ///
35 int compare_no_case(string const & s, 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(string const & str);
61
62 /// does the string represent an unsigned integer value ?
63 bool isStrUnsignedInt(string const & str);
64
65 ///
66 int strToInt(string const & str);
67
68 /// convert string to an unsigned integer
69 unsigned int strToUnsignedInt(string const & str);
70
71 ///
72 bool isStrDbl(string const & str);
73
74 ///
75 double strToDbl(string const & str);
76
77 ///
78 char lowercase(char c);
79
80 ///
81 char uppercase(char c);
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 ostr.str().c_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(char const * a, string const & b);
136
137 ///
138 bool contains(string const & a, char const * b);
139
140 ///
141 bool contains(string const & a, string const & b);
142
143 ///
144 bool contains(string const & a, char b);
145
146 ///
147 bool contains(char const * a, char const * b);
148
149 /// This should probably we rewritten to be more general.
150 class contains_functor {
151 public:
152         typedef string first_argument_type;
153         typedef string second_argument_type;
154         typedef bool result_type;
155
156         bool operator()(string const & haystack, string const & needle) const {
157                 return contains(haystack, needle);
158         }
159 };
160
161
162 ///
163 bool containsOnly(string const &, char const *);
164
165 ///
166 bool containsOnly(string const &, string const &);
167
168 ///
169 bool containsOnly(char const *, char const *);
170
171 ///
172 bool containsOnly(char const *, string const &);
173
174 /** Extracts a token from this string at the nth delim.
175     Doesn't modify the original string. Similar to strtok.
176     Example:
177     \code
178     "a;bc;d".token(';', 1) == "bc";
179     "a;bc;d".token(';', 2) == "d";
180     \endcode
181 */
182 string const token(string const & a, char delim, int n);
183
184
185 /** Search a token in this string using the delim.
186     Doesn't modify the original string. Returns -1 in case of
187     failure.
188     Example:
189     \code
190     "a;bc;d".tokenPos(';', "bc") == 1;
191     "a;bc;d".token(';', "d") == 2;
192     \endcode
193 */
194 int tokenPos(string const & a, char delim, string const & tok);
195
196
197 /** Compares a string and a (simple) regular expression
198   The only element allowed is "*" for any string of characters
199   */
200 bool regexMatch(string const & a, string const & pattern);
201
202 /// Substitute all \a oldchar with \a newchar
203 string const subst(string const & a, char oldchar, char newchar);
204
205 /// Substitutes all instances of \a oldstr with \a newstr
206 string const subst(string const & a,
207              char const * oldstr, string const & newstr);
208
209 /// substitutes all instances of \a oldstr with \a newstr
210 string const subst(string const & a,
211                    string const & oldstr, string const & newstr);
212
213 /** Strips characters off the end of a string.
214     \code
215     "abccc".strip('c') = "ab".
216     \endcode
217 */
218 string const strip(string const & a, char c = ' ');
219
220 /** Strips characters of the beginning of a string.
221     \code
222     "cccba".frontstrip('c') = "ba"
223     \endcode
224 */
225 string const frontStrip(string const & a, char c = ' ');
226
227 /** Strips characters off the beginning of a string.
228     \code
229     "ababcdef".frontstrip("ab") = "cdef"
230     \endcode
231 */
232 string const frontStrip(string const & a, char const * p);
233
234 /** Splits the string by the first delim.
235     Splits the string by the first appearance of delim.
236     The leading string up to delim is returned in piece (not including
237     delim), while the original string is cut from after the delimiter.
238     Example:
239     \code
240     s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
241     \endcode
242 */
243 string const split(string const & a, string & piece, char delim);
244
245 /// Same as split but does not return a piece
246 string const split(string const & a, char delim);
247
248 /// Same as split but uses the last delim.
249 string const rsplit(string const & a, string & piece, char delim);
250
251 /// Escapes non ASCII chars
252 string const escape(string const & lab);
253
254 /// gives a vector of stringparts which have the delimiter delim
255 std::vector<string> const getVectorFromString(string const & str,
256                                               string const & delim = ",");
257
258 // the same vice versa
259 string const getStringFromVector(std::vector<string> const & vec,
260                                  string const & delim = ",");
261
262 #endif