regex_iterator.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_V5_REGEX_ITERATOR_HPP
  18. #define BOOST_REGEX_V5_REGEX_ITERATOR_HPP
  19. #include <boost/regex/v5/basic_regex.hpp>
  20. #include <boost/regex/v5/match_results.hpp>
  21. #ifndef BOOST_REGEX_AS_MODULE
  22. #include <memory>
  23. #endif
  24. namespace boost{
  25. template <class BidirectionalIterator,
  26. class charT,
  27. class traits>
  28. class regex_iterator_implementation
  29. {
  30. typedef basic_regex<charT, traits> regex_type;
  31. match_results<BidirectionalIterator> what; // current match
  32. BidirectionalIterator base; // start of sequence
  33. BidirectionalIterator end; // end of sequence
  34. const regex_type re; // the expression
  35. match_flag_type flags; // flags for matching
  36. public:
  37. regex_iterator_implementation(const regex_type* p, BidirectionalIterator last, match_flag_type f)
  38. : base(), end(last), re(*p), flags(f){}
  39. regex_iterator_implementation(const regex_iterator_implementation& other)
  40. :what(other.what), base(other.base), end(other.end), re(other.re), flags(other.flags){}
  41. bool init(BidirectionalIterator first)
  42. {
  43. base = first;
  44. return regex_search(first, end, what, re, flags);
  45. }
  46. bool compare(const regex_iterator_implementation& that)
  47. {
  48. if(this == &that) return true;
  49. 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);
  50. }
  51. const match_results<BidirectionalIterator>& get()
  52. { return what; }
  53. bool next()
  54. {
  55. //if(what.prefix().first != what[0].second)
  56. // flags |= match_prev_avail;
  57. BidirectionalIterator next_start = what[0].second;
  58. match_flag_type f(flags);
  59. if(!what.length() || (f & regex_constants::match_posix))
  60. f |= regex_constants::match_not_initial_null;
  61. //if(base != next_start)
  62. // f |= regex_constants::match_not_bob;
  63. bool result = regex_search(next_start, end, what, re, f, base);
  64. if(result)
  65. what.set_base(base);
  66. return result;
  67. }
  68. private:
  69. regex_iterator_implementation& operator=(const regex_iterator_implementation&);
  70. };
  71. BOOST_REGEX_MODULE_EXPORT template <class BidirectionalIterator,
  72. class charT = typename std::iterator_traits<BidirectionalIterator>::value_type,
  73. class traits = regex_traits<charT> >
  74. class regex_iterator
  75. {
  76. private:
  77. typedef regex_iterator_implementation<BidirectionalIterator, charT, traits> impl;
  78. typedef std::shared_ptr<impl> pimpl;
  79. public:
  80. typedef basic_regex<charT, traits> regex_type;
  81. typedef match_results<BidirectionalIterator> value_type;
  82. typedef typename std::iterator_traits<BidirectionalIterator>::difference_type
  83. difference_type;
  84. typedef const value_type* pointer;
  85. typedef const value_type& reference;
  86. typedef std::forward_iterator_tag iterator_category;
  87. regex_iterator(){}
  88. regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
  89. const regex_type& re,
  90. match_flag_type m = match_default)
  91. : pdata(new impl(&re, b, m))
  92. {
  93. if(!pdata->init(a))
  94. {
  95. pdata.reset();
  96. }
  97. }
  98. regex_iterator(const regex_iterator& that)
  99. : pdata(that.pdata) {}
  100. regex_iterator& operator=(const regex_iterator& that)
  101. {
  102. pdata = that.pdata;
  103. return *this;
  104. }
  105. bool operator==(const regex_iterator& that)const
  106. {
  107. if((pdata.get() == 0) || (that.pdata.get() == 0))
  108. return pdata.get() == that.pdata.get();
  109. return pdata->compare(*(that.pdata.get()));
  110. }
  111. bool operator!=(const regex_iterator& that)const
  112. { return !(*this == that); }
  113. const value_type& operator*()const
  114. { return pdata->get(); }
  115. const value_type* operator->()const
  116. { return &(pdata->get()); }
  117. regex_iterator& operator++()
  118. {
  119. cow();
  120. if(0 == pdata->next())
  121. {
  122. pdata.reset();
  123. }
  124. return *this;
  125. }
  126. regex_iterator operator++(int)
  127. {
  128. regex_iterator result(*this);
  129. ++(*this);
  130. return result;
  131. }
  132. private:
  133. pimpl pdata;
  134. void cow()
  135. {
  136. // copy-on-write
  137. if(pdata.get() && (pdata.use_count() > 1))
  138. {
  139. pdata.reset(new impl(*(pdata.get())));
  140. }
  141. }
  142. };
  143. BOOST_REGEX_MODULE_EXPORT typedef regex_iterator<const char*> cregex_iterator;
  144. BOOST_REGEX_MODULE_EXPORT typedef regex_iterator<std::string::const_iterator> sregex_iterator;
  145. #ifndef BOOST_NO_WREGEX
  146. BOOST_REGEX_MODULE_EXPORT typedef regex_iterator<const wchar_t*> wcregex_iterator;
  147. BOOST_REGEX_MODULE_EXPORT typedef regex_iterator<std::wstring::const_iterator> wsregex_iterator;
  148. #endif
  149. // make_regex_iterator:
  150. BOOST_REGEX_MODULE_EXPORT template <class charT, class traits>
  151. 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)
  152. {
  153. return regex_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, m);
  154. }
  155. BOOST_REGEX_MODULE_EXPORT template <class charT, class traits, class ST, class SA>
  156. 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)
  157. {
  158. return regex_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, m);
  159. }
  160. } // namespace boost
  161. #endif // BOOST_REGEX_V5_REGEX_ITERATOR_HPP