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