regex_iterator.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. *
  3. * Copyright (c) 2003
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE regex_iterator.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Provides regex_iterator implementation.
  16. */
  17. #ifndef BOOST_REGEX_V4_REGEX_ITERATOR_HPP
  18. #define BOOST_REGEX_V4_REGEX_ITERATOR_HPP
  19. #include <boost/shared_ptr.hpp>
  20. namespace boost{
  21. #ifdef BOOST_MSVC
  22. #pragma warning(push)
  23. #pragma warning(disable: 4103)
  24. #endif
  25. #ifdef BOOST_HAS_ABI_HEADERS
  26. # include BOOST_ABI_PREFIX
  27. #endif
  28. #ifdef BOOST_MSVC
  29. #pragma warning(pop)
  30. #endif
  31. template <class BidirectionalIterator,
  32. class charT,
  33. class traits>
  34. class regex_iterator_implementation
  35. {
  36. typedef basic_regex<charT, traits> regex_type;
  37. match_results<BidirectionalIterator> what; // current match
  38. BidirectionalIterator base; // start of sequence
  39. BidirectionalIterator end; // end of sequence
  40. const regex_type re; // the expression
  41. match_flag_type flags; // flags for matching
  42. public:
  43. regex_iterator_implementation(const regex_type* p, BidirectionalIterator last, match_flag_type f)
  44. : base(), end(last), re(*p), flags(f){}
  45. bool init(BidirectionalIterator first)
  46. {
  47. base = first;
  48. return regex_search(first, end, what, re, flags);
  49. }
  50. bool compare(const regex_iterator_implementation& that)
  51. {
  52. if(this == &that) return true;
  53. return (&re.get_data() == &that.re.get_data()) && (end == that.end) && (flags == that.flags) && (what[0].first == that.what[0].first) && (what[0].second == that.what[0].second);
  54. }
  55. const match_results<BidirectionalIterator>& get()
  56. { return what; }
  57. bool next()
  58. {
  59. //if(what.prefix().first != what[0].second)
  60. // flags |= match_prev_avail;
  61. BidirectionalIterator next_start = what[0].second;
  62. match_flag_type f(flags);
  63. if(!what.length())
  64. f |= regex_constants::match_not_initial_null;
  65. //if(base != next_start)
  66. // f |= regex_constants::match_not_bob;
  67. bool result = regex_search(next_start, end, what, re, f, base);
  68. if(result)
  69. what.set_base(base);
  70. return result;
  71. }
  72. private:
  73. regex_iterator_implementation& operator=(const regex_iterator_implementation&);
  74. };
  75. template <class BidirectionalIterator,
  76. class charT = BOOST_DEDUCED_TYPENAME re_detail::regex_iterator_traits<BidirectionalIterator>::value_type,
  77. class traits = regex_traits<charT> >
  78. class regex_iterator
  79. #ifndef BOOST_NO_STD_ITERATOR
  80. : public std::iterator<
  81. std::forward_iterator_tag,
  82. match_results<BidirectionalIterator>,
  83. typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type,
  84. const match_results<BidirectionalIterator>*,
  85. const match_results<BidirectionalIterator>& >
  86. #endif
  87. {
  88. private:
  89. typedef regex_iterator_implementation<BidirectionalIterator, charT, traits> impl;
  90. typedef shared_ptr<impl> pimpl;
  91. public:
  92. typedef basic_regex<charT, traits> regex_type;
  93. typedef match_results<BidirectionalIterator> value_type;
  94. typedef typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type
  95. difference_type;
  96. typedef const value_type* pointer;
  97. typedef const value_type& reference;
  98. typedef std::forward_iterator_tag iterator_category;
  99. regex_iterator(){}
  100. regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
  101. const regex_type& re,
  102. match_flag_type m = match_default)
  103. : pdata(new impl(&re, b, m))
  104. {
  105. if(!pdata->init(a))
  106. {
  107. pdata.reset();
  108. }
  109. }
  110. regex_iterator(const regex_iterator& that)
  111. : pdata(that.pdata) {}
  112. regex_iterator& operator=(const regex_iterator& that)
  113. {
  114. pdata = that.pdata;
  115. return *this;
  116. }
  117. bool operator==(const regex_iterator& that)const
  118. {
  119. if((pdata.get() == 0) || (that.pdata.get() == 0))
  120. return pdata.get() == that.pdata.get();
  121. return pdata->compare(*(that.pdata.get()));
  122. }
  123. bool operator!=(const regex_iterator& that)const
  124. { return !(*this == that); }
  125. const value_type& operator*()const
  126. { return pdata->get(); }
  127. const value_type* operator->()const
  128. { return &(pdata->get()); }
  129. regex_iterator& operator++()
  130. {
  131. cow();
  132. if(0 == pdata->next())
  133. {
  134. pdata.reset();
  135. }
  136. return *this;
  137. }
  138. regex_iterator operator++(int)
  139. {
  140. regex_iterator result(*this);
  141. ++(*this);
  142. return result;
  143. }
  144. private:
  145. pimpl pdata;
  146. void cow()
  147. {
  148. // copy-on-write
  149. if(pdata.get() && !pdata.unique())
  150. {
  151. pdata.reset(new impl(*(pdata.get())));
  152. }
  153. }
  154. };
  155. typedef regex_iterator<const char*> cregex_iterator;
  156. typedef regex_iterator<std::string::const_iterator> sregex_iterator;
  157. #ifndef BOOST_NO_WREGEX
  158. typedef regex_iterator<const wchar_t*> wcregex_iterator;
  159. typedef regex_iterator<std::wstring::const_iterator> wsregex_iterator;
  160. #endif
  161. // make_regex_iterator:
  162. template <class charT, class traits>
  163. inline regex_iterator<const charT*, charT, traits> make_regex_iterator(const charT* p, const basic_regex<charT, traits>& e, regex_constants::match_flag_type m = regex_constants::match_default)
  164. {
  165. return regex_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, m);
  166. }
  167. template <class charT, class traits, class ST, class SA>
  168. inline regex_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, regex_constants::match_flag_type m = regex_constants::match_default)
  169. {
  170. return regex_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, m);
  171. }
  172. #ifdef BOOST_MSVC
  173. #pragma warning(push)
  174. #pragma warning(disable: 4103)
  175. #endif
  176. #ifdef BOOST_HAS_ABI_HEADERS
  177. # include BOOST_ABI_SUFFIX
  178. #endif
  179. #ifdef BOOST_MSVC
  180. #pragma warning(pop)
  181. #endif
  182. } // namespace boost
  183. #endif // BOOST_REGEX_V4_REGEX_ITERATOR_HPP