123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- /*=============================================================================
- 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 <bitset>
- #include <climits>
- #include <boost/spirit/home/support/char_set/range_run.hpp>
- namespace boost { namespace spirit { namespace support { namespace detail
- {
- ///////////////////////////////////////////////////////////////////////////
- //
- // basic_chset: basic character set implementation using range_run
- //
- ///////////////////////////////////////////////////////////////////////////
- template <typename Char>
- 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<Char>(from, to));
- }
- void
- set(Char c)
- {
- rr.set(range<Char>(c, c));
- }
- void
- clear(Char from, Char to)
- {
- rr.clear(range<Char>(from, to));
- }
- void
- clear(Char c)
- {
- rr.clear(range<Char>(c, c));
- }
- void
- clear()
- {
- rr.clear();
- }
- void
- inverse()
- {
- basic_chset inv;
- inv.set(
- (std::numeric_limits<Char>::min)(),
- (std::numeric_limits<Char>::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<Char>::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<Char>::min)(),
- (std::numeric_limits<Char>::max)()
- );
- inv -= x;
- *this -= inv;
- return *this;
- }
- basic_chset&
- operator-=(basic_chset const& x)
- {
- typedef typename range_run<Char>::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<Char> rr;
- };
- #if (CHAR_BIT == 8)
- ///////////////////////////////////////////////////////////////////////////
- //
- // basic_chset: specializations for 8 bit chars using std::bitset
- //
- ///////////////////////////////////////////////////////////////////////////
- template <typename Char>
- 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<char>
- : basic_chset_8bit<char> {};
- /////////////////////////////////
- template <>
- struct basic_chset<signed char>
- : basic_chset_8bit<signed char> {};
- /////////////////////////////////
- template <>
- struct basic_chset<unsigned char>
- : basic_chset_8bit<unsigned char> {};
- #endif // #if (CHAR_BIT == 8)
- }}}}
- #endif
|