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