]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.h
another safety belt
[lyx.git] / src / support / lstrings.h
1 // -*- C++ -*-
2 /**
3  * \file lstrings.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  *
10  * Full author contact details are available in file CREDITS
11  *
12  * A collection of string helper functions that works with string.
13  * Some of these would certainly benefit from a rewrite/optimization.
14  */
15
16 #ifndef LSTRINGS_H
17 #define LSTRINGS_H
18
19 #ifdef __GNUG__
20 #pragma interface
21 #endif
22
23 #include <vector>
24
25 #include "Lsstream.h"
26
27 #include "LString.h"
28
29 ///
30 int compare_no_case(string const & s, string const & s2);
31
32 ///
33 int compare_ascii_no_case(string const & s, string const & s2);
34
35 ///
36 int compare_no_case(string const & s, string const & s2, unsigned int len);
37
38 ///
39 inline
40 int compare(char const * a, char const * b)
41 {
42 #ifndef CXX_GLOBAL_CSTD
43         return std::strcmp(a, b);
44 #else
45         return strcmp(a, b);
46 #endif
47 }
48
49 ///
50 inline
51 int compare(char const * a, char const * b, unsigned int len)
52 {
53 #ifndef CXX_GLOBAL_CSTD
54         return std::strncmp(a, b, len);
55 #else
56         return strncmp(a, b, len);
57 #endif
58 }
59
60 ///
61 bool isStrInt(string const & str);
62
63 /// does the string represent an unsigned integer value ?
64 bool isStrUnsignedInt(string const & str);
65
66 ///
67 int strToInt(string const & str);
68
69 /// convert string to an unsigned integer
70 unsigned int strToUnsignedInt(string const & str);
71
72 ///
73 bool isStrDbl(string const & str);
74
75 ///
76 double strToDbl(string const & str);
77
78 ///
79 char lowercase(char c);
80
81 ///
82 char uppercase(char c);
83
84 /// same as lowercase(), but ignores locale
85 string const ascii_lowercase(string const &);
86
87 ///
88 string const lowercase(string const &);
89
90 ///
91 string const uppercase(string const &);
92
93 /// convert \a T to string
94 template<typename T>
95 inline
96 string const tostr(T const & t)
97 {
98         ostringstream ostr;
99         ostr << t;
100         return STRCONV(ostr.str());
101         // We need to use the .c_str since we sometimes are using
102         // our own string class and that is not compatible with
103         // basic_string<char>. (of course we don't want this later)
104 }
105
106
107 ///
108 template<>
109 inline
110 string const tostr(bool const & b)
111 {
112         return (b ? "true" : "false");
113 }
114
115 ///
116 template<>
117 inline
118 string const tostr(string const & s)
119 {
120         return s;
121 }
122
123 /// Does the string start with this prefix?
124 bool prefixIs(string const &, char const *);
125
126 /// Does the string start with this prefix?
127 bool prefixIs(string const &, string const &);
128
129 /// Does the string end with this char?
130 bool suffixIs(string const &, char);
131
132 /// Does the string end with this suffix?
133 bool suffixIs(string const &, char const *);
134
135 /// Does the string end with this suffix?
136 bool suffixIs(string const &, string const &);
137
138 ///
139 bool contains(string const & a, string const & b);
140
141 ///
142 bool contains(string const & a, char b);
143
144 /// This should probably we rewritten to be more general.
145 class contains_functor {
146 public:
147         typedef string first_argument_type;
148         typedef string second_argument_type;
149         typedef bool result_type;
150
151         bool operator()(string const & haystack, string const & needle) const {
152                 return contains(haystack, needle);
153         }
154 };
155
156
157 ///
158 bool containsOnly(string const &, string const &);
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     token("a;bc;d", ';', 1) == "bc";
165     token("a;bc;d", ';', 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     tokenPos("a;bc;d", ';', "bc") == 1;
177     tokenPos("a;bc;d", ';', "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 /** Trims characters off the end and beginning of a string.
200     \code
201     trim("ccabccc", "c") == "ab".
202     \endcode
203 */
204 string const trim(string const & a, char const * p = " ");
205
206 /** Trims characters off the end of a string.
207     \code
208     rtrim("abccc", "c") == "ab".
209     \endcode
210 */
211 string const rtrim(string const & a, char const * p = " ");
212
213 /** Trims characters off the beginning of a string.
214     \code
215    ltrim("ababcdef", "ab") = "cdef"
216     \endcode
217 */
218 string const ltrim(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 /// Escapes non ASCII chars
238 string const escape(string const & lab);
239
240 /// gives a vector of stringparts which have the delimiter delim
241 std::vector<string> const getVectorFromString(string const & str,
242                                               string const & delim = ",");
243
244 // the same vice versa
245 string const getStringFromVector(std::vector<string> const & vec,
246                                  string const & delim = ",");
247
248 #endif