3 * Copyright (c) 1998-2009
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 match_results.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Declares template class match_results.
19 #ifndef BOOST_REGEX_V5_MATCH_RESULTS_HPP
20 #define BOOST_REGEX_V5_MATCH_RESULTS_HPP
23 #ifdef BOOST_REGEX_MSVC
25 #pragma warning(disable : 4251 4459)
26 #if BOOST_REGEX_MSVC < 1700
27 # pragma warning(disable : 4231)
29 # if BOOST_REGEX_MSVC < 1600
30 # pragma warning(disable : 4660)
34 namespace BOOST_REGEX_DETAIL_NS{
36 class named_subexpressions;
40 template <class BidiIterator, class Allocator>
44 typedef std::vector<sub_match<BidiIterator>, Allocator> vector_type;
46 typedef sub_match<BidiIterator> value_type;
47 typedef typename std::allocator_traits<Allocator>::value_type const & const_reference;
48 typedef const_reference reference;
49 typedef typename vector_type::const_iterator const_iterator;
50 typedef const_iterator iterator;
51 typedef typename std::iterator_traits<
52 BidiIterator>::difference_type difference_type;
53 typedef typename std::allocator_traits<Allocator>::size_type size_type;
54 typedef Allocator allocator_type;
55 typedef typename std::iterator_traits<
56 BidiIterator>::value_type char_type;
57 typedef std::basic_string<char_type> string_type;
58 typedef BOOST_REGEX_DETAIL_NS::named_subexpressions named_sub_type;
60 // construct/copy/destroy:
61 explicit match_results(const Allocator& a = Allocator())
62 : m_subs(a), m_base(), m_null(), m_last_closed_paren(0), m_is_singular(true) {}
64 // IMPORTANT: in the code below, the crazy looking checks around m_is_singular are
65 // all required because it is illegal to copy a singular iterator.
66 // See https://svn.boost.org/trac/boost/ticket/3632.
68 match_results(const match_results& m)
69 : m_subs(m.m_subs), m_base(), m_null(), m_named_subs(m.m_named_subs), m_last_closed_paren(m.m_last_closed_paren), m_is_singular(m.m_is_singular)
77 match_results& operator=(const match_results& m)
80 m_named_subs = m.m_named_subs;
81 m_last_closed_paren = m.m_last_closed_paren;
82 m_is_singular = m.m_is_singular;
93 size_type size() const
94 { return empty() ? 0 : m_subs.size() - 2; }
95 size_type max_size() const
96 { return m_subs.max_size(); }
98 { return m_subs.size() < 2; }
100 difference_type length(int sub = 0) const
105 if((sub < (int)m_subs.size()) && (sub > 0))
106 return m_subs[sub].length();
109 difference_type length(const char_type* sub) const
113 const char_type* sub_end = sub;
114 while(*sub_end) ++sub_end;
115 return length(named_subexpression_index(sub, sub_end));
117 template <class charT>
118 difference_type length(const charT* sub) const
122 const charT* sub_end = sub;
123 while(*sub_end) ++sub_end;
124 return length(named_subexpression_index(sub, sub_end));
126 template <class charT, class Traits, class A>
127 difference_type length(const std::basic_string<charT, Traits, A>& sub) const
129 return length(sub.c_str());
131 difference_type position(size_type sub = 0) const
136 if(sub < m_subs.size())
138 const sub_match<BidiIterator>& s = m_subs[sub];
139 if(s.matched || (sub == 2))
141 return std::distance((BidiIterator)(m_base), (BidiIterator)(s.first));
144 return ~static_cast<difference_type>(0);
146 difference_type position(const char_type* sub) const
148 const char_type* sub_end = sub;
149 while(*sub_end) ++sub_end;
150 return position(named_subexpression_index(sub, sub_end));
152 template <class charT>
153 difference_type position(const charT* sub) const
155 const charT* sub_end = sub;
156 while(*sub_end) ++sub_end;
157 return position(named_subexpression_index(sub, sub_end));
159 template <class charT, class Traits, class A>
160 difference_type position(const std::basic_string<charT, Traits, A>& sub) const
162 return position(sub.c_str());
164 string_type str(int sub = 0) const
170 if(sub < (int)m_subs.size() && (sub > 0))
172 const sub_match<BidiIterator>& s = m_subs[sub];
180 string_type str(const char_type* sub) const
182 return (*this)[sub].str();
184 template <class Traits, class A>
185 string_type str(const std::basic_string<char_type, Traits, A>& sub) const
187 return (*this)[sub].str();
189 template <class charT>
190 string_type str(const charT* sub) const
192 return (*this)[sub].str();
194 template <class charT, class Traits, class A>
195 string_type str(const std::basic_string<charT, Traits, A>& sub) const
197 return (*this)[sub].str();
199 const_reference operator[](int sub) const
201 if(m_is_singular && m_subs.empty())
204 if(sub < (int)m_subs.size() && (sub >= 0))
211 // Named sub-expressions:
213 const_reference named_subexpression(const char_type* i, const char_type* j) const
216 // Scan for the leftmost *matched* subexpression with the specified named:
220 BOOST_REGEX_DETAIL_NS::named_subexpressions::range_type r = m_named_subs->equal_range(i, j);
221 while((r.first != r.second) && ((*this)[r.first->index].matched == false))
223 return r.first != r.second ? (*this)[r.first->index] : m_null;
225 template <class charT>
226 const_reference named_subexpression(const charT* i, const charT* j) const
228 static_assert(sizeof(charT) <= sizeof(char_type), "Failed internal logic");
231 std::vector<char_type> s;
233 s.insert(s.end(), *i++);
234 return named_subexpression(&*s.begin(), &*s.begin() + s.size());
236 int named_subexpression_index(const char_type* i, const char_type* j) const
239 // Scan for the leftmost *matched* subexpression with the specified named.
240 // If none found then return the leftmost expression with that name,
241 // otherwise an invalid index:
245 BOOST_REGEX_DETAIL_NS::named_subexpressions::range_type s, r;
246 s = r = m_named_subs->equal_range(i, j);
247 while((r.first != r.second) && ((*this)[r.first->index].matched == false))
249 if(r.first == r.second)
251 return r.first != r.second ? r.first->index : -20;
253 template <class charT>
254 int named_subexpression_index(const charT* i, const charT* j) const
256 static_assert(sizeof(charT) <= sizeof(char_type), "Failed internal logic");
259 std::vector<char_type> s;
261 s.insert(s.end(), *i++);
262 return named_subexpression_index(&*s.begin(), &*s.begin() + s.size());
264 template <class Traits, class A>
265 const_reference operator[](const std::basic_string<char_type, Traits, A>& s) const
267 return named_subexpression(s.c_str(), s.c_str() + s.size());
269 const_reference operator[](const char_type* p) const
271 const char_type* e = p;
273 return named_subexpression(p, e);
276 template <class charT>
277 const_reference operator[](const charT* p) const
279 static_assert(sizeof(charT) <= sizeof(char_type), "Failed internal logic");
282 std::vector<char_type> s;
284 s.insert(s.end(), *p++);
285 return named_subexpression(&*s.begin(), &*s.begin() + s.size());
287 template <class charT, class Traits, class A>
288 const_reference operator[](const std::basic_string<charT, Traits, A>& ns) const
290 static_assert(sizeof(charT) <= sizeof(char_type), "Failed internal logic");
293 std::vector<char_type> s;
294 for(unsigned i = 0; i < ns.size(); ++i)
295 s.insert(s.end(), ns[i]);
296 return named_subexpression(&*s.begin(), &*s.begin() + s.size());
299 const_reference prefix() const
306 const_reference suffix() const
312 const_iterator begin() const
314 return (m_subs.size() > 2) ? (m_subs.begin() + 2) : m_subs.end();
316 const_iterator end() const
321 template <class OutputIterator, class Functor>
322 OutputIterator format(OutputIterator out,
324 match_flag_type flags = format_default) const
328 typedef typename BOOST_REGEX_DETAIL_NS::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, OutputIterator>::type F;
330 return func(*this, out, flags);
332 template <class Functor>
333 string_type format(Functor fmt, match_flag_type flags = format_default) const
337 std::basic_string<char_type> result;
338 BOOST_REGEX_DETAIL_NS::string_out_iterator<std::basic_string<char_type> > i(result);
340 typedef typename BOOST_REGEX_DETAIL_NS::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, BOOST_REGEX_DETAIL_NS::string_out_iterator<std::basic_string<char_type> > >::type F;
343 func(*this, i, flags);
346 // format with locale:
347 template <class OutputIterator, class Functor, class RegexT>
348 OutputIterator format(OutputIterator out,
350 match_flag_type flags,
351 const RegexT& re) const
355 typedef ::boost::regex_traits_wrapper<typename RegexT::traits_type> traits_type;
356 typedef typename BOOST_REGEX_DETAIL_NS::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, OutputIterator, traits_type>::type F;
358 return func(*this, out, flags, re.get_traits());
360 template <class RegexT, class Functor>
361 string_type format(Functor fmt,
362 match_flag_type flags,
363 const RegexT& re) const
367 typedef ::boost::regex_traits_wrapper<typename RegexT::traits_type> traits_type;
368 std::basic_string<char_type> result;
369 BOOST_REGEX_DETAIL_NS::string_out_iterator<std::basic_string<char_type> > i(result);
371 typedef typename BOOST_REGEX_DETAIL_NS::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, BOOST_REGEX_DETAIL_NS::string_out_iterator<std::basic_string<char_type> >, traits_type >::type F;
374 func(*this, i, flags, re.get_traits());
378 const_reference get_last_closed_paren()const
382 return m_last_closed_paren == 0 ? m_null : (*this)[m_last_closed_paren];
385 allocator_type get_allocator() const
387 return m_subs.get_allocator();
389 void swap(match_results& that)
391 std::swap(m_subs, that.m_subs);
392 std::swap(m_named_subs, that.m_named_subs);
393 std::swap(m_last_closed_paren, that.m_last_closed_paren);
396 if(!that.m_is_singular)
398 m_base = that.m_base;
399 m_null = that.m_null;
402 else if(that.m_is_singular)
404 that.m_base = m_base;
405 that.m_null = m_null;
409 std::swap(m_base, that.m_base);
410 std::swap(m_null, that.m_null);
412 std::swap(m_is_singular, that.m_is_singular);
414 bool operator==(const match_results& that)const
418 return that.m_is_singular;
420 else if(that.m_is_singular)
424 return (m_subs == that.m_subs) && (m_base == that.m_base) && (m_last_closed_paren == that.m_last_closed_paren);
426 bool operator!=(const match_results& that)const
427 { return !(*this == that); }
429 #ifdef BOOST_REGEX_MATCH_EXTRA
430 typedef typename sub_match<BidiIterator>::capture_sequence_type capture_sequence_type;
432 const capture_sequence_type& captures(int i)const
436 return (*this)[i].captures();
441 // private access functions:
442 void set_second(BidiIterator i)
444 BOOST_REGEX_ASSERT(m_subs.size() > 2);
445 m_subs[2].second = i;
446 m_subs[2].matched = true;
448 m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
451 m_null.matched = false;
452 m_is_singular = false;
455 void set_second(BidiIterator i, size_type pos, bool m = true, bool escape_k = false)
458 m_last_closed_paren = static_cast<int>(pos);
460 BOOST_REGEX_ASSERT(m_subs.size() > pos);
461 m_subs[pos].second = i;
462 m_subs[pos].matched = m;
463 if((pos == 2) && !escape_k)
466 m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
469 m_null.matched = false;
470 m_is_singular = false;
473 void set_size(size_type n, BidiIterator i, BidiIterator j)
476 size_type len = m_subs.size();
479 m_subs.erase(m_subs.begin()+n+2, m_subs.end());
480 std::fill(m_subs.begin(), m_subs.end(), v);
484 std::fill(m_subs.begin(), m_subs.end(), v);
486 m_subs.insert(m_subs.end(), n+2-len, v);
489 m_last_closed_paren = 0;
491 void set_base(BidiIterator pos)
495 BidiIterator base()const
499 void set_first(BidiIterator i)
501 BOOST_REGEX_ASSERT(m_subs.size() > 2);
503 m_subs[1].second = i;
504 m_subs[1].matched = (m_subs[1].first != i);
507 // zero out everything else:
508 for(size_type n = 3; n < m_subs.size(); ++n)
510 m_subs[n].first = m_subs[n].second = m_subs[0].second;
511 m_subs[n].matched = false;
514 void set_first(BidiIterator i, size_type pos, bool escape_k = false)
516 BOOST_REGEX_ASSERT(pos+2 < m_subs.size());
519 m_subs[pos+2].first = i;
522 m_subs[1].second = i;
523 m_subs[1].matched = (m_subs[1].first != m_subs[1].second);
529 void maybe_assign(const match_results<BidiIterator, Allocator>& m);
531 void set_named_subs(std::shared_ptr<named_sub_type> subs)
538 // Error handler called when an uninitialized match_results is accessed:
540 static void raise_logic_error()
542 std::logic_error e("Attempt to access an uninitialized boost::match_results<> class.");
543 #ifndef BOOST_REGEX_STANDALONE
544 boost::throw_exception(e);
551 vector_type m_subs; // subexpressions
552 BidiIterator m_base; // where the search started from
553 sub_match<BidiIterator> m_null; // a null match
554 std::shared_ptr<named_sub_type> m_named_subs; // Shared copy of named subs in the regex object
555 int m_last_closed_paren; // Last ) to be seen - used for formatting
556 bool m_is_singular; // True if our stored iterators are singular
559 template <class BidiIterator, class Allocator>
560 void match_results<BidiIterator, Allocator>::maybe_assign(const match_results<BidiIterator, Allocator>& m)
567 const_iterator p1, p2;
571 // Distances are measured from the start of *this* match, unless this isn't
572 // a valid match in which case we use the start of the whole sequence. Note that
573 // no subsequent match-candidate can ever be to the left of the first match found.
574 // This ensures that when we are using bidirectional iterators, that distances
575 // measured are as short as possible, and therefore as efficient as possible
576 // to compute. Finally note that we don't use the "matched" data member to test
577 // whether a sub-expression is a valid match, because partial matches set this
578 // to false for sub-expression 0.
580 BidiIterator l_end = this->suffix().second;
581 BidiIterator l_base = (p1->first == l_end) ? this->prefix().first : (*this)[0].first;
582 difference_type len1 = 0;
583 difference_type len2 = 0;
584 difference_type base1 = 0;
585 difference_type base2 = 0;
587 for(i = 0; i < size(); ++i, ++p1, ++p2)
590 // Leftmost takes priority over longest; handle special cases
591 // where distances need not be computed first (an optimisation
592 // for bidirectional iterators: ensure that we don't accidently
593 // compute the length of the whole sequence, as this can be really
596 if(p1->first == l_end)
598 if(p2->first != l_end)
600 // p2 must be better than p1, and no need to calculate
608 // *p1 and *p2 are either unmatched or match end-of sequence,
609 // either way no need to calculate distances:
610 if((p1->matched == false) && (p2->matched == true))
612 if((p1->matched == true) && (p2->matched == false))
617 else if(p2->first == l_end)
619 // p1 better than p2, and no need to calculate distances:
622 base1 = std::distance(l_base, p1->first);
623 base2 = std::distance(l_base, p2->first);
624 BOOST_REGEX_ASSERT(base1 >= 0);
625 BOOST_REGEX_ASSERT(base2 >= 0);
626 if(base1 < base2) return;
627 if(base2 < base1) break;
629 len1 = std::distance((BidiIterator)p1->first, (BidiIterator)p1->second);
630 len2 = std::distance((BidiIterator)p2->first, (BidiIterator)p2->second);
631 BOOST_REGEX_ASSERT(len1 >= 0);
632 BOOST_REGEX_ASSERT(len2 >= 0);
633 if((len1 != len2) || ((p1->matched == false) && (p2->matched == true)))
635 if((p1->matched == true) && (p2->matched == false))
642 else if((len2 > len1) || ((p1->matched == false) && (p2->matched == true)) )
646 template <class BidiIterator, class Allocator>
647 void swap(match_results<BidiIterator, Allocator>& a, match_results<BidiIterator, Allocator>& b)
652 template <class charT, class traits, class BidiIterator, class Allocator>
653 std::basic_ostream<charT, traits>&
654 operator << (std::basic_ostream<charT, traits>& os,
655 const match_results<BidiIterator, Allocator>& s)
657 return (os << s.str());
660 #ifdef BOOST_REGEX_MSVC