3 // Copyright Thorsten Ottosen, Neil Groves 2006. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // For more information, see http://www.boost.org/libs/range/
11 #ifndef BOOST_RANGE_ADAPTOR_TOKENIZED_HPP
12 #define BOOST_RANGE_ADAPTOR_TOKENIZED_HPP
14 #include <boost/regex.hpp>
15 #include <boost/range/iterator_range.hpp>
19 namespace range_detail
23 struct tokenized_range :
24 public boost::iterator_range<
25 boost::regex_token_iterator<
26 BOOST_DEDUCED_TYPENAME range_iterator<R>::type
32 boost::regex_token_iterator<
33 BOOST_DEDUCED_TYPENAME range_iterator<R>::type
37 typedef BOOST_DEDUCED_TYPENAME regex_iter::regex_type
40 typedef boost::iterator_range<regex_iter>
44 template< class Regex, class Submatch, class Flag >
45 tokenized_range( R& r, const Regex& re, const Submatch& sub, Flag f )
46 : base( regex_iter( boost::begin(r), boost::end(r),
47 regex_type(re), sub, f ),
52 template< class T, class U, class V >
59 regex_holder( const T& rex, const U& subm, V flag ) :
60 re(rex), sub(subm), f(flag)
64 void operator=(const regex_holder&);
67 struct regex_forwarder
69 template< class Regex >
70 regex_holder<Regex,int,regex_constants::match_flag_type>
71 operator()( const Regex& re,
73 regex_constants::match_flag_type f =
74 regex_constants::match_default ) const
76 return regex_holder<Regex,int,
77 regex_constants::match_flag_type>( re, submatch, f );
80 template< class Regex, class Submatch >
81 regex_holder<Regex,Submatch,regex_constants::match_flag_type>
82 operator()( const Regex& re,
84 regex_constants::match_flag_type f =
85 regex_constants::match_default ) const
87 return regex_holder<Regex,Submatch,
88 regex_constants::match_flag_type>( re, sub, f );
92 template< class BidirectionalRng, class R, class S, class F >
93 inline tokenized_range<BidirectionalRng>
94 operator|( BidirectionalRng& r,
95 const regex_holder<R,S,F>& f )
97 return tokenized_range<BidirectionalRng>( r, f.re, f.sub, f.f );
100 template< class BidirectionalRng, class R, class S, class F >
101 inline tokenized_range<const BidirectionalRng>
102 operator|( const BidirectionalRng& r,
103 const regex_holder<R,S,F>& f )
105 return tokenized_range<const BidirectionalRng>( r, f.re, f.sub, f.f );
110 using range_detail::tokenized_range;
116 const range_detail::regex_forwarder tokenized =
117 range_detail::regex_forwarder();
120 template<class BidirectionalRange, class Regex, class Submatch, class Flag>
121 inline tokenized_range<BidirectionalRange>
122 tokenize(BidirectionalRange& rng, const Regex& reg, const Submatch& sub, Flag f)
124 return tokenized_range<BidirectionalRange>(rng, reg, sub, f);
127 template<class BidirectionalRange, class Regex, class Submatch, class Flag>
128 inline tokenized_range<const BidirectionalRange>
129 tokenize(const BidirectionalRange& rng, const Regex& reg, const Submatch& sub, Flag f)
131 return tokenized_range<const BidirectionalRange>(rng, reg, sub, f);