regex_iterator.hpp 5.9 KB

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