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