]> git.lyx.org Git - features.git/blob - src/support/lstrings.h
parlist-16-a.diff
[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 &, string const &);
89
90 /// Does the string end with this char?
91 bool suffixIs(string const &, char);
92
93 /// Does the string end with this suffix?
94 bool suffixIs(string const &, string const &);
95
96 ///
97 bool contains(string const & a, string const & b);
98
99 ///
100 bool contains(string const & a, char b);
101
102 /// This should probably we rewritten to be more general.
103 class contains_functor {
104 public:
105         typedef string first_argument_type;
106         typedef string second_argument_type;
107         typedef bool result_type;
108
109         bool operator()(string const & haystack, string const & needle) const {
110                 return contains(haystack, needle);
111         }
112 };
113
114
115 ///
116 bool containsOnly(string const &, string const &);
117
118 /** Extracts a token from this string at the nth delim.
119     Doesn't modify the original string. Similar to strtok.
120     Example:
121     \code
122     token("a;bc;d", ';', 1) == "bc";
123     token("a;bc;d", ';', 2) == "d";
124     \endcode
125 */
126 string const token(string const & a, char delim, int n);
127
128
129 /** Search a token in this string using the delim.
130     Doesn't modify the original string. Returns -1 in case of
131     failure.
132     Example:
133     \code
134     tokenPos("a;bc;d", ';', "bc") == 1;
135     tokenPos("a;bc;d", ';', "d") == 2;
136     \endcode
137 */
138 int tokenPos(string const & a, char delim, string const & tok);
139
140
141 /** Compares a string and a (simple) regular expression
142   The only element allowed is "*" for any string of characters
143   */
144 bool regexMatch(string const & a, string const & pattern);
145
146 /// Substitute all \a oldchar with \a newchar
147 string const subst(string const & a, char oldchar, char newchar);
148
149 /// substitutes all instances of \a oldstr with \a newstr
150 string const subst(string const & a,
151                    string const & oldstr, string const & newstr);
152
153 /** Trims characters off the end and beginning of a string.
154     \code
155     trim("ccabccc", "c") == "ab".
156     \endcode
157 */
158 string const trim(string const & a, char const * p = " ");
159
160 /** Trims characters off the end of a string.
161     \code
162     rtrim("abccc", "c") == "ab".
163     \endcode
164 */
165 string const rtrim(string const & a, char const * p = " ");
166
167 /** Trims characters off the beginning of a string.
168     \code
169    ltrim("ababcdef", "ab") = "cdef"
170     \endcode
171 */
172 string const ltrim(string const & a, char const * p = " ");
173
174 /** Splits the string by the first delim.
175     Splits the string by the first appearance of delim.
176     The leading string up to delim is returned in piece (not including
177     delim), while the original string is cut from after the delimiter.
178     Example:
179     \code
180     s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
181     \endcode
182 */
183 string const split(string const & a, string & piece, char delim);
184
185 /// Same as split but does not return a piece
186 string const split(string const & a, char delim);
187
188 /// Same as split but uses the last delim.
189 string const rsplit(string const & a, string & piece, char delim);
190
191 /// Escapes non ASCII chars
192 string const escape(string const & lab);
193
194 /// gives a vector of stringparts which have the delimiter delim
195 std::vector<string> const getVectorFromString(string const & str,
196                                               string const & delim = ",");
197
198 // the same vice versa
199 string const getStringFromVector(std::vector<string> const & vec,
200                                  string const & delim = ",");
201
202 // wrapper around boost::format using one argument %1$s
203 string bformat(string const & fmt, string const & arg1);
204 // arguments %1$s and %2$s
205 string bformat(string const & fmt, string const & arg1, string const & arg2);
206 // arguments %1$s and %2$s and %3$s
207 string bformat(string const & fmt, string const & arg1, string const & arg2,
208                string const & arg3);
209 // arguments %1$s and %2$s and %3$s and %4$s
210 string bformat(string const & fmt, string const & arg1, string const & arg2,
211                string const & arg3, string const & arg4);
212
213 #endif