u32regex_iterator.hpp 6.2 KB

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