]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.h
lyxserver cleanup patch + andre's small patches
[lyx.git] / src / support / lstrings.h
1 // -*- C++ -*-
2
3 /** String helper functions.
4     \file lstrings.h
5     This is a collection of string helper functions that works
6     together with string (and later also with STL String. Some of these
7     would certainly benefit from a rewrite/optimization.
8     \author Lars Gullik Bjønnes
9     \author Jean-Marc Lasgouttes
10 */
11
12 #ifndef LSTRINGS_H
13 #define LSTRINGS_H
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 //#include <cstring>
20 #include <cctype>
21
22 #include "Lsstream.h"
23
24 #include "LString.h"
25
26
27 ///
28 int compare_no_case(string const & s, string const & s2);
29
30 ///
31 int compare_ascii_no_case(string const & s, string const & s2);
32
33 ///
34 int compare_no_case(string const & s, string const & s2, unsigned int len);
35
36 ///
37 inline
38 int compare(char const * a, char const * b)
39 {
40 #ifndef CXX_GLOBAL_CSTD
41         return std::strcmp(a, b);
42 #else
43         return strcmp(a, b);
44 #endif  
45 }
46
47 ///
48 inline
49 int compare(char const * a, char const * b, unsigned int len)
50 {
51 #ifndef CXX_GLOBAL_CSTD
52         return std::strncmp(a, b, len);
53 #else
54         return strncmp(a, b, len);
55 #endif  
56 }
57
58 ///
59 bool isStrInt(string const & str);
60
61 /// does the string represent an unsigned integer value ?
62 bool isStrUnsignedInt(string const & str);
63
64 ///
65 int strToInt(string const & str);
66
67 /// convert string to an unsigned integer
68 unsigned int strToUnsignedInt(string const & str);
69
70 ///
71 bool isStrDbl(string const & str);
72
73 ///
74 double strToDbl(string const & str);
75
76 /// 
77 char lowercase(char c);
78
79 /// 
80 char uppercase(char c);
81
82 ///
83 string const lowercase(string const &);
84
85 ///
86 string const uppercase(string const &);
87
88 /// convert \a T to string
89 template<typename T>
90 inline
91 string const tostr(T const & t) 
92 {
93         ostringstream ostr;
94         ostr << t;
95         return ostr.str().c_str();
96         // We need to use the .c_str since we sometimes are using
97         // our own string class and that is not compatible with
98         // basic_string<char>. (of course we don't want this later)
99 }
100
101
102 ///
103 template<>
104 inline
105 string const tostr(bool const & b)
106 {
107         return (b ? "true" : "false");
108 }
109
110 ///
111 template<>
112 inline
113 string const tostr(string const & s)
114 {
115         return s;
116 }
117
118 /// Does the string start with this prefix?
119 bool prefixIs(string const &, char const *);
120
121 /// Does the string start with this prefix?
122 bool prefixIs(string const &, string const &);
123
124 /// Does the string end with this char?
125 bool suffixIs(string const &, char);
126
127 /// Does the string end with this suffix?
128 bool suffixIs(string const &, char const *);
129
130 /// Does the string end with this suffix?
131 bool suffixIs(string const &, string const &);
132
133 ///
134 bool contains(char const * a, string const & b);
135
136 ///
137 bool contains(string const & a, char const * b);
138
139 ///
140 bool contains(string const & a, string const & b);
141
142 ///
143 bool contains(string const & a, char b);
144
145 ///
146 bool contains(char const * a, char const * b);
147
148 ///
149 bool containsOnly(string const &, char const *);
150
151 ///
152 bool containsOnly(string const &, string const &);
153
154 ///
155 bool containsOnly(char const *, char const *);
156
157 ///
158 bool containsOnly(char const *, string const &);
159
160 /// Counts how many of character c there is in a
161 string::size_type countChar(string const & a, char c);
162
163 /** Extracts a token from this string at the nth delim.
164     Doesn't modify the original string. Similar to strtok.
165     Example:
166     \code
167     "a;bc;d".token(';', 1) == "bc";
168     "a;bc;d".token(';', 2) == "d";
169     \endcode
170 */
171 string const token(string const & a, char delim, int n);
172
173
174 /** Search a token in this string using the delim.
175     Doesn't modify the original string. Returns -1 in case of
176     failure. 
177     Example:
178     \code
179     "a;bc;d".tokenPos(';', "bc") == 1;
180     "a;bc;d".token(';', "d") == 2;
181     \endcode
182 */
183 int tokenPos(string const & a, char delim, string const & tok);
184
185
186 /** Compares a string and a (simple) regular expression
187   The only element allowed is "*" for any string of characters
188   */
189 bool regexMatch(string const & a, string const & pattern);
190
191 /// Substitute all \a oldchar with \a newchar
192 string const subst(string const & a, char oldchar, char newchar);
193
194 /// Substitutes all instances of \a oldstr with \a newstr
195 string const subst(string const & a,
196              char const * oldstr, string const & newstr);
197
198 /// substitutes all instances of \a oldstr with \a newstr
199 string const subst(string const & a,
200                    string const & oldstr, string const & newstr);
201
202 /** Strips characters off the end of a string.
203     \code
204     "abccc".strip('c') = "ab".
205     \endcode
206 */
207 string const strip(string const & a, char c = ' ');
208
209 /** Strips characters of the beginning of a string.
210     \code
211     "cccba".frontstrip('c') = "ba"
212     \endcode
213 */
214 string const frontStrip(string const & a, char c = ' ');
215
216 /** Strips characters off the beginning of a string.
217     \code
218     "ababcdef".frontstrip("ab") = "cdef"
219     \endcode
220 */
221 string const frontStrip(string const & a, char const * p);
222
223 /** Splits the string by the first delim.
224     Splits the string by the first appearance of delim.
225     The leading string up to delim is returned in piece (not including
226     delim), while the original string is cut from after the delimiter.
227     Example:
228     \code
229     s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
230     \endcode
231 */
232 string const split(string const & a, string & piece, char delim);
233
234 /// Same as split but does not return a piece
235 string const split(string const & a, char delim);
236
237 /// Same as split but uses the last delim.
238 string const rsplit(string const & a, string & piece, char delim);
239
240 /// Escapes non ASCII chars
241 string const escape(string const & lab);
242
243 #endif