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