u32regex_iterator.hpp 6.7 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 u32regex_iterator.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Provides u32regex_iterator implementation.
  16. */
  17. #ifndef BOOST_REGEX_V4_U32REGEX_ITERATOR_HPP
  18. #define BOOST_REGEX_V4_U32REGEX_ITERATOR_HPP
  19. namespace boost{
  20. #ifdef BOOST_HAS_ABI_HEADERS
  21. # include BOOST_ABI_PREFIX
  22. #endif
  23. template <class BidirectionalIterator>
  24. class u32regex_iterator_implementation
  25. {
  26. typedef u32regex 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. u32regex_iterator_implementation(const regex_type* p, BidirectionalIterator last, match_flag_type f)
  34. : base(), end(last), re(*p), flags(f){}
  35. bool init(BidirectionalIterator first)
  36. {
  37. base = first;
  38. return u32regex_search(first, end, what, re, flags, base);
  39. }
  40. bool compare(const u32regex_iterator_implementation& that)
  41. {
  42. if(this == &that) return true;
  43. 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);
  44. }
  45. const match_results<BidirectionalIterator>& get()
  46. { return what; }
  47. bool next()
  48. {
  49. //if(what.prefix().first != what[0].second)
  50. // flags |= match_prev_avail;
  51. BidirectionalIterator next_start = what[0].second;
  52. match_flag_type f(flags);
  53. if(!what.length())
  54. f |= regex_constants::match_not_initial_null;
  55. //if(base != next_start)
  56. // f |= regex_constants::match_not_bob;
  57. bool result = u32regex_search(next_start, end, what, re, f, base);
  58. if(result)
  59. what.set_base(base);
  60. return result;
  61. }
  62. private:
  63. u32regex_iterator_implementation& operator=(const u32regex_iterator_implementation&);
  64. };
  65. template <class BidirectionalIterator>
  66. class u32regex_iterator
  67. #ifndef BOOST_NO_STD_ITERATOR
  68. : public std::iterator<
  69. std::forward_iterator_tag,
  70. match_results<BidirectionalIterator>,
  71. typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type,
  72. const match_results<BidirectionalIterator>*,
  73. const match_results<BidirectionalIterator>& >
  74. #endif
  75. {
  76. private:
  77. typedef u32regex_iterator_implementation<BidirectionalIterator> impl;
  78. typedef shared_ptr<impl> pimpl;
  79. public:
  80. typedef u32regex regex_type;
  81. typedef match_results<BidirectionalIterator> value_type;
  82. typedef typename re_detail::regex_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. u32regex_iterator(){}
  88. u32regex_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. u32regex_iterator(const u32regex_iterator& that)
  99. : pdata(that.pdata) {}
  100. u32regex_iterator& operator=(const u32regex_iterator& that)
  101. {
  102. pdata = that.pdata;
  103. return *this;
  104. }
  105. bool operator==(const u32regex_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 u32regex_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. u32regex_iterator& operator++()
  118. {
  119. cow();
  120. if(0 == pdata->next())
  121. {
  122. pdata.reset();
  123. }
  124. return *this;
  125. }
  126. u32regex_iterator operator++(int)
  127. {
  128. u32regex_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.unique())
  138. {
  139. pdata.reset(new impl(*(pdata.get())));
  140. }
  141. }
  142. };
  143. typedef u32regex_iterator<const char*> utf8regex_iterator;
  144. typedef u32regex_iterator<const UChar*> utf16regex_iterator;
  145. typedef u32regex_iterator<const UChar32*> utf32regex_iterator;
  146. inline u32regex_iterator<const char*> make_u32regex_iterator(const char* p, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default)
  147. {
  148. return u32regex_iterator<const char*>(p, p+std::strlen(p), e, m);
  149. }
  150. #ifndef BOOST_NO_WREGEX
  151. 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)
  152. {
  153. return u32regex_iterator<const wchar_t*>(p, p+std::wcslen(p), e, m);
  154. }
  155. #endif
  156. #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2)
  157. inline u32regex_iterator<const UChar*> make_u32regex_iterator(const UChar* p, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default)
  158. {
  159. return u32regex_iterator<const UChar*>(p, p+u_strlen(p), e, m);
  160. }
  161. #endif
  162. template <class charT, class Traits, class Alloc>
  163. 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)
  164. {
  165. typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
  166. return u32regex_iterator<iter_type>(p.begin(), p.end(), e, m);
  167. }
  168. inline u32regex_iterator<const UChar*> make_u32regex_iterator(const UnicodeString& s, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default)
  169. {
  170. return u32regex_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, m);
  171. }
  172. #ifdef BOOST_HAS_ABI_HEADERS
  173. # include BOOST_ABI_SUFFIX
  174. #endif
  175. } // namespace boost
  176. #endif // BOOST_REGEX_V4_REGEX_ITERATOR_HPP