1 // Boost string_algo library find_iterator.hpp header file ---------------------------//
3 // Copyright Pavol Droba 2002-2004.
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)
9 // See http://www.boost.org/ for updates, documentation, and revision history.
11 #ifndef BOOST_STRING_FIND_ITERATOR_HPP
12 #define BOOST_STRING_FIND_ITERATOR_HPP
14 #include <boost/algorithm/string/config.hpp>
15 #include <boost/iterator/iterator_facade.hpp>
16 #include <boost/iterator/iterator_categories.hpp>
18 #include <boost/range/iterator_range_core.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>
24 #include <boost/algorithm/string/detail/find_iterator.hpp>
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.
36 // find_iterator -----------------------------------------------//
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.
44 Find iterator is a readable forward traversal iterator.
46 Dereferencing the iterator yields an iterator_range delimiting
49 template<typename IteratorT>
51 public iterator_facade<
52 find_iterator<IteratorT>,
53 const iterator_range<IteratorT>,
54 forward_traversal_tag >,
55 private detail::find_iterator_base<IteratorT>
59 friend class ::boost::iterator_core_access;
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;
71 //! Default constructor
73 Construct null iterator. All null iterators are equal.
81 Construct a copy of the find_iterator
83 find_iterator( const find_iterator& Other ) :
85 m_Match(Other.m_Match),
90 Construct new find_iterator for a given finder
93 template<typename FinderT>
98 detail::find_iterator_base<IteratorT>(Finder,0),
107 Construct new find_iterator for a given finder
110 template<typename FinderT, typename RangeT>
114 detail::find_iterator_base<IteratorT>(Finder,0)
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);
124 // iterator operations
127 const match_type& dereference() const
135 m_Match=this->do_find(m_Match.end(),m_End);
139 bool equal( const find_iterator& Other ) const
142 bool bOtherEof=Other.eof();
144 return bEof || bOtherEof ? bEof==bOtherEof :
146 m_Match==Other.m_Match &&
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.
165 m_Match.begin() == m_End &&
166 m_Match.end() == m_End
173 input_iterator_type m_End;
176 //! find iterator construction helper
178 * Construct a find iterator to iterate through the specified string
180 template<typename RangeT, typename FinderT>
181 inline find_iterator<
182 BOOST_STRING_TYPENAME range_iterator<RangeT>::type>
187 return find_iterator<BOOST_STRING_TYPENAME range_iterator<RangeT>::type>(
191 // split iterator -----------------------------------------------//
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.
200 Find iterator is a readable forward traversal iterator.
202 Dereferencing the iterator yields an iterator_range delimiting
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>
215 friend class ::boost::iterator_core_access;
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;
227 //! Default constructor
229 Construct null iterator. All null iterators are equal.
241 Construct a copy of the split_iterator
243 split_iterator( const split_iterator& Other ) :
245 m_Match(Other.m_Match),
246 m_Next(Other.m_Next),
253 Construct new split_iterator for a given finder
256 template<typename FinderT>
261 detail::find_iterator_base<IteratorT>(Finder,0),
262 m_Match(Begin,Begin),
267 // force the correct behavior for empty sequences and yield at least one token
275 Construct new split_iterator for a given finder
278 template<typename FinderT, typename RangeT>
282 detail::find_iterator_base<IteratorT>(Finder,0),
285 iterator_range<BOOST_STRING_TYPENAME range_iterator<RangeT>::type> lit_col(::boost::as_literal(Col));
286 m_Match=make_iterator_range(::boost::begin(lit_col), ::boost::begin(lit_col));
287 m_Next=::boost::begin(lit_col);
288 m_End=::boost::end(lit_col);
290 // force the correct behavior for empty sequences and yield at least one token
299 // iterator operations
302 const match_type& dereference() const
310 match_type FindMatch=this->do_find( m_Next, m_End );
312 if(FindMatch.begin()==m_End && FindMatch.end()==m_End)
314 if(m_Match.end()==m_End)
316 // Mark iterator as eof
321 m_Match=match_type( m_Next, FindMatch.begin() );
322 m_Next=FindMatch.end();
326 bool equal( const split_iterator& Other ) const
329 bool bOtherEof=Other.eof();
331 return bEof || bOtherEof ? bEof==bOtherEof :
333 m_Match==Other.m_Match &&
334 m_Next==Other.m_Next &&
344 Check the eof condition. Eof condition means that
345 there is nothing more to be searched i.e. find_iterator
346 is after the last match.
350 return this->is_null() || m_bEof;
356 input_iterator_type m_Next;
357 input_iterator_type m_End;
361 //! split iterator construction helper
363 * Construct a split iterator to iterate through the specified collection
365 template<typename RangeT, typename FinderT>
366 inline split_iterator<
367 BOOST_STRING_TYPENAME range_iterator<RangeT>::type>
372 return split_iterator<BOOST_STRING_TYPENAME range_iterator<RangeT>::type>(
377 } // namespace algorithm
379 // pull names to the boost namespace
380 using algorithm::find_iterator;
381 using algorithm::make_find_iterator;
382 using algorithm::split_iterator;
383 using algorithm::make_split_iterator;
388 #endif // BOOST_STRING_FIND_ITERATOR_HPP