1 /*=============================================================================
2 Copyright (c) 2001-2003 Joel de Guzman
3 Copyright (c) 2001-2003 Daniel Nuffer
4 http://spirit.sourceforge.net/
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_XPRESSIVE_SPIRIT_BASIC_CHSET_IPP
11 #define BOOST_XPRESSIVE_SPIRIT_BASIC_CHSET_IPP
13 ///////////////////////////////////////////////////////////////////////////////
15 #include <boost/xpressive/detail/utility/chset/basic_chset.hpp>
17 ///////////////////////////////////////////////////////////////////////////////
18 namespace boost { namespace xpressive { namespace detail
21 ///////////////////////////////////////////////////////////////////////////////
23 // basic_chset: character set implementation
25 ///////////////////////////////////////////////////////////////////////////////
26 template<typename Char>
27 inline basic_chset<Char>::basic_chset()
31 //////////////////////////////////
32 template<typename Char>
33 inline basic_chset<Char>::basic_chset(basic_chset const &arg)
38 //////////////////////////////////
39 template<typename Char>
40 inline bool basic_chset<Char>::empty() const
42 return this->rr_.empty();
45 //////////////////////////////////
46 template<typename Char>
47 template<typename Traits>
48 inline bool basic_chset<Char>::test(Char v, Traits const &, mpl::false_) const // case-sensitive
50 return this->rr_.test(v);
53 //////////////////////////////////
54 template<typename Char>
55 template<typename Traits>
56 inline bool basic_chset<Char>::test(Char v, Traits const &tr, mpl::true_) const // case-insensitive
58 return this->rr_.test(v, tr);
61 //////////////////////////////////
62 template<typename Char>
63 inline void basic_chset<Char>::set(Char from, Char to)
65 this->rr_.set(range<Char>(from, to));
68 //////////////////////////////////
69 template<typename Char>
70 template<typename Traits>
71 inline void basic_chset<Char>::set(Char from, Char to, Traits const &)
73 this->rr_.set(range<Char>(from, to));
76 //////////////////////////////////
77 template<typename Char>
78 inline void basic_chset<Char>::set(Char c)
80 this->rr_.set(range<Char>(c, c));
83 //////////////////////////////////
84 template<typename Char>
85 template<typename Traits>
86 inline void basic_chset<Char>::set(Char c, Traits const &)
88 this->rr_.set(range<Char>(c, c));
91 //////////////////////////////////
92 template<typename Char>
93 inline void basic_chset<Char>::clear(Char c)
95 this->rr_.clear(range<Char>(c, c));
98 //////////////////////////////////
99 template<typename Char>
100 template<typename Traits>
101 inline void basic_chset<Char>::clear(Char c, Traits const &)
103 this->rr_.clear(range<Char>(c, c));
106 //////////////////////////////////
107 template<typename Char>
108 inline void basic_chset<Char>::clear(Char from, Char to)
110 this->rr_.clear(range<Char>(from, to));
113 //////////////////////////////////
114 template<typename Char>
115 template<typename Traits>
116 inline void basic_chset<Char>::clear(Char from, Char to, Traits const &)
118 this->rr_.clear(range<Char>(from, to));
121 //////////////////////////////////
122 template<typename Char>
123 inline void basic_chset<Char>::clear()
128 /////////////////////////////////
129 template<typename Char>
130 inline void basic_chset<Char>::inverse()
132 // BUGBUG is this right? Does this handle icase correctly?
133 basic_chset<Char> inv;
134 inv.set((std::numeric_limits<Char>::min)(), (std::numeric_limits<Char>::max)());
139 /////////////////////////////////
140 template<typename Char>
141 inline void basic_chset<Char>::swap(basic_chset<Char> &that)
143 this->rr_.swap(that.rr_);
146 /////////////////////////////////
147 template<typename Char>
148 inline basic_chset<Char> &
149 basic_chset<Char>::operator |=(basic_chset<Char> const &that)
151 typedef typename range_run<Char>::const_iterator const_iterator;
152 for(const_iterator iter = that.rr_.begin(); iter != that.rr_.end(); ++iter)
154 this->rr_.set(*iter);
159 /////////////////////////////////
160 template<typename Char>
161 inline basic_chset<Char> &
162 basic_chset<Char>::operator &=(basic_chset<Char> const &that)
164 basic_chset<Char> inv;
165 inv.set((std::numeric_limits<Char>::min)(), (std::numeric_limits<Char>::max)());
171 /////////////////////////////////
172 template<typename Char>
173 inline basic_chset<Char> &
174 basic_chset<Char>::operator -=(basic_chset<Char> const &that)
176 typedef typename range_run<Char>::const_iterator const_iterator;
177 for(const_iterator iter = that.rr_.begin(); iter != that.rr_.end(); ++iter)
179 this->rr_.clear(*iter);
184 /////////////////////////////////
185 template<typename Char>
186 inline basic_chset<Char> &
187 basic_chset<Char>::operator ^=(basic_chset<Char> const &that)
189 basic_chset bma = that;
198 ///////////////////////////////////////////////////////////////////////////////
200 // basic_chset: specializations for 8 bit chars using std::bitset
202 ///////////////////////////////////////////////////////////////////////////////
203 template<typename Char>
204 inline basic_chset_8bit<Char>::basic_chset_8bit()
208 /////////////////////////////////
209 template<typename Char>
210 inline basic_chset_8bit<Char>::basic_chset_8bit(basic_chset_8bit<Char> const &arg)
215 /////////////////////////////////
216 template<typename Char>
217 inline bool basic_chset_8bit<Char>::empty() const
219 return !this->bset_.any();
222 /////////////////////////////////
223 template<typename Char>
224 template<typename Traits>
225 inline bool basic_chset_8bit<Char>::test(Char v, Traits const &, mpl::false_) const // case-sensitive
227 return this->bset_.test((unsigned char)v);
230 /////////////////////////////////
231 template<typename Char>
232 template<typename Traits>
233 inline bool basic_chset_8bit<Char>::test(Char v, Traits const &tr, mpl::true_) const // case-insensitive
235 return this->bset_.test((unsigned char)tr.translate_nocase(v));
238 /////////////////////////////////
239 template<typename Char>
240 inline void basic_chset_8bit<Char>::set(Char from, Char to)
242 for(int i = from; i <= to; ++i)
244 this->bset_.set((unsigned char)i);
248 /////////////////////////////////
249 template<typename Char>
250 template<typename Traits>
251 inline void basic_chset_8bit<Char>::set(Char from, Char to, Traits const &tr)
253 for(int i = from; i <= to; ++i)
255 this->bset_.set((unsigned char)tr.translate_nocase((Char)i));
259 /////////////////////////////////
260 template<typename Char>
261 inline void basic_chset_8bit<Char>::set(Char c)
263 this->bset_.set((unsigned char)c);
266 /////////////////////////////////
267 template<typename Char>
268 template<typename Traits>
269 inline void basic_chset_8bit<Char>::set(Char c, Traits const &tr)
271 this->bset_.set((unsigned char)tr.translate_nocase(c));
274 /////////////////////////////////
275 template<typename Char>
276 inline void basic_chset_8bit<Char>::clear(Char from, Char to)
278 for(int i = from; i <= to; ++i)
280 this->bset_.reset((unsigned char)i);
284 /////////////////////////////////
285 template<typename Char>
286 template<typename Traits>
287 inline void basic_chset_8bit<Char>::clear(Char from, Char to, Traits const &tr)
289 for(int i = from; i <= to; ++i)
291 this->bset_.reset((unsigned char)tr.translate_nocase((Char)i));
295 /////////////////////////////////
296 template<typename Char>
297 inline void basic_chset_8bit<Char>::clear(Char c)
299 this->bset_.reset((unsigned char)c);
302 /////////////////////////////////
303 template<typename Char>
304 template<typename Traits>
305 inline void basic_chset_8bit<Char>::clear(Char c, Traits const &tr)
307 this->bset_.reset((unsigned char)tr.tranlsate_nocase(c));
310 /////////////////////////////////
311 template<typename Char>
312 inline void basic_chset_8bit<Char>::clear()
317 /////////////////////////////////
318 template<typename Char>
319 inline void basic_chset_8bit<Char>::inverse()
324 /////////////////////////////////
325 template<typename Char>
326 inline void basic_chset_8bit<Char>::swap(basic_chset_8bit<Char> &that)
328 std::swap(this->bset_, that.bset_);
331 /////////////////////////////////
332 template<typename Char>
333 inline basic_chset_8bit<Char> &
334 basic_chset_8bit<Char>::operator |=(basic_chset_8bit<Char> const &that)
336 this->bset_ |= that.bset_;
340 /////////////////////////////////
341 template<typename Char>
342 inline basic_chset_8bit<Char> &
343 basic_chset_8bit<Char>::operator &=(basic_chset_8bit<Char> const &that)
345 this->bset_ &= that.bset_;
349 /////////////////////////////////
350 template<typename Char>
351 inline basic_chset_8bit<Char> &
352 basic_chset_8bit<Char>::operator -=(basic_chset_8bit<Char> const &that)
354 this->bset_ &= ~that.bset_;
358 /////////////////////////////////
359 template<typename Char>
360 inline basic_chset_8bit<Char> &
361 basic_chset_8bit<Char>::operator ^=(basic_chset_8bit<Char> const &that)
363 this->bset_ ^= that.bset_;
367 template<typename Char>
368 inline std::bitset<256> const &
369 basic_chset_8bit<Char>::base() const
374 #endif // if(CHAR_BIT == 8)
377 ///////////////////////////////////////////////////////////////////////////////
379 template<typename Char, typename Traits>
380 inline void set_char(basic_chset<Char> &chset, Char ch, Traits const &tr, bool icase)
382 icase ? chset.set(ch, tr) : chset.set(ch);
385 template<typename Char, typename Traits>
386 inline void set_range(basic_chset<Char> &chset, Char from, Char to, Traits const &tr, bool icase)
388 icase ? chset.set(from, to, tr) : chset.set(from, to);
391 template<typename Char, typename Traits>
392 inline void set_class(basic_chset<Char> &chset, typename Traits::char_class_type char_class, bool no, Traits const &tr)
394 BOOST_MPL_ASSERT_RELATION(1, ==, sizeof(Char));
395 for(std::size_t i = 0; i <= UCHAR_MAX; ++i)
397 typedef typename std::char_traits<Char>::int_type int_type;
398 Char ch = std::char_traits<Char>::to_char_type(static_cast<int_type>(i));
399 if(no != tr.isctype(ch, char_class))
406 }}} // namespace boost::xpressive::detail