]> git.lyx.org Git - features.git/blob - src/support/lstrings.C
fixes because of SUN CC warnings, bmtable now compiled with C compilator, countChar...
[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         string tmp(a);
93         transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
94         return tmp;
95 }
96
97
98 string uppercase(string const & a)
99 {
100         string tmp(a);
101         transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
102         return tmp;
103 }
104
105
106 string tostr(long i)
107 {
108         // should use string stream
109         char str[30];
110         sprintf(str, "%ld", i);
111         return string(str);
112 }
113
114
115 string tostr(unsigned long i)
116 {
117         // should use string stream
118         char str[30];
119         sprintf(str, "%lu", i);
120         return string(str);
121 }
122
123
124 string tostr(void * v)
125 {
126         return tostr(long(v));
127 }
128
129
130 string tostr(int i)
131 {
132         return tostr(long(i));
133 }
134
135
136 string tostr(unsigned int ui)
137 {
138         return tostr(long(ui));
139 }
140
141
142 string tostr(char c)
143 {
144         return string(1, c);
145 }
146
147
148 string tostr(bool b)
149 {
150         return b ? "true" : "false";
151 }
152
153
154 string tostr(double d)
155 {
156         // should use string stream
157         char tmp[40];
158         sprintf(tmp, "%f", d);
159         return string(tmp);
160 }
161
162
163 bool prefixIs(string const & a, char const * pre)
164 {
165         unsigned int l = strlen(pre);
166         if (l > a.length() || a.empty())
167                 return false;
168         else
169                 return a.compare(0, l, pre, l) == 0;
170 }
171
172
173 bool suffixIs(string const & a, char c)
174 {
175         if (a.empty()) return false;
176         return a[a.length() - 1] == c;
177 }
178
179
180 bool suffixIs(string const & a, char const * suf)
181 {
182         unsigned int suflen = strlen(suf);
183         if (suflen > a.length())
184                 return false;
185         else {
186                 return a.compare(a.length() - suflen, suflen, suf) == 0;
187         }
188 }
189
190
191 bool contains(char const * a, string const & b)
192 {
193         if (!a || !*a || b.empty()) return false;
194         return strstr(a, b.c_str()) != 0;
195 }
196
197
198 bool contains(string const & a, char const * b)
199 {
200         if (a.empty())
201                 return false;
202         return a.find(b) != string::npos;
203 }
204
205
206 bool contains(string const & a, string const & b)
207 {
208         if (a.empty())
209                 return false;
210         return a.find(b) != string::npos;
211 }
212
213
214 bool contains(char const * a, char const * b)
215 {
216         if (!a || !b || !*a || !*b) return false;
217         return strstr(a, b) != 0;
218 }
219
220
221 unsigned int countChar(string const & a, char const c)
222 {
223 #ifdef HAVE_STD_COUNT
224         return count(a.begin(), a.end(), c);
225 #else
226         unsigned int n = 0;
227         count(a.begin(), a.end(), c, n);
228         return n;
229 #endif
230 }
231
232
233 // ale970405+lasgoutt-970425
234 // rewritten to use new string (Lgb)
235 string token(string const & a, char delim, int n)
236 {
237         if (a.empty()) return string();
238         
239         string::size_type k = 0;
240         string::size_type i = 0;
241
242         // Find delimiter or end of string
243         for (; n--;)
244                 if ((i = a.find(delim, i)) == string::npos)
245                         break;
246                 else
247                         ++i; // step delim
248         // i is now the n'th delim (or string::npos)
249         if (i == string::npos) return string();
250         k = a.find(delim, i);
251         // k is now the n'th + 1 delim (or string::npos)
252
253         return a.substr(i, k - i);
254 }
255
256
257 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
258 // rewritten to use new string (Lgb)
259 int tokenPos(string const & a, char delim, string const & tok)
260 {
261         int i = 0;
262         string str = a;
263         string tmptok;
264
265         while (!str.empty()) {
266                 str = split(str, tmptok, delim);
267                 if (tok == tmptok)
268                         return i;
269                 ++i;
270         }
271         return -1;
272 }
273
274
275 bool regexMatch(string const & a, string const & pattern)
276 {
277         // We massage the pattern a bit so that the usual
278         // shell pattern we all are used to will work.
279         // One nice thing about using a real regex is that
280         // things like "*.*[^~]" will work also.
281         // build the regex string.
282         string regex(pattern);
283         regex = subst(regex, ".", "\\.");
284         regex = subst(regex, "*", ".*");
285         LRegex reg(regex);
286         return reg.exact_match(a);
287 }
288
289
290 string subst(string const & a, char oldchar, char newchar)
291 {
292         string tmp = a;
293         string::iterator lit = tmp.begin();
294         for(; lit != tmp.end(); ++lit)
295                 if ((*lit) == oldchar)
296                         (*lit) = newchar;
297         return tmp;
298 }
299
300
301 string subst(string const & a,
302              char const * oldstr, string const & newstr)
303 {
304         string lstr(a);
305         string::size_type i = 0;
306         int olen = strlen(oldstr);
307         while((i = lstr.find(oldstr, i)) != string::npos) {
308                 lstr.replace(i, olen, newstr);
309                 i += newstr.length(); // We need to be sure that we dont
310                 // use the same i over and over again.
311         }
312         return lstr;
313 }
314
315
316 string strip(string const & a, char const c)
317 {
318         if (a.empty()) return a;
319         string tmp = a;
320         string::size_type i = tmp.find_last_not_of(c);
321         if (i == a.length() - 1) return tmp; // no c's at end of a
322         if (i != string::npos) 
323                 tmp.erase(i + 1, string::npos);
324         else
325                 tmp.clear(); // only c in the whole string
326         return tmp;
327 }
328
329
330 string frontStrip(string const & a, char const * p)
331 {
332         if (a.empty() || !p || !*p) return a;
333         string tmp = a;
334         string::size_type i = tmp.find_first_not_of(p);
335         if (i > 0)
336                 tmp.erase(0, i);
337         return tmp;
338 }
339
340
341 string frontStrip(string const & a, char const c)
342 {
343         if (a.empty()) return a;
344         string tmp = a;
345         string::size_type i = tmp.find_first_not_of(c);
346         if (i > 0)
347                 tmp.erase(0, i);
348         return tmp;
349 }
350
351
352 string split(string const & a, string & piece, char delim)
353 {
354         string tmp;
355         string::size_type i = a.find(delim);
356         if (i == a.length() - 1) {
357                 piece = a.substr(0, i);
358         } else if (i != string::npos) {
359                 piece = a.substr(0, i);
360                 tmp = a.substr(i + 1);
361         } else if (i == 0) {
362                 piece.clear();
363                 tmp = a.substr(i + 1);
364         } else {
365                 piece = a;
366         }
367         return tmp;
368 }
369
370
371 string split(string const & a, char delim)
372 {
373         string tmp;
374         string::size_type i = a.find(delim);
375         if (i != string::npos) // found delim
376                 tmp = a.substr(i + 1);
377         return tmp;
378 }
379
380
381 // ale970521
382 string rsplit(string const & a, string & piece, char delim)
383 {
384         string tmp;
385         string::size_type i = a.rfind(delim);
386         if (i != string::npos) { // delimiter was found
387                 piece = a.substr(0, i);
388                 tmp = a.substr(i + 1);
389         } else { // delimter was not found
390                 piece.clear();
391         }
392         return tmp;
393 }