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
36namespace std
37{
38 /// INTERNAL ONLY
39 template<typename, typename> struct pair {};
40}
41#endif
42//}}AFX_DOC_COMMENT
43
44namespace 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.
62template<typename BidiIter>
63struct sub_match
64 : std::pair<BidiIter, BidiIter>
65{
66private:
67 /// INTERNAL ONLY
68 ///
69 struct dummy { int i_; };
70 typedef int dummy::*bool_type;
71
72public:
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
146template<typename BidiIter>
147inline BidiIter range_begin(sub_match<BidiIter> &sub)
148{
149 return sub.first;
150}
151
152/// \overload
153///
154template<typename BidiIter>
155inline 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
165template<typename BidiIter>
166inline BidiIter range_end(sub_match<BidiIter> &sub)
167{
168 return sub.second;
169}
170
171/// \overload
172///
173template<typename BidiIter>
174inline 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()
184template<typename BidiIter, typename Char, typename Traits>
185inline 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
208template<typename BidiIter>
209bool operator == (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
210{
211 return lhs.compare(rhs) == 0;
212}
213
214template<typename BidiIter>
215bool operator != (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
216{
217 return lhs.compare(rhs) != 0;
218}
219
220template<typename BidiIter>
221bool operator < (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
222{
223 return lhs.compare(rhs) < 0;
224}
225
226template<typename BidiIter>
227bool operator <= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
228{
229 return lhs.compare(rhs) <= 0;
230}
231
232template<typename BidiIter>
233bool operator >= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
234{
235 return lhs.compare(rhs) >= 0;
236}
237
238template<typename BidiIter>
239bool operator > (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
240{
241 return lhs.compare(rhs) > 0;
242}
243
244template<typename BidiIter>
245bool operator == (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
246{
247 return lhs == rhs.str();
248}
249
250template<typename BidiIter>
251bool operator != (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
252{
253 return lhs != rhs.str();
254}
255
256template<typename BidiIter>
257bool operator < (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
258{
259 return lhs < rhs.str();
260}
261
262template<typename BidiIter>
263bool operator > (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
264{
265 return lhs> rhs.str();
266}
267
268template<typename BidiIter>
269bool operator >= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
270{
271 return lhs >= rhs.str();
272}
273
274template<typename BidiIter>
275bool operator <= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
276{
277 return lhs <= rhs.str();
278}
279
280template<typename BidiIter>
281bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
282{
283 return lhs.str() == rhs;
284}
285
286template<typename BidiIter>
287bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
288{
289 return lhs.str() != rhs;
290}
291
292template<typename BidiIter>
293bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
294{
295 return lhs.str() < rhs;
296}
297
298template<typename BidiIter>
299bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
300{
301 return lhs.str() > rhs;
302}
303
304template<typename BidiIter>
305bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
306{
307 return lhs.str() >= rhs;
308}
309
310template<typename BidiIter>
311bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
312{
313 return lhs.str() <= rhs;
314}
315
316template<typename BidiIter>
317bool operator == (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
318{
319 return lhs == rhs.str();
320}
321
322template<typename BidiIter>
323bool operator != (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
324{
325 return lhs != rhs.str();
326}
327
328template<typename BidiIter>
329bool operator < (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
330{
331 return lhs < rhs.str();
332}
333
334template<typename BidiIter>
335bool operator > (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
336{
337 return lhs> rhs.str();
338}
339
340template<typename BidiIter>
341bool operator >= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
342{
343 return lhs >= rhs.str();
344}
345
346template<typename BidiIter>
347bool operator <= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
348{
349 return lhs <= rhs.str();
350}
351
352template<typename BidiIter>
353bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
354{
355 return lhs.str() == rhs;
356}
357
358template<typename BidiIter>
359bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
360{
361 return lhs.str() != rhs;
362}
363
364template<typename BidiIter>
365bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
366{
367 return lhs.str() < rhs;
368}
369
370template<typename BidiIter>
371bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
372{
373 return lhs.str() > rhs;
374}
375
376template<typename BidiIter>
377bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
378{
379 return lhs.str() >= rhs;
380}
381
382template<typename BidiIter>
383bool 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
389template<typename BidiIter>
390typename sub_match<BidiIter>::string_type
391operator + (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
392{
393 return lhs.str() + rhs.str();
394}
395
396template<typename BidiIter>
397typename sub_match<BidiIter>::string_type
398operator + (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
399{
400 return lhs.str() + rhs;
401}
402
403template<typename BidiIter>
404typename sub_match<BidiIter>::string_type
405operator + (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
406{
407 return lhs + rhs.str();
408}
409
410template<typename BidiIter>
411typename sub_match<BidiIter>::string_type
412operator + (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
413{
414 return lhs.str() + rhs;
415}
416
417template<typename BidiIter>
418typename sub_match<BidiIter>::string_type
419operator + (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
420{
421 return lhs + rhs.str();
422}
423
424template<typename BidiIter>
425typename sub_match<BidiIter>::string_type
426operator + (sub_match<BidiIter> const &lhs, typename sub_match<BidiIter>::string_type const &rhs)
427{
428 return lhs.str() + rhs;
429}
430
431template<typename BidiIter>
432typename sub_match<BidiIter>::string_type
433operator + (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.
441namespace 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
461

source code of boost/boost/xpressive/sub_match.hpp