regex_iterator.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. regex_iterator_implementation(const regex_iterator_implementation& other)
  46. :what(other.what), base(other.base), end(other.end), re(other.re), flags(other.flags){}
  47. bool init(BidirectionalIterator first)
  48. {
  49. base = first;
  50. return regex_search(first, end, what, re, flags);
  51. }
  52. bool compare(const regex_iterator_implementation& that)
  53. {
  54. if(this == &that) return true;
  55. 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);
  56. }
  57. const match_results<BidirectionalIterator>& get()
  58. { return what; }
  59. bool next()
  60. {
  61. //if(what.prefix().first != what[0].second)
  62. // flags |= match_prev_avail;
  63. BidirectionalIterator next_start = what[0].second;
  64. match_flag_type f(flags);
  65. if(!what.length() || (f & regex_constants::match_posix))
  66. f |= regex_constants::match_not_initial_null;
  67. //if(base != next_start)
  68. // f |= regex_constants::match_not_bob;
  69. bool result = regex_search(next_start, end, what, re, f, base);
  70. if(result)
  71. what.set_base(base);
  72. return result;
  73. }
  74. private:
  75. regex_iterator_implementation& operator=(const regex_iterator_implementation&);
  76. };
  77. template <class BidirectionalIterator,
  78. class charT = BOOST_DEDUCED_TYPENAME BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::value_type,
  79. class traits = regex_traits<charT> >
  80. class regex_iterator
  81. {
  82. private:
  83. typedef regex_iterator_implementation<BidirectionalIterator, charT, traits> impl;
  84. typedef shared_ptr<impl> pimpl;
  85. public:
  86. typedef basic_regex<charT, traits> regex_type;
  87. typedef match_results<BidirectionalIterator> value_type;
  88. typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type
  89. difference_type;
  90. typedef const value_type* pointer;
  91. typedef const value_type& reference;
  92. typedef std::forward_iterator_tag iterator_category;
  93. regex_iterator(){}
  94. regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
  95. const regex_type& re,
  96. match_flag_type m = match_default)
  97. : pdata(new impl(&re, b, m))
  98. {
  99. if(!pdata->init(a))
  100. {
  101. pdata.reset();
  102. }
  103. }
  104. regex_iterator(const regex_iterator& that)
  105. : pdata(that.pdata) {}
  106. regex_iterator& operator=(const regex_iterator& that)
  107. {
  108. pdata = that.pdata;
  109. return *this;
  110. }
  111. bool operator==(const regex_iterator& that)const
  112. {
  113. if((pdata.get() == 0) || (that.pdata.get() == 0))
  114. return pdata.get() == that.pdata.get();
  115. return pdata->compare(*(that.pdata.get()));
  116. }
  117. bool operator!=(const regex_iterator& that)const
  118. { return !(*this == that); }
  119. const value_type& operator*()const
  120. { return pdata->get(); }
  121. const value_type* operator->()const
  122. { return &(pdata->get()); }
  123. regex_iterator& operator++()
  124. {
  125. cow();
  126. if(0 == pdata->next())
  127. {
  128. pdata.reset();
  129. }
  130. return *this;
  131. }
  132. regex_iterator operator++(int)
  133. {
  134. regex_iterator result(*this);
  135. ++(*this);
  136. return result;
  137. }
  138. private:
  139. pimpl pdata;
  140. void cow()
  141. {
  142. // copy-on-write
  143. if(pdata.get() && !pdata.unique())
  144. {
  145. pdata.reset(new impl(*(pdata.get())));
  146. }
  147. }
  148. };
  149. typedef regex_iterator<const char*> cregex_iterator;
  150. typedef regex_iterator<std::string::const_iterator> sregex_iterator;
  151. #ifndef BOOST_NO_WREGEX
  152. typedef regex_iterator<const wchar_t*> wcregex_iterator;
  153. typedef regex_iterator<std::wstring::const_iterator> wsregex_iterator;
  154. #endif
  155. // make_regex_iterator:
  156. template <class charT, class traits>
  157. 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)
  158. {
  159. return regex_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, m);
  160. }
  161. template <class charT, class traits, class ST, class SA>
  162. 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)
  163. {
  164. return regex_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, m);
  165. }
  166. #ifdef BOOST_MSVC
  167. #pragma warning(push)
  168. #pragma warning(disable: 4103)
  169. #endif
  170. #ifdef BOOST_HAS_ABI_HEADERS
  171. # include BOOST_ABI_SUFFIX
  172. #endif
  173. #ifdef BOOST_MSVC
  174. #pragma warning(pop)
  175. #endif
  176. } // namespace boost
  177. #endif // BOOST_REGEX_V4_REGEX_ITERATOR_HPP