PageRenderTime 45ms CodeModel.GetById 18ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/xpressive/sub_match.hpp

http://hadesmem.googlecode.com/
C++ Header | 392 lines | 285 code | 60 blank | 47 comment | 22 complexity | c55c80ebe95c2b71060798d5405b203d MD5 | raw file
  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) && (_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#include <boost/xpressive/detail/detail_fwd.hpp>
 25
 26//{{AFX_DOC_COMMENT
 27///////////////////////////////////////////////////////////////////////////////
 28// This is a hack to get Doxygen to show the inheritance relation between
 29// sub_match<T> and std::pair<T,T>.
 30#ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED
 31/// INTERNAL ONLY
 32namespace std
 33{
 34    /// INTERNAL ONLY
 35    template<typename, typename> struct pair {};
 36}
 37#endif
 38//}}AFX_DOC_COMMENT
 39
 40namespace boost { namespace xpressive
 41{
 42
 43///////////////////////////////////////////////////////////////////////////////
 44// sub_match
 45//
 46/// \brief Class template sub_match denotes the sequence of characters matched by a particular marked sub-expression.
 47///
 48/// When the marked sub-expression denoted by an object of type sub_match\<\> participated in a
 49/// regular expression match then member matched evaluates to true, and members first and second
 50/// denote the range of characters [first,second) which formed that match. Otherwise matched is false,
 51/// and members first and second contained undefined values.
 52///
 53/// If an object of type sub_match\<\> represents sub-expression 0 - that is to say the whole match -
 54/// then member matched is always true, unless a partial match was obtained as a result of the flag
 55/// match_partial being passed to a regular expression algorithm, in which case member matched is
 56/// false, and members first and second represent the character range that formed the partial match.
 57template<typename BidiIter>
 58struct sub_match
 59  : std::pair<BidiIter, BidiIter>
 60{
 61private:
 62    /// INTERNAL ONLY
 63    ///
 64    struct dummy { int i_; };
 65    typedef int dummy::*bool_type;
 66
 67public:
 68    typedef typename iterator_value<BidiIter>::type value_type;
 69    typedef typename iterator_difference<BidiIter>::type difference_type;
 70    typedef typename detail::string_type<value_type>::type string_type;
 71    typedef BidiIter iterator;
 72
 73    sub_match()
 74      : std::pair<BidiIter, BidiIter>()
 75      , matched(false)
 76    {
 77    }
 78
 79    sub_match(BidiIter first, BidiIter second, bool matched_ = false)
 80      : std::pair<BidiIter, BidiIter>(first, second)
 81      , matched(matched_)
 82    {
 83    }
 84
 85    string_type str() const
 86    {
 87        return this->matched ? string_type(this->first, this->second) : string_type();
 88    }
 89
 90    operator string_type() const
 91    {
 92        return this->matched ? string_type(this->first, this->second) : string_type();
 93    }
 94
 95    difference_type length() const
 96    {
 97        return this->matched ? std::distance(this->first, this->second) : 0;
 98    }
 99
100    operator bool_type() const
101    {
102        return this->matched ? &dummy::i_ : 0;
103    }
104
105    bool operator !() const
106    {
107        return !this->matched;
108    }
109
110    /// \brief Performs a lexicographic string comparison
111    /// \param str the string against which to compare
112    /// \return the results of (*this).str().compare(str)
113    int compare(string_type const &str) const
114    {
115        return this->str().compare(str);
116    }
117
118    /// \overload
119    ///
120    int compare(sub_match const &sub) const
121    {
122        return this->str().compare(sub.str());
123    }
124
125    /// \overload
126    ///
127    int compare(value_type const *ptr) const
128    {
129        return this->str().compare(ptr);
130    }
131
132    /// \brief true if this sub-match participated in the full match.
133    bool matched;
134};
135
136///////////////////////////////////////////////////////////////////////////////
137/// \brief insertion operator for sending sub-matches to ostreams
138/// \param sout output stream.
139/// \param sub sub_match object to be written to the stream.
140/// \return sout \<\< sub.str()
141template<typename BidiIter, typename Char, typename Traits>
142inline std::basic_ostream<Char, Traits> &operator <<
143(
144    std::basic_ostream<Char, Traits> &sout
145  , sub_match<BidiIter> const &sub
146)
147{
148    typedef typename iterator_value<BidiIter>::type char_type;
149    if(sub.matched)
150    {
151        std::ostream_iterator<char_type, Char, Traits> iout(sout);
152        std::copy(sub.first, sub.second, iout);
153    }
154    return sout;
155}
156
157
158// BUGBUG make these more efficient
159
160template<typename BidiIter>
161bool operator == (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
162{
163    return lhs.compare(rhs) == 0;
164}
165
166template<typename BidiIter>
167bool operator != (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
168{
169    return lhs.compare(rhs) != 0;
170}
171
172template<typename BidiIter>
173bool operator < (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
174{
175    return lhs.compare(rhs) < 0;
176}
177
178template<typename BidiIter>
179bool operator <= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
180{
181    return lhs.compare(rhs) <= 0;
182}
183
184template<typename BidiIter>
185bool operator >= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
186{
187    return lhs.compare(rhs) >= 0;
188}
189
190template<typename BidiIter>
191bool operator > (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
192{
193    return lhs.compare(rhs) > 0;
194}
195
196template<typename BidiIter>
197bool operator == (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
198{
199    return lhs == rhs.str();
200}
201
202template<typename BidiIter>
203bool operator != (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
204{
205    return lhs != rhs.str();
206}
207
208template<typename BidiIter>
209bool operator < (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
210{
211    return lhs < rhs.str();
212}
213
214template<typename BidiIter>
215bool operator > (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
216{
217    return lhs> rhs.str();
218}
219
220template<typename BidiIter>
221bool operator >= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
222{
223    return lhs >= rhs.str();
224}
225
226template<typename BidiIter>
227bool operator <= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
228{
229    return lhs <= rhs.str();
230}
231
232template<typename BidiIter>
233bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
234{
235    return lhs.str() == rhs;
236}
237
238template<typename BidiIter>
239bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
240{
241    return lhs.str() != rhs;
242}
243
244template<typename BidiIter>
245bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
246{
247    return lhs.str() < rhs;
248}
249
250template<typename BidiIter>
251bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
252{
253    return lhs.str() > rhs;
254}
255
256template<typename BidiIter>
257bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
258{
259    return lhs.str() >= rhs;
260}
261
262template<typename BidiIter>
263bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
264{
265    return lhs.str() <= rhs;
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 < (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
282{
283    return lhs < rhs.str();
284}
285
286template<typename BidiIter>
287bool operator > (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
288{
289    return lhs> rhs.str();
290}
291
292template<typename BidiIter>
293bool operator >= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
294{
295    return lhs >= rhs.str();
296}
297
298template<typename BidiIter>
299bool operator <= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
300{
301    return lhs <= rhs.str();
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 < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
318{
319    return lhs.str() < rhs;
320}
321
322template<typename BidiIter>
323bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
324{
325    return lhs.str() > rhs;
326}
327
328template<typename BidiIter>
329bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
330{
331    return lhs.str() >= rhs;
332}
333
334template<typename BidiIter>
335bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
336{
337    return lhs.str() <= rhs;
338}
339
340// Operator+ convenience function
341template<typename BidiIter>
342typename sub_match<BidiIter>::string_type
343operator + (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
344{
345    return lhs.str() + rhs.str();
346}
347
348template<typename BidiIter>
349typename sub_match<BidiIter>::string_type
350operator + (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
351{
352    return lhs.str() + rhs;
353}
354
355template<typename BidiIter>
356typename sub_match<BidiIter>::string_type
357operator + (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
358{
359    return lhs + rhs.str();
360}
361
362template<typename BidiIter>
363typename sub_match<BidiIter>::string_type
364operator + (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
365{
366    return lhs.str() + rhs;
367}
368
369template<typename BidiIter>
370typename sub_match<BidiIter>::string_type
371operator + (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
372{
373    return lhs + rhs.str();
374}
375
376template<typename BidiIter>
377typename sub_match<BidiIter>::string_type
378operator + (sub_match<BidiIter> const &lhs, typename sub_match<BidiIter>::string_type const &rhs)
379{
380    return lhs.str() + rhs;
381}
382
383template<typename BidiIter>
384typename sub_match<BidiIter>::string_type
385operator + (typename sub_match<BidiIter>::string_type const &lhs, sub_match<BidiIter> const &rhs)
386{
387    return lhs + rhs.str();
388}
389
390}} // namespace boost::xpressive
391
392#endif