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