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