]> git.lyx.org Git - features.git/blob - src/support/lstrings.h
The std::string mammoth path.
[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 #include <string>
21
22
23 namespace lyx {
24 namespace support {
25
26 ///
27 int compare_no_case(std::string const & s, std::string const & s2);
28
29 ///
30 int compare_ascii_no_case(std::string const & s, std::string const & s2);
31
32 ///
33 int compare_no_case(std::string const & s, std::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(std::string const & str);
59
60 /// does the std::string represent an unsigned integer value ?
61 bool isStrUnsignedInt(std::string const & str);
62
63 ///
64 int strToInt(std::string const & str);
65
66 /// convert string to an unsigned integer
67 unsigned int strToUnsignedInt(std::string const & str);
68
69 ///
70 bool isStrDbl(std::string const & str);
71
72 ///
73 double strToDbl(std::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 std::string const ascii_lowercase(std::string const &);
83
84 ///
85 std::string const lowercase(std::string const &);
86
87 ///
88 std::string const uppercase(std::string const &);
89
90 /// Does the std::string start with this prefix?
91 bool prefixIs(std::string const &, std::string const &);
92
93 /// Does the string end with this char?
94 bool suffixIs(std::string const &, char);
95
96 /// Does the std::string end with this suffix?
97 bool suffixIs(std::string const &, std::string const &);
98
99 ///
100 bool contains(std::string const & a, std::string const & b);
101
102 ///
103 bool contains(std::string const & a, char b);
104
105 /// This should probably we rewritten to be more general.
106 class contains_functor {
107 public:
108         typedef std::string first_argument_type;
109         typedef std::string second_argument_type;
110         typedef bool result_type;
111
112         bool operator()(std::string const & haystack, std::string const & needle) const {
113                 return contains(haystack, needle);
114         }
115 };
116
117
118 ///
119 bool containsOnly(std::string const &, std::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 std::string const token(std::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(std::string const & a, char delim, std::string const & tok);
142
143
144 /// Substitute all \a oldchar with \a newchar
145 std::string const subst(std::string const & a, char oldchar, char newchar);
146
147 /// substitutes all instances of \a oldstr with \a newstr
148 std::string const subst(std::string const & a,
149                    std::string const & oldstr, std::string const & newstr);
150
151 /** Trims characters off the end and beginning of a string.
152     \code
153     trim("ccabccc", "c") == "ab".
154     \endcode
155 */
156 std::string const trim(std::string const & a, char const * p = " ");
157
158 /** Trims characters off the end of a string.
159     \code
160     rtrim("abccc", "c") == "ab".
161     \endcode
162 */
163 std::string const rtrim(std::string const & a, char const * p = " ");
164
165 /** Trims characters off the beginning of a string.
166     \code
167    ltrim("ababcdef", "ab") = "cdef"
168     \endcode
169 */
170 std::string const ltrim(std::string const & a, char const * p = " ");
171
172 /** Splits the string by the first delim.
173     Splits the string by the first appearance of delim.
174     The leading string up to delim is returned in piece (not including
175     delim), while the original string is cut from after the delimiter.
176     Example:
177     \code
178     s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
179     \endcode
180 */
181 std::string const split(std::string const & a, std::string & piece, char delim);
182
183 /// Same as split but does not return a piece
184 std::string const split(std::string const & a, char delim);
185
186 /// Same as split but uses the last delim.
187 std::string const rsplit(std::string const & a, std::string & piece, char delim);
188
189 /// Escapes non ASCII chars
190 std::string const escape(std::string const & lab);
191
192 /// gives a vector of stringparts which have the delimiter delim
193 std::vector<std::string> const getVectorFromString(std::string const & str,
194                                               std::string const & delim = std::string(","));
195
196 // the same vice versa
197 std::string const getStringFromVector(std::vector<std::string> const & vec,
198                                  std::string const & delim = std::string(","));
199
200 // wrapper around boost::format using one argument %1$s
201 std::string bformat(std::string const & fmt, std::string const & arg1);
202 // arguments %1$s and %2$s
203 std::string bformat(std::string const & fmt, std::string const & arg1, std::string const & arg2);
204 // arguments %1$d and %2$d
205 std::string bformat(std::string const & fmt, int arg1, int arg2);
206 // arguments %1$s and %2$s and %3$s
207 std::string bformat(std::string const & fmt, std::string const & arg1, std::string const & arg2,
208                std::string const & arg3);
209 // arguments %1$s and %2$s and %3$s and %4$s
210 std::string bformat(std::string const & fmt, std::string const & arg1, std::string const & arg2,
211                std::string const & arg3, std::string const & arg4);
212
213 } // namespace support
214 } // namespace lyx
215
216 #endif