1 | /////////////////////////////////////////////////////////////////////////////// |
---|
2 | // chset.hpp |
---|
3 | // |
---|
4 | // Copyright 2004 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) |
---|
7 | |
---|
8 | #ifndef BOOST_XPRESSIVE_DETAIL_CHSET_CHSET_HPP_EAN_10_04_2005 |
---|
9 | #define BOOST_XPRESSIVE_DETAIL_CHSET_CHSET_HPP_EAN_10_04_2005 |
---|
10 | |
---|
11 | // MS compatible compilers support #pragma once |
---|
12 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
---|
13 | # pragma once |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <vector> |
---|
17 | #include <boost/xpressive/detail/detail_fwd.hpp> |
---|
18 | #include <boost/xpressive/detail/utility/algorithm.hpp> |
---|
19 | #include <boost/xpressive/detail/utility/chset/basic_chset.ipp> |
---|
20 | |
---|
21 | namespace boost { namespace xpressive { namespace detail |
---|
22 | { |
---|
23 | |
---|
24 | /////////////////////////////////////////////////////////////////////////////// |
---|
25 | // compound_charset |
---|
26 | // |
---|
27 | template<typename Traits> |
---|
28 | struct compound_charset |
---|
29 | : private basic_chset<typename Traits::char_type> |
---|
30 | { |
---|
31 | typedef typename Traits::char_type char_type; |
---|
32 | typedef basic_chset<char_type> base_type; |
---|
33 | typedef Traits traits_type; |
---|
34 | typedef typename Traits::char_class_type char_class_type; |
---|
35 | |
---|
36 | compound_charset() |
---|
37 | : base_type() |
---|
38 | , complement_(false) |
---|
39 | , has_posix_(false) |
---|
40 | , posix_yes_() |
---|
41 | , posix_no_() |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | /////////////////////////////////////////////////////////////////////////////// |
---|
46 | // accessors |
---|
47 | basic_chset<char_type> const &basic_chset() const |
---|
48 | { |
---|
49 | return *this; |
---|
50 | } |
---|
51 | |
---|
52 | bool is_inverted() const |
---|
53 | { |
---|
54 | return this->complement_; |
---|
55 | } |
---|
56 | |
---|
57 | char_class_type posix_yes() const |
---|
58 | { |
---|
59 | return this->posix_yes_; |
---|
60 | } |
---|
61 | |
---|
62 | std::vector<char_class_type> const &posix_no() const |
---|
63 | { |
---|
64 | return this->posix_no_; |
---|
65 | } |
---|
66 | |
---|
67 | /////////////////////////////////////////////////////////////////////////////// |
---|
68 | // complement |
---|
69 | void inverse() |
---|
70 | { |
---|
71 | this->complement_ = !this->complement_; |
---|
72 | } |
---|
73 | |
---|
74 | /////////////////////////////////////////////////////////////////////////////// |
---|
75 | // set |
---|
76 | void set_char(char_type ch, Traits const &traits, bool icase) |
---|
77 | { |
---|
78 | icase ? this->base_type::set(ch, traits) : this->base_type::set(ch); |
---|
79 | } |
---|
80 | |
---|
81 | void set_range(char_type from, char_type to, Traits const &traits, bool icase) |
---|
82 | { |
---|
83 | icase ? this->base_type::set(from, to, traits) : this->base_type::set(from, to); |
---|
84 | } |
---|
85 | |
---|
86 | void set_class(char_class_type const &m, bool no) |
---|
87 | { |
---|
88 | this->has_posix_ = true; |
---|
89 | |
---|
90 | if(no) |
---|
91 | { |
---|
92 | this->posix_no_.push_back(m); |
---|
93 | } |
---|
94 | else |
---|
95 | { |
---|
96 | this->posix_yes_ |= m; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | /////////////////////////////////////////////////////////////////////////////// |
---|
101 | // test |
---|
102 | template<typename ICase> |
---|
103 | bool test(char_type ch, Traits const &traits, ICase) const |
---|
104 | { |
---|
105 | return this->complement_ != |
---|
106 | (this->base_type::test(ch, traits, ICase()) || |
---|
107 | (this->has_posix_ && this->test_posix(ch, traits))); |
---|
108 | } |
---|
109 | |
---|
110 | private: |
---|
111 | |
---|
112 | /////////////////////////////////////////////////////////////////////////////// |
---|
113 | // not_posix_pred |
---|
114 | struct not_posix_pred |
---|
115 | { |
---|
116 | char_type ch_; |
---|
117 | Traits const *traits_ptr_; |
---|
118 | |
---|
119 | bool operator ()(typename call_traits<char_class_type>::param_type m) const |
---|
120 | { |
---|
121 | return !this->traits_ptr_->isctype(this->ch_, m); |
---|
122 | } |
---|
123 | }; |
---|
124 | |
---|
125 | /////////////////////////////////////////////////////////////////////////////// |
---|
126 | // test_posix |
---|
127 | bool test_posix(char_type ch, Traits const &traits) const |
---|
128 | { |
---|
129 | not_posix_pred const pred = {ch, &traits}; |
---|
130 | return traits.isctype(ch, this->posix_yes_) |
---|
131 | || any(this->posix_no_.begin(), this->posix_no_.end(), pred); |
---|
132 | } |
---|
133 | |
---|
134 | bool complement_; |
---|
135 | bool has_posix_; |
---|
136 | char_class_type posix_yes_; |
---|
137 | std::vector<char_class_type> posix_no_; |
---|
138 | }; |
---|
139 | |
---|
140 | |
---|
141 | /////////////////////////////////////////////////////////////////////////////// |
---|
142 | // helpers |
---|
143 | template<typename Char, typename Traits> |
---|
144 | inline void set_char(compound_charset<Traits> &chset, Char ch, Traits const &traits, bool icase) |
---|
145 | { |
---|
146 | chset.set_char(ch, traits, icase); |
---|
147 | } |
---|
148 | |
---|
149 | template<typename Char, typename Traits> |
---|
150 | inline void set_range(compound_charset<Traits> &chset, Char from, Char to, Traits const &traits, bool icase) |
---|
151 | { |
---|
152 | chset.set_range(from, to, traits, icase); |
---|
153 | } |
---|
154 | |
---|
155 | template<typename Traits> |
---|
156 | inline void set_class(compound_charset<Traits> &chset, typename Traits::char_class_type char_class, bool no, Traits const &) |
---|
157 | { |
---|
158 | chset.set_class(char_class, no); |
---|
159 | } |
---|
160 | |
---|
161 | }}} // namespace boost::xpressive::detail |
---|
162 | |
---|
163 | #endif |
---|
164 | |
---|