]> git.lyx.org Git - features.git/blob - src/support/lstrings.C
- Got rid of two global variables:
[features.git] / src / support / lstrings.C
1 #include <config.h>
2
3 #include <algorithm>
4 #include <cctype>
5 #include <cstdio>
6 #include <cstdlib>
7
8 #include "LString.h"
9 #include "lstrings.h"
10 #include "LRegex.h"
11
12 using std::count;
13 using std::transform;
14
15 int compare_no_case(string const & s, string const & s2)
16 {
17         // ANSI C
18         string::const_iterator p = s.begin();
19         string::const_iterator p2 = s2.begin();
20
21         while (p != s.end() && p2 != s2.end()) {
22                 int const lc1 = tolower(*p);
23                 int const lc2 = tolower(*p2);
24                 if (lc1 != lc2)
25                         return (lc1 < lc2) ? -1 : 1;
26                 ++p;
27                 ++p2;
28         }
29         
30         if (s.size() == s2.size())
31                 return 0;
32         if (s.size() < s2.size())
33                 return -1;
34         return 1;
35 }
36
37 int compare_no_case(string const & s, string const & s2, unsigned int len)
38 {
39 //#warning verify this func please
40         string::const_iterator p = s.begin();
41         string::const_iterator p2 = s2.begin();
42         unsigned int i = 0;
43         while (i < len && p != s.end() && p2 != s2.end()) {
44                 int const lc1 = tolower(*p);
45                 int const lc2 = tolower(*p2);
46                 if (lc1 != lc2)
47                         return (lc1 < lc2) ? -1 : 1;
48                 ++i;
49                 ++p;
50                 ++p2;
51         }
52         if (s.size() == s2.size())
53                 return 0;
54         if (s.size() < s2.size())
55                 return -1;
56         return 1;
57 }
58
59 bool isStrInt(string const & str)
60 {
61         if (str.empty()) return false;
62        
63         // Remove leading and trailing white space chars.
64         string tmpstr = frontStrip(strip(str, ' '), ' ');
65         if (tmpstr.empty()) return false;
66        
67         string::const_iterator cit = tmpstr.begin();
68         if ( (*cit) == '-') ++cit;
69         for (; cit != tmpstr.end(); ++cit) {
70                 if (!isdigit((*cit))) return false;
71         }
72         return true;
73 }
74
75
76 int  strToInt(string const & str)
77 {
78         string tmpstr;
79
80         if (isStrInt(str)) {
81                 // Remove leading and trailing white space chars.
82                 tmpstr = frontStrip(strip(str, ' '), ' ');
83                 // Do the conversion proper.
84                 return atoi(tmpstr.c_str());
85         } else
86                 return 0;
87 }
88
89
90 string lowercase(string const & a)
91 {
92 #if 0
93         string tmp;
94         string::const_iterator cit = a.begin();
95         for(; cit != a.end(); ++cit) {
96                 tmp += static_cast<char>(tolower(*cit));
97         }
98         return tmp;
99 #endif
100         string tmp(a);
101         transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
102         return tmp;
103 }
104
105
106 string uppercase(string const & a)
107 {
108         string tmp(a);
109         transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
110         return tmp;
111 }
112
113
114 string tostr(long i)
115 {
116         // should use string stream
117         char str[30];
118         sprintf(str, "%ld", i);
119         return string(str);
120 }
121
122
123 string tostr(unsigned long i)
124 {
125         // should use string stream
126         char str[30];
127         sprintf(str, "%lu", i);
128         return string(str);
129 }
130
131
132 string tostr(void * v)
133 {
134         return tostr(long(v));
135 }
136
137
138 string tostr(int i)
139 {
140         return tostr(long(i));
141 }
142
143
144 string tostr(unsigned int ui)
145 {
146         return tostr(long(ui));
147 }
148
149
150 string tostr(char c)
151 {
152         return string(1, c);
153 }
154
155
156 string tostr(bool b)
157 {
158         return b ? "true" : "false";
159 }
160
161
162 #if 0
163 string tostr(float f)
164 {
165         return tostr(double(f));
166 }
167 #endif
168
169
170 string tostr(double d)
171 {
172         // should use string stream
173         char tmp[40];
174         sprintf(tmp, "%f", d);
175         return string(tmp);
176 }
177
178
179 bool prefixIs(string const & a, char const * pre)
180 {
181         unsigned int l = strlen(pre);
182         if (l > a.length() || a.empty())
183                 return false;
184         else
185                 return a.compare(0, l, pre, l) == 0;
186 }
187
188
189 bool suffixIs(string const & a, char c)
190 {
191         if (a.empty()) return false;
192         return a[a.length() - 1] == c;
193 }
194
195
196 bool suffixIs(string const & a, char const * suf)
197 {
198         unsigned int suflen = strlen(suf);
199         if (suflen > a.length())
200                 return false;
201         else {
202                 return a.compare(a.length() - suflen, suflen, suf) == 0;
203         }
204 }
205
206
207 bool contains(char const * a, string const & b)
208 {
209         if (!a || !*a || b.empty()) return false;
210         return strstr(a, b.c_str()) != 0;
211 }
212
213
214 bool contains(string const & a, char const * b)
215 {
216         if (a.empty())
217                 return false;
218         return a.find(b) != string::npos;
219 }
220
221
222 bool contains(string const & a, string const & b)
223 {
224         if (a.empty())
225                 return false;
226         return a.find(b) != string::npos;
227 }
228
229
230 bool contains(char const * a, char const * b)
231 {
232         if (!a || !b || !*a || !*b) return false;
233         return strstr(a, b) != 0;
234 }
235
236
237 int countChar(string const & a, char const c)
238 {
239         return count(a.begin(), a.end(), c);
240 }
241
242
243 // ale970405+lasgoutt-970425
244 // rewritten to use new string (Lgb)
245 string token(string const & a, char delim, int n)
246 {
247         if (a.empty()) return string();
248         
249         string::size_type k = 0;
250         string::size_type i = 0;
251
252         // Find delimiter or end of string
253         for (; n--;)
254                 if ((i = a.find(delim, i)) == string::npos)
255                         break;
256                 else
257                         ++i; // step delim
258         // i is now the n'th delim (or string::npos)
259         if (i == string::npos) return string();
260         k = a.find(delim, i);
261         // k is now the n'th + 1 delim (or string::npos)
262
263         return a.substr(i, k - i);
264 }
265
266
267 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
268 // rewritten to use new string (Lgb)
269 int tokenPos(string const & a, char delim, string const & tok)
270 {
271         int i = 0;
272         string str = a;
273         string tmptok;
274
275         while (!str.empty()) {
276                 str = split(str, tmptok, delim);
277                 if (tok == tmptok)
278                         return i;
279                 ++i;
280         }
281         return -1;
282 }
283
284
285 bool regexMatch(string const & a, string const & pattern)
286 {
287         // We massage the pattern a bit so that the usual
288         // shell pattern we all are used to will work.
289         // One nice thing about using a real regex is that
290         // things like "*.*[^~]" will work also.
291         // build the regex string.
292         string regex(pattern);
293         regex = subst(regex, ".", "\\.");
294         regex = subst(regex, "*", ".*");
295         LRegex reg(regex);
296         return reg.exact_match(a);
297 }
298
299
300 string subst(string const & a, char oldchar, char newchar)
301 {
302         string tmp = a;
303         string::iterator lit = tmp.begin();
304         for(; lit != tmp.end(); ++lit)
305                 if ((*lit) == oldchar)
306                         (*lit) = newchar;
307         return tmp;
308 }
309
310
311 string subst(string const & a,
312              char const * oldstr, string const & newstr)
313 {
314         string lstr(a);
315         string::size_type i = 0;
316         int olen = strlen(oldstr);
317         while((i = lstr.find(oldstr, i)) != string::npos) {
318                 lstr.replace(i, olen, newstr);
319                 i += newstr.length(); // We need to be sure that we dont
320                 // use the same i over and over again.
321         }
322         return lstr;
323 }
324
325
326 string strip(string const & a, char const c)
327 {
328         if (a.empty()) return a;
329         string tmp = a;
330         string::size_type i = tmp.find_last_not_of(c);
331         if (i == a.length() - 1) return tmp; // no c's at end of a
332         if (i != string::npos) 
333                 tmp.erase(i + 1, string::npos);
334         else
335                 tmp.clear(); // only c in the whole string
336         return tmp;
337 }
338
339
340 string frontStrip(string const & a, char const * p)
341 {
342         if (a.empty() || !p || !*p) return a;
343         string tmp = a;
344         string::size_type i = tmp.find_first_not_of(p);
345         if (i > 0)
346                 tmp.erase(0, i);
347         return tmp;
348 }
349
350
351 string frontStrip(string const & a, char const c)
352 {
353         if (a.empty()) return a;
354         string tmp = a;
355         string::size_type i = tmp.find_first_not_of(c);
356         if (i > 0)
357                 tmp.erase(0, i);
358         return tmp;
359 }
360
361
362 string split(string const & a, string & piece, char delim)
363 {
364         string tmp;
365         string::size_type i = a.find(delim);
366         if (i == a.length() - 1) {
367                 piece = a.substr(0, i);
368         } else if (i != string::npos) {
369                 piece = a.substr(0, i);
370                 tmp = a.substr(i + 1);
371         } else if (i == 0) {
372                 piece.clear();
373                 tmp = a.substr(i + 1);
374         } else {
375                 piece = a;
376         }
377         return tmp;
378 }
379
380
381 string split(string const & a, char delim)
382 {
383         string tmp;
384         string::size_type i = a.find(delim);
385         if (i != string::npos) // found delim
386                 tmp = a.substr(i + 1);
387         return tmp;
388 }
389
390
391 // ale970521
392 string rsplit(string const & a, string & piece, char delim)
393 {
394         string tmp;
395         string::size_type i = a.rfind(delim);
396         if (i != string::npos) { // delimiter was found
397                 piece = a.substr(0, i);
398                 tmp = a.substr(i + 1);
399         } else { // delimter was not found
400                 piece.clear();
401         }
402         return tmp;
403 }