/*============================================================================= Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2001-2009 Daniel Nuffer http://spirit.sourceforge.net/ Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #ifndef BOOST_SPIRIT_BASIC_CHSET_APRIL_17_2008 #define BOOST_SPIRIT_BASIC_CHSET_APRIL_17_2008 #if defined(_MSC_VER) #pragma once #endif /////////////////////////////////////////////////////////////////////////////// #include #include #include namespace boost { namespace spirit { namespace support { namespace detail { /////////////////////////////////////////////////////////////////////////// // // basic_chset: basic character set implementation using range_run // /////////////////////////////////////////////////////////////////////////// template struct basic_chset { basic_chset() {} basic_chset(basic_chset const& arg_) : rr(arg_.rr) {} bool test(Char v) const { return rr.test(v); } void set(Char from, Char to) { rr.set(range(from, to)); } void set(Char c) { rr.set(range(c, c)); } void clear(Char from, Char to) { rr.clear(range(from, to)); } void clear(Char c) { rr.clear(range(c, c)); } void clear() { rr.clear(); } void inverse() { basic_chset inv; inv.set( (std::numeric_limits::min)(), (std::numeric_limits::max)() ); inv -= *this; swap(inv); } void swap(basic_chset& x) { rr.swap(x.rr); } basic_chset& operator|=(basic_chset const& x) { typedef typename range_run::const_iterator const_iterator; for (const_iterator iter = x.rr.begin(); iter != x.rr.end(); ++iter) rr.set(*iter); return *this; } basic_chset& operator&=(basic_chset const& x) { basic_chset inv; inv.set( (std::numeric_limits::min)(), (std::numeric_limits::max)() ); inv -= x; *this -= inv; return *this; } basic_chset& operator-=(basic_chset const& x) { typedef typename range_run::const_iterator const_iterator; for (const_iterator iter = x.rr.begin(); iter != x.rr.end(); ++iter) rr.clear(*iter); return *this; } basic_chset& operator^=(basic_chset const& x) { basic_chset bma = x; bma -= *this; *this -= x; *this |= bma; return *this; } private: range_run rr; }; #if (CHAR_BIT == 8) /////////////////////////////////////////////////////////////////////////// // // basic_chset: specializations for 8 bit chars using std::bitset // /////////////////////////////////////////////////////////////////////////// template struct basic_chset_8bit { basic_chset_8bit() {} basic_chset_8bit(basic_chset_8bit const& arg_) : bset(arg_.bset) {} bool test(Char v) const { return bset.test((unsigned char)v); } void set(Char from, Char to) { for (int i = from; i <= to; ++i) bset.set((unsigned char)i); } void set(Char c) { bset.set((unsigned char)c); } void clear(Char from, Char to) { for (int i = from; i <= to; ++i) bset.reset((unsigned char)i); } void clear(Char c) { bset.reset((unsigned char)c); } void clear() { bset.reset(); } void inverse() { bset.flip(); } void swap(basic_chset_8bit& x) { std::swap(bset, x.bset); } basic_chset_8bit& operator|=(basic_chset_8bit const& x) { bset |= x.bset; return *this; } basic_chset_8bit& operator&=(basic_chset_8bit const& x) { bset &= x.bset; return *this; } basic_chset_8bit& operator-=(basic_chset_8bit const& x) { bset &= ~x.bset; return *this; } basic_chset_8bit& operator^=(basic_chset_8bit const& x) { bset ^= x.bset; return *this; } private: std::bitset<256> bset; }; ///////////////////////////////// template <> struct basic_chset : basic_chset_8bit {}; ///////////////////////////////// template <> struct basic_chset : basic_chset_8bit {}; ///////////////////////////////// template <> struct basic_chset : basic_chset_8bit {}; #endif // #if (CHAR_BIT == 8) }}}} #endif