]> git.lyx.org Git - features.git/blob - src/support/lstrings.h
lstrings.[Ch]: use string const & instead of char const * for format
[features.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 #include <vector>
20
21 #include "LString.h"
22
23 ///
24 int compare_no_case(string const & s, string const & s2);
25
26 ///
27 int compare_ascii_no_case(string const & s, string const & s2);
28
29 ///
30 int compare_no_case(string const & s, string const & s2, unsigned int len);
31
32 ///
33 inline
34 int compare(char const * a, char const * b)
35 {
36 #ifndef CXX_GLOBAL_CSTD
37         return std::strcmp(a, b);
38 #else
39         return strcmp(a, b);
40 #endif
41 }
42
43 ///
44 inline
45 int compare(char const * a, char const * b, unsigned int len)
46 {
47 #ifndef CXX_GLOBAL_CSTD
48         return std::strncmp(a, b, len);
49 #else
50         return strncmp(a, b, len);
51 #endif
52 }
53
54 ///
55 bool isStrInt(string const & str);
56
57 /// does the string represent an unsigned integer value ?
58 bool isStrUnsignedInt(string const & str);
59
60 ///
61 int strToInt(string const & str);
62
63 /// convert string to an unsigned integer
64 unsigned int strToUnsignedInt(string const & str);
65
66 ///
67 bool isStrDbl(string const & str);
68
69 ///
70 double strToDbl(string const & str);
71
72 ///
73 char lowercase(char c);
74
75 ///
76 char uppercase(char c);
77
78 /// same as lowercase(), but ignores locale
79 string const ascii_lowercase(string const &);
80
81 ///
82 string const lowercase(string const &);
83
84 ///
85 string const uppercase(string const &);
86
87 /// Does the string start with this prefix?
88 bool prefixIs(string const &, char const *);
89
90 /// Does the string start with this prefix?
91 bool prefixIs(string const &, string const &);
92
93 /// Does the string end with this char?
94 bool suffixIs(string const &, char);
95
96 /// Does the string end with this suffix?
97 bool suffixIs(string const &, char const *);
98
99 /// Does the string end with this suffix?
100 bool suffixIs(string const &, string const &);
101
102 ///
103 bool contains(string const & a, string const & b);
104
105 ///
106 bool contains(string const & a, char b);
107
108 /// This should probably we rewritten to be more general.
109 class contains_functor {
110 public:
111         typedef string first_argument_type;
112         typedef string second_argument_type;
113         typedef bool result_type;
114
115         bool operator()(string const & haystack, string const & needle) const {
116                 return contains(haystack, needle);
117         }
118 };
119
120
121 ///
122 bool containsOnly(string const &, string const &);
123
124 /** Extracts a token from this string at the nth delim.
125     Doesn't modify the original string. Similar to strtok.
126     Example:
127     \code
128     token("a;bc;d", ';', 1) == "bc";
129     token("a;bc;d", ';', 2) == "d";
130     \endcode
131 */
132 string const token(string const & a, char delim, int n);
133
134
135 /** Search a token in this string using the delim.
136     Doesn't modify the original string. Returns -1 in case of
137     failure.
138     Example:
139     \code
140     tokenPos("a;bc;d", ';', "bc") == 1;
141     tokenPos("a;bc;d", ';', "d") == 2;
142     \endcode
143 */
144 int tokenPos(string const & a, char delim, string const & tok);
145
146
147 /** Compares a string and a (simple) regular expression
148   The only element allowed is "*" for any string of characters
149   */
150 bool regexMatch(string const & a, string const & pattern);
151
152 /// Substitute all \a oldchar with \a newchar
153 string const subst(string const & a, char oldchar, char newchar);
154
155 /// Substitutes all instances of \a oldstr with \a newstr
156 string const subst(string const & a,
157              char const * oldstr, string const & newstr);
158
159 /// substitutes all instances of \a oldstr with \a newstr
160 string const subst(string const & a,
161                    string const & oldstr, string const & newstr);
162
163 /** Trims characters off the end and beginning of a string.
164     \code
165     trim("ccabccc", "c") == "ab".
166     \endcode
167 */
168 string const trim(string const & a, char const * p = " ");
169
170 /** Trims characters off the end of a string.
171     \code
172     rtrim("abccc", "c") == "ab".
173     \endcode
174 */
175 string const rtrim(string const & a, char const * p = " ");
176
177 /** Trims characters off the beginning of a string.
178     \code
179    ltrim("ababcdef", "ab") = "cdef"
180     \endcode
181 */
182 string const ltrim(string const & a, char const * p = " ");
183
184 /** Splits the string by the first delim.
185     Splits the string by the first appearance of delim.
186     The leading string up to delim is returned in piece (not including
187     delim), while the original string is cut from after the delimiter.
188     Example:
189     \code
190     s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
191     \endcode
192 */
193 string const split(string const & a, string & piece, char delim);
194
195 /// Same as split but does not return a piece
196 string const split(string const & a, char delim);
197
198 /// Same as split but uses the last delim.
199 string const rsplit(string const & a, string & piece, char delim);
200
201 /// Escapes non ASCII chars
202 string const escape(string const & lab);
203
204 /// gives a vector of stringparts which have the delimiter delim
205 std::vector<string> const getVectorFromString(string const & str,
206                                               string const & delim = ",");
207
208 // the same vice versa
209 string const getStringFromVector(std::vector<string> const & vec,
210                                  string const & delim = ",");
211
212 // wrapper around boost::format using one argument %1$s
213 string bformat(string const & fmt, string const & arg1);
214 // arguments %1$s and %2$s
215 string bformat(string const & fmt, string const & arg1, string const & arg2);
216 // arguments %1$s and %2$s and %3$s
217 string bformat(string const & fmt, string const & arg1, string const & arg2,
218                string const & arg3);
219 // arguments %1$s and %2$s and %3$s and %4$s
220 string bformat(string const & fmt, string const & arg1, string const & arg2,
221                string const & arg3, string const & arg4);
222
223 #endif