| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2003 Joel de Guzman |
| 3 | Copyright (c) 2001-2003 Daniel Nuffer |
| 4 | http://spirit.sourceforge.net/ |
| 5 | |
| 6 | Use, modification and distribution is subject to the Boost Software |
| 7 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 8 | http://www.boost.org/LICENSE_1_0.txt) |
| 9 | =============================================================================*/ |
| 10 | #ifndef BOOST_SPIRIT_BASIC_CHSET_IPP |
| 11 | #define BOOST_SPIRIT_BASIC_CHSET_IPP |
| 12 | |
| 13 | /////////////////////////////////////////////////////////////////////////////// |
| 14 | #include <bitset> |
| 15 | #include <boost/spirit/home/classic/utility/impl/chset/basic_chset.hpp> |
| 16 | |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | namespace boost { namespace spirit { |
| 19 | |
| 20 | BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN |
| 21 | |
| 22 | /////////////////////////////////////////////////////////////////////////////// |
| 23 | // |
| 24 | // basic_chset: character set implementation |
| 25 | // |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | template <typename CharT> |
| 28 | inline basic_chset<CharT>::basic_chset() {} |
| 29 | |
| 30 | ////////////////////////////////// |
| 31 | template <typename CharT> |
| 32 | inline basic_chset<CharT>::basic_chset(basic_chset const& arg_) |
| 33 | : rr(arg_.rr) {} |
| 34 | |
| 35 | ////////////////////////////////// |
| 36 | template <typename CharT> |
| 37 | inline bool |
| 38 | basic_chset<CharT>::test(CharT v) const |
| 39 | { return rr.test(v); } |
| 40 | |
| 41 | ////////////////////////////////// |
| 42 | template <typename CharT> |
| 43 | inline void |
| 44 | basic_chset<CharT>::set(CharT from, CharT to) |
| 45 | { rr.set(utility::impl::range<CharT>(from, to)); } |
| 46 | |
| 47 | ////////////////////////////////// |
| 48 | template <typename CharT> |
| 49 | inline void |
| 50 | basic_chset<CharT>::set(CharT c) |
| 51 | { rr.set(utility::impl::range<CharT>(c, c)); } |
| 52 | |
| 53 | ////////////////////////////////// |
| 54 | template <typename CharT> |
| 55 | inline void |
| 56 | basic_chset<CharT>::clear(CharT from, CharT to) |
| 57 | { rr.clear(utility::impl::range<CharT>(from, to)); } |
| 58 | |
| 59 | ////////////////////////////////// |
| 60 | template <typename CharT> |
| 61 | inline void |
| 62 | basic_chset<CharT>::clear() |
| 63 | { rr.clear(); } |
| 64 | |
| 65 | ///////////////////////////////// |
| 66 | template <typename CharT> |
| 67 | inline void |
| 68 | basic_chset<CharT>::inverse() |
| 69 | { |
| 70 | basic_chset inv; |
| 71 | inv.set( |
| 72 | (std::numeric_limits<CharT>::min)(), |
| 73 | (std::numeric_limits<CharT>::max)() |
| 74 | ); |
| 75 | inv -= *this; |
| 76 | swap(x&: inv); |
| 77 | } |
| 78 | |
| 79 | ///////////////////////////////// |
| 80 | template <typename CharT> |
| 81 | inline void |
| 82 | basic_chset<CharT>::swap(basic_chset& x) |
| 83 | { rr.swap(x.rr); } |
| 84 | |
| 85 | ///////////////////////////////// |
| 86 | template <typename CharT> |
| 87 | inline basic_chset<CharT>& |
| 88 | basic_chset<CharT>::operator|=(basic_chset<CharT> const& x) |
| 89 | { |
| 90 | typedef typename utility::impl::range_run<CharT>::const_iterator const_iterator; |
| 91 | for (const_iterator iter = x.rr.begin(); iter != x.rr.end(); ++iter) |
| 92 | rr.set(*iter); |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | ///////////////////////////////// |
| 97 | template <typename CharT> |
| 98 | inline basic_chset<CharT>& |
| 99 | basic_chset<CharT>::operator&=(basic_chset<CharT> const& x) |
| 100 | { |
| 101 | basic_chset inv; |
| 102 | inv.set( |
| 103 | (std::numeric_limits<CharT>::min)(), |
| 104 | (std::numeric_limits<CharT>::max)() |
| 105 | ); |
| 106 | inv -= x; |
| 107 | *this -= inv; |
| 108 | return *this; |
| 109 | } |
| 110 | |
| 111 | ///////////////////////////////// |
| 112 | template <typename CharT> |
| 113 | inline basic_chset<CharT>& |
| 114 | basic_chset<CharT>::operator-=(basic_chset<CharT> const& x) |
| 115 | { |
| 116 | typedef typename utility::impl::range_run<CharT>::const_iterator const_iterator; |
| 117 | for (const_iterator iter = x.rr.begin(); iter != x.rr.end(); ++iter) |
| 118 | rr.clear(*iter); |
| 119 | return *this; |
| 120 | } |
| 121 | |
| 122 | ///////////////////////////////// |
| 123 | template <typename CharT> |
| 124 | inline basic_chset<CharT>& |
| 125 | basic_chset<CharT>::operator^=(basic_chset<CharT> const& x) |
| 126 | { |
| 127 | basic_chset bma = x; |
| 128 | bma -= *this; |
| 129 | *this -= x; |
| 130 | *this |= bma; |
| 131 | return *this; |
| 132 | } |
| 133 | |
| 134 | #if (CHAR_BIT == 8) |
| 135 | |
| 136 | /////////////////////////////////////////////////////////////////////////////// |
| 137 | // |
| 138 | // basic_chset: specializations for 8 bit chars using std::bitset |
| 139 | // |
| 140 | /////////////////////////////////////////////////////////////////////////////// |
| 141 | template <typename CharT> |
| 142 | inline basic_chset_8bit<CharT>::basic_chset_8bit() {} |
| 143 | |
| 144 | ///////////////////////////////// |
| 145 | template <typename CharT> |
| 146 | inline basic_chset_8bit<CharT>::basic_chset_8bit(basic_chset_8bit const& arg_) |
| 147 | : bset(arg_.bset) {} |
| 148 | |
| 149 | ///////////////////////////////// |
| 150 | template <typename CharT> |
| 151 | inline bool |
| 152 | basic_chset_8bit<CharT>::test(CharT v) const |
| 153 | { return bset.test(position: (unsigned char)v); } |
| 154 | |
| 155 | ///////////////////////////////// |
| 156 | template <typename CharT> |
| 157 | inline void |
| 158 | basic_chset_8bit<CharT>::set(CharT from, CharT to) |
| 159 | { |
| 160 | for (int i = from; i <= to; ++i) |
| 161 | bset.set(position: (unsigned char)i); |
| 162 | } |
| 163 | |
| 164 | ///////////////////////////////// |
| 165 | template <typename CharT> |
| 166 | inline void |
| 167 | basic_chset_8bit<CharT>::set(CharT c) |
| 168 | { bset.set(position: (unsigned char)c); } |
| 169 | |
| 170 | ///////////////////////////////// |
| 171 | template <typename CharT> |
| 172 | inline void |
| 173 | basic_chset_8bit<CharT>::clear(CharT from, CharT to) |
| 174 | { |
| 175 | for (int i = from; i <= to; ++i) |
| 176 | bset.reset(position: (unsigned char)i); |
| 177 | } |
| 178 | |
| 179 | ///////////////////////////////// |
| 180 | template <typename CharT> |
| 181 | inline void |
| 182 | basic_chset_8bit<CharT>::clear(CharT c) |
| 183 | { bset.reset(position: (unsigned char)c); } |
| 184 | |
| 185 | ///////////////////////////////// |
| 186 | template <typename CharT> |
| 187 | inline void |
| 188 | basic_chset_8bit<CharT>::clear() |
| 189 | { bset.reset(); } |
| 190 | |
| 191 | ///////////////////////////////// |
| 192 | template <typename CharT> |
| 193 | inline void |
| 194 | basic_chset_8bit<CharT>::inverse() |
| 195 | { bset.flip(); } |
| 196 | |
| 197 | ///////////////////////////////// |
| 198 | template <typename CharT> |
| 199 | inline void |
| 200 | basic_chset_8bit<CharT>::swap(basic_chset_8bit& x) |
| 201 | { std::swap(bset, x.bset); } |
| 202 | |
| 203 | ///////////////////////////////// |
| 204 | template <typename CharT> |
| 205 | inline basic_chset_8bit<CharT>& |
| 206 | basic_chset_8bit<CharT>::operator|=(basic_chset_8bit const& x) |
| 207 | { |
| 208 | bset |= x.bset; |
| 209 | return *this; |
| 210 | } |
| 211 | |
| 212 | ///////////////////////////////// |
| 213 | template <typename CharT> |
| 214 | inline basic_chset_8bit<CharT>& |
| 215 | basic_chset_8bit<CharT>::operator&=(basic_chset_8bit const& x) |
| 216 | { |
| 217 | bset &= x.bset; |
| 218 | return *this; |
| 219 | } |
| 220 | |
| 221 | ///////////////////////////////// |
| 222 | template <typename CharT> |
| 223 | inline basic_chset_8bit<CharT>& |
| 224 | basic_chset_8bit<CharT>::operator-=(basic_chset_8bit const& x) |
| 225 | { |
| 226 | bset &= ~x.bset; |
| 227 | return *this; |
| 228 | } |
| 229 | |
| 230 | ///////////////////////////////// |
| 231 | template <typename CharT> |
| 232 | inline basic_chset_8bit<CharT>& |
| 233 | basic_chset_8bit<CharT>::operator^=(basic_chset_8bit const& x) |
| 234 | { |
| 235 | bset ^= x.bset; |
| 236 | return *this; |
| 237 | } |
| 238 | |
| 239 | #endif |
| 240 | |
| 241 | BOOST_SPIRIT_CLASSIC_NAMESPACE_END |
| 242 | |
| 243 | }} // namespace boost::spirit |
| 244 | |
| 245 | #endif |
| 246 | |
| 247 | |