1 | // Boost string_algo library find_iterator.hpp header file ---------------------------// |
---|
2 | |
---|
3 | // Copyright Pavol Droba 2002-2004. |
---|
4 | // |
---|
5 | // Distributed under the Boost Software License, Version 1.0. |
---|
6 | // (See accompanying file LICENSE_1_0.txt or copy at |
---|
7 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
8 | |
---|
9 | // See http://www.boost.org/ for updates, documentation, and revision history. |
---|
10 | |
---|
11 | #ifndef BOOST_STRING_FIND_ITERATOR_HPP |
---|
12 | #define BOOST_STRING_FIND_ITERATOR_HPP |
---|
13 | |
---|
14 | #include <boost/algorithm/string/config.hpp> |
---|
15 | #include <boost/iterator/iterator_facade.hpp> |
---|
16 | #include <boost/iterator/iterator_categories.hpp> |
---|
17 | |
---|
18 | #include <boost/range/iterator_range.hpp> |
---|
19 | #include <boost/range/begin.hpp> |
---|
20 | #include <boost/range/end.hpp> |
---|
21 | #include <boost/range/iterator.hpp> |
---|
22 | #include <boost/range/as_literal.hpp> |
---|
23 | |
---|
24 | #include <boost/algorithm/string/detail/find_iterator.hpp> |
---|
25 | |
---|
26 | /*! \file |
---|
27 | Defines find iterator classes. Find iterator repeatedly applies a Finder |
---|
28 | to the specified input string to search for matches. Dereferencing |
---|
29 | the iterator yields the current match or a range between the last and the current |
---|
30 | match depending on the iterator used. |
---|
31 | */ |
---|
32 | |
---|
33 | namespace boost { |
---|
34 | namespace algorithm { |
---|
35 | |
---|
36 | // find_iterator -----------------------------------------------// |
---|
37 | |
---|
38 | //! find_iterator |
---|
39 | /*! |
---|
40 | Find iterator encapsulates a Finder and allows |
---|
41 | for incremental searching in a string. |
---|
42 | Each increment moves the iterator to the next match. |
---|
43 | |
---|
44 | Find iterator is a readable forward traversal iterator. |
---|
45 | |
---|
46 | Dereferencing the iterator yields an iterator_range delimiting |
---|
47 | the current match. |
---|
48 | */ |
---|
49 | template<typename IteratorT> |
---|
50 | class find_iterator : |
---|
51 | public iterator_facade< |
---|
52 | find_iterator<IteratorT>, |
---|
53 | const iterator_range<IteratorT>, |
---|
54 | forward_traversal_tag >, |
---|
55 | private detail::find_iterator_base<IteratorT> |
---|
56 | { |
---|
57 | private: |
---|
58 | // facade support |
---|
59 | friend class ::boost::iterator_core_access; |
---|
60 | |
---|
61 | private: |
---|
62 | // typedefs |
---|
63 | |
---|
64 | typedef detail::find_iterator_base<IteratorT> base_type; |
---|
65 | typedef BOOST_STRING_TYPENAME |
---|
66 | base_type::input_iterator_type input_iterator_type; |
---|
67 | typedef BOOST_STRING_TYPENAME |
---|
68 | base_type::match_type match_type; |
---|
69 | |
---|
70 | public: |
---|
71 | //! Default constructor |
---|
72 | /*! |
---|
73 | Construct null iterator. All null iterators are equal. |
---|
74 | |
---|
75 | \post eof()==true |
---|
76 | */ |
---|
77 | find_iterator() {} |
---|
78 | |
---|
79 | //! Copy constructor |
---|
80 | /*! |
---|
81 | Construct a copy of the find_iterator |
---|
82 | */ |
---|
83 | find_iterator( const find_iterator& Other ) : |
---|
84 | base_type(Other), |
---|
85 | m_Match(Other.m_Match), |
---|
86 | m_End(Other.m_End) {} |
---|
87 | |
---|
88 | //! Constructor |
---|
89 | /*! |
---|
90 | Construct new find_iterator for a given finder |
---|
91 | and a range. |
---|
92 | */ |
---|
93 | template<typename FinderT> |
---|
94 | find_iterator( |
---|
95 | IteratorT Begin, |
---|
96 | IteratorT End, |
---|
97 | FinderT Finder ) : |
---|
98 | detail::find_iterator_base<IteratorT>(Finder,0), |
---|
99 | m_Match(Begin,Begin), |
---|
100 | m_End(End) |
---|
101 | { |
---|
102 | increment(); |
---|
103 | } |
---|
104 | |
---|
105 | //! Constructor |
---|
106 | /*! |
---|
107 | Construct new find_iterator for a given finder |
---|
108 | and a range. |
---|
109 | */ |
---|
110 | template<typename FinderT, typename RangeT> |
---|
111 | find_iterator( |
---|
112 | RangeT& Col, |
---|
113 | FinderT Finder ) : |
---|
114 | detail::find_iterator_base<IteratorT>(Finder,0) |
---|
115 | { |
---|
116 | iterator_range<BOOST_STRING_TYPENAME range_iterator<RangeT>::type> lit_col(::boost::as_literal(Col)); |
---|
117 | m_Match=::boost::make_iterator_range(::boost::begin(lit_col), ::boost::begin(lit_col)); |
---|
118 | m_End=::boost::end(lit_col); |
---|
119 | |
---|
120 | increment(); |
---|
121 | } |
---|
122 | |
---|
123 | private: |
---|
124 | // iterator operations |
---|
125 | |
---|
126 | // dereference |
---|
127 | const match_type& dereference() const |
---|
128 | { |
---|
129 | return m_Match; |
---|
130 | } |
---|
131 | |
---|
132 | // increment |
---|
133 | void increment() |
---|
134 | { |
---|
135 | m_Match=this->do_find(m_Match.end(),m_End); |
---|
136 | } |
---|
137 | |
---|
138 | // comparison |
---|
139 | bool equal( const find_iterator& Other ) const |
---|
140 | { |
---|
141 | bool bEof=eof(); |
---|
142 | bool bOtherEof=Other.eof(); |
---|
143 | |
---|
144 | return bEof || bOtherEof ? bEof==bOtherEof : |
---|
145 | ( |
---|
146 | m_Match==Other.m_Match && |
---|
147 | m_End==Other.m_End |
---|
148 | ); |
---|
149 | } |
---|
150 | |
---|
151 | public: |
---|
152 | // operations |
---|
153 | |
---|
154 | //! Eof check |
---|
155 | /*! |
---|
156 | Check the eof condition. Eof condition means that |
---|
157 | there is nothing more to be searched i.e. find_iterator |
---|
158 | is after the last match. |
---|
159 | */ |
---|
160 | bool eof() const |
---|
161 | { |
---|
162 | return |
---|
163 | this->is_null() || |
---|
164 | ( |
---|
165 | m_Match.begin() == m_End && |
---|
166 | m_Match.end() == m_End |
---|
167 | ); |
---|
168 | } |
---|
169 | |
---|
170 | private: |
---|
171 | // Attributes |
---|
172 | match_type m_Match; |
---|
173 | input_iterator_type m_End; |
---|
174 | }; |
---|
175 | |
---|
176 | //! find iterator construction helper |
---|
177 | /*! |
---|
178 | * Construct a find iterator to iterate through the specified string |
---|
179 | */ |
---|
180 | template<typename RangeT, typename FinderT> |
---|
181 | inline find_iterator< |
---|
182 | BOOST_STRING_TYPENAME range_iterator<RangeT>::type> |
---|
183 | make_find_iterator( |
---|
184 | RangeT& Collection, |
---|
185 | FinderT Finder) |
---|
186 | { |
---|
187 | return find_iterator<BOOST_STRING_TYPENAME range_iterator<RangeT>::type>( |
---|
188 | Collection, Finder); |
---|
189 | } |
---|
190 | |
---|
191 | // split iterator -----------------------------------------------// |
---|
192 | |
---|
193 | //! split_iterator |
---|
194 | /*! |
---|
195 | Split iterator encapsulates a Finder and allows |
---|
196 | for incremental searching in a string. |
---|
197 | Unlike the find iterator, split iterator iterates |
---|
198 | through gaps between matches. |
---|
199 | |
---|
200 | Find iterator is a readable forward traversal iterator. |
---|
201 | |
---|
202 | Dereferencing the iterator yields an iterator_range delimiting |
---|
203 | the current match. |
---|
204 | */ |
---|
205 | template<typename IteratorT> |
---|
206 | class split_iterator : |
---|
207 | public iterator_facade< |
---|
208 | split_iterator<IteratorT>, |
---|
209 | const iterator_range<IteratorT>, |
---|
210 | forward_traversal_tag >, |
---|
211 | private detail::find_iterator_base<IteratorT> |
---|
212 | { |
---|
213 | private: |
---|
214 | // facade support |
---|
215 | friend class ::boost::iterator_core_access; |
---|
216 | |
---|
217 | private: |
---|
218 | // typedefs |
---|
219 | |
---|
220 | typedef detail::find_iterator_base<IteratorT> base_type; |
---|
221 | typedef BOOST_STRING_TYPENAME |
---|
222 | base_type::input_iterator_type input_iterator_type; |
---|
223 | typedef BOOST_STRING_TYPENAME |
---|
224 | base_type::match_type match_type; |
---|
225 | |
---|
226 | public: |
---|
227 | //! Default constructor |
---|
228 | /*! |
---|
229 | Construct null iterator. All null iterators are equal. |
---|
230 | |
---|
231 | \post eof()==true |
---|
232 | */ |
---|
233 | split_iterator() {} |
---|
234 | //! Copy constructor |
---|
235 | /*! |
---|
236 | Construct a copy of the split_iterator |
---|
237 | */ |
---|
238 | split_iterator( const split_iterator& Other ) : |
---|
239 | base_type(Other), |
---|
240 | m_Match(Other.m_Match), |
---|
241 | m_Next(Other.m_Next), |
---|
242 | m_End(Other.m_End), |
---|
243 | m_bEof(Other.m_bEof) |
---|
244 | {} |
---|
245 | |
---|
246 | //! Constructor |
---|
247 | /*! |
---|
248 | Construct new split_iterator for a given finder |
---|
249 | and a range. |
---|
250 | */ |
---|
251 | template<typename FinderT> |
---|
252 | split_iterator( |
---|
253 | IteratorT Begin, |
---|
254 | IteratorT End, |
---|
255 | FinderT Finder ) : |
---|
256 | detail::find_iterator_base<IteratorT>(Finder,0), |
---|
257 | m_Match(Begin,Begin), |
---|
258 | m_Next(Begin), |
---|
259 | m_End(End), |
---|
260 | m_bEof(false) |
---|
261 | { |
---|
262 | increment(); |
---|
263 | } |
---|
264 | //! Constructor |
---|
265 | /*! |
---|
266 | Construct new split_iterator for a given finder |
---|
267 | and a collection. |
---|
268 | */ |
---|
269 | template<typename FinderT, typename RangeT> |
---|
270 | split_iterator( |
---|
271 | RangeT& Col, |
---|
272 | FinderT Finder ) : |
---|
273 | detail::find_iterator_base<IteratorT>(Finder,0), |
---|
274 | m_bEof(false) |
---|
275 | { |
---|
276 | iterator_range<BOOST_STRING_TYPENAME range_iterator<RangeT>::type> lit_col(::boost::as_literal(Col)); |
---|
277 | m_Match=make_iterator_range(::boost::begin(lit_col), ::boost::begin(lit_col)); |
---|
278 | m_Next=::boost::begin(lit_col); |
---|
279 | m_End=::boost::end(lit_col); |
---|
280 | |
---|
281 | increment(); |
---|
282 | } |
---|
283 | |
---|
284 | |
---|
285 | private: |
---|
286 | // iterator operations |
---|
287 | |
---|
288 | // dereference |
---|
289 | const match_type& dereference() const |
---|
290 | { |
---|
291 | return m_Match; |
---|
292 | } |
---|
293 | |
---|
294 | // increment |
---|
295 | void increment() |
---|
296 | { |
---|
297 | match_type FindMatch=this->do_find( m_Next, m_End ); |
---|
298 | |
---|
299 | if(FindMatch.begin()==m_End && FindMatch.end()==m_End) |
---|
300 | { |
---|
301 | if(m_Match.end()==m_End) |
---|
302 | { |
---|
303 | // Mark iterator as eof |
---|
304 | m_bEof=true; |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|
308 | m_Match=match_type( m_Next, FindMatch.begin() ); |
---|
309 | m_Next=FindMatch.end(); |
---|
310 | } |
---|
311 | |
---|
312 | // comparison |
---|
313 | bool equal( const split_iterator& Other ) const |
---|
314 | { |
---|
315 | bool bEof=eof(); |
---|
316 | bool bOtherEof=Other.eof(); |
---|
317 | |
---|
318 | return bEof || bOtherEof ? bEof==bOtherEof : |
---|
319 | ( |
---|
320 | m_Match==Other.m_Match && |
---|
321 | m_Next==Other.m_Next && |
---|
322 | m_End==Other.m_End |
---|
323 | ); |
---|
324 | } |
---|
325 | |
---|
326 | public: |
---|
327 | // operations |
---|
328 | |
---|
329 | //! Eof check |
---|
330 | /*! |
---|
331 | Check the eof condition. Eof condition means that |
---|
332 | there is nothing more to be searched i.e. find_iterator |
---|
333 | is after the last match. |
---|
334 | */ |
---|
335 | bool eof() const |
---|
336 | { |
---|
337 | return this->is_null() || m_bEof; |
---|
338 | } |
---|
339 | |
---|
340 | private: |
---|
341 | // Attributes |
---|
342 | match_type m_Match; |
---|
343 | input_iterator_type m_Next; |
---|
344 | input_iterator_type m_End; |
---|
345 | bool m_bEof; |
---|
346 | }; |
---|
347 | |
---|
348 | //! split iterator construction helper |
---|
349 | /*! |
---|
350 | * Construct a split iterator to iterate through the specified collection |
---|
351 | */ |
---|
352 | template<typename RangeT, typename FinderT> |
---|
353 | inline split_iterator< |
---|
354 | BOOST_STRING_TYPENAME range_iterator<RangeT>::type> |
---|
355 | make_split_iterator( |
---|
356 | RangeT& Collection, |
---|
357 | FinderT Finder) |
---|
358 | { |
---|
359 | return split_iterator<BOOST_STRING_TYPENAME range_iterator<RangeT>::type>( |
---|
360 | Collection, Finder); |
---|
361 | } |
---|
362 | |
---|
363 | |
---|
364 | } // namespace algorithm |
---|
365 | |
---|
366 | // pull names to the boost namespace |
---|
367 | using algorithm::find_iterator; |
---|
368 | using algorithm::make_find_iterator; |
---|
369 | using algorithm::split_iterator; |
---|
370 | using algorithm::make_split_iterator; |
---|
371 | |
---|
372 | } // namespace boost |
---|
373 | |
---|
374 | |
---|
375 | #endif // BOOST_STRING_FIND_ITERATOR_HPP |
---|