regex_impl.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // regex_impl.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_XPRESSIVE_DETAIL_CORE_REGEX_IMPL_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_CORE_REGEX_IMPL_HPP_EAN_10_04_2005
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <vector>
  14. #include <boost/intrusive_ptr.hpp>
  15. #include <boost/xpressive/regex_traits.hpp>
  16. #include <boost/xpressive/detail/detail_fwd.hpp>
  17. #include <boost/xpressive/detail/dynamic/matchable.hpp>
  18. #include <boost/xpressive/detail/utility/tracking_ptr.hpp>
  19. #include <boost/xpressive/detail/utility/counted_base.hpp>
  20. namespace boost { namespace xpressive { namespace detail
  21. {
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // finder
  24. template<typename BidiIter>
  25. struct finder
  26. : counted_base<finder<BidiIter> >
  27. {
  28. virtual ~finder() {}
  29. virtual bool ok_for_partial_matches() const { return true; }
  30. virtual bool operator ()(match_state<BidiIter> &state) const = 0;
  31. };
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // traits
  34. template<typename Char>
  35. struct traits
  36. : counted_base<traits<Char> >
  37. {
  38. virtual ~traits() {}
  39. virtual Char tolower(Char ch) const = 0;
  40. virtual Char toupper(Char ch) const = 0;
  41. virtual bool in_range(Char from, Char to, Char ch) const = 0;
  42. virtual int value(Char ch, int radix) const = 0;
  43. };
  44. ///////////////////////////////////////////////////////////////////////////////
  45. // named_mark
  46. template<typename Char>
  47. struct named_mark
  48. {
  49. typedef typename detail::string_type<Char>::type string_type;
  50. named_mark(string_type name, std::size_t mark_nbr)
  51. : name_(name)
  52. , mark_nbr_(mark_nbr)
  53. {}
  54. string_type name_;
  55. std::size_t mark_nbr_;
  56. };
  57. ///////////////////////////////////////////////////////////////////////////////
  58. // traits_holder
  59. template<typename Traits>
  60. struct traits_holder
  61. : traits<typename Traits::char_type>
  62. {
  63. typedef typename Traits::char_type char_type;
  64. explicit traits_holder(Traits const &tr)
  65. : traits_(tr)
  66. {
  67. }
  68. Traits const &traits() const
  69. {
  70. return this->traits_;
  71. }
  72. char_type tolower(char_type ch) const
  73. {
  74. return this->tolower_(ch, typename Traits::version_tag());
  75. }
  76. char_type toupper(char_type ch) const
  77. {
  78. return this->toupper_(ch, typename Traits::version_tag());
  79. }
  80. int value(char_type ch, int radix) const
  81. {
  82. return this->traits_.value(ch, radix);
  83. }
  84. bool in_range(char_type from, char_type to, char_type ch) const
  85. {
  86. return this->traits_.in_range(from, to, ch);
  87. }
  88. private:
  89. char_type tolower_(char_type ch, regex_traits_version_1_tag) const
  90. {
  91. return ch;
  92. }
  93. char_type toupper_(char_type ch, regex_traits_version_1_tag) const
  94. {
  95. return ch;
  96. }
  97. char_type tolower_(char_type ch, regex_traits_version_2_tag) const
  98. {
  99. return this->traits_.tolower(ch);
  100. }
  101. char_type toupper_(char_type ch, regex_traits_version_2_tag) const
  102. {
  103. return this->traits_.toupper(ch);
  104. }
  105. Traits traits_;
  106. };
  107. ///////////////////////////////////////////////////////////////////////////////
  108. // regex_impl
  109. //
  110. template<typename BidiIter>
  111. struct regex_impl
  112. : enable_reference_tracking<regex_impl<BidiIter> >
  113. {
  114. typedef typename iterator_value<BidiIter>::type char_type;
  115. regex_impl()
  116. : enable_reference_tracking<regex_impl<BidiIter> >()
  117. , xpr_()
  118. , traits_()
  119. , finder_()
  120. , named_marks_()
  121. , mark_count_(0)
  122. , hidden_mark_count_(0)
  123. {
  124. #ifdef BOOST_XPRESSIVE_DEBUG_CYCLE_TEST
  125. ++instances;
  126. #endif
  127. }
  128. regex_impl(regex_impl<BidiIter> const &that)
  129. : enable_reference_tracking<regex_impl<BidiIter> >(that)
  130. , xpr_(that.xpr_)
  131. , traits_(that.traits_)
  132. , finder_(that.finder_)
  133. , named_marks_(that.named_marks_)
  134. , mark_count_(that.mark_count_)
  135. , hidden_mark_count_(that.hidden_mark_count_)
  136. {
  137. #ifdef BOOST_XPRESSIVE_DEBUG_CYCLE_TEST
  138. ++instances;
  139. #endif
  140. }
  141. ~regex_impl()
  142. {
  143. #ifdef BOOST_XPRESSIVE_DEBUG_CYCLE_TEST
  144. --instances;
  145. #endif
  146. }
  147. void swap(regex_impl<BidiIter> &that)
  148. {
  149. enable_reference_tracking<regex_impl<BidiIter> >::swap(that);
  150. this->xpr_.swap(that.xpr_);
  151. this->traits_.swap(that.traits_);
  152. this->finder_.swap(that.finder_);
  153. this->named_marks_.swap(that.named_marks_);
  154. std::swap(this->mark_count_, that.mark_count_);
  155. std::swap(this->hidden_mark_count_, that.hidden_mark_count_);
  156. }
  157. intrusive_ptr<matchable_ex<BidiIter> const> xpr_;
  158. intrusive_ptr<traits<char_type> const> traits_;
  159. intrusive_ptr<finder<BidiIter> > finder_;
  160. std::vector<named_mark<char_type> > named_marks_;
  161. std::size_t mark_count_;
  162. std::size_t hidden_mark_count_;
  163. #ifdef BOOST_XPRESSIVE_DEBUG_CYCLE_TEST
  164. static int instances;
  165. #endif
  166. private:
  167. regex_impl &operator =(regex_impl const &);
  168. };
  169. template<typename BidiIter>
  170. void swap(regex_impl<BidiIter> &left, regex_impl<BidiIter> &right)
  171. {
  172. left.swap(right);
  173. }
  174. #ifdef BOOST_XPRESSIVE_DEBUG_CYCLE_TEST
  175. template<typename BidiIter>
  176. int regex_impl<BidiIter>::instances = 0;
  177. #endif
  178. }}} // namespace boost::xpressive::detail
  179. #endif