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)
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.
19 #ifndef BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP
20 #define BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP
22 #include <boost/shared_ptr.hpp>
23 #include <boost/detail/workaround.hpp>
24 #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
25 || BOOST_WORKAROUND(BOOST_MSVC, < 1300) \
26 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
28 // Borland C++ Builder 6, and Visual C++ 6,
29 // can't cope with the array template constructor
30 // so we have a template member that will accept any type as
31 // argument, and then assert that is really is an array:
33 #include <boost/static_assert.hpp>
34 #include <boost/type_traits/is_array.hpp>
41 #pragma warning(disable: 4103)
43 #ifdef BOOST_HAS_ABI_HEADERS
44 # include BOOST_ABI_PREFIX
49 #if BOOST_WORKAROUND(BOOST_MSVC, > 1300)
50 # pragma warning(push)
51 # pragma warning(disable:4700)
54 template <class BidirectionalIterator,
57 class regex_token_iterator_implementation
59 typedef basic_regex<charT, traits> regex_type;
60 typedef sub_match<BidirectionalIterator> value_type;
62 match_results<BidirectionalIterator> what; // current match
63 BidirectionalIterator base; // start of search area
64 BidirectionalIterator end; // end of search area
65 const regex_type re; // the expression
66 match_flag_type flags; // match flags
67 value_type result; // the current string result
68 int N; // the current sub-expression being enumerated
69 std::vector<int> subs; // the sub-expressions to enumerate
72 regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f)
73 : end(last), re(*p), flags(f){ subs.push_back(sub); }
74 regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector<int>& v, match_flag_type f)
75 : end(last), re(*p), flags(f), subs(v){}
76 #if !BOOST_WORKAROUND(__HP_aCC, < 60700)
77 #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
78 || BOOST_WORKAROUND(BOOST_MSVC, < 1300) \
79 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \
80 || BOOST_WORKAROUND(__HP_aCC, < 60700)
82 regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const T& submatches, match_flag_type f)
83 : end(last), re(*p), flags(f)
85 // assert that T really is an array:
86 BOOST_STATIC_ASSERT(::boost::is_array<T>::value);
87 const std::size_t array_size = sizeof(T) / sizeof(submatches[0]);
88 for(std::size_t i = 0; i < array_size; ++i)
90 subs.push_back(submatches[i]);
94 template <std::size_t CN>
95 regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f)
96 : end(last), re(*p), flags(f)
98 for(std::size_t i = 0; i < CN; ++i)
100 subs.push_back(submatches[i]);
105 bool init(BidirectionalIterator first)
109 if(regex_search(first, end, what, re, flags, base) == true)
112 result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]);
115 else if((subs[N] == -1) && (first != end))
117 result.first = first;
119 result.matched = (first != end);
125 bool compare(const regex_token_iterator_implementation& that)
127 if(this == &that) return true;
128 return (&re.get_data() == &that.re.get_data())
130 && (flags == that.flags)
132 && (what[0].first == that.what[0].first)
133 && (what[0].second == that.what[0].second);
135 const value_type& get()
141 if(N+1 < (int)subs.size())
144 result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
147 //if(what.prefix().first != what[0].second)
148 // flags |= /*match_prev_avail |*/ regex_constants::match_not_bob;
149 BidirectionalIterator last_end(what[0].second);
150 if(regex_search(last_end, end, what, re, ((what[0].first == what[0].second) ? flags | regex_constants::match_not_initial_null : flags), base))
153 result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
156 else if((last_end != end) && (subs[0] == -1))
159 result.first = last_end;
161 result.matched = (last_end != end);
167 regex_token_iterator_implementation& operator=(const regex_token_iterator_implementation&);
170 template <class BidirectionalIterator,
171 class charT = BOOST_DEDUCED_TYPENAME re_detail::regex_iterator_traits<BidirectionalIterator>::value_type,
172 class traits = regex_traits<charT> >
173 class regex_token_iterator
174 #ifndef BOOST_NO_STD_ITERATOR
175 : public std::iterator<
176 std::forward_iterator_tag,
177 sub_match<BidirectionalIterator>,
178 typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type,
179 const sub_match<BidirectionalIterator>*,
180 const sub_match<BidirectionalIterator>& >
184 typedef regex_token_iterator_implementation<BidirectionalIterator, charT, traits> impl;
185 typedef shared_ptr<impl> pimpl;
187 typedef basic_regex<charT, traits> regex_type;
188 typedef sub_match<BidirectionalIterator> value_type;
189 typedef typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type
191 typedef const value_type* pointer;
192 typedef const value_type& reference;
193 typedef std::forward_iterator_tag iterator_category;
195 regex_token_iterator(){}
196 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
197 int submatch = 0, match_flag_type m = match_default)
198 : pdata(new impl(&re, b, submatch, m))
203 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
204 const std::vector<int>& submatches, match_flag_type m = match_default)
205 : pdata(new impl(&re, b, submatches, m))
210 #if !BOOST_WORKAROUND(__HP_aCC, < 60700)
211 #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
212 || BOOST_WORKAROUND(BOOST_MSVC, < 1300) \
213 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \
214 || BOOST_WORKAROUND(__HP_aCC, < 60700)
216 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
217 const T& submatches, match_flag_type m = match_default)
218 : pdata(new impl(&re, b, submatches, m))
224 template <std::size_t N>
225 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
226 const int (&submatches)[N], match_flag_type m = match_default)
227 : pdata(new impl(&re, b, submatches, m))
234 regex_token_iterator(const regex_token_iterator& that)
235 : pdata(that.pdata) {}
236 regex_token_iterator& operator=(const regex_token_iterator& that)
241 bool operator==(const regex_token_iterator& that)const
243 if((pdata.get() == 0) || (that.pdata.get() == 0))
244 return pdata.get() == that.pdata.get();
245 return pdata->compare(*(that.pdata.get()));
247 bool operator!=(const regex_token_iterator& that)const
248 { return !(*this == that); }
249 const value_type& operator*()const
250 { return pdata->get(); }
251 const value_type* operator->()const
252 { return &(pdata->get()); }
253 regex_token_iterator& operator++()
256 if(0 == pdata->next())
262 regex_token_iterator operator++(int)
264 regex_token_iterator result(*this);
275 if(pdata.get() && !pdata.unique())
277 pdata.reset(new impl(*(pdata.get())));
282 typedef regex_token_iterator<const char*> cregex_token_iterator;
283 typedef regex_token_iterator<std::string::const_iterator> sregex_token_iterator;
284 #ifndef BOOST_NO_WREGEX
285 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
286 typedef regex_token_iterator<std::wstring::const_iterator> wsregex_token_iterator;
289 template <class charT, class traits>
290 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)
292 return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
294 template <class charT, class traits, class ST, class SA>
295 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)
297 return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
299 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
300 template <class charT, class traits, std::size_t N>
301 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)
303 return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
305 template <class charT, class traits, class ST, class SA, std::size_t N>
306 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)
308 return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
311 template <class charT, class traits>
312 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)
314 return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
316 template <class charT, class traits, class ST, class SA>
317 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)
319 return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
322 #if BOOST_WORKAROUND(BOOST_MSVC, > 1300)
323 # pragma warning(pop)
326 #pragma warning(push)
327 #pragma warning(disable: 4103)
329 #ifdef BOOST_HAS_ABI_HEADERS
330 # include BOOST_ABI_SUFFIX
338 #endif // BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP