1 ///////////////////////////////////////////////////////////////////////////////
2 /// \file sub_match.hpp
3 /// Contains the definition of the class template sub_match\<\>
4 /// and associated helper functions
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)
10 #ifndef BOOST_XPRESSIVE_SUB_MATCH_HPP_EAN_10_04_2005
11 #define BOOST_XPRESSIVE_SUB_MATCH_HPP_EAN_10_04_2005
13 // MS compatible compilers support #pragma once
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>
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
39 template<typename, typename> struct pair {};
44 namespace boost { namespace xpressive
47 ///////////////////////////////////////////////////////////////////////////////
50 /// \brief Class template \c sub_match denotes the sequence of characters matched by a particular
51 /// marked sub-expression.
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.
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>
64 : std::pair<BidiIter, BidiIter>
69 struct dummy { int i_; };
70 typedef int dummy::*bool_type;
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;
79 : std::pair<BidiIter, BidiIter>()
84 sub_match(BidiIter first, BidiIter second, bool matched_ = false)
85 : std::pair<BidiIter, BidiIter>(first, second)
90 string_type str() const
92 return this->matched ? string_type(this->first, this->second) : string_type();
95 operator string_type() const
97 return this->matched ? string_type(this->first, this->second) : string_type();
100 difference_type length() const
102 return this->matched ? std::distance(this->first, this->second) : 0;
105 operator bool_type() const
107 return this->matched ? &dummy::i_ : 0;
110 bool operator !() const
112 return !this->matched;
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
120 return this->str().compare(str);
125 int compare(sub_match const &sub) const
127 return this->str().compare(sub.str());
132 int compare(value_type const *ptr) const
134 return this->str().compare(ptr);
137 /// \brief true if this sub-match participated in the full match.
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)
154 template<typename BidiIter>
155 inline BidiIter range_begin(sub_match<BidiIter> const &sub)
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)
173 template<typename BidiIter>
174 inline BidiIter range_end(sub_match<BidiIter> const &sub)
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 <<
187 std::basic_ostream<Char, Traits> &sout
188 , sub_match<BidiIter> const &sub
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
199 std::ostream_iterator<char_type, Char, Traits> iout(sout);
200 std::copy(sub.first, sub.second, iout);
206 // BUGBUG make these more efficient
208 template<typename BidiIter>
209 bool operator == (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
211 return lhs.compare(rhs) == 0;
214 template<typename BidiIter>
215 bool operator != (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
217 return lhs.compare(rhs) != 0;
220 template<typename BidiIter>
221 bool operator < (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
223 return lhs.compare(rhs) < 0;
226 template<typename BidiIter>
227 bool operator <= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
229 return lhs.compare(rhs) <= 0;
232 template<typename BidiIter>
233 bool operator >= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
235 return lhs.compare(rhs) >= 0;
238 template<typename BidiIter>
239 bool operator > (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
241 return lhs.compare(rhs) > 0;
244 template<typename BidiIter>
245 bool operator == (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
247 return lhs == rhs.str();
250 template<typename BidiIter>
251 bool operator != (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
253 return lhs != rhs.str();
256 template<typename BidiIter>
257 bool operator < (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
259 return lhs < rhs.str();
262 template<typename BidiIter>
263 bool operator > (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
265 return lhs> rhs.str();
268 template<typename BidiIter>
269 bool operator >= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
271 return lhs >= rhs.str();
274 template<typename BidiIter>
275 bool operator <= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
277 return lhs <= rhs.str();
280 template<typename BidiIter>
281 bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
283 return lhs.str() == rhs;
286 template<typename BidiIter>
287 bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
289 return lhs.str() != rhs;
292 template<typename BidiIter>
293 bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
295 return lhs.str() < rhs;
298 template<typename BidiIter>
299 bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
301 return lhs.str() > rhs;
304 template<typename BidiIter>
305 bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
307 return lhs.str() >= rhs;
310 template<typename BidiIter>
311 bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
313 return lhs.str() <= rhs;
316 template<typename BidiIter>
317 bool operator == (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
319 return lhs == rhs.str();
322 template<typename BidiIter>
323 bool operator != (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
325 return lhs != rhs.str();
328 template<typename BidiIter>
329 bool operator < (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
331 return lhs < rhs.str();
334 template<typename BidiIter>
335 bool operator > (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
337 return lhs> rhs.str();
340 template<typename BidiIter>
341 bool operator >= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
343 return lhs >= rhs.str();
346 template<typename BidiIter>
347 bool operator <= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
349 return lhs <= rhs.str();
352 template<typename BidiIter>
353 bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
355 return lhs.str() == rhs;
358 template<typename BidiIter>
359 bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
361 return lhs.str() != rhs;
364 template<typename BidiIter>
365 bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
367 return lhs.str() < rhs;
370 template<typename BidiIter>
371 bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
373 return lhs.str() > rhs;
376 template<typename BidiIter>
377 bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
379 return lhs.str() >= rhs;
382 template<typename BidiIter>
383 bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
385 return lhs.str() <= rhs;
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)
393 return lhs.str() + rhs.str();
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)
400 return lhs.str() + rhs;
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)
407 return lhs + rhs.str();
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)
414 return lhs.str() + rhs;
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)
421 return lhs + rhs.str();
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)
428 return lhs.str() + rhs;
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)
435 return lhs + rhs.str();
438 }} // namespace boost::xpressive
440 // Hook the Boost.Range customization points to make sub_match a valid range.
445 template<typename BidiIter>
446 struct range_mutable_iterator<xpressive::sub_match<BidiIter> >
448 typedef BidiIter type;
453 template<typename BidiIter>
454 struct range_const_iterator<xpressive::sub_match<BidiIter> >
456 typedef BidiIter type;