Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/boost/xpressive/sub_match.hpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 9.3 KB
RevLine 
[29]1///////////////////////////////////////////////////////////////////////////////
2/// \file sub_match.hpp
3/// Contains the definition of the class template sub_match\<\>
4/// and associated helper functions
5//
6//  Copyright 2004 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) && (_MSC_VER >= 1020)
15# pragma once
16#endif
17
18#include <iosfwd>
19#include <string>
20#include <utility>
21#include <iterator>
22#include <algorithm>
23#include <boost/iterator/iterator_traits.hpp>
24
25//{{AFX_DOC_COMMENT
26///////////////////////////////////////////////////////////////////////////////
27// This is a hack to get Doxygen to show the inheritance relation between
28// sub_match<T> and std::pair<T,T>.
29#ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED
30/// INTERNAL ONLY
31namespace std
32{
33    /// INTERNAL ONLY
34    template<typename, typename> struct pair {};
35}
36#endif
37//}}AFX_DOC_COMMENT
38
39namespace boost { namespace xpressive
40{
41
42///////////////////////////////////////////////////////////////////////////////
43// sub_match
44//
45/// \brief Class template sub_match denotes the sequence of characters matched by a particular marked sub-expression.
46///
47/// When the marked sub-expression denoted by an object of type sub_match\<\> participated in a
48/// regular expression match then member matched evaluates to true, and members first and second
49/// denote the range of characters [first,second) which formed that match. Otherwise matched is false,
50/// and members first and second contained undefined values.
51///
52/// If an object of type sub_match\<\> represents sub-expression 0 - that is to say the whole match -
53/// then member matched is always true, unless a partial match was obtained as a result of the flag
54/// match_partial being passed to a regular expression algorithm, in which case member matched is
55/// false, and members first and second represent the character range that formed the partial match.
56template<typename BidiIter>
57struct sub_match
58  : std::pair<BidiIter, BidiIter>
59{
60private:
61    struct dummy { int i_; };
62    typedef int dummy::*bool_type;
63
64public:
65    typedef typename iterator_value<BidiIter>::type value_type;
66    typedef typename iterator_difference<BidiIter>::type difference_type;
67    typedef std::basic_string<value_type> string_type;
68    typedef BidiIter iterator;
69
70    explicit sub_match(BidiIter first = BidiIter(), BidiIter second = BidiIter(), bool matched_ = false)
71      : std::pair<BidiIter, BidiIter>(first, second)
72      , matched(matched_)
73    {
74    }
75
76    string_type str() const
77    {
78        return this->matched ? string_type(this->first, this->second) : string_type();
79    }
80
81    operator string_type() const
82    {
83        return this->matched ? string_type(this->first, this->second) : string_type();
84    }
85
86    difference_type length() const
87    {
88        return this->matched ? std::distance(this->first, this->second) : 0;
89    }
90
91    operator bool_type() const
92    {
93        return this->matched ? &dummy::i_ : 0;
94    }
95
96    bool operator !() const
97    {
98        return !this->matched;
99    }
100
101    /// \brief Performs a lexicographic string comparison
102    /// \param str the string against which to compare
103    /// \return the results of (*this).str().compare(str)
104    int compare(string_type const &str) const
105    {
106        return this->str().compare(str);
107    }
108
109    /// \overload
110    int compare(sub_match const &sub) const
111    {
112        return this->str().compare(sub.str());
113    }
114
115    /// \overload
116    int compare(value_type const *ptr) const
117    {
118        return this->str().compare(ptr);
119    }
120
121    /// \brief true if this sub-match participated in the full match.
122    bool matched;
123};
124
125///////////////////////////////////////////////////////////////////////////////
126/// \brief insertion operator for sending sub-matches to ostreams
127/// \param sout output stream.
128/// \param sub sub_match object to be written to the stream.
129/// \return sout \<\< sub.str()
130template<typename BidiIter, typename Char, typename Traits>
131inline std::basic_ostream<Char, Traits> &operator <<
132(
133    std::basic_ostream<Char, Traits> &sout
134  , sub_match<BidiIter> const &sub
135)
136{
137    typedef typename iterator_value<BidiIter>::type char_type;
138    if(sub.matched)
139    {
140        std::ostream_iterator<char_type, Char, Traits> iout(sout);
141        std::copy(sub.first, sub.second, iout);
142    }
143    return sout;
144}
145
146
147// BUGBUG make these more efficient
148
149template<typename BidiIter>
150bool operator == (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
151{
152    return lhs.compare(rhs) == 0;
153}
154
155template<typename BidiIter>
156bool operator != (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
157{
158    return lhs.compare(rhs) != 0;
159}
160
161template<typename BidiIter>
162bool operator < (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
163{
164    return lhs.compare(rhs) < 0;
165}
166
167template<typename BidiIter>
168bool operator <= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
169{
170    return lhs.compare(rhs) <= 0;
171}
172
173template<typename BidiIter>
174bool operator >= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
175{
176    return lhs.compare(rhs)>= 0;
177}
178
179template<typename BidiIter>
180bool operator> (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
181{
182    return lhs.compare(rhs)> 0;
183}
184
185template<typename BidiIter>
186bool operator == (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
187{
188    return lhs == rhs.str();
189}
190
191template<typename BidiIter>
192bool operator != (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
193{
194    return lhs != rhs.str();
195}
196
197template<typename BidiIter>
198bool operator < (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
199{
200    return lhs < rhs.str();
201}
202
203template<typename BidiIter>
204bool operator> (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
205{
206    return lhs> rhs.str();
207}
208
209template<typename BidiIter>
210bool operator >= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
211{
212    return lhs >= rhs.str();
213}
214
215template<typename BidiIter>
216bool operator <= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
217{
218    return lhs <= rhs.str();
219}
220
221template<typename BidiIter>
222bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
223{
224    return lhs.str() == rhs;
225}
226
227template<typename BidiIter>
228bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
229{
230    return lhs.str() != rhs;
231}
232
233template<typename BidiIter>
234bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
235{
236    return lhs.str() < rhs;
237}
238
239template<typename BidiIter>
240bool operator> (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
241{
242    return lhs.str()> rhs;
243}
244
245template<typename BidiIter>
246bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
247{
248    return lhs.str()>= rhs;
249}
250
251template<typename BidiIter>
252bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
253{
254    return lhs.str() <= rhs;
255}
256
257template<typename BidiIter>
258bool operator == (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
259{
260    return lhs == rhs.str();
261}
262
263template<typename BidiIter>
264bool operator != (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
265{
266    return lhs != rhs.str();
267}
268
269template<typename BidiIter>
270bool operator < (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
271{
272    return lhs < rhs.str();
273}
274
275template<typename BidiIter>
276bool operator> (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
277{
278    return lhs> rhs.str();
279}
280
281template<typename BidiIter>
282bool operator >= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
283{
284    return lhs >= rhs.str();
285}
286
287template<typename BidiIter>
288bool operator <= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
289{
290    return lhs <= rhs.str();
291}
292
293template<typename BidiIter>
294bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
295{
296    return lhs.str() == rhs;
297}
298
299template<typename BidiIter>
300bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
301{
302    return lhs.str() != rhs;
303}
304
305template<typename BidiIter>
306bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
307{
308    return lhs.str() < rhs;
309}
310
311template<typename BidiIter>
312bool operator> (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
313{
314    return lhs.str()> rhs;
315}
316
317template<typename BidiIter>
318bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
319{
320    return lhs.str()>= rhs;
321}
322
323template<typename BidiIter>
324bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
325{
326    return lhs.str() <= rhs;
327}
328
329}} // namespace boost::xpressive
330
331#endif
Note: See TracBrowser for help on using the repository browser.