Chris@16
|
1 // Boost.Range library
|
Chris@16
|
2 //
|
Chris@16
|
3 // Copyright Thorsten Ottosen, Neil Groves 2006. Use, modification and
|
Chris@16
|
4 // distribution is subject to the Boost Software License, Version
|
Chris@16
|
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 //
|
Chris@16
|
8 // For more information, see http://www.boost.org/libs/range/
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_RANGE_ADAPTOR_TOKENIZED_HPP
|
Chris@16
|
12 #define BOOST_RANGE_ADAPTOR_TOKENIZED_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/regex.hpp>
|
Chris@16
|
15 #include <boost/range/iterator_range.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 namespace boost
|
Chris@16
|
18 {
|
Chris@16
|
19 namespace range_detail
|
Chris@16
|
20 {
|
Chris@16
|
21
|
Chris@16
|
22 template< class R >
|
Chris@16
|
23 struct tokenized_range :
|
Chris@16
|
24 public boost::iterator_range<
|
Chris@16
|
25 boost::regex_token_iterator<
|
Chris@16
|
26 BOOST_DEDUCED_TYPENAME range_iterator<R>::type
|
Chris@16
|
27 >
|
Chris@16
|
28 >
|
Chris@16
|
29 {
|
Chris@16
|
30 private:
|
Chris@16
|
31 typedef
|
Chris@16
|
32 boost::regex_token_iterator<
|
Chris@16
|
33 BOOST_DEDUCED_TYPENAME range_iterator<R>::type
|
Chris@16
|
34 >
|
Chris@16
|
35 regex_iter;
|
Chris@16
|
36
|
Chris@16
|
37 typedef BOOST_DEDUCED_TYPENAME regex_iter::regex_type
|
Chris@16
|
38 regex_type;
|
Chris@16
|
39
|
Chris@16
|
40 typedef boost::iterator_range<regex_iter>
|
Chris@16
|
41 base;
|
Chris@16
|
42
|
Chris@16
|
43 public:
|
Chris@16
|
44 template< class Regex, class Submatch, class Flag >
|
Chris@16
|
45 tokenized_range( R& r, const Regex& re, const Submatch& sub, Flag f )
|
Chris@16
|
46 : base( regex_iter( boost::begin(r), boost::end(r),
|
Chris@16
|
47 regex_type(re), sub, f ),
|
Chris@16
|
48 regex_iter() )
|
Chris@16
|
49 { }
|
Chris@16
|
50 };
|
Chris@16
|
51
|
Chris@16
|
52 template< class T, class U, class V >
|
Chris@16
|
53 struct regex_holder
|
Chris@16
|
54 {
|
Chris@101
|
55 T re;
|
Chris@101
|
56 U sub;
|
Chris@101
|
57 V f;
|
Chris@16
|
58
|
Chris@16
|
59 regex_holder( const T& rex, const U& subm, V flag ) :
|
Chris@16
|
60 re(rex), sub(subm), f(flag)
|
Chris@16
|
61 { }
|
Chris@16
|
62 private:
|
Chris@16
|
63 // Not assignable
|
Chris@16
|
64 void operator=(const regex_holder&);
|
Chris@16
|
65 };
|
Chris@16
|
66
|
Chris@16
|
67 struct regex_forwarder
|
Chris@16
|
68 {
|
Chris@16
|
69 template< class Regex >
|
Chris@16
|
70 regex_holder<Regex,int,regex_constants::match_flag_type>
|
Chris@16
|
71 operator()( const Regex& re,
|
Chris@16
|
72 int submatch = 0,
|
Chris@16
|
73 regex_constants::match_flag_type f =
|
Chris@16
|
74 regex_constants::match_default ) const
|
Chris@16
|
75 {
|
Chris@16
|
76 return regex_holder<Regex,int,
|
Chris@16
|
77 regex_constants::match_flag_type>( re, submatch, f );
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 template< class Regex, class Submatch >
|
Chris@16
|
81 regex_holder<Regex,Submatch,regex_constants::match_flag_type>
|
Chris@16
|
82 operator()( const Regex& re,
|
Chris@16
|
83 const Submatch& sub,
|
Chris@16
|
84 regex_constants::match_flag_type f =
|
Chris@16
|
85 regex_constants::match_default ) const
|
Chris@16
|
86 {
|
Chris@16
|
87 return regex_holder<Regex,Submatch,
|
Chris@16
|
88 regex_constants::match_flag_type>( re, sub, f );
|
Chris@16
|
89 }
|
Chris@16
|
90 };
|
Chris@16
|
91
|
Chris@16
|
92 template< class BidirectionalRng, class R, class S, class F >
|
Chris@16
|
93 inline tokenized_range<BidirectionalRng>
|
Chris@16
|
94 operator|( BidirectionalRng& r,
|
Chris@16
|
95 const regex_holder<R,S,F>& f )
|
Chris@16
|
96 {
|
Chris@16
|
97 return tokenized_range<BidirectionalRng>( r, f.re, f.sub, f.f );
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 template< class BidirectionalRng, class R, class S, class F >
|
Chris@16
|
101 inline tokenized_range<const BidirectionalRng>
|
Chris@16
|
102 operator|( const BidirectionalRng& r,
|
Chris@16
|
103 const regex_holder<R,S,F>& f )
|
Chris@16
|
104 {
|
Chris@16
|
105 return tokenized_range<const BidirectionalRng>( r, f.re, f.sub, f.f );
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 } // 'range_detail'
|
Chris@16
|
109
|
Chris@16
|
110 using range_detail::tokenized_range;
|
Chris@16
|
111
|
Chris@16
|
112 namespace adaptors
|
Chris@16
|
113 {
|
Chris@16
|
114 namespace
|
Chris@16
|
115 {
|
Chris@16
|
116 const range_detail::regex_forwarder tokenized =
|
Chris@16
|
117 range_detail::regex_forwarder();
|
Chris@16
|
118 }
|
Chris@16
|
119
|
Chris@16
|
120 template<class BidirectionalRange, class Regex, class Submatch, class Flag>
|
Chris@16
|
121 inline tokenized_range<BidirectionalRange>
|
Chris@16
|
122 tokenize(BidirectionalRange& rng, const Regex& reg, const Submatch& sub, Flag f)
|
Chris@16
|
123 {
|
Chris@16
|
124 return tokenized_range<BidirectionalRange>(rng, reg, sub, f);
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@16
|
127 template<class BidirectionalRange, class Regex, class Submatch, class Flag>
|
Chris@16
|
128 inline tokenized_range<const BidirectionalRange>
|
Chris@16
|
129 tokenize(const BidirectionalRange& rng, const Regex& reg, const Submatch& sub, Flag f)
|
Chris@16
|
130 {
|
Chris@16
|
131 return tokenized_range<const BidirectionalRange>(rng, reg, sub, f);
|
Chris@16
|
132 }
|
Chris@16
|
133 } // 'adaptors'
|
Chris@16
|
134
|
Chris@16
|
135 }
|
Chris@16
|
136
|
Chris@16
|
137 #endif
|