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_V4_MATCH_RESULTS_HPP
20 #define BOOST_REGEX_V4_MATCH_RESULTS_HPP
24 #pragma warning(disable: 4103)
26 #ifdef BOOST_HAS_ABI_HEADERS
27 # include BOOST_ABI_PREFIX
36 #pragma warning(disable : 4251)
38 # pragma warning(disable : 4231)
40 # if BOOST_MSVC < 1600
41 # pragma warning(disable : 4660)
45 namespace BOOST_REGEX_DETAIL_NS{
47 class named_subexpressions;
51 template <class BidiIterator, class Allocator>
55 #ifndef BOOST_NO_STD_ALLOCATOR
56 typedef std::vector<sub_match<BidiIterator>, Allocator> vector_type;
58 typedef std::vector<sub_match<BidiIterator> > vector_type;
61 typedef sub_match<BidiIterator> value_type;
62 #ifndef BOOST_NO_CXX11_ALLOCATOR
63 typedef typename std::allocator_traits<Allocator>::value_type const & const_reference;
64 #elif !defined(BOOST_NO_STD_ALLOCATOR) && !(defined(BOOST_MSVC) && defined(_STLPORT_VERSION))
65 typedef typename Allocator::const_reference const_reference;
67 typedef const value_type& const_reference;
69 typedef const_reference reference;
70 typedef typename vector_type::const_iterator const_iterator;
71 typedef const_iterator iterator;
72 typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<
73 BidiIterator>::difference_type difference_type;
74 #ifdef BOOST_NO_CXX11_ALLOCATOR
75 typedef typename Allocator::size_type size_type;
77 typedef typename std::allocator_traits<Allocator>::size_type size_type;
79 typedef Allocator allocator_type;
80 typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<
81 BidiIterator>::value_type char_type;
82 typedef std::basic_string<char_type> string_type;
83 typedef BOOST_REGEX_DETAIL_NS::named_subexpressions named_sub_type;
85 // construct/copy/destroy:
86 explicit match_results(const Allocator& a = Allocator())
87 #ifndef BOOST_NO_STD_ALLOCATOR
88 : m_subs(a), m_base(), m_null(), m_last_closed_paren(0), m_is_singular(true) {}
90 : m_subs(), m_base(), m_null(), m_last_closed_paren(0), m_is_singular(true) { (void)a; }
93 // IMPORTANT: in the code below, the crazy looking checks around m_is_singular are
94 // all required because it is illegal to copy a singular iterator.
95 // See https://svn.boost.org/trac/boost/ticket/3632.
97 match_results(const match_results& m)
98 : m_subs(m.m_subs), m_named_subs(m.m_named_subs), m_last_closed_paren(m.m_last_closed_paren), m_is_singular(m.m_is_singular)
106 match_results& operator=(const match_results& m)
109 m_named_subs = m.m_named_subs;
110 m_last_closed_paren = m.m_last_closed_paren;
111 m_is_singular = m.m_is_singular;
122 size_type size() const
123 { return empty() ? 0 : m_subs.size() - 2; }
124 size_type max_size() const
125 { return m_subs.max_size(); }
127 { return m_subs.size() < 2; }
129 difference_type length(int sub = 0) const
134 if((sub < (int)m_subs.size()) && (sub > 0))
135 return m_subs[sub].length();
138 difference_type length(const char_type* sub) const
142 const char_type* sub_end = sub;
143 while(*sub_end) ++sub_end;
144 return length(named_subexpression_index(sub, sub_end));
146 template <class charT>
147 difference_type length(const charT* sub) const
151 const charT* sub_end = sub;
152 while(*sub_end) ++sub_end;
153 return length(named_subexpression_index(sub, sub_end));
155 template <class charT, class Traits, class A>
156 difference_type length(const std::basic_string<charT, Traits, A>& sub) const
158 return length(sub.c_str());
160 difference_type position(size_type sub = 0) const
165 if(sub < m_subs.size())
167 const sub_match<BidiIterator>& s = m_subs[sub];
168 if(s.matched || (sub == 2))
170 return ::boost::BOOST_REGEX_DETAIL_NS::distance((BidiIterator)(m_base), (BidiIterator)(s.first));
173 return ~static_cast<difference_type>(0);
175 difference_type position(const char_type* sub) const
177 const char_type* sub_end = sub;
178 while(*sub_end) ++sub_end;
179 return position(named_subexpression_index(sub, sub_end));
181 template <class charT>
182 difference_type position(const charT* sub) const
184 const charT* sub_end = sub;
185 while(*sub_end) ++sub_end;
186 return position(named_subexpression_index(sub, sub_end));
188 template <class charT, class Traits, class A>
189 difference_type position(const std::basic_string<charT, Traits, A>& sub) const
191 return position(sub.c_str());
193 string_type str(int sub = 0) const
199 if(sub < (int)m_subs.size() && (sub > 0))
201 const sub_match<BidiIterator>& s = m_subs[sub];
209 string_type str(const char_type* sub) const
211 return (*this)[sub].str();
213 template <class Traits, class A>
214 string_type str(const std::basic_string<char_type, Traits, A>& sub) const
216 return (*this)[sub].str();
218 template <class charT>
219 string_type str(const charT* sub) const
221 return (*this)[sub].str();
223 template <class charT, class Traits, class A>
224 string_type str(const std::basic_string<charT, Traits, A>& sub) const
226 return (*this)[sub].str();
228 const_reference operator[](int sub) const
230 if(m_is_singular && m_subs.empty())
233 if(sub < (int)m_subs.size() && (sub >= 0))
240 // Named sub-expressions:
242 const_reference named_subexpression(const char_type* i, const char_type* j) const
245 // Scan for the leftmost *matched* subexpression with the specified named:
249 BOOST_REGEX_DETAIL_NS::named_subexpressions::range_type r = m_named_subs->equal_range(i, j);
250 while((r.first != r.second) && ((*this)[r.first->index].matched == false))
252 return r.first != r.second ? (*this)[r.first->index] : m_null;
254 template <class charT>
255 const_reference named_subexpression(const charT* i, const charT* j) const
257 BOOST_STATIC_ASSERT(sizeof(charT) <= sizeof(char_type));
260 std::vector<char_type> s;
262 s.insert(s.end(), *i++);
263 return named_subexpression(&*s.begin(), &*s.begin() + s.size());
265 int named_subexpression_index(const char_type* i, const char_type* j) const
268 // Scan for the leftmost *matched* subexpression with the specified named.
269 // If none found then return the leftmost expression with that name,
270 // otherwise an invalid index:
274 BOOST_REGEX_DETAIL_NS::named_subexpressions::range_type s, r;
275 s = r = m_named_subs->equal_range(i, j);
276 while((r.first != r.second) && ((*this)[r.first->index].matched == false))
278 if(r.first == r.second)
280 return r.first != r.second ? r.first->index : -20;
282 template <class charT>
283 int named_subexpression_index(const charT* i, const charT* j) const
285 BOOST_STATIC_ASSERT(sizeof(charT) <= sizeof(char_type));
288 std::vector<char_type> s;
290 s.insert(s.end(), *i++);
291 return named_subexpression_index(&*s.begin(), &*s.begin() + s.size());
293 template <class Traits, class A>
294 const_reference operator[](const std::basic_string<char_type, Traits, A>& s) const
296 return named_subexpression(s.c_str(), s.c_str() + s.size());
298 const_reference operator[](const char_type* p) const
300 const char_type* e = p;
302 return named_subexpression(p, e);
305 template <class charT>
306 const_reference operator[](const charT* p) const
308 BOOST_STATIC_ASSERT(sizeof(charT) <= sizeof(char_type));
311 std::vector<char_type> s;
313 s.insert(s.end(), *p++);
314 return named_subexpression(&*s.begin(), &*s.begin() + s.size());
316 template <class charT, class Traits, class A>
317 const_reference operator[](const std::basic_string<charT, Traits, A>& ns) const
319 BOOST_STATIC_ASSERT(sizeof(charT) <= sizeof(char_type));
322 std::vector<char_type> s;
323 for(unsigned i = 0; i < ns.size(); ++i)
324 s.insert(s.end(), ns[i]);
325 return named_subexpression(&*s.begin(), &*s.begin() + s.size());
328 const_reference prefix() const
335 const_reference suffix() const
341 const_iterator begin() const
343 return (m_subs.size() > 2) ? (m_subs.begin() + 2) : m_subs.end();
345 const_iterator end() const
350 template <class OutputIterator, class Functor>
351 OutputIterator format(OutputIterator out,
353 match_flag_type flags = format_default) const
357 typedef typename BOOST_REGEX_DETAIL_NS::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, OutputIterator>::type F;
359 return func(*this, out, flags);
361 template <class Functor>
362 string_type format(Functor fmt, match_flag_type flags = format_default) const
366 std::basic_string<char_type> result;
367 BOOST_REGEX_DETAIL_NS::string_out_iterator<std::basic_string<char_type> > i(result);
369 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;
372 func(*this, i, flags);
375 // format with locale:
376 template <class OutputIterator, class Functor, class RegexT>
377 OutputIterator format(OutputIterator out,
379 match_flag_type flags,
380 const RegexT& re) const
384 typedef ::boost::regex_traits_wrapper<typename RegexT::traits_type> traits_type;
385 typedef typename BOOST_REGEX_DETAIL_NS::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, OutputIterator, traits_type>::type F;
387 return func(*this, out, flags, re.get_traits());
389 template <class RegexT, class Functor>
390 string_type format(Functor fmt,
391 match_flag_type flags,
392 const RegexT& re) const
396 typedef ::boost::regex_traits_wrapper<typename RegexT::traits_type> traits_type;
397 std::basic_string<char_type> result;
398 BOOST_REGEX_DETAIL_NS::string_out_iterator<std::basic_string<char_type> > i(result);
400 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;
403 func(*this, i, flags, re.get_traits());
407 const_reference get_last_closed_paren()const
411 return m_last_closed_paren == 0 ? m_null : (*this)[m_last_closed_paren];
414 allocator_type get_allocator() const
416 #ifndef BOOST_NO_STD_ALLOCATOR
417 return m_subs.get_allocator();
419 return allocator_type();
422 void swap(match_results& that)
424 std::swap(m_subs, that.m_subs);
425 std::swap(m_named_subs, that.m_named_subs);
426 std::swap(m_last_closed_paren, that.m_last_closed_paren);
429 if(!that.m_is_singular)
431 m_base = that.m_base;
432 m_null = that.m_null;
435 else if(that.m_is_singular)
437 that.m_base = m_base;
438 that.m_null = m_null;
442 std::swap(m_base, that.m_base);
443 std::swap(m_null, that.m_null);
445 std::swap(m_is_singular, that.m_is_singular);
447 bool operator==(const match_results& that)const
451 return that.m_is_singular;
453 else if(that.m_is_singular)
457 return (m_subs == that.m_subs) && (m_base == that.m_base) && (m_last_closed_paren == that.m_last_closed_paren);
459 bool operator!=(const match_results& that)const
460 { return !(*this == that); }
462 #ifdef BOOST_REGEX_MATCH_EXTRA
463 typedef typename sub_match<BidiIterator>::capture_sequence_type capture_sequence_type;
465 const capture_sequence_type& captures(int i)const
469 return (*this)[i].captures();
474 // private access functions:
475 void BOOST_REGEX_CALL set_second(BidiIterator i)
477 BOOST_ASSERT(m_subs.size() > 2);
478 m_subs[2].second = i;
479 m_subs[2].matched = true;
481 m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
484 m_null.matched = false;
485 m_is_singular = false;
488 void BOOST_REGEX_CALL set_second(BidiIterator i, size_type pos, bool m = true, bool escape_k = false)
491 m_last_closed_paren = static_cast<int>(pos);
493 BOOST_ASSERT(m_subs.size() > pos);
494 m_subs[pos].second = i;
495 m_subs[pos].matched = m;
496 if((pos == 2) && !escape_k)
499 m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
502 m_null.matched = false;
503 m_is_singular = false;
506 void BOOST_REGEX_CALL set_size(size_type n, BidiIterator i, BidiIterator j)
509 size_type len = m_subs.size();
512 m_subs.erase(m_subs.begin()+n+2, m_subs.end());
513 std::fill(m_subs.begin(), m_subs.end(), v);
517 std::fill(m_subs.begin(), m_subs.end(), v);
519 m_subs.insert(m_subs.end(), n+2-len, v);
522 m_last_closed_paren = 0;
524 void BOOST_REGEX_CALL set_base(BidiIterator pos)
528 BidiIterator base()const
532 void BOOST_REGEX_CALL set_first(BidiIterator i)
534 BOOST_ASSERT(m_subs.size() > 2);
536 m_subs[1].second = i;
537 m_subs[1].matched = (m_subs[1].first != i);
540 // zero out everything else:
541 for(size_type n = 3; n < m_subs.size(); ++n)
543 m_subs[n].first = m_subs[n].second = m_subs[0].second;
544 m_subs[n].matched = false;
547 void BOOST_REGEX_CALL set_first(BidiIterator i, size_type pos, bool escape_k = false)
549 BOOST_ASSERT(pos+2 < m_subs.size());
552 m_subs[pos+2].first = i;
555 m_subs[1].second = i;
556 m_subs[1].matched = (m_subs[1].first != m_subs[1].second);
562 void BOOST_REGEX_CALL maybe_assign(const match_results<BidiIterator, Allocator>& m);
564 void BOOST_REGEX_CALL set_named_subs(boost::shared_ptr<named_sub_type> subs)
571 // Error handler called when an uninitialized match_results is accessed:
573 static void raise_logic_error()
575 std::logic_error e("Attempt to access an uninitialized boost::match_results<> class.");
576 boost::throw_exception(e);
580 vector_type m_subs; // subexpressions
581 BidiIterator m_base; // where the search started from
582 sub_match<BidiIterator> m_null; // a null match
583 boost::shared_ptr<named_sub_type> m_named_subs; // Shared copy of named subs in the regex object
584 int m_last_closed_paren; // Last ) to be seen - used for formatting
585 bool m_is_singular; // True if our stored iterators are singular
588 template <class BidiIterator, class Allocator>
589 void BOOST_REGEX_CALL match_results<BidiIterator, Allocator>::maybe_assign(const match_results<BidiIterator, Allocator>& m)
596 const_iterator p1, p2;
600 // Distances are measured from the start of *this* match, unless this isn't
601 // a valid match in which case we use the start of the whole sequence. Note that
602 // no subsequent match-candidate can ever be to the left of the first match found.
603 // This ensures that when we are using bidirectional iterators, that distances
604 // measured are as short as possible, and therefore as efficient as possible
605 // to compute. Finally note that we don't use the "matched" data member to test
606 // whether a sub-expression is a valid match, because partial matches set this
607 // to false for sub-expression 0.
609 BidiIterator l_end = this->suffix().second;
610 BidiIterator l_base = (p1->first == l_end) ? this->prefix().first : (*this)[0].first;
611 difference_type len1 = 0;
612 difference_type len2 = 0;
613 difference_type base1 = 0;
614 difference_type base2 = 0;
616 for(i = 0; i < size(); ++i, ++p1, ++p2)
619 // Leftmost takes priority over longest; handle special cases
620 // where distances need not be computed first (an optimisation
621 // for bidirectional iterators: ensure that we don't accidently
622 // compute the length of the whole sequence, as this can be really
625 if(p1->first == l_end)
627 if(p2->first != l_end)
629 // p2 must be better than p1, and no need to calculate
637 // *p1 and *p2 are either unmatched or match end-of sequence,
638 // either way no need to calculate distances:
639 if((p1->matched == false) && (p2->matched == true))
641 if((p1->matched == true) && (p2->matched == false))
646 else if(p2->first == l_end)
648 // p1 better than p2, and no need to calculate distances:
651 base1 = ::boost::BOOST_REGEX_DETAIL_NS::distance(l_base, p1->first);
652 base2 = ::boost::BOOST_REGEX_DETAIL_NS::distance(l_base, p2->first);
653 BOOST_ASSERT(base1 >= 0);
654 BOOST_ASSERT(base2 >= 0);
655 if(base1 < base2) return;
656 if(base2 < base1) break;
658 len1 = ::boost::BOOST_REGEX_DETAIL_NS::distance((BidiIterator)p1->first, (BidiIterator)p1->second);
659 len2 = ::boost::BOOST_REGEX_DETAIL_NS::distance((BidiIterator)p2->first, (BidiIterator)p2->second);
660 BOOST_ASSERT(len1 >= 0);
661 BOOST_ASSERT(len2 >= 0);
662 if((len1 != len2) || ((p1->matched == false) && (p2->matched == true)))
664 if((p1->matched == true) && (p2->matched == false))
671 else if((len2 > len1) || ((p1->matched == false) && (p2->matched == true)) )
675 template <class BidiIterator, class Allocator>
676 void swap(match_results<BidiIterator, Allocator>& a, match_results<BidiIterator, Allocator>& b)
681 #ifndef BOOST_NO_STD_LOCALE
682 template <class charT, class traits, class BidiIterator, class Allocator>
683 std::basic_ostream<charT, traits>&
684 operator << (std::basic_ostream<charT, traits>& os,
685 const match_results<BidiIterator, Allocator>& s)
687 return (os << s.str());
690 template <class BidiIterator, class Allocator>
691 std::ostream& operator << (std::ostream& os,
692 const match_results<BidiIterator, Allocator>& s)
694 return (os << s.str());
704 #pragma warning(push)
705 #pragma warning(disable: 4103)
707 #ifdef BOOST_HAS_ABI_HEADERS
708 # include BOOST_ABI_SUFFIX