regex_iterator.hpp 6.2 KB

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