1 ///////////////////////////////////////////////////////////////////////////////
2 /// \file regex_token_iterator.hpp
3 /// Contains the definition of regex_token_iterator, and STL-compatible iterator
4 /// for tokenizing a string using a regular expression.
6 // Copyright 2008 Eric Niebler. Distributed under the Boost
7 // Software License, Version 1.0. (See accompanying file
8 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_XPRESSIVE_REGEX_TOKEN_ITERATOR_HPP_EAN_10_04_2005
11 #define BOOST_XPRESSIVE_REGEX_TOKEN_ITERATOR_HPP_EAN_10_04_2005
13 // MS compatible compilers support #pragma once
19 #include <boost/assert.hpp>
20 #include <boost/mpl/assert.hpp>
21 #include <boost/type_traits/is_same.hpp>
22 #include <boost/type_traits/is_convertible.hpp>
23 #include <boost/xpressive/regex_iterator.hpp>
25 namespace boost { namespace xpressive { namespace detail
28 //////////////////////////////////////////////////////////////////////////
29 // regex_token_iterator_impl
31 template<typename BidiIter>
32 struct regex_token_iterator_impl
33 : counted_base<regex_token_iterator_impl<BidiIter> >
35 typedef sub_match<BidiIter> value_type;
37 regex_token_iterator_impl
42 , BidiIter next_search
43 , basic_regex<BidiIter> const &rex
44 , regex_constants::match_flag_type flags = regex_constants::match_default
45 , std::vector<int> subs = std::vector<int>(1, 0)
47 , bool not_null = false
49 : iter_(begin, cur, end, next_search, rex, flags, not_null)
51 , n_((-2 == n) ? (int)subs.size() - 1 : n)
54 BOOST_ASSERT(0 != subs.size());
55 this->subs_.swap(subs);
62 BidiIter cur = this->iter_.state_.cur_;
63 if(0 != (++this->n_ %= (int)this->subs_.size()) || this->iter_.next())
65 this->result_ = (-1 == this->subs_[ this->n_ ])
66 ? this->iter_.what_.prefix()
67 : this->iter_.what_[ this->subs_[ this->n_ ] ];
70 else if(-1 == this->subs_[ this->n_-- ] && cur != this->iter_.state_.end_)
72 this->result_ = value_type(cur, this->iter_.state_.end_, true);
80 bool equal_to(regex_token_iterator_impl<BidiIter> const &that) const
82 return this->iter_.equal_to(that.iter_) && this->n_ == that.n_;
85 regex_iterator_impl<BidiIter> iter_;
88 std::vector<int> subs_;
91 inline int get_mark_number(int i)
96 inline std::vector<int> to_vector(int subs)
98 return std::vector<int>(1, subs);
101 inline std::vector<int> const &to_vector(std::vector<int> const &subs)
106 template<typename Int, std::size_t Size>
107 inline std::vector<int> to_vector(Int const (&sub_matches)[ Size ])
109 // so that people can specify sub-match indices inline with
110 // string literals, like "\1\2\3", leave off the trailing '\0'
111 std::size_t const size = Size - is_same<Int, char>::value;
112 std::vector<int> vect(size);
113 for(std::size_t i = 0; i < size; ++i)
115 vect[i] = get_mark_number(sub_matches[i]);
120 template<typename Int>
121 inline std::vector<int> to_vector(std::vector<Int> const &sub_matches)
123 BOOST_MPL_ASSERT((is_convertible<Int, int>));
124 return std::vector<int>(sub_matches.begin(), sub_matches.end());
127 } // namespace detail
129 //////////////////////////////////////////////////////////////////////////
130 // regex_token_iterator
132 template<typename BidiIter>
133 struct regex_token_iterator
135 typedef basic_regex<BidiIter> regex_type;
136 typedef typename iterator_value<BidiIter>::type char_type;
137 typedef sub_match<BidiIter> value_type;
138 typedef std::ptrdiff_t difference_type;
139 typedef value_type const *pointer;
140 typedef value_type const &reference;
141 typedef std::forward_iterator_tag iterator_category;
144 typedef detail::regex_token_iterator_impl<BidiIter> impl_type_;
146 /// \post \c *this is the end of sequence iterator.
147 regex_token_iterator()
152 /// \param begin The beginning of the character range to search.
153 /// \param end The end of the character range to search.
154 /// \param rex The regex pattern to search for.
155 /// \pre \c [begin,end) is a valid range.
160 , basic_regex<BidiIter> const &rex
164 if(0 != rex.regex_id())
166 this->impl_ = new impl_type_(begin, begin, end, begin, rex);
171 /// \param begin The beginning of the character range to search.
172 /// \param end The end of the character range to search.
173 /// \param rex The regex pattern to search for.
174 /// \param args A let() expression with argument bindings for semantic actions.
175 /// \pre \c [begin,end) is a valid range.
176 template<typename LetExpr>
181 , basic_regex<BidiIter> const &rex
182 , detail::let_<LetExpr> const &args
186 if(0 != rex.regex_id())
188 this->impl_ = new impl_type_(begin, begin, end, begin, rex);
189 detail::bind_args(args, this->impl_->iter_.what_);
194 /// \param begin The beginning of the character range to search.
195 /// \param end The end of the character range to search.
196 /// \param rex The regex pattern to search for.
197 /// \param subs A range of integers designating sub-matches to be treated as tokens.
198 /// \param flags Optional match flags, used to control how the expression is matched against the sequence. (See match_flag_type.)
199 /// \pre \c [begin,end) is a valid range.
200 /// \pre \c subs is either an integer greater or equal to -1,
201 /// or else an array or non-empty \c std::vector\<\> of such integers.
202 template<typename Subs>
207 , basic_regex<BidiIter> const &rex
209 , regex_constants::match_flag_type flags = regex_constants::match_default
213 if(0 != rex.regex_id())
215 this->impl_ = new impl_type_(begin, begin, end, begin, rex, flags, detail::to_vector(subs));
220 /// \param begin The beginning of the character range to search.
221 /// \param end The end of the character range to search.
222 /// \param rex The regex pattern to search for.
223 /// \param subs A range of integers designating sub-matches to be treated as tokens.
224 /// \param args A let() expression with argument bindings for semantic actions.
225 /// \param flags Optional match flags, used to control how the expression is matched against the sequence. (See match_flag_type.)
226 /// \pre \c [begin,end) is a valid range.
227 /// \pre \c subs is either an integer greater or equal to -1,
228 /// or else an array or non-empty \c std::vector\<\> of such integers.
229 template<typename Subs, typename LetExpr>
234 , basic_regex<BidiIter> const &rex
236 , detail::let_<LetExpr> const &args
237 , regex_constants::match_flag_type flags = regex_constants::match_default
241 if(0 != rex.regex_id())
243 this->impl_ = new impl_type_(begin, begin, end, begin, rex, flags, detail::to_vector(subs));
244 detail::bind_args(args, this->impl_->iter_.what_);
249 /// \post <tt>*this == that</tt>
250 regex_token_iterator(regex_token_iterator<BidiIter> const &that)
251 : impl_(that.impl_) // COW
255 /// \post <tt>*this == that</tt>
256 regex_token_iterator<BidiIter> &operator =(regex_token_iterator<BidiIter> const &that)
258 this->impl_ = that.impl_; // COW
262 friend bool operator ==(regex_token_iterator<BidiIter> const &left, regex_token_iterator<BidiIter> const &right)
264 if(!left.impl_ || !right.impl_)
266 return !left.impl_ && !right.impl_;
269 return left.impl_->equal_to(*right.impl_);
272 friend bool operator !=(regex_token_iterator<BidiIter> const &left, regex_token_iterator<BidiIter> const &right)
274 return !(left == right);
277 value_type const &operator *() const
279 return this->impl_->result_;
282 value_type const *operator ->() const
284 return &this->impl_->result_;
287 /// If N == -1 then sets *this equal to the end of sequence iterator.
288 /// Otherwise if N+1 \< subs.size(), then increments N and sets result equal to
289 /// ((subs[N] == -1) ? value_type(what.prefix().str()) : value_type(what[subs[N]].str())).
290 /// Otherwise if what.prefix().first != what[0].second and if the element match_prev_avail is
291 /// not set in flags then sets it. Then locates the next match as if by calling
292 /// regex_search(what[0].second, end, what, *pre, flags), with the following variation:
293 /// in the event that the previous match found was of zero length (what[0].length() == 0)
294 /// then attempts to find a non-zero length match starting at what[0].second, only if that
295 /// fails and provided what[0].second != suffix().second does it look for a (possibly zero
296 /// length) match starting from what[0].second + 1. If such a match is found then sets N
297 /// equal to zero, and sets result equal to
298 /// ((subs[N] == -1) ? value_type(what.prefix().str()) : value_type(what[subs[N]].str())).
299 /// Otherwise if no further matches were found, then let last_end be the endpoint of the last
300 /// match that was found. Then if last_end != end and subs[0] == -1 sets N equal to -1 and
301 /// sets result equal to value_type(last_end, end). Otherwise sets *this equal to the end
302 /// of sequence iterator.
303 regex_token_iterator<BidiIter> &operator ++()
305 this->fork_(); // un-share the implementation
310 regex_token_iterator<BidiIter> operator ++(int)
312 regex_token_iterator<BidiIter> tmp(*this);
322 if(1 != this->impl_->use_count())
324 intrusive_ptr<impl_type_> clone = new impl_type_
326 this->impl_->iter_.state_.begin_
327 , this->impl_->iter_.state_.cur_
328 , this->impl_->iter_.state_.end_
329 , this->impl_->iter_.state_.next_search_
330 , this->impl_->iter_.rex_
331 , this->impl_->iter_.flags_
334 , this->impl_->iter_.not_null_
337 // only copy the match_results struct if we have to. Note: if the next call
338 // to impl_->next() will return false or call regex_search, we don't need to
339 // copy the match_results struct.
340 if(-1 != this->impl_->n_ && this->impl_->n_ + 1 != static_cast<int>(this->impl_->subs_.size()))
342 // BUGBUG This is expensive -- it causes the sequence_stack to be cleared.
344 clone->iter_.what_ = this->impl_->iter_.what_;
348 // At the very least, copy the action args
349 detail::core_access<BidiIter>::get_action_args(clone->iter_.what_)
350 = detail::core_access<BidiIter>::get_action_args(this->impl_->iter_.what_);
353 this->impl_.swap(clone);
360 BOOST_ASSERT(this->impl_ && 1 == this->impl_->use_count());
361 if(!this->impl_->next())
367 intrusive_ptr<impl_type_> impl_;
370 }} // namespace boost::xpressive