Chris@16: // Boost.Range library Chris@16: // Chris@16: // Copyright Thorsten Ottosen, Neil Groves 2006. Use, modification and Chris@16: // distribution is subject to the Boost Software License, Version Chris@16: // 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // For more information, see http://www.boost.org/libs/range/ Chris@16: // Chris@16: Chris@16: #ifndef BOOST_RANGE_ADAPTOR_TOKENIZED_HPP Chris@16: #define BOOST_RANGE_ADAPTOR_TOKENIZED_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace range_detail Chris@16: { Chris@16: Chris@16: template< class R > Chris@16: struct tokenized_range : Chris@16: public boost::iterator_range< Chris@16: boost::regex_token_iterator< Chris@16: BOOST_DEDUCED_TYPENAME range_iterator::type Chris@16: > Chris@16: > Chris@16: { Chris@16: private: Chris@16: typedef Chris@16: boost::regex_token_iterator< Chris@16: BOOST_DEDUCED_TYPENAME range_iterator::type Chris@16: > Chris@16: regex_iter; Chris@16: Chris@16: typedef BOOST_DEDUCED_TYPENAME regex_iter::regex_type Chris@16: regex_type; Chris@16: Chris@16: typedef boost::iterator_range Chris@16: base; Chris@16: Chris@16: public: Chris@16: template< class Regex, class Submatch, class Flag > Chris@16: tokenized_range( R& r, const Regex& re, const Submatch& sub, Flag f ) Chris@16: : base( regex_iter( boost::begin(r), boost::end(r), Chris@16: regex_type(re), sub, f ), Chris@16: regex_iter() ) Chris@16: { } Chris@16: }; Chris@16: Chris@16: template< class T, class U, class V > Chris@16: struct regex_holder Chris@16: { Chris@101: T re; Chris@101: U sub; Chris@101: V f; Chris@16: Chris@16: regex_holder( const T& rex, const U& subm, V flag ) : Chris@16: re(rex), sub(subm), f(flag) Chris@16: { } Chris@16: private: Chris@16: // Not assignable Chris@16: void operator=(const regex_holder&); Chris@16: }; Chris@16: Chris@16: struct regex_forwarder Chris@16: { Chris@16: template< class Regex > Chris@16: regex_holder Chris@16: operator()( const Regex& re, Chris@16: int submatch = 0, Chris@16: regex_constants::match_flag_type f = Chris@16: regex_constants::match_default ) const Chris@16: { Chris@16: return regex_holder( re, submatch, f ); Chris@16: } Chris@16: Chris@16: template< class Regex, class Submatch > Chris@16: regex_holder Chris@16: operator()( const Regex& re, Chris@16: const Submatch& sub, Chris@16: regex_constants::match_flag_type f = Chris@16: regex_constants::match_default ) const Chris@16: { Chris@16: return regex_holder( re, sub, f ); Chris@16: } Chris@16: }; Chris@16: Chris@16: template< class BidirectionalRng, class R, class S, class F > Chris@16: inline tokenized_range Chris@16: operator|( BidirectionalRng& r, Chris@16: const regex_holder& f ) Chris@16: { Chris@16: return tokenized_range( r, f.re, f.sub, f.f ); Chris@16: } Chris@16: Chris@16: template< class BidirectionalRng, class R, class S, class F > Chris@16: inline tokenized_range Chris@16: operator|( const BidirectionalRng& r, Chris@16: const regex_holder& f ) Chris@16: { Chris@16: return tokenized_range( r, f.re, f.sub, f.f ); Chris@16: } Chris@16: Chris@16: } // 'range_detail' Chris@16: Chris@16: using range_detail::tokenized_range; Chris@16: Chris@16: namespace adaptors Chris@16: { Chris@16: namespace Chris@16: { Chris@16: const range_detail::regex_forwarder tokenized = Chris@16: range_detail::regex_forwarder(); Chris@16: } Chris@16: Chris@16: template Chris@16: inline tokenized_range Chris@16: tokenize(BidirectionalRange& rng, const Regex& reg, const Submatch& sub, Flag f) Chris@16: { Chris@16: return tokenized_range(rng, reg, sub, f); Chris@16: } Chris@16: Chris@16: template Chris@16: inline tokenized_range Chris@16: tokenize(const BidirectionalRange& rng, const Regex& reg, const Submatch& sub, Flag f) Chris@16: { Chris@16: return tokenized_range(rng, reg, sub, f); Chris@16: } Chris@16: } // 'adaptors' Chris@16: Chris@16: } Chris@16: Chris@16: #endif