u32regex_iterator.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. {
  68. private:
  69. typedef u32regex_iterator_implementation<BidirectionalIterator> impl;
  70. typedef shared_ptr<impl> pimpl;
  71. public:
  72. typedef u32regex regex_type;
  73. typedef match_results<BidirectionalIterator> value_type;
  74. typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type
  75. difference_type;
  76. typedef const value_type* pointer;
  77. typedef const value_type& reference;
  78. typedef std::forward_iterator_tag iterator_category;
  79. u32regex_iterator(){}
  80. u32regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
  81. const regex_type& re,
  82. match_flag_type m = match_default)
  83. : pdata(new impl(&re, b, m))
  84. {
  85. if(!pdata->init(a))
  86. {
  87. pdata.reset();
  88. }
  89. }
  90. u32regex_iterator(const u32regex_iterator& that)
  91. : pdata(that.pdata) {}
  92. u32regex_iterator& operator=(const u32regex_iterator& that)
  93. {
  94. pdata = that.pdata;
  95. return *this;
  96. }
  97. bool operator==(const u32regex_iterator& that)const
  98. {
  99. if((pdata.get() == 0) || (that.pdata.get() == 0))
  100. return pdata.get() == that.pdata.get();
  101. return pdata->compare(*(that.pdata.get()));
  102. }
  103. bool operator!=(const u32regex_iterator& that)const
  104. { return !(*this == that); }
  105. const value_type& operator*()const
  106. { return pdata->get(); }
  107. const value_type* operator->()const
  108. { return &(pdata->get()); }
  109. u32regex_iterator& operator++()
  110. {
  111. cow();
  112. if(0 == pdata->next())
  113. {
  114. pdata.reset();
  115. }
  116. return *this;
  117. }
  118. u32regex_iterator operator++(int)
  119. {
  120. u32regex_iterator result(*this);
  121. ++(*this);
  122. return result;
  123. }
  124. private:
  125. pimpl pdata;
  126. void cow()
  127. {
  128. // copy-on-write
  129. if(pdata.get() && !pdata.unique())
  130. {
  131. pdata.reset(new impl(*(pdata.get())));
  132. }
  133. }
  134. };
  135. typedef u32regex_iterator<const char*> utf8regex_iterator;
  136. typedef u32regex_iterator<const UChar*> utf16regex_iterator;
  137. typedef u32regex_iterator<const UChar32*> utf32regex_iterator;
  138. inline u32regex_iterator<const char*> make_u32regex_iterator(const char* p, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default)
  139. {
  140. return u32regex_iterator<const char*>(p, p+std::strlen(p), e, m);
  141. }
  142. #ifndef BOOST_NO_WREGEX
  143. 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)
  144. {
  145. return u32regex_iterator<const wchar_t*>(p, p+std::wcslen(p), e, m);
  146. }
  147. #endif
  148. #if !defined(BOOST_REGEX_UCHAR_IS_WCHAR_T)
  149. inline u32regex_iterator<const UChar*> make_u32regex_iterator(const UChar* p, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default)
  150. {
  151. return u32regex_iterator<const UChar*>(p, p+u_strlen(p), e, m);
  152. }
  153. #endif
  154. template <class charT, class Traits, class Alloc>
  155. 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)
  156. {
  157. typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
  158. return u32regex_iterator<iter_type>(p.begin(), p.end(), e, m);
  159. }
  160. 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)
  161. {
  162. return u32regex_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, m);
  163. }
  164. #ifdef BOOST_HAS_ABI_HEADERS
  165. # include BOOST_ABI_SUFFIX
  166. #endif
  167. } // namespace boost
  168. #endif // BOOST_REGEX_V4_REGEX_ITERATOR_HPP