1 ///////////////////////////////////////////////////////////////////////////////
2 /// \file match_results.hpp
3 /// Contains the definition of the match_results type and associated helpers.
4 /// The match_results type holds the results of a regex_match() or
5 /// regex_search() operation.
7 // Copyright 2008 Eric Niebler. Distributed under the Boost
8 // Software License, Version 1.0. (See accompanying file
9 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 // Acknowledgements: Thanks to Markus Schoepflin for helping to track down
12 // a tricky formatting bug on HP Tru64, and to Steven Watanabe for suggesting
15 #ifndef BOOST_XPRESSIVE_MATCH_RESULTS_HPP_EAN_10_04_2005
16 #define BOOST_XPRESSIVE_MATCH_RESULTS_HPP_EAN_10_04_2005
18 // MS compatible compilers support #pragma once
30 #include <boost/config.hpp>
31 #include <boost/assert.hpp>
32 #include <boost/integer.hpp>
33 #include <boost/mpl/if.hpp>
34 #include <boost/mpl/not.hpp>
35 #include <boost/mpl/size_t.hpp>
36 #include <boost/mpl/assert.hpp>
37 #include <boost/intrusive_ptr.hpp>
38 #include <boost/throw_exception.hpp>
39 #include <boost/iterator_adaptors.hpp>
40 #include <boost/utility/enable_if.hpp>
41 #include <boost/detail/workaround.hpp>
42 #include <boost/numeric/conversion/converter.hpp>
43 #include <boost/optional.hpp>
44 #include <boost/range/end.hpp>
45 #include <boost/range/begin.hpp>
46 #include <boost/range/as_literal.hpp>
47 #include <boost/range/const_iterator.hpp>
48 #include <boost/type_traits/is_function.hpp>
49 #if BOOST_ITERATOR_ADAPTORS_VERSION >= 0x0200
50 # include <boost/iterator/filter_iterator.hpp>
52 #include <boost/xpressive/regex_constants.hpp>
53 #include <boost/xpressive/detail/detail_fwd.hpp>
54 #include <boost/xpressive/detail/core/regex_impl.hpp>
55 #include <boost/xpressive/detail/core/sub_match_vector.hpp>
56 #include <boost/xpressive/detail/utility/sequence_stack.hpp>
57 #include <boost/xpressive/detail/core/results_cache.hpp>
58 #include <boost/xpressive/detail/utility/literals.hpp>
59 #include <boost/xpressive/detail/utility/algorithm.hpp>
60 #include <boost/xpressive/detail/utility/counted_base.hpp>
61 // Doxygen can't handle proto :-(
62 #ifndef BOOST_XPRESSIVE_DOXYGEN_INVOKED
63 # include <boost/proto/proto_fwd.hpp>
64 # include <boost/proto/traits.hpp>
67 namespace boost { namespace xpressive { namespace detail
70 ///////////////////////////////////////////////////////////////////////////////
75 bool operator()(std::type_info const *left, std::type_info const *right) const
77 return 0 != left->before(*right);
81 ///////////////////////////////////////////////////////////////////////////////
84 struct ActionArgBinding
85 : proto::assign<proto::terminal<action_arg<proto::_, proto::_> >, proto::terminal<proto::_> >
89 ///////////////////////////////////////////////////////////////////////////////
92 template<typename BidiIter>
94 : counted_base<results_extras<BidiIter> >
96 sequence_stack<sub_match_impl<BidiIter> > sub_match_stack_;
97 results_cache<BidiIter> results_cache_;
100 ///////////////////////////////////////////////////////////////////////////////
101 // char_overflow_handler_
103 struct char_overflow_handler_
105 void operator ()(numeric::range_check_result result) const // throw(regex_error)
107 if(numeric::cInRange != result)
109 BOOST_THROW_EXCEPTION(
111 regex_constants::error_escape
112 , "character escape too large to fit in target character type"
119 ///////////////////////////////////////////////////////////////////////////////
122 enum transform_op { op_none = 0, op_upper = 1, op_lower = 2 };
123 enum transform_scope { scope_next = 0, scope_rest = 1 };
125 ///////////////////////////////////////////////////////////////////////////////
126 // case_converting_iterator
128 template<typename OutputIterator, typename Char>
129 struct case_converting_iterator
131 typedef std::output_iterator_tag iterator_category;
132 typedef Char value_type;
133 typedef void difference_type;
134 typedef void pointer;
135 typedef case_converting_iterator<OutputIterator, Char> reference;
137 case_converting_iterator(OutputIterator const &out, traits<Char> const *tr)
144 OutputIterator base() const
149 case_converting_iterator &operator ++()
152 this->next_ = op_none;
156 case_converting_iterator operator ++(int)
158 case_converting_iterator tmp(*this);
163 case_converting_iterator &operator *()
168 friend bool set_transform(case_converting_iterator &iter, transform_op trans, transform_scope scope)
170 BOOST_ASSERT(scope == scope_next || scope == scope_rest);
171 if(scope == scope_next)
178 case_converting_iterator &operator =(Char ch)
180 switch(this->next_ ? this->next_ : this->rest_)
183 ch = this->traits_->tolower(ch);
187 ch = this->traits_->toupper(ch);
199 traits<Char> const *traits_;
200 transform_op next_, rest_;
203 template<typename Iterator>
204 inline bool set_transform(Iterator &, transform_op, transform_scope)
209 ///////////////////////////////////////////////////////////////////////////////
210 // noop_output_iterator
212 template<typename Char>
213 struct noop_output_iterator
215 typedef std::output_iterator_tag iterator_category;
216 typedef Char value_type;
217 typedef void difference_type;
218 typedef void pointer;
219 typedef noop_output_iterator<Char> reference;
221 noop_output_iterator &operator ++()
226 noop_output_iterator &operator ++(int)
231 noop_output_iterator &operator *()
236 noop_output_iterator &operator =(Char const &)
242 struct any_type { any_type(...); };
243 typedef char no_type;
244 typedef char (&unary_type)[2];
245 typedef char (&binary_type)[3];
246 typedef char (&ternary_type)[4];
248 no_type check_is_formatter(unary_type, binary_type, ternary_type);
251 unary_type check_is_formatter(T const &, binary_type, ternary_type);
254 binary_type check_is_formatter(unary_type, T const &, ternary_type);
256 template<typename T, typename U>
257 binary_type check_is_formatter(T const &, U const &, ternary_type);
260 ternary_type check_is_formatter(unary_type, binary_type, T const &);
262 template<typename T, typename U>
263 ternary_type check_is_formatter(T const &, binary_type, U const &);
265 template<typename T, typename U>
266 ternary_type check_is_formatter(unary_type, T const &, U const &);
268 template<typename T, typename U, typename V>
269 ternary_type check_is_formatter(T const &, U const &, V const &);
271 struct unary_binary_ternary
273 typedef unary_type (*unary_fun)(any_type);
274 typedef binary_type (*binary_fun)(any_type, any_type);
275 typedef ternary_type (*ternary_fun)(any_type, any_type, any_type);
276 operator unary_fun();
277 operator binary_fun();
278 operator ternary_fun();
281 template<typename Formatter, bool IsFunction = is_function<Formatter>::value>
282 struct formatter_wrapper
284 , unary_binary_ternary
289 template<typename Formatter>
290 struct formatter_wrapper<Formatter, true>
291 : unary_binary_ternary
293 operator Formatter *();
296 template<typename Formatter>
297 struct formatter_wrapper<Formatter *, false>
298 : unary_binary_ternary
300 operator Formatter *();
303 template<typename Formatter, typename What, typename Out, typename Void = void>
304 struct formatter_arity
306 static formatter_wrapper<Formatter> &formatter;
309 BOOST_STATIC_CONSTANT(
314 , formatter(what, out)
315 , formatter(what, out, regex_constants::format_default)
319 typedef mpl::size_t<value> type;
322 template<typename Formatter, typename What, typename Out>
323 struct formatter_arity<Formatter, What, Out, typename Formatter::proto_is_expr_>
333 struct is_char_ptr<T *>
334 : mpl::not_<is_function<T> >
337 #if BOOST_WORKAROUND(__GNUC__, == 4) && (__GNUC_MINOR__ == 0)
338 // work around gcc-4.0.1 compiler bug wrt function references
340 typename mpl::if_<is_function<T>, T *, T const &>::type
341 as_callable(T const &t)
349 ///////////////////////////////////////////////////////////////////////////////
351 /// \brief Class template match_results\<\> holds the results of a regex_match() or a
352 /// regex_search() as a collection of sub_match objects.
354 /// Class template match_results\<\> denotes a collection of sequences representing the result of
355 /// a regular expression match. Storage for the collection is allocated and freed as necessary by
356 /// the member functions of class match_results\<\>.
358 /// The class template match_results\<\> conforms to the requirements of a Sequence, as specified
359 /// in (lib.sequence.reqmts), except that only operations defined for const-qualified Sequences are
361 template<typename BidiIter>
367 struct dummy { int i_; };
368 typedef int dummy::*bool_type;
371 typedef typename iterator_value<BidiIter>::type char_type;
372 typedef typename detail::string_type<char_type>::type string_type;
373 typedef std::size_t size_type;
374 typedef sub_match<BidiIter> value_type;
375 typedef typename iterator_difference<BidiIter>::type difference_type;
376 typedef value_type const &reference;
377 typedef value_type const &const_reference;
379 typedef typename detail::sub_match_vector<BidiIter>::iterator iterator;
380 typedef typename detail::sub_match_vector<BidiIter>::const_iterator const_iterator;
381 typedef typename detail::nested_results<BidiIter> nested_results_type;
383 /// \post regex_id() == 0
384 /// \post size() == 0
385 /// \post empty() == true
386 /// \post str() == string_type()
401 /// \param that The match_results object to copy
402 /// \post regex_id() == that.regex_id().
403 /// \post size() == that.size().
404 /// \post empty() == that.empty().
405 /// \post str(n) == that.str(n) for all positive integers n \< that.size().
406 /// \post prefix() == that.prefix().
407 /// \post suffix() == that.suffix().
408 /// \post (*this)[n] == that[n] for all positive integers n \< that.size().
409 /// \post length(n) == that.length(n) for all positive integers n \< that.size().
410 /// \post position(n) == that.position(n) for all positive integers n \< that.size().
411 match_results(match_results<BidiIter> const &that)
412 : regex_id_(that.regex_id_)
421 , named_marks_(that.named_marks_)
425 extras_type &extras = this->get_extras_();
426 std::size_t size = that.sub_matches_.size();
427 detail::sub_match_impl<BidiIter> *sub_matches = extras.sub_match_stack_.push_sequence(size, detail::sub_match_impl<BidiIter>(*that.base_), detail::fill);
428 detail::core_access<BidiIter>::init_sub_match_vector(this->sub_matches_, sub_matches, size, that.sub_matches_);
430 this->base_ = that.base_;
431 this->prefix_ = that.prefix_;
432 this->suffix_ = that.suffix_;
433 // BUGBUG this doesn't share the extras::sequence_stack
434 this->nested_results_ = that.nested_results_;
435 this->traits_ = that.traits_;
443 /// \param that The match_results object to copy.
444 /// \post regex_id() == that.regex_id().
445 /// \post size() == that.size().
446 /// \post empty() == that.empty().
447 /// \post str(n) == that.str(n) for all positive integers n \< that.size().
448 /// \post prefix() == that.prefix().
449 /// \post suffix() == that.suffix().
450 /// \post (*this)[n] == that[n] for all positive integers n \< that.size().
451 /// \post length(n) == that.length(n) for all positive integers n \< that.size().
452 /// \post position(n) == that.position(n) for all positive integers n \< that.size().
453 match_results<BidiIter> &operator =(match_results<BidiIter> const &that)
455 match_results<BidiIter>(that).swap(*this);
459 /// Returns one plus the number of marked sub-expressions in the regular
460 /// expression that was matched if *this represents the result of a
461 /// successful match. Otherwise returns 0.
462 size_type size() const
464 return this->sub_matches_.size();
467 /// Returns size() == 0.
471 return 0 == this->size();
474 /// Returns (*this)[sub].length().
476 difference_type length(size_type sub = 0) const
478 return this->sub_matches_[ sub ].length();
481 /// If !(*this)[sub].matched then returns -1. Otherwise returns std::distance(base, (*this)[sub].first),
482 /// where base is the start iterator of the sequence that was searched. [Note - unless this is part
483 /// of a repeated search with a regex_iterator then base is the same as prefix().first - end note]
484 difference_type position(size_type sub = 0) const
486 return this->sub_matches_[ sub ].matched ? std::distance(*this->base_, this->sub_matches_[ sub ].first) : -1;
489 /// Returns (*this)[sub].str().
491 string_type str(size_type sub = 0) const
493 return this->sub_matches_[ sub ].str();
496 /// Returns a reference to the sub_match object representing the sequence that
497 /// matched marked sub-expression sub. If sub == 0 then returns a reference to
498 /// a sub_match object representing the sequence that matched the whole regular
499 /// expression. If sub >= size() then returns a sub_match object representing an
500 /// unmatched sub-expression.
501 template<typename Sub>
502 const_reference operator [](Sub const &sub) const
504 return this->at_(sub);
507 /// Returns a reference to the sub_match object representing the character sequence from
508 /// the start of the string being matched/searched, to the start of the match found.
510 /// \pre (*this)[0].matched is true
511 const_reference prefix() const
513 return this->prefix_ ? *this->prefix_ : this->sub_matches_[this->sub_matches_.size()];
516 /// Returns a reference to the sub_match object representing the character sequence from
517 /// the end of the match found to the end of the string being matched/searched.
519 /// \pre (*this)[0].matched is true
520 const_reference suffix() const
522 return this->suffix_ ? *this->suffix_ : this->sub_matches_[this->sub_matches_.size()];
525 /// Returns a starting iterator that enumerates over all the marked sub-expression matches
528 const_iterator begin() const
530 return this->sub_matches_.begin();
533 /// Returns a terminating iterator that enumerates over all the marked sub-expression
534 /// matches stored in *this.
536 const_iterator end() const
538 return this->sub_matches_.end();
541 /// Returns a true value if (*this)[0].matched, else returns a false value.
543 operator bool_type() const
545 return (!this->empty() && this->sub_matches_[ 0 ].matched) ? &dummy::i_ : 0;
548 /// Returns true if empty() || !(*this)[0].matched, else returns false.
550 bool operator !() const
552 return this->empty() || !this->sub_matches_[ 0 ].matched;
555 /// Returns the id of the basic_regex object most recently used with this match_results object.
557 regex_id_type regex_id() const
559 return this->regex_id_;
562 /// Returns a Sequence of nested match_results elements.
564 nested_results_type const &nested_results() const
566 return this->nested_results_;
569 /// If \c Format models \c ForwardRange or is a null-terminated string, this function
570 /// copies the character sequence in \c fmt to \c OutputIterator \c out. For each format
571 /// specifier or escape sequence in \c fmt, replace that sequence with either the character(s) it
572 /// represents, or the sequence within <tt>*this</tt> to which it refers. The bitmasks specified in flags
573 /// determines what format specifiers or escape sequences are recognized. By default, this is the
574 /// format used by ECMA-262, ECMAScript Language Specification, Chapter 15 part 5.4.11 String.prototype.replace.
576 /// Otherwise, if \c Format models <tt>Callable\<match_results\<BidiIter\>, OutputIterator, regex_constants::match_flag_type\></tt>,
577 /// this function returns <tt>fmt(*this, out, flags)</tt>.
579 /// Otherwise, if \c Format models <tt>Callable\<match_results\<BidiIter\>, OutputIterator\></tt>, this function
580 /// returns <tt>fmt(*this, out)</tt>.
582 /// Otherwise, if \c Format models <tt>Callable\<match_results\<BidiIter\> \></tt>, this function
583 /// returns <tt>std::copy(x.begin(), x.end(), out)</tt>, where \c x is the result of
584 /// calling <tt>fmt(*this)</tt>.
585 template<typename Format, typename OutputIterator>
586 OutputIterator format
590 , regex_constants::match_flag_type flags = regex_constants::format_default
591 , typename disable_if<detail::is_char_ptr<Format> >::type * = 0
594 // Is this a formatter object, or a format string?
596 typename detail::formatter_arity<
598 , match_results<BidiIter>
603 return this->format_(out, fmt, flags, arity());
608 template<typename OutputIterator>
609 OutputIterator format
612 , char_type const *fmt
613 , regex_constants::match_flag_type flags = regex_constants::format_default
616 return this->format_(out, boost::as_literal(fmt), flags, mpl::size_t<0>());
619 /// If \c Format models \c ForwardRange or is a null-terminated string, this function
620 /// returns a copy of the character sequence \c fmt. For each format specifier or escape sequence in \c fmt,
621 /// replace that sequence with either the character(s) it represents, or the sequence within
622 /// <tt>*this</tt> to which it refers. The bitmasks specified in \c flags determines what format specifiers
623 /// or escape sequences are recognized. By default this is the format used by ECMA-262,
624 /// ECMAScript Language Specification, Chapter 15 part 5.4.11 String.prototype.replace.
626 /// Otherwise, if \c Format models <tt>Callable\<match_results\<BidiIter\>, OutputIterator, regex_constants::match_flag_type\></tt>,
627 /// this function returns a \c string_type object \c x populated by calling <tt>fmt(*this, out, flags)</tt>,
628 /// where \c out is a \c back_insert_iterator into \c x.
630 /// Otherwise, if \c Format models <tt>Callable\<match_results\<BidiIter\>, OutputIterator\></tt>, this function
631 /// returns a \c string_type object \c x populated by calling <tt>fmt(*this, out)</tt>,
632 /// where \c out is a \c back_insert_iterator into \c x.
634 /// Otherwise, if \c Format models <tt>Callable\<match_results\<BidiIter\> \></tt>, this function
635 /// returns <tt>fmt(*this)</tt>.
636 template<typename Format, typename OutputIterator>
640 , regex_constants::match_flag_type flags = regex_constants::format_default
641 , typename disable_if<detail::is_char_ptr<Format> >::type * = 0
645 this->format(std::back_inserter(result), fmt, flags);
654 , regex_constants::match_flag_type flags = regex_constants::format_default
658 this->format(std::back_inserter(result), fmt, flags);
662 /// Swaps the contents of two match_results objects. Guaranteed not to throw.
663 /// \param that The match_results object to swap with.
664 /// \post *this contains the sequence of matched sub-expressions that were in that,
665 /// that contains the sequence of matched sub-expressions that were in *this.
667 void swap(match_results<BidiIter> &that) // throw()
670 swap(this->regex_id_, that.regex_id_);
671 this->sub_matches_.swap(that.sub_matches_);
672 this->base_.swap(that.base_);
673 this->prefix_.swap(that.prefix_);
674 this->suffix_.swap(that.suffix_);
675 this->nested_results_.swap(that.nested_results_);
676 this->extras_ptr_.swap(that.extras_ptr_);
677 this->traits_.swap(that.traits_);
678 this->args_.swap(that.args_);
683 template<typename Arg>
684 match_results<BidiIter> &let(Arg const &arg)
686 typedef typename proto::result_of::left<Arg>::type left_type;
687 typedef typename proto::result_of::right<Arg>::type right_type;
688 typedef typename proto::result_of::value<left_type>::type arg_left_type;
689 typedef typename proto::result_of::value<right_type>::type arg_right_type;
690 BOOST_MPL_ASSERT((proto::matches<Arg, detail::ActionArgBinding>));
691 BOOST_MPL_ASSERT((is_same<typename arg_left_type::type, arg_right_type>));
692 this->args_[&typeid(proto::value(proto::left(arg)))] = &proto::value(proto::right(arg));
698 match_results<BidiIter> const &operator ()(regex_id_type regex_id, size_type index = 0) const
700 // BUGBUG this is linear, make it O(1)
701 static match_results<BidiIter> const s_null;
703 regex_id_filter_predicate<BidiIter> pred(regex_id);
704 typename nested_results_type::const_iterator
705 begin = this->nested_results_.begin()
706 , end = this->nested_results_.end()
707 , cur = detail::find_nth_if(begin, end, index, pred);
709 return (cur == end) ? s_null : *cur;
714 match_results<BidiIter> const &operator ()(basic_regex<BidiIter> const &rex, std::size_t index = 0) const
716 return (*this)(rex.regex_id(), index);
721 friend struct detail::core_access<BidiIter>;
722 typedef detail::results_extras<BidiIter> extras_type;
728 regex_id_type regex_id
729 , intrusive_ptr<detail::traits<char_type> const> const &tr
730 , detail::sub_match_impl<BidiIter> *sub_matches
732 , std::vector<detail::named_mark<char_type> > const &named_marks
736 this->regex_id_ = regex_id;
737 this->named_marks_ = named_marks;
738 detail::core_access<BidiIter>::init_sub_match_vector(this->sub_matches_, sub_matches, size);
743 extras_type &get_extras_()
745 if(!this->extras_ptr_)
747 this->extras_ptr_ = new extras_type;
750 return *this->extras_ptr_;
755 void set_prefix_suffix_(BidiIter begin, BidiIter end)
758 this->prefix_ = sub_match<BidiIter>(begin, this->sub_matches_[ 0 ].first, begin != this->sub_matches_[ 0 ].first);
759 this->suffix_ = sub_match<BidiIter>(this->sub_matches_[ 0 ].second, end, this->sub_matches_[ 0 ].second != end);
761 typename nested_results_type::iterator ibegin = this->nested_results_.begin();
762 typename nested_results_type::iterator iend = this->nested_results_.end();
763 for( ; ibegin != iend; ++ibegin )
765 ibegin->set_prefix_suffix_(begin, end);
773 detail::core_access<BidiIter>::init_sub_match_vector(this->sub_matches_, 0, 0);
778 void set_base_(BidiIter base)
782 typename nested_results_type::iterator ibegin = this->nested_results_.begin();
783 typename nested_results_type::iterator iend = this->nested_results_.end();
784 for( ; ibegin != iend; ++ibegin )
786 ibegin->set_base_(base);
792 const_reference at_(size_type sub) const
794 return this->sub_matches_[ sub ];
799 const_reference at_(detail::basic_mark_tag const &mark) const
801 return this->sub_matches_[ detail::get_mark_number(mark) ];
806 const_reference at_(char_type const *name) const
808 for(std::size_t i = 0; i < this->named_marks_.size(); ++i)
810 if(this->named_marks_[i].name_ == name)
812 return this->sub_matches_[ this->named_marks_[i].mark_nbr_ ];
815 BOOST_THROW_EXCEPTION(
816 regex_error(regex_constants::error_badmark, "invalid named back-reference")
818 // Should never execute, but if it does, this returns
819 // a "null" sub_match.
820 return this->sub_matches_[this->sub_matches_.size()];
825 const_reference at_(string_type const &name) const
827 return (*this)[name.c_str()];
832 template<typename OutputIterator, typename ForwardRange>
833 OutputIterator format2_(OutputIterator out, ForwardRange const &result) const
835 return std::copy(boost::begin(result), boost::end(result), out);
840 template<typename OutputIterator, typename Char>
841 OutputIterator format2_(OutputIterator out, Char const *const &result) const
843 Char const *tmp = result;
844 BOOST_ASSERT(0 != tmp);
845 for(; 0 != *tmp; ++tmp, ++out)
854 template<typename OutputIterator, typename ForwardRange>
855 OutputIterator format_
858 , ForwardRange const &format
859 , regex_constants::match_flag_type flags
863 typedef typename range_const_iterator<ForwardRange>::type iterator;
864 iterator cur = boost::begin(format), end = boost::end(format);
866 if(0 != (regex_constants::format_literal & flags))
868 return std::copy(cur, end, out);
870 else if(0 != (regex_constants::format_perl & flags))
872 return this->format_perl_(cur, end, out);
874 else if(0 != (regex_constants::format_sed & flags))
876 return this->format_sed_(cur, end, out);
878 else if(0 != (regex_constants::format_all & flags))
880 return this->format_all_(cur, end, out);
883 return this->format_ecma_262_(cur, end, out);
888 template<typename OutputIterator, typename Callable1>
889 OutputIterator format_
892 , Callable1 const &format
893 , regex_constants::match_flag_type
897 #if BOOST_WORKAROUND(__GNUC__, == 4) && (__GNUC_MINOR__ == 0)
898 return this->format2_(out, detail::as_callable(format)(*this));
900 return this->format2_(out, format(*this));
906 template<typename OutputIterator, typename Callable2>
907 OutputIterator format_
910 , Callable2 const &format
911 , regex_constants::match_flag_type
915 #if BOOST_WORKAROUND(__GNUC__, == 4) && (__GNUC_MINOR__ == 0)
916 return detail::as_callable(format)(*this, out);
918 return format(*this, out);
924 template<typename OutputIterator, typename Callable3>
925 OutputIterator format_
928 , Callable3 const &format
929 , regex_constants::match_flag_type flags
933 #if BOOST_WORKAROUND(__GNUC__, == 4) && (__GNUC_MINOR__ == 0)
934 return detail::as_callable(format)(*this, out, flags);
936 return format(*this, out, flags);
942 template<typename OutputIterator, typename Expr>
943 OutputIterator format_
947 , regex_constants::match_flag_type
951 // detail::ReplaceAlgo may be an incomplete type at this point, so
952 // we can't construct it directly.
953 typedef typename mpl::if_c<true, detail::ReplaceAlgo, OutputIterator>::type ReplaceAlgo;
954 return this->format2_(out, ReplaceAlgo()(format, 0, *this));
959 template<typename ForwardIterator, typename OutputIterator>
960 OutputIterator format_ecma_262_(ForwardIterator cur, ForwardIterator end, OutputIterator out) const
966 case BOOST_XPR_CHAR_(char_type, '$'):
967 out = this->format_backref_(++cur, end, out);
981 template<typename ForwardIterator, typename OutputIterator>
982 OutputIterator format_sed_(ForwardIterator cur, ForwardIterator end, OutputIterator out) const
988 case BOOST_XPR_CHAR_(char_type, '&'):
990 out = std::copy(this->sub_matches_[ 0 ].first, this->sub_matches_[ 0 ].second, out);
993 case BOOST_XPR_CHAR_(char_type, '\\'):
994 out = this->format_escape_(++cur, end, out);
1008 template<typename ForwardIterator, typename OutputIterator>
1009 OutputIterator format_perl_(ForwardIterator cur, ForwardIterator end, OutputIterator out) const
1011 detail::case_converting_iterator<OutputIterator, char_type> iout(out, this->traits_.get());
1017 case BOOST_XPR_CHAR_(char_type, '$'):
1018 iout = this->format_backref_(++cur, end, iout);
1021 case BOOST_XPR_CHAR_(char_type, '\\'):
1022 if(++cur != end && BOOST_XPR_CHAR_(char_type, 'g') == *cur)
1024 iout = this->format_named_backref_(++cur, end, iout);
1028 iout = this->format_escape_(cur, end, iout);
1043 template<typename ForwardIterator, typename OutputIterator>
1044 OutputIterator format_all_(ForwardIterator cur, ForwardIterator end, OutputIterator out) const
1046 detail::case_converting_iterator<OutputIterator, char_type> iout(out, this->traits_.get());
1047 iout = this->format_all_impl_(cur, end, iout);
1048 BOOST_XPR_ENSURE_(cur == end
1049 , regex_constants::error_paren, "unbalanced parentheses in format string");
1055 template<typename ForwardIterator, typename OutputIterator>
1056 OutputIterator format_all_impl_(ForwardIterator &cur, ForwardIterator end, OutputIterator out, bool metacolon = false) const
1058 int max = 0, sub = 0;
1059 detail::noop_output_iterator<char_type> noop;
1065 case BOOST_XPR_CHAR_(char_type, '$'):
1066 out = this->format_backref_(++cur, end, out);
1069 case BOOST_XPR_CHAR_(char_type, '\\'):
1070 if(++cur != end && BOOST_XPR_CHAR_(char_type, 'g') == *cur)
1072 out = this->format_named_backref_(++cur, end, out);
1076 out = this->format_escape_(cur, end, out);
1080 case BOOST_XPR_CHAR_(char_type, '('):
1081 out = this->format_all_impl_(++cur, end, out);
1082 BOOST_XPR_ENSURE_(BOOST_XPR_CHAR_(char_type, ')') == *(cur-1)
1083 , regex_constants::error_paren, "unbalanced parentheses in format string");
1086 case BOOST_XPR_CHAR_(char_type, '?'):
1087 BOOST_XPR_ENSURE_(++cur != end
1088 , regex_constants::error_subreg, "malformed conditional in format string");
1089 max = static_cast<int>(this->size() - 1);
1090 sub = detail::toi(cur, end, *this->traits_, 10, max);
1091 BOOST_XPR_ENSURE_(0 != sub, regex_constants::error_subreg, "invalid back-reference");
1092 if(this->sub_matches_[ sub ].matched)
1094 out = this->format_all_impl_(cur, end, out, true);
1095 if(BOOST_XPR_CHAR_(char_type, ':') == *(cur-1))
1096 this->format_all_impl_(cur, end, noop);
1100 this->format_all_impl_(cur, end, noop, true);
1101 if(BOOST_XPR_CHAR_(char_type, ':') == *(cur-1))
1102 out = this->format_all_impl_(cur, end, out);
1106 case BOOST_XPR_CHAR_(char_type, ':'):
1110 case BOOST_XPR_CHAR_(char_type, ')'):
1127 template<typename ForwardIterator, typename OutputIterator>
1128 OutputIterator format_backref_
1130 ForwardIterator &cur
1131 , ForwardIterator end
1132 , OutputIterator out
1137 *out++ = BOOST_XPR_CHAR_(char_type, '$');
1139 else if(BOOST_XPR_CHAR_(char_type, '$') == *cur)
1143 else if(BOOST_XPR_CHAR_(char_type, '&') == *cur) // whole match
1146 out = std::copy(this->sub_matches_[ 0 ].first, this->sub_matches_[ 0 ].second, out);
1148 else if(BOOST_XPR_CHAR_(char_type, '`') == *cur) // prefix
1151 out = std::copy(this->prefix().first, this->prefix().second, out);
1153 else if(BOOST_XPR_CHAR_(char_type, '\'') == *cur) // suffix
1156 out = std::copy(this->suffix().first, this->suffix().second, out);
1158 else if(-1 != this->traits_->value(*cur, 10)) // a sub-match
1160 int max = static_cast<int>(this->size() - 1);
1161 int sub = detail::toi(cur, end, *this->traits_, 10, max);
1162 BOOST_XPR_ENSURE_(0 != sub, regex_constants::error_subreg, "invalid back-reference");
1163 if(this->sub_matches_[ sub ].matched)
1164 out = std::copy(this->sub_matches_[ sub ].first, this->sub_matches_[ sub ].second, out);
1168 *out++ = BOOST_XPR_CHAR_(char_type, '$');
1177 template<typename ForwardIterator, typename OutputIterator>
1178 OutputIterator format_escape_
1180 ForwardIterator &cur
1181 , ForwardIterator end
1182 , OutputIterator out
1185 using namespace regex_constants;
1186 ForwardIterator tmp;
1187 // define an unsigned type the same size as char_type
1188 typedef typename boost::uint_t<CHAR_BIT * sizeof(char_type)>::least uchar_t;
1189 BOOST_MPL_ASSERT_RELATION(sizeof(uchar_t), ==, sizeof(char_type));
1190 typedef numeric::conversion_traits<uchar_t, int> converstion_traits;
1191 numeric::converter<int, uchar_t, converstion_traits, detail::char_overflow_handler_> converter;
1195 *out++ = BOOST_XPR_CHAR_(char_type, '\\');
1199 char_type ch = *cur++;
1202 case BOOST_XPR_CHAR_(char_type, 'a'):
1203 *out++ = BOOST_XPR_CHAR_(char_type, '\a');
1206 case BOOST_XPR_CHAR_(char_type, 'e'):
1207 *out++ = converter(27);
1210 case BOOST_XPR_CHAR_(char_type, 'f'):
1211 *out++ = BOOST_XPR_CHAR_(char_type, '\f');
1214 case BOOST_XPR_CHAR_(char_type, 'n'):
1215 *out++ = BOOST_XPR_CHAR_(char_type, '\n');
1218 case BOOST_XPR_CHAR_(char_type, 'r'):
1219 *out++ = BOOST_XPR_CHAR_(char_type, '\r');
1222 case BOOST_XPR_CHAR_(char_type, 't'):
1223 *out++ = BOOST_XPR_CHAR_(char_type, '\t');
1226 case BOOST_XPR_CHAR_(char_type, 'v'):
1227 *out++ = BOOST_XPR_CHAR_(char_type, '\v');
1230 case BOOST_XPR_CHAR_(char_type, 'x'):
1231 BOOST_XPR_ENSURE_(cur != end, error_escape, "unexpected end of format found");
1232 if(BOOST_XPR_CHAR_(char_type, '{') == *cur)
1234 BOOST_XPR_ENSURE_(++cur != end, error_escape, "unexpected end of format found");
1236 *out++ = converter(detail::toi(cur, end, *this->traits_, 16, 0xffff));
1237 BOOST_XPR_ENSURE_(4 == std::distance(tmp, cur) && cur != end && BOOST_XPR_CHAR_(char_type, '}') == *cur++
1238 , error_escape, "invalid hex escape : must be \\x { HexDigit HexDigit HexDigit HexDigit }");
1243 *out++ = converter(detail::toi(cur, end, *this->traits_, 16, 0xff));
1244 BOOST_XPR_ENSURE_(2 == std::distance(tmp, cur), error_escape
1245 , "invalid hex escape : must be \\x HexDigit HexDigit");
1249 case BOOST_XPR_CHAR_(char_type, 'c'):
1250 BOOST_XPR_ENSURE_(cur != end, error_escape, "unexpected end of format found");
1253 this->traits_->in_range(BOOST_XPR_CHAR_(char_type, 'a'), BOOST_XPR_CHAR_(char_type, 'z'), *cur)
1254 || this->traits_->in_range(BOOST_XPR_CHAR_(char_type, 'A'), BOOST_XPR_CHAR_(char_type, 'Z'), *cur)
1256 , "invalid escape control letter; must be one of a-z or A-Z"
1258 // Convert to character according to ECMA-262, section 15.10.2.10:
1259 *out++ = converter(*cur % 32);
1263 case BOOST_XPR_CHAR_(char_type, 'l'):
1264 if(!set_transform(out, detail::op_lower, detail::scope_next))
1266 *out++ = BOOST_XPR_CHAR_(char_type, 'l');
1270 case BOOST_XPR_CHAR_(char_type, 'L'):
1271 if(!set_transform(out, detail::op_lower, detail::scope_rest))
1273 *out++ = BOOST_XPR_CHAR_(char_type, 'L');
1277 case BOOST_XPR_CHAR_(char_type, 'u'):
1278 if(!set_transform(out, detail::op_upper, detail::scope_next))
1280 *out++ = BOOST_XPR_CHAR_(char_type, 'u');
1284 case BOOST_XPR_CHAR_(char_type, 'U'):
1285 if(!set_transform(out, detail::op_upper, detail::scope_rest))
1287 *out++ = BOOST_XPR_CHAR_(char_type, 'U');
1291 case BOOST_XPR_CHAR_(char_type, 'E'):
1292 if(!set_transform(out, detail::op_none, detail::scope_rest))
1294 *out++ = BOOST_XPR_CHAR_(char_type, 'E');
1299 // BUGBUG what about backreferences like \12 ?
1300 if(0 < this->traits_->value(ch, 10))
1302 int sub = this->traits_->value(ch, 10);
1303 if(this->sub_matches_[ sub ].matched)
1304 out = std::copy(this->sub_matches_[ sub ].first, this->sub_matches_[ sub ].second, out);
1318 template<typename ForwardIterator, typename OutputIterator>
1319 OutputIterator format_named_backref_
1321 ForwardIterator &cur
1322 , ForwardIterator end
1323 , OutputIterator out
1326 using namespace regex_constants;
1327 BOOST_XPR_ENSURE_(cur != end && BOOST_XPR_CHAR_(char_type, '<') == *cur++
1328 , error_badmark, "invalid named back-reference");
1329 ForwardIterator begin = cur;
1330 for(; cur != end && BOOST_XPR_CHAR_(char_type, '>') != *cur; ++cur)
1332 BOOST_XPR_ENSURE_(cur != begin && cur != end && BOOST_XPR_CHAR_(char_type, '>') == *cur
1333 , error_badmark, "invalid named back-reference");
1335 string_type name(begin, cur++);
1336 for(std::size_t i = 0; i < this->named_marks_.size(); ++i)
1338 if(this->named_marks_[i].name_ == name)
1340 std::size_t sub = this->named_marks_[i].mark_nbr_;
1341 return std::copy(this->sub_matches_[ sub ].first, this->sub_matches_[ sub ].second, out);
1345 BOOST_THROW_EXCEPTION(regex_error(error_badmark, "invalid named back-reference"));
1346 // Should never get here
1350 regex_id_type regex_id_;
1351 detail::sub_match_vector<BidiIter> sub_matches_;
1352 boost::optional<BidiIter> base_;
1353 boost::optional<sub_match<BidiIter> > prefix_;
1354 boost::optional<sub_match<BidiIter> > suffix_;
1355 nested_results_type nested_results_;
1356 intrusive_ptr<extras_type> extras_ptr_;
1357 intrusive_ptr<detail::traits<char_type> const> traits_;
1358 detail::action_args_type args_;
1359 std::vector<detail::named_mark<char_type> > named_marks_;
1362 ///////////////////////////////////////////////////////////////////////////////
1363 // regex_id_filter_predicate
1365 template<typename BidiIter>
1366 struct regex_id_filter_predicate
1368 typedef match_results<BidiIter> argument_type;
1369 typedef bool result_type;
1371 regex_id_filter_predicate(regex_id_type regex_id)
1372 : regex_id_(regex_id)
1376 bool operator ()(match_results<BidiIter> const &res) const
1378 return this->regex_id_ == res.regex_id();
1383 regex_id_type regex_id_;
1386 }} // namespace boost::xpressive
1388 #ifdef BOOST_HAS_CONCEPTS
1389 // Better living through concepts. :-P
1392 template<typename Iter_, typename Char_>
1393 concept_map OutputIterator<
1394 boost::xpressive::detail::case_converting_iterator<Iter_, Char_>
1399 template<typename Char_>
1400 concept_map OutputIterator<
1401 boost::xpressive::detail::noop_output_iterator<Char_>