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