]> git.lyx.org Git - features.git/blob - src/support/lstrings.h
toc support reorganization; changes to xform_helpers; aspect ratio patch from herbert
[features.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 /// same as lowercase(), but ignores locale
84 string const ascii_lowercase(string const &);
85
86 ///
87 string const lowercase(string const &);
88
89 ///
90 string const uppercase(string const &);
91
92 /// convert \a T to string
93 template<typename T>
94 inline
95 string const tostr(T const & t)
96 {
97         ostringstream ostr;
98         ostr << t;
99         return ostr.str().c_str();
100         // We need to use the .c_str since we sometimes are using
101         // our own string class and that is not compatible with
102         // basic_string<char>. (of course we don't want this later)
103 }
104
105
106 ///
107 template<>
108 inline
109 string const tostr(bool const & b)
110 {
111         return (b ? "true" : "false");
112 }
113
114 ///
115 template<>
116 inline
117 string const tostr(string const & s)
118 {
119         return s;
120 }
121
122 /// Does the string start with this prefix?
123 bool prefixIs(string const &, char const *);
124
125 /// Does the string start with this prefix?
126 bool prefixIs(string const &, string const &);
127
128 /// Does the string end with this char?
129 bool suffixIs(string const &, char);
130
131 /// Does the string end with this suffix?
132 bool suffixIs(string const &, char const *);
133
134 /// Does the string end with this suffix?
135 bool suffixIs(string const &, string const &);
136
137 ///
138 bool contains(char const * a, string const & b);
139
140 ///
141 bool contains(string const & a, char const * b);
142
143 ///
144 bool contains(string const & a, string const & b);
145
146 ///
147 bool contains(string const & a, char b);
148
149 ///
150 bool contains(char const * a, char const * b);
151
152 /// This should probably we rewritten to be more general.
153 class contains_functor {
154 public:
155         typedef string first_argument_type;
156         typedef string second_argument_type;
157         typedef bool result_type;
158
159         bool operator()(string const & haystack, string const & needle) const {
160                 return contains(haystack, needle);
161         }
162 };
163
164
165 ///
166 bool containsOnly(string const &, char const *);
167
168 ///
169 bool containsOnly(string const &, string const &);
170
171 ///
172 bool containsOnly(char const *, char const *);
173
174 ///
175 bool containsOnly(char const *, string const &);
176
177 /** Extracts a token from this string at the nth delim.
178     Doesn't modify the original string. Similar to strtok.
179     Example:
180     \code
181     token("a;bc;d", ';', 1) == "bc";
182     token("a;bc;d", ';', 2) == "d";
183     \endcode
184 */
185 string const token(string const & a, char delim, int n);
186
187
188 /** Search a token in this string using the delim.
189     Doesn't modify the original string. Returns -1 in case of
190     failure.
191     Example:
192     \code
193     tokenPos("a;bc;d", ';', "bc") == 1;
194     tokenPos("a;bc;d", ';', "d") == 2;
195     \endcode
196 */
197 int tokenPos(string const & a, char delim, string const & tok);
198
199
200 /** Compares a string and a (simple) regular expression
201   The only element allowed is "*" for any string of characters
202   */
203 bool regexMatch(string const & a, string const & pattern);
204
205 /// Substitute all \a oldchar with \a newchar
206 string const subst(string const & a, char oldchar, char newchar);
207
208 /// Substitutes all instances of \a oldstr with \a newstr
209 string const subst(string const & a,
210              char const * oldstr, string const & newstr);
211
212 /// substitutes all instances of \a oldstr with \a newstr
213 string const subst(string const & a,
214                    string const & oldstr, string const & newstr);
215
216 /** Strips characters off the end of a string.
217     \code
218     "abccc".strip('c') = "ab".
219     \endcode
220 */
221 string const strip(string const & a, char c = ' ');
222
223 /** Strips characters of the beginning of a string.
224     \code
225     "cccba".frontstrip('c') = "ba"
226     \endcode
227 */
228 string const frontStrip(string const & a, char c = ' ');
229
230 /** Strips characters off the beginning of a string.
231     \code
232     "ababcdef".frontstrip("ab") = "cdef"
233     \endcode
234 */
235 string const frontStrip(string const & a, char const * p);
236
237 /** Splits the string by the first delim.
238     Splits the string by the first appearance of delim.
239     The leading string up to delim is returned in piece (not including
240     delim), while the original string is cut from after the delimiter.
241     Example:
242     \code
243     s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
244     \endcode
245 */
246 string const split(string const & a, string & piece, char delim);
247
248 /// Same as split but does not return a piece
249 string const split(string const & a, char delim);
250
251 /// Same as split but uses the last delim.
252 string const rsplit(string const & a, string & piece, char delim);
253
254 /// Escapes non ASCII chars
255 string const escape(string const & lab);
256
257 /// gives a vector of stringparts which have the delimiter delim
258 std::vector<string> const getVectorFromString(string const & str,
259                                               string const & delim = ",");
260
261 // the same vice versa
262 string const getStringFromVector(std::vector<string> const & vec,
263                                  string const & delim = ",");
264
265 #endif