]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | * | |
3 | * Copyright (c) 2003 | |
4 | * John Maddock | |
5 | * | |
6 | * Use, modification and distribution are subject to the | |
7 | * Boost Software License, Version 1.0. (See accompanying file | |
8 | * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | * | |
10 | */ | |
11 | ||
12 | /* | |
13 | * LOCATION: see http://www.boost.org for most recent version. | |
14 | * FILE regex_token_iterator.hpp | |
15 | * VERSION see <boost/version.hpp> | |
16 | * DESCRIPTION: Provides regex_token_iterator implementation. | |
17 | */ | |
18 | ||
19 | #ifndef BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP | |
20 | #define BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP | |
21 | ||
22 | #include <boost/shared_ptr.hpp> | |
23 | #include <boost/detail/workaround.hpp> | |
20effc67 | 24 | #if (BOOST_WORKAROUND(BOOST_BORLANDC, >= 0x560) && BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x570)))\ |
7c673cae FG |
25 | || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) |
26 | // | |
27 | // Borland C++ Builder 6, and Visual C++ 6, | |
28 | // can't cope with the array template constructor | |
29 | // so we have a template member that will accept any type as | |
30 | // argument, and then assert that is really is an array: | |
31 | // | |
32 | #include <boost/static_assert.hpp> | |
33 | #include <boost/type_traits/is_array.hpp> | |
34 | #endif | |
35 | ||
36 | namespace boost{ | |
37 | ||
38 | #ifdef BOOST_MSVC | |
39 | #pragma warning(push) | |
40 | #pragma warning(disable: 4103) | |
41 | #endif | |
42 | #ifdef BOOST_HAS_ABI_HEADERS | |
43 | # include BOOST_ABI_PREFIX | |
44 | #endif | |
45 | #ifdef BOOST_MSVC | |
46 | #pragma warning(pop) | |
47 | #pragma warning(push) | |
48 | #pragma warning(disable:4700) | |
49 | #endif | |
50 | ||
51 | template <class BidirectionalIterator, | |
52 | class charT, | |
53 | class traits> | |
54 | class regex_token_iterator_implementation | |
55 | { | |
56 | typedef basic_regex<charT, traits> regex_type; | |
57 | typedef sub_match<BidirectionalIterator> value_type; | |
58 | ||
59 | match_results<BidirectionalIterator> what; // current match | |
60 | BidirectionalIterator base; // start of search area | |
61 | BidirectionalIterator end; // end of search area | |
62 | const regex_type re; // the expression | |
63 | match_flag_type flags; // match flags | |
64 | value_type result; // the current string result | |
65 | int N; // the current sub-expression being enumerated | |
66 | std::vector<int> subs; // the sub-expressions to enumerate | |
67 | ||
68 | public: | |
1e59de90 TL |
69 | regex_token_iterator_implementation(const regex_token_iterator_implementation& other) |
70 | : what(other.what), base(other.base), end(other.end), re(other.re), flags(other.flags), result(other.result), N(other.N), subs(other.subs) {} | |
7c673cae | 71 | regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f) |
20effc67 | 72 | : end(last), re(*p), flags(f), N(0){ subs.push_back(sub); } |
7c673cae | 73 | regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector<int>& v, match_flag_type f) |
20effc67 | 74 | : end(last), re(*p), flags(f), N(0), subs(v){} |
7c673cae | 75 | #if !BOOST_WORKAROUND(__HP_aCC, < 60700) |
20effc67 | 76 | #if (BOOST_WORKAROUND(BOOST_BORLANDC, >= 0x560) && BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x570)))\ |
7c673cae FG |
77 | || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \ |
78 | || BOOST_WORKAROUND(__HP_aCC, < 60700) | |
79 | template <class T> | |
80 | regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const T& submatches, match_flag_type f) | |
20effc67 | 81 | : end(last), re(*p), flags(f), N(0) |
7c673cae FG |
82 | { |
83 | // assert that T really is an array: | |
84 | BOOST_STATIC_ASSERT(::boost::is_array<T>::value); | |
85 | const std::size_t array_size = sizeof(T) / sizeof(submatches[0]); | |
86 | for(std::size_t i = 0; i < array_size; ++i) | |
87 | { | |
88 | subs.push_back(submatches[i]); | |
89 | } | |
90 | } | |
91 | #else | |
92 | template <std::size_t CN> | |
93 | regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f) | |
20effc67 | 94 | : end(last), re(*p), flags(f), N(0) |
7c673cae FG |
95 | { |
96 | for(std::size_t i = 0; i < CN; ++i) | |
97 | { | |
98 | subs.push_back(submatches[i]); | |
99 | } | |
100 | } | |
101 | #endif | |
102 | #endif | |
103 | bool init(BidirectionalIterator first) | |
104 | { | |
105 | N = 0; | |
106 | base = first; | |
107 | if(regex_search(first, end, what, re, flags, base) == true) | |
108 | { | |
109 | N = 0; | |
110 | result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]); | |
111 | return true; | |
112 | } | |
113 | else if((subs[N] == -1) && (first != end)) | |
114 | { | |
115 | result.first = first; | |
116 | result.second = end; | |
117 | result.matched = (first != end); | |
118 | N = -1; | |
119 | return true; | |
120 | } | |
121 | return false; | |
122 | } | |
123 | bool compare(const regex_token_iterator_implementation& that) | |
124 | { | |
125 | if(this == &that) return true; | |
126 | return (&re.get_data() == &that.re.get_data()) | |
127 | && (end == that.end) | |
128 | && (flags == that.flags) | |
129 | && (N == that.N) | |
130 | && (what[0].first == that.what[0].first) | |
131 | && (what[0].second == that.what[0].second); | |
132 | } | |
133 | const value_type& get() | |
134 | { return result; } | |
135 | bool next() | |
136 | { | |
137 | if(N == -1) | |
138 | return false; | |
139 | if(N+1 < (int)subs.size()) | |
140 | { | |
141 | ++N; | |
142 | result =((subs[N] == -1) ? what.prefix() : what[subs[N]]); | |
143 | return true; | |
144 | } | |
145 | //if(what.prefix().first != what[0].second) | |
146 | // flags |= /*match_prev_avail |*/ regex_constants::match_not_bob; | |
147 | BidirectionalIterator last_end(what[0].second); | |
148 | if(regex_search(last_end, end, what, re, ((what[0].first == what[0].second) ? flags | regex_constants::match_not_initial_null : flags), base)) | |
149 | { | |
150 | N =0; | |
151 | result =((subs[N] == -1) ? what.prefix() : what[subs[N]]); | |
152 | return true; | |
153 | } | |
154 | else if((last_end != end) && (subs[0] == -1)) | |
155 | { | |
156 | N =-1; | |
157 | result.first = last_end; | |
158 | result.second = end; | |
159 | result.matched = (last_end != end); | |
160 | return true; | |
161 | } | |
162 | return false; | |
163 | } | |
164 | private: | |
165 | regex_token_iterator_implementation& operator=(const regex_token_iterator_implementation&); | |
166 | }; | |
167 | ||
168 | template <class BidirectionalIterator, | |
169 | class charT = BOOST_DEDUCED_TYPENAME BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::value_type, | |
170 | class traits = regex_traits<charT> > | |
171 | class regex_token_iterator | |
7c673cae FG |
172 | { |
173 | private: | |
174 | typedef regex_token_iterator_implementation<BidirectionalIterator, charT, traits> impl; | |
175 | typedef shared_ptr<impl> pimpl; | |
176 | public: | |
177 | typedef basic_regex<charT, traits> regex_type; | |
178 | typedef sub_match<BidirectionalIterator> value_type; | |
179 | typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type | |
180 | difference_type; | |
181 | typedef const value_type* pointer; | |
182 | typedef const value_type& reference; | |
183 | typedef std::forward_iterator_tag iterator_category; | |
184 | ||
185 | regex_token_iterator(){} | |
186 | regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, | |
187 | int submatch = 0, match_flag_type m = match_default) | |
188 | : pdata(new impl(&re, b, submatch, m)) | |
189 | { | |
190 | if(!pdata->init(a)) | |
191 | pdata.reset(); | |
192 | } | |
193 | regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, | |
194 | const std::vector<int>& submatches, match_flag_type m = match_default) | |
195 | : pdata(new impl(&re, b, submatches, m)) | |
196 | { | |
197 | if(!pdata->init(a)) | |
198 | pdata.reset(); | |
199 | } | |
200 | #if !BOOST_WORKAROUND(__HP_aCC, < 60700) | |
20effc67 | 201 | #if (BOOST_WORKAROUND(BOOST_BORLANDC, >= 0x560) && BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x570)))\ |
7c673cae FG |
202 | || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \ |
203 | || BOOST_WORKAROUND(__HP_aCC, < 60700) | |
204 | template <class T> | |
205 | regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, | |
206 | const T& submatches, match_flag_type m = match_default) | |
207 | : pdata(new impl(&re, b, submatches, m)) | |
208 | { | |
209 | if(!pdata->init(a)) | |
210 | pdata.reset(); | |
211 | } | |
212 | #else | |
213 | template <std::size_t N> | |
214 | regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, | |
215 | const int (&submatches)[N], match_flag_type m = match_default) | |
216 | : pdata(new impl(&re, b, submatches, m)) | |
217 | { | |
218 | if(!pdata->init(a)) | |
219 | pdata.reset(); | |
220 | } | |
221 | #endif | |
222 | #endif | |
223 | regex_token_iterator(const regex_token_iterator& that) | |
224 | : pdata(that.pdata) {} | |
225 | regex_token_iterator& operator=(const regex_token_iterator& that) | |
226 | { | |
227 | pdata = that.pdata; | |
228 | return *this; | |
229 | } | |
230 | bool operator==(const regex_token_iterator& that)const | |
231 | { | |
232 | if((pdata.get() == 0) || (that.pdata.get() == 0)) | |
233 | return pdata.get() == that.pdata.get(); | |
234 | return pdata->compare(*(that.pdata.get())); | |
235 | } | |
236 | bool operator!=(const regex_token_iterator& that)const | |
237 | { return !(*this == that); } | |
238 | const value_type& operator*()const | |
239 | { return pdata->get(); } | |
240 | const value_type* operator->()const | |
241 | { return &(pdata->get()); } | |
242 | regex_token_iterator& operator++() | |
243 | { | |
244 | cow(); | |
245 | if(0 == pdata->next()) | |
246 | { | |
247 | pdata.reset(); | |
248 | } | |
249 | return *this; | |
250 | } | |
251 | regex_token_iterator operator++(int) | |
252 | { | |
253 | regex_token_iterator result(*this); | |
254 | ++(*this); | |
255 | return result; | |
256 | } | |
257 | private: | |
258 | ||
259 | pimpl pdata; | |
260 | ||
261 | void cow() | |
262 | { | |
263 | // copy-on-write | |
264 | if(pdata.get() && !pdata.unique()) | |
265 | { | |
266 | pdata.reset(new impl(*(pdata.get()))); | |
267 | } | |
268 | } | |
269 | }; | |
270 | ||
271 | typedef regex_token_iterator<const char*> cregex_token_iterator; | |
272 | typedef regex_token_iterator<std::string::const_iterator> sregex_token_iterator; | |
273 | #ifndef BOOST_NO_WREGEX | |
274 | typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator; | |
275 | typedef regex_token_iterator<std::wstring::const_iterator> wsregex_token_iterator; | |
276 | #endif | |
277 | ||
278 | template <class charT, class traits> | |
279 | inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default) | |
280 | { | |
281 | return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m); | |
282 | } | |
283 | template <class charT, class traits, class ST, class SA> | |
284 | inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default) | |
285 | { | |
286 | return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m); | |
287 | } | |
288 | template <class charT, class traits, std::size_t N> | |
289 | inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default) | |
290 | { | |
291 | return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m); | |
292 | } | |
293 | template <class charT, class traits, class ST, class SA, std::size_t N> | |
294 | inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default) | |
295 | { | |
296 | return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m); | |
297 | } | |
298 | template <class charT, class traits> | |
299 | inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default) | |
300 | { | |
301 | return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m); | |
302 | } | |
303 | template <class charT, class traits, class ST, class SA> | |
304 | inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default) | |
305 | { | |
306 | return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m); | |
307 | } | |
308 | ||
309 | #ifdef BOOST_MSVC | |
310 | #pragma warning(pop) | |
311 | #pragma warning(push) | |
312 | #pragma warning(disable: 4103) | |
313 | #endif | |
314 | #ifdef BOOST_HAS_ABI_HEADERS | |
315 | # include BOOST_ABI_SUFFIX | |
316 | #endif | |
317 | #ifdef BOOST_MSVC | |
318 | #pragma warning(pop) | |
319 | #endif | |
320 | ||
321 | } // namespace boost | |
322 | ||
323 | #endif // BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP | |
324 | ||
325 | ||
326 | ||
327 |