]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | /// \file sub_match.hpp | |
3 | /// Contains the definition of the class template sub_match\<\> | |
4 | /// and associated helper functions | |
5 | // | |
6 | // Copyright 2008 Eric Niebler. Distributed under the Boost | |
7 | // Software License, Version 1.0. (See accompanying file | |
8 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | ||
10 | #ifndef BOOST_XPRESSIVE_SUB_MATCH_HPP_EAN_10_04_2005 | |
11 | #define BOOST_XPRESSIVE_SUB_MATCH_HPP_EAN_10_04_2005 | |
12 | ||
13 | // MS compatible compilers support #pragma once | |
14 | #if defined(_MSC_VER) | |
15 | # pragma once | |
16 | #endif | |
17 | ||
18 | #include <iosfwd> | |
19 | #include <string> | |
20 | #include <utility> | |
21 | #include <iterator> | |
22 | #include <algorithm> | |
23 | #include <boost/mpl/assert.hpp> | |
24 | #include <boost/type_traits/is_same.hpp> | |
25 | #include <boost/iterator/iterator_traits.hpp> | |
26 | #include <boost/range/const_iterator.hpp> | |
27 | #include <boost/range/mutable_iterator.hpp> | |
28 | #include <boost/xpressive/detail/detail_fwd.hpp> | |
29 | ||
30 | //{{AFX_DOC_COMMENT | |
31 | /////////////////////////////////////////////////////////////////////////////// | |
32 | // This is a hack to get Doxygen to show the inheritance relation between | |
33 | // sub_match<T> and std::pair<T,T>. | |
34 | #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED | |
35 | /// INTERNAL ONLY | |
36 | namespace std | |
37 | { | |
38 | /// INTERNAL ONLY | |
39 | template<typename, typename> struct pair {}; | |
40 | } | |
41 | #endif | |
42 | //}}AFX_DOC_COMMENT | |
43 | ||
44 | namespace boost { namespace xpressive | |
45 | { | |
46 | ||
47 | /////////////////////////////////////////////////////////////////////////////// | |
48 | // sub_match | |
49 | // | |
50 | /// \brief Class template \c sub_match denotes the sequence of characters matched by a particular | |
51 | /// marked sub-expression. | |
52 | /// | |
53 | /// When the marked sub-expression denoted by an object of type \c sub_match\<\> participated in a | |
54 | /// regular expression match then member \c matched evaluates to \c true, and members \c first and \c second | |
55 | /// denote the range of characters <tt>[first,second)</tt> which formed that match. Otherwise \c matched is \c false, | |
56 | /// and members \c first and \c second contained undefined values. | |
57 | /// | |
58 | /// If an object of type \c sub_match\<\> represents sub-expression 0 - that is to say the whole match - | |
59 | /// then member \c matched is always \c true, unless a partial match was obtained as a result of the flag | |
60 | /// \c match_partial being passed to a regular expression algorithm, in which case member \c matched is | |
61 | /// \c false, and members \c first and \c second represent the character range that formed the partial match. | |
62 | template<typename BidiIter> | |
63 | struct sub_match | |
64 | : std::pair<BidiIter, BidiIter> | |
65 | { | |
66 | private: | |
67 | /// INTERNAL ONLY | |
68 | /// | |
69 | struct dummy { int i_; }; | |
70 | typedef int dummy::*bool_type; | |
71 | ||
72 | public: | |
73 | typedef typename iterator_value<BidiIter>::type value_type; | |
74 | typedef typename iterator_difference<BidiIter>::type difference_type; | |
75 | typedef typename detail::string_type<value_type>::type string_type; | |
76 | typedef BidiIter iterator; | |
77 | ||
78 | sub_match() | |
79 | : std::pair<BidiIter, BidiIter>() | |
80 | , matched(false) | |
81 | { | |
82 | } | |
83 | ||
84 | sub_match(BidiIter first, BidiIter second, bool matched_ = false) | |
85 | : std::pair<BidiIter, BidiIter>(first, second) | |
86 | , matched(matched_) | |
87 | { | |
88 | } | |
89 | ||
90 | string_type str() const | |
91 | { | |
92 | return this->matched ? string_type(this->first, this->second) : string_type(); | |
93 | } | |
94 | ||
95 | operator string_type() const | |
96 | { | |
97 | return this->matched ? string_type(this->first, this->second) : string_type(); | |
98 | } | |
99 | ||
100 | difference_type length() const | |
101 | { | |
102 | return this->matched ? std::distance(this->first, this->second) : 0; | |
103 | } | |
104 | ||
105 | operator bool_type() const | |
106 | { | |
107 | return this->matched ? &dummy::i_ : 0; | |
108 | } | |
109 | ||
110 | bool operator !() const | |
111 | { | |
112 | return !this->matched; | |
113 | } | |
114 | ||
115 | /// \brief Performs a lexicographic string comparison | |
116 | /// \param str the string against which to compare | |
117 | /// \return the results of <tt>(*this).str().compare(str)</tt> | |
118 | int compare(string_type const &str) const | |
119 | { | |
120 | return this->str().compare(str); | |
121 | } | |
122 | ||
123 | /// \overload | |
124 | /// | |
125 | int compare(sub_match const &sub) const | |
126 | { | |
127 | return this->str().compare(sub.str()); | |
128 | } | |
129 | ||
130 | /// \overload | |
131 | /// | |
132 | int compare(value_type const *ptr) const | |
133 | { | |
134 | return this->str().compare(ptr); | |
135 | } | |
136 | ||
137 | /// \brief true if this sub-match participated in the full match. | |
138 | bool matched; | |
139 | }; | |
140 | ||
141 | /////////////////////////////////////////////////////////////////////////////// | |
142 | /// \brief \c range_begin() to make \c sub_match\<\> a valid range | |
143 | /// \param sub the \c sub_match\<\> object denoting the range | |
144 | /// \return \c sub.first | |
145 | /// \pre \c sub.first is not singular | |
146 | template<typename BidiIter> | |
147 | inline BidiIter range_begin(sub_match<BidiIter> &sub) | |
148 | { | |
149 | return sub.first; | |
150 | } | |
151 | ||
152 | /// \overload | |
153 | /// | |
154 | template<typename BidiIter> | |
155 | inline BidiIter range_begin(sub_match<BidiIter> const &sub) | |
156 | { | |
157 | return sub.first; | |
158 | } | |
159 | ||
160 | /////////////////////////////////////////////////////////////////////////////// | |
161 | /// \brief \c range_end() to make \c sub_match\<\> a valid range | |
162 | /// \param sub the \c sub_match\<\> object denoting the range | |
163 | /// \return \c sub.second | |
164 | /// \pre \c sub.second is not singular | |
165 | template<typename BidiIter> | |
166 | inline BidiIter range_end(sub_match<BidiIter> &sub) | |
167 | { | |
168 | return sub.second; | |
169 | } | |
170 | ||
171 | /// \overload | |
172 | /// | |
173 | template<typename BidiIter> | |
174 | inline BidiIter range_end(sub_match<BidiIter> const &sub) | |
175 | { | |
176 | return sub.second; | |
177 | } | |
178 | ||
179 | /////////////////////////////////////////////////////////////////////////////// | |
180 | /// \brief insertion operator for sending sub-matches to ostreams | |
181 | /// \param sout output stream. | |
182 | /// \param sub sub_match object to be written to the stream. | |
183 | /// \return sout \<\< sub.str() | |
184 | template<typename BidiIter, typename Char, typename Traits> | |
185 | inline std::basic_ostream<Char, Traits> &operator << | |
186 | ( | |
187 | std::basic_ostream<Char, Traits> &sout | |
188 | , sub_match<BidiIter> const &sub | |
189 | ) | |
190 | { | |
191 | typedef typename iterator_value<BidiIter>::type char_type; | |
192 | BOOST_MPL_ASSERT_MSG( | |
193 | (boost::is_same<Char, char_type>::value) | |
194 | , CHARACTER_TYPES_OF_STREAM_AND_SUB_MATCH_MUST_MATCH | |
195 | , (Char, char_type) | |
196 | ); | |
197 | if(sub.matched) | |
198 | { | |
199 | std::ostream_iterator<char_type, Char, Traits> iout(sout); | |
200 | std::copy(sub.first, sub.second, iout); | |
201 | } | |
202 | return sout; | |
203 | } | |
204 | ||
205 | ||
206 | // BUGBUG make these more efficient | |
207 | ||
208 | template<typename BidiIter> | |
209 | bool operator == (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs) | |
210 | { | |
211 | return lhs.compare(rhs) == 0; | |
212 | } | |
213 | ||
214 | template<typename BidiIter> | |
215 | bool operator != (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs) | |
216 | { | |
217 | return lhs.compare(rhs) != 0; | |
218 | } | |
219 | ||
220 | template<typename BidiIter> | |
221 | bool operator < (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs) | |
222 | { | |
223 | return lhs.compare(rhs) < 0; | |
224 | } | |
225 | ||
226 | template<typename BidiIter> | |
227 | bool operator <= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs) | |
228 | { | |
229 | return lhs.compare(rhs) <= 0; | |
230 | } | |
231 | ||
232 | template<typename BidiIter> | |
233 | bool operator >= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs) | |
234 | { | |
235 | return lhs.compare(rhs) >= 0; | |
236 | } | |
237 | ||
238 | template<typename BidiIter> | |
239 | bool operator > (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs) | |
240 | { | |
241 | return lhs.compare(rhs) > 0; | |
242 | } | |
243 | ||
244 | template<typename BidiIter> | |
245 | bool operator == (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs) | |
246 | { | |
247 | return lhs == rhs.str(); | |
248 | } | |
249 | ||
250 | template<typename BidiIter> | |
251 | bool operator != (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs) | |
252 | { | |
253 | return lhs != rhs.str(); | |
254 | } | |
255 | ||
256 | template<typename BidiIter> | |
257 | bool operator < (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs) | |
258 | { | |
259 | return lhs < rhs.str(); | |
260 | } | |
261 | ||
262 | template<typename BidiIter> | |
263 | bool operator > (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs) | |
264 | { | |
265 | return lhs> rhs.str(); | |
266 | } | |
267 | ||
268 | template<typename BidiIter> | |
269 | bool operator >= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs) | |
270 | { | |
271 | return lhs >= rhs.str(); | |
272 | } | |
273 | ||
274 | template<typename BidiIter> | |
275 | bool operator <= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs) | |
276 | { | |
277 | return lhs <= rhs.str(); | |
278 | } | |
279 | ||
280 | template<typename BidiIter> | |
281 | bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs) | |
282 | { | |
283 | return lhs.str() == rhs; | |
284 | } | |
285 | ||
286 | template<typename BidiIter> | |
287 | bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs) | |
288 | { | |
289 | return lhs.str() != rhs; | |
290 | } | |
291 | ||
292 | template<typename BidiIter> | |
293 | bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs) | |
294 | { | |
295 | return lhs.str() < rhs; | |
296 | } | |
297 | ||
298 | template<typename BidiIter> | |
299 | bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs) | |
300 | { | |
301 | return lhs.str() > rhs; | |
302 | } | |
303 | ||
304 | template<typename BidiIter> | |
305 | bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs) | |
306 | { | |
307 | return lhs.str() >= rhs; | |
308 | } | |
309 | ||
310 | template<typename BidiIter> | |
311 | bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs) | |
312 | { | |
313 | return lhs.str() <= rhs; | |
314 | } | |
315 | ||
316 | template<typename BidiIter> | |
317 | bool operator == (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs) | |
318 | { | |
319 | return lhs == rhs.str(); | |
320 | } | |
321 | ||
322 | template<typename BidiIter> | |
323 | bool operator != (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs) | |
324 | { | |
325 | return lhs != rhs.str(); | |
326 | } | |
327 | ||
328 | template<typename BidiIter> | |
329 | bool operator < (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs) | |
330 | { | |
331 | return lhs < rhs.str(); | |
332 | } | |
333 | ||
334 | template<typename BidiIter> | |
335 | bool operator > (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs) | |
336 | { | |
337 | return lhs> rhs.str(); | |
338 | } | |
339 | ||
340 | template<typename BidiIter> | |
341 | bool operator >= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs) | |
342 | { | |
343 | return lhs >= rhs.str(); | |
344 | } | |
345 | ||
346 | template<typename BidiIter> | |
347 | bool operator <= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs) | |
348 | { | |
349 | return lhs <= rhs.str(); | |
350 | } | |
351 | ||
352 | template<typename BidiIter> | |
353 | bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs) | |
354 | { | |
355 | return lhs.str() == rhs; | |
356 | } | |
357 | ||
358 | template<typename BidiIter> | |
359 | bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs) | |
360 | { | |
361 | return lhs.str() != rhs; | |
362 | } | |
363 | ||
364 | template<typename BidiIter> | |
365 | bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs) | |
366 | { | |
367 | return lhs.str() < rhs; | |
368 | } | |
369 | ||
370 | template<typename BidiIter> | |
371 | bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs) | |
372 | { | |
373 | return lhs.str() > rhs; | |
374 | } | |
375 | ||
376 | template<typename BidiIter> | |
377 | bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs) | |
378 | { | |
379 | return lhs.str() >= rhs; | |
380 | } | |
381 | ||
382 | template<typename BidiIter> | |
383 | bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs) | |
384 | { | |
385 | return lhs.str() <= rhs; | |
386 | } | |
387 | ||
388 | // Operator+ convenience function | |
389 | template<typename BidiIter> | |
390 | typename sub_match<BidiIter>::string_type | |
391 | operator + (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs) | |
392 | { | |
393 | return lhs.str() + rhs.str(); | |
394 | } | |
395 | ||
396 | template<typename BidiIter> | |
397 | typename sub_match<BidiIter>::string_type | |
398 | operator + (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs) | |
399 | { | |
400 | return lhs.str() + rhs; | |
401 | } | |
402 | ||
403 | template<typename BidiIter> | |
404 | typename sub_match<BidiIter>::string_type | |
405 | operator + (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs) | |
406 | { | |
407 | return lhs + rhs.str(); | |
408 | } | |
409 | ||
410 | template<typename BidiIter> | |
411 | typename sub_match<BidiIter>::string_type | |
412 | operator + (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs) | |
413 | { | |
414 | return lhs.str() + rhs; | |
415 | } | |
416 | ||
417 | template<typename BidiIter> | |
418 | typename sub_match<BidiIter>::string_type | |
419 | operator + (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs) | |
420 | { | |
421 | return lhs + rhs.str(); | |
422 | } | |
423 | ||
424 | template<typename BidiIter> | |
425 | typename sub_match<BidiIter>::string_type | |
426 | operator + (sub_match<BidiIter> const &lhs, typename sub_match<BidiIter>::string_type const &rhs) | |
427 | { | |
428 | return lhs.str() + rhs; | |
429 | } | |
430 | ||
431 | template<typename BidiIter> | |
432 | typename sub_match<BidiIter>::string_type | |
433 | operator + (typename sub_match<BidiIter>::string_type const &lhs, sub_match<BidiIter> const &rhs) | |
434 | { | |
435 | return lhs + rhs.str(); | |
436 | } | |
437 | ||
438 | }} // namespace boost::xpressive | |
439 | ||
440 | // Hook the Boost.Range customization points to make sub_match a valid range. | |
441 | namespace boost | |
442 | { | |
443 | /// INTERNAL ONLY | |
444 | /// | |
445 | template<typename BidiIter> | |
446 | struct range_mutable_iterator<xpressive::sub_match<BidiIter> > | |
447 | { | |
448 | typedef BidiIter type; | |
449 | }; | |
450 | ||
451 | /// INTERNAL ONLY | |
452 | /// | |
453 | template<typename BidiIter> | |
454 | struct range_const_iterator<xpressive::sub_match<BidiIter> > | |
455 | { | |
456 | typedef BidiIter type; | |
457 | }; | |
458 | } | |
459 | ||
460 | #endif |