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