]> git.lyx.org Git - features.git/blob - src/support/lstrings.h
Add -enable-assertions to configure, cleanup assertions, fix crash in LyXFont::ascent...
[features.git] / src / support / lstrings.h
1 // -*- C++ -*-
2
3 /** This is a collection of string helper functions that works
4     together with string (and later also with STL String. Some of these
5     would certainly benefit from a rewrite/optimization.
6 */
7
8 #ifndef LSTRINGS_H
9 #define LSTRINGS_H
10
11 #include <cstring>
12
13 #include "LAssert.h"
14
15 //#warning verify this please. Lgb
16 ///
17 template<class T>
18 size_t lstrlen(T const * t)
19 {
20         Assert(t); // we don't want null pointers
21         size_t count = 0;
22         T const * r = t;
23         while(*r != 0) ++r, ++count;
24         return count;
25 }
26
27
28 //#warning verify this please. Lgb
29 ///
30 template<class T>
31 T * lstrchr(T const * t, int c)
32 {
33   Assert(t); // we don't want null pointers
34   T * r = const_cast<T*>(t);
35   while(*r != 0) 
36     if (*r == c) return r; else ++r;
37   return 0;
38 }
39
40 #include <cctype>
41 #include "LString.h"
42
43
44 ///
45 inline int compare_no_case(string const & s, string const & s2)
46 {
47         // ANSI C
48         string::const_iterator p = s.begin();
49         string::const_iterator p2 = s2.begin();
50
51         while (p != s.end() && p2 != s2.end()) {
52                 if (tolower(*p) != tolower(*p2))
53                         return (tolower(*p) < tolower(*p2)) ? -1 : 1;
54                 ++p;
55                 ++p2;
56         }
57
58         return s.size() - s2.size();
59 }
60
61
62 ///
63 inline int compare_no_case(string const & s, string const & s2,
64                            unsigned int len)
65 {
66 //#warning verify this func please
67         string::const_iterator p = s.begin();
68         string::const_iterator p2 = s2.begin();
69         unsigned int i = 0;
70         while (i < len && p != s.end() && p2 != s2.end()) {
71                 if (tolower(*p) != tolower(*p2))
72                         return (tolower(*p) < tolower(*p2)) ? -1 : 1;
73                 ++i;
74                 ++p;
75                 ++p2;
76         }
77         return s.size() - s2.size();
78 }
79
80
81 ///
82 inline int compare(char const * a, char const * b)
83 {
84         return strcmp(a, b);
85 }
86
87
88 ///
89 inline int compare(char const * a, char const * b, unsigned int len)
90 {
91         return strncmp(a, b, len);
92 }
93
94
95 ///
96 bool isStrInt(string const & str);
97
98 ///
99 int strToInt(string const & str);
100
101 ///
102 string lowercase(string const &);
103
104 ///
105 string uppercase(string const &);
106
107 /// int to string
108 string tostr(int i);
109
110 ///
111 string tostr(unsigned int);
112
113 /// long to string
114 string tostr(long l);
115
116 ///
117 string tostr(unsigned long l); 
118
119 ///
120 string tostr(char c);
121
122 /// void * to string
123 string tostr(void * v);
124
125 /// bool to string
126 string tostr(bool b);
127
128 ///
129 string tostr(double d);
130
131 /// Does the string start with this prefix?
132 bool prefixIs(string const &, char const *);
133
134 /// Does the string end with this char?
135 bool suffixIs(string const &, char);
136
137 /// Does the string end with this suffix?
138 bool suffixIs(string const &, char const *);
139
140 ///
141 bool contains(char const * a, string const & b);
142
143 ///
144 bool contains(string const & a, char const * b);
145
146 ///
147 bool contains(string const & a, string const & b);
148
149 ///
150 bool contains(char const * a, char const * b);
151
152 /// Counts how many of character c there is in a
153 int countChar(string const & a, char const c);
154
155 /** Extracts a token from this string at the nth delim.
156   Doesn't modify the original string. Similar to strtok.
157   Example:
158   #"a;bc;d".token(';', 1) == "bc";#
159   #"a;bc;d".token(';', 2) == "d";#
160 */
161 string token(string const & a, char delim, int n);
162
163
164 /** Search a token in this string using the delim.
165   Doesn't modify the original string. Returns -1 in case of
166   failure. 
167   Example:
168   #"a;bc;d".tokenPos(';', "bc") == 1;#
169   #"a;bc;d".token(';', "d") == 2;#
170 */
171 int tokenPos(string const & a, char delim, string const & tok);
172
173
174 /** Compares a string and a (simple) regular expression
175   The only element allowed is "*" for any string of characters
176   */
177 bool regexMatch(string const & a, string const & pattern);
178
179 /// Substitute all "oldchar"s with "newchar"
180 string subst(string const & a, char oldchar, char newchar);
181
182 /// Substitutes all instances of oldstr with newstr
183 string subst(string const & a,
184              char const * oldstr, string const & newstr);
185
186 /** Strips characters off the end of a string.
187   #"abccc".strip('c') = "ab".#
188   */
189 string strip(string const & a, char const c = ' ');
190
191 /** Strips characters of the beginning of a string.
192   #"cccba".frontstrip('c') = "ba"#. */
193 string frontStrip(string const & a, char const c = ' ');
194
195 /** Strips characters off the beginning of a string.
196     #"ababcdef".frontstrip("ab") = "cdef"# .*/
197 string frontStrip(string const & a, char const * p);
198
199 /** Splits the string by the first delim.
200   Splits the string by the first appearance of delim.
201   The leading string up to delim is returned in piece (not including
202   delim), while the original string is cut from after the delimiter.
203   Example:
204   #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
205   */
206 string split(string const & a, string & piece, char delim);
207
208 /// Same as split but does not return a piece
209 string split(string const & a, char delim);
210
211 /// Same as split but uses the last delim.
212 string rsplit(string const & a, string & piece, char delim);
213
214 #endif