6 * Use, modification and distribution are subject to the
\r
7 * Boost Software License, Version 1.0. (See accompanying file
\r
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
13 * LOCATION: see http://www.boost.org for most recent version.
\r
14 * FILE u32regex_token_iterator.hpp
\r
15 * VERSION see <boost/version.hpp>
\r
16 * DESCRIPTION: Provides u32regex_token_iterator implementation.
\r
19 #ifndef BOOST_REGEX_V4_U32REGEX_TOKEN_ITERATOR_HPP
\r
20 #define BOOST_REGEX_V4_U32REGEX_TOKEN_ITERATOR_HPP
\r
22 #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
\r
23 || BOOST_WORKAROUND(BOOST_MSVC, < 1300) \
\r
24 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
\r
26 // Borland C++ Builder 6, and Visual C++ 6,
\r
27 // can't cope with the array template constructor
\r
28 // so we have a template member that will accept any type as
\r
29 // argument, and then assert that is really is an array:
\r
31 #include <boost/static_assert.hpp>
\r
32 #include <boost/type_traits/is_array.hpp>
\r
37 #ifdef BOOST_HAS_ABI_HEADERS
\r
38 # include BOOST_ABI_PREFIX
\r
40 #if BOOST_WORKAROUND(BOOST_MSVC, > 1300)
\r
41 # pragma warning(push)
\r
42 # pragma warning(disable:4700)
\r
45 template <class BidirectionalIterator>
\r
46 class u32regex_token_iterator_implementation
\r
48 typedef u32regex regex_type;
\r
49 typedef sub_match<BidirectionalIterator> value_type;
\r
51 match_results<BidirectionalIterator> what; // current match
\r
52 BidirectionalIterator end; // end of search area
\r
53 BidirectionalIterator base; // start of search area
\r
54 const regex_type re; // the expression
\r
55 match_flag_type flags; // match flags
\r
56 value_type result; // the current string result
\r
57 int N; // the current sub-expression being enumerated
\r
58 std::vector<int> subs; // the sub-expressions to enumerate
\r
61 u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f)
\r
62 : end(last), re(*p), flags(f){ subs.push_back(sub); }
\r
63 u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector<int>& v, match_flag_type f)
\r
64 : end(last), re(*p), flags(f), subs(v){}
\r
65 #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
\r
66 // can't reliably get this to work....
\r
67 #elif (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
\r
68 || BOOST_WORKAROUND(BOOST_MSVC, < 1300) \
\r
69 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \
\r
70 || BOOST_WORKAROUND(__HP_aCC, < 60700)
\r
72 u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const T& submatches, match_flag_type f)
\r
73 : end(last), re(*p), flags(f)
\r
75 // assert that T really is an array:
\r
76 BOOST_STATIC_ASSERT(::boost::is_array<T>::value);
\r
77 const std::size_t array_size = sizeof(T) / sizeof(submatches[0]);
\r
78 for(std::size_t i = 0; i < array_size; ++i)
\r
80 subs.push_back(submatches[i]);
\r
84 template <std::size_t CN>
\r
85 u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f)
\r
86 : end(last), re(*p), flags(f)
\r
88 for(std::size_t i = 0; i < CN; ++i)
\r
90 subs.push_back(submatches[i]);
\r
95 bool init(BidirectionalIterator first)
\r
99 if(u32regex_search(first, end, what, re, flags, base) == true)
\r
102 result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]);
\r
105 else if((subs[N] == -1) && (first != end))
\r
107 result.first = first;
\r
108 result.second = end;
\r
109 result.matched = (first != end);
\r
115 bool compare(const u32regex_token_iterator_implementation& that)
\r
117 if(this == &that) return true;
\r
118 return (&re.get_data() == &that.re.get_data())
\r
119 && (end == that.end)
\r
120 && (flags == that.flags)
\r
122 && (what[0].first == that.what[0].first)
\r
123 && (what[0].second == that.what[0].second);
\r
125 const value_type& get()
\r
131 if(N+1 < (int)subs.size())
\r
134 result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
\r
137 //if(what.prefix().first != what[0].second)
\r
138 // flags |= match_prev_avail | regex_constants::match_not_bob;
\r
139 BidirectionalIterator last_end(what[0].second);
\r
140 if(u32regex_search(last_end, end, what, re, ((what[0].first == what[0].second) ? flags | regex_constants::match_not_initial_null : flags), base))
\r
143 result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
\r
146 else if((last_end != end) && (subs[0] == -1))
\r
149 result.first = last_end;
\r
150 result.second = end;
\r
151 result.matched = (last_end != end);
\r
157 u32regex_token_iterator_implementation& operator=(const u32regex_token_iterator_implementation&);
\r
160 template <class BidirectionalIterator>
\r
161 class u32regex_token_iterator
\r
162 #ifndef BOOST_NO_STD_ITERATOR
\r
163 : public std::iterator<
\r
164 std::forward_iterator_tag,
\r
165 sub_match<BidirectionalIterator>,
\r
166 typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type,
\r
167 const sub_match<BidirectionalIterator>*,
\r
168 const sub_match<BidirectionalIterator>& >
\r
172 typedef u32regex_token_iterator_implementation<BidirectionalIterator> impl;
\r
173 typedef shared_ptr<impl> pimpl;
\r
175 typedef u32regex regex_type;
\r
176 typedef sub_match<BidirectionalIterator> value_type;
\r
177 typedef typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type
\r
179 typedef const value_type* pointer;
\r
180 typedef const value_type& reference;
\r
181 typedef std::forward_iterator_tag iterator_category;
\r
183 u32regex_token_iterator(){}
\r
184 u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
\r
185 int submatch = 0, match_flag_type m = match_default)
\r
186 : pdata(new impl(&re, b, submatch, m))
\r
188 if(!pdata->init(a))
\r
191 u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
\r
192 const std::vector<int>& submatches, match_flag_type m = match_default)
\r
193 : pdata(new impl(&re, b, submatches, m))
\r
195 if(!pdata->init(a))
\r
198 #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
\r
199 // can't reliably get this to work....
\r
200 #elif (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
\r
201 || BOOST_WORKAROUND(BOOST_MSVC, < 1300) \
\r
202 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \
\r
203 || BOOST_WORKAROUND(__HP_aCC, < 60700)
\r
205 u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
\r
206 const T& submatches, match_flag_type m = match_default)
\r
207 : pdata(new impl(&re, b, submatches, m))
\r
209 if(!pdata->init(a))
\r
213 template <std::size_t N>
\r
214 u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
\r
215 const int (&submatches)[N], match_flag_type m = match_default)
\r
216 : pdata(new impl(&re, b, submatches, m))
\r
218 if(!pdata->init(a))
\r
222 u32regex_token_iterator(const u32regex_token_iterator& that)
\r
223 : pdata(that.pdata) {}
\r
224 u32regex_token_iterator& operator=(const u32regex_token_iterator& that)
\r
226 pdata = that.pdata;
\r
229 bool operator==(const u32regex_token_iterator& that)const
\r
231 if((pdata.get() == 0) || (that.pdata.get() == 0))
\r
232 return pdata.get() == that.pdata.get();
\r
233 return pdata->compare(*(that.pdata.get()));
\r
235 bool operator!=(const u32regex_token_iterator& that)const
\r
236 { return !(*this == that); }
\r
237 const value_type& operator*()const
\r
238 { return pdata->get(); }
\r
239 const value_type* operator->()const
\r
240 { return &(pdata->get()); }
\r
241 u32regex_token_iterator& operator++()
\r
244 if(0 == pdata->next())
\r
250 u32regex_token_iterator operator++(int)
\r
252 u32regex_token_iterator result(*this);
\r
263 if(pdata.get() && !pdata.unique())
\r
265 pdata.reset(new impl(*(pdata.get())));
\r
270 typedef u32regex_token_iterator<const char*> utf8regex_token_iterator;
\r
271 typedef u32regex_token_iterator<const UChar*> utf16regex_token_iterator;
\r
272 typedef u32regex_token_iterator<const UChar32*> utf32regex_token_iterator;
\r
274 // construction from an integral sub_match state_id:
\r
275 inline u32regex_token_iterator<const char*> make_u32regex_token_iterator(const char* p, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
\r
277 return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
\r
279 #ifndef BOOST_NO_WREGEX
\r
280 inline u32regex_token_iterator<const wchar_t*> make_u32regex_token_iterator(const wchar_t* p, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
\r
282 return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
\r
285 #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2)
\r
286 inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const UChar* p, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
\r
288 return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
\r
291 template <class charT, class Traits, class Alloc>
\r
292 inline u32regex_token_iterator<typename std::basic_string<charT, Traits, Alloc>::const_iterator> make_u32regex_token_iterator(const std::basic_string<charT, Traits, Alloc>& p, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
\r
294 typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
\r
295 return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, m);
\r
297 inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const UnicodeString& s, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
\r
299 return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
\r
302 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
\r
303 // construction from a reference to an array:
\r
304 template <std::size_t N>
\r
305 inline u32regex_token_iterator<const char*> make_u32regex_token_iterator(const char* p, const u32regex& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
\r
307 return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
\r
309 #ifndef BOOST_NO_WREGEX
\r
310 template <std::size_t N>
\r
311 inline u32regex_token_iterator<const wchar_t*> make_u32regex_token_iterator(const wchar_t* p, const u32regex& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
\r
313 return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
\r
316 #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2)
\r
317 template <std::size_t N>
\r
318 inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const UChar* p, const u32regex& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
\r
320 return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, m);
\r
323 template <class charT, class Traits, class Alloc, std::size_t N>
\r
324 inline u32regex_token_iterator<typename std::basic_string<charT, Traits, Alloc>::const_iterator> make_u32regex_token_iterator(const std::basic_string<charT, Traits, Alloc>& p, const u32regex& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
\r
326 typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
\r
327 return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, m);
\r
329 template <std::size_t N>
\r
330 inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const UnicodeString& s, const u32regex& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
\r
332 return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
\r
334 #endif // BOOST_MSVC < 1300
\r
336 // construction from a vector of sub_match state_id's:
\r
337 inline u32regex_token_iterator<const char*> make_u32regex_token_iterator(const char* p, const u32regex& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
\r
339 return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
\r
341 #ifndef BOOST_NO_WREGEX
\r
342 inline u32regex_token_iterator<const wchar_t*> make_u32regex_token_iterator(const wchar_t* p, const u32regex& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
\r
344 return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
\r
347 #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2)
\r
348 inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const UChar* p, const u32regex& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
\r
350 return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
\r
353 template <class charT, class Traits, class Alloc>
\r
354 inline u32regex_token_iterator<typename std::basic_string<charT, Traits, Alloc>::const_iterator> make_u32regex_token_iterator(const std::basic_string<charT, Traits, Alloc>& p, const u32regex& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
\r
356 typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
\r
357 return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, m);
\r
359 inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const UnicodeString& s, const u32regex& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
\r
361 return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
\r
364 #if BOOST_WORKAROUND(BOOST_MSVC, == 1310)
\r
365 # pragma warning(pop)
\r
367 #ifdef BOOST_HAS_ABI_HEADERS
\r
368 # include BOOST_ABI_SUFFIX
\r
371 } // namespace boost
\r
373 #endif // BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP
\r