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