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