1 ///////////////////////////////////////////////////////////////////////////////
2 // hash_peek_bitset.hpp
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)
8 #ifndef BOOST_XPRESSIVE_DETAIL_HASH_PEEK_BITSET_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_HASH_PEEK_BITSET_HPP_EAN_10_04_2005
11 // MS compatible compilers support #pragma once
14 # pragma warning(push)
15 # pragma warning(disable : 4100) // unreferenced formal parameter
16 # pragma warning(disable : 4127) // conditional expression constant
20 #include <string> // for std::char_traits
21 #include <boost/xpressive/detail/utility/chset/basic_chset.ipp>
23 namespace boost { namespace xpressive { namespace detail
26 ///////////////////////////////////////////////////////////////////////////////
29 template<typename Char>
30 struct hash_peek_bitset
32 typedef Char char_type;
33 typedef typename std::char_traits<char_type>::int_type int_type;
41 std::size_t count() const
43 return this->bset_.count();
52 template<typename Traits>
53 void set_char(char_type ch, bool icase, Traits const &tr)
55 if(this->test_icase_(icase))
57 ch = icase ? tr.translate_nocase(ch) : tr.translate(ch);
58 this->bset_.set(tr.hash(ch));
62 template<typename Traits>
63 void set_range(char_type from, char_type to, bool no, bool icase, Traits const &tr)
65 int_type ifrom = std::char_traits<char_type>::to_int_type(from);
66 int_type ito = std::char_traits<char_type>::to_int_type(to);
67 BOOST_ASSERT(ifrom <= ito);
68 // bound the computational complexity. BUGBUG could set the inverse range
69 if(no || 256 < (ito - ifrom))
73 else if(this->test_icase_(icase))
75 for(int_type i = ifrom; i <= ito; ++i)
77 char_type ch = std::char_traits<char_type>::to_char_type(i);
78 ch = icase ? tr.translate_nocase(ch) : tr.translate(ch);
79 this->bset_.set(tr.hash(ch));
84 template<typename Traits>
85 void set_class(typename Traits::char_class_type char_class, bool no, Traits const &tr)
87 if(1 != sizeof(char_type))
89 // wide character set, no efficient way of filling in the bitset, so set them all to 1
94 for(std::size_t i = 0; i <= UCHAR_MAX; ++i)
96 char_type ch = std::char_traits<char_type>::to_char_type(static_cast<int_type>(i));
97 if(no != tr.isctype(ch, char_class))
105 void set_bitset(hash_peek_bitset<Char> const &that)
107 if(this->test_icase_(that.icase()))
109 this->bset_ |= that.bset_;
113 void set_charset(basic_chset_8bit<Char> const &that, bool icase)
115 if(this->test_icase_(icase))
117 this->bset_ |= that.base();
126 template<typename Traits>
127 bool test(char_type ch, Traits const &tr) const
129 ch = this->icase_ ? tr.translate_nocase(ch) : tr.translate(ch);
130 return this->bset_.test(tr.hash(ch));
133 template<typename Traits>
134 bool test(char_type ch, Traits const &tr, mpl::false_) const
136 BOOST_ASSERT(!this->icase_);
137 return this->bset_.test(tr.hash(tr.translate(ch)));
140 template<typename Traits>
141 bool test(char_type ch, Traits const &tr, mpl::true_) const
143 BOOST_ASSERT(this->icase_);
144 return this->bset_.test(tr.hash(tr.translate_nocase(ch)));
149 // Make sure all sub-expressions being merged have the same case-sensitivity
150 bool test_icase_(bool icase)
152 std::size_t count = this->bset_.count();
156 return false; // all set already, nothing to do
158 else if(0 != count && this->icase_ != icase)
160 this->set_all(); // icase mismatch! set all and bail
164 this->icase_ = icase;
169 std::bitset<256> bset_;
172 }}} // namespace boost::xpressive::detail
174 #if defined(_MSC_VER)
175 # pragma warning(pop)