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