find_iterator.hpp 12 KB

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