Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: /// \file regex_algorithms.hpp Chris@16: /// Contains the regex_match(), regex_search() and regex_replace() algorithms. Chris@16: // Chris@16: // Copyright 2008 Eric Niebler. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_XPRESSIVE_ALGORITHMS_HPP_EAN_10_04_2005 Chris@16: #define BOOST_XPRESSIVE_ALGORITHMS_HPP_EAN_10_04_2005 Chris@16: Chris@16: // MS compatible compilers support #pragma once Chris@101: #if defined(_MSC_VER) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: /// Chris@16: #define BOOST_XPR_NONDEDUCED_TYPE_(x) typename mpl::identity::type Chris@16: Chris@16: namespace boost { namespace xpressive Chris@16: { Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // regex_match Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // regex_match_impl Chris@16: template Chris@16: inline bool regex_match_impl Chris@16: ( Chris@16: BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin Chris@16: , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end Chris@16: , match_results &what Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: BOOST_ASSERT(0 != re.regex_id()); Chris@16: Chris@16: // the state object holds matching state and Chris@16: // is passed by reference to all the matchers Chris@16: detail::match_state state(begin, end, what, *access::get_regex_impl(re), flags); Chris@16: state.flags_.match_all_ = true; Chris@16: state.sub_match(0).begin_ = begin; Chris@16: Chris@16: if(access::match(re, state)) Chris@16: { Chris@16: access::set_prefix_suffix(what, begin, end); Chris@16: return true; Chris@16: } Chris@16: Chris@16: // handle partial matches Chris@16: else if(state.found_partial_match_ && 0 != (flags & regex_constants::match_partial)) Chris@16: { Chris@16: state.set_partial_match(); Chris@16: return true; Chris@16: } Chris@16: Chris@16: access::reset(what); Chris@16: return false; Chris@16: } Chris@16: } // namespace detail Chris@16: Chris@16: /// \brief See if a regex matches a sequence from beginning to end. Chris@16: /// Chris@16: /// Determines whether there is an exact match between the regular expression \c re, Chris@16: /// and all of the sequence [begin, end). Chris@16: /// Chris@16: /// \pre Type \c BidiIter meets the requirements of a Bidirectional Iterator (24.1.4). Chris@16: /// \pre [begin,end) denotes a valid iterator range. Chris@16: /// \param begin The beginning of the sequence. Chris@16: /// \param end The end of the sequence. Chris@16: /// \param what The \c match_results struct into which the sub_matches will be written Chris@16: /// \param re The regular expression object to use Chris@16: /// \param flags Optional match flags, used to control how the expression is matched Chris@16: /// against the sequence. (See \c match_flag_type.) Chris@16: /// \return \c true if a match is found, \c false otherwise Chris@16: /// \throw regex_error on stack exhaustion Chris@16: template Chris@16: inline bool regex_match Chris@16: ( Chris@16: BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin Chris@16: , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end Chris@16: , match_results &what Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: access::reset(what); Chris@16: return false; Chris@16: } Chris@16: Chris@16: return detail::regex_match_impl(begin, end, what, re, flags); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_match Chris@16: ( Chris@16: BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin Chris@16: , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: ) Chris@16: { Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: // BUGBUG this is inefficient Chris@16: match_results what; Chris@16: return detail::regex_match_impl(begin, end, what, re, flags); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_match Chris@16: ( Chris@16: BOOST_XPR_NONDEDUCED_TYPE_(Char) *begin Chris@16: , match_results &what Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: access::reset(what); Chris@16: return false; Chris@16: } Chris@16: Chris@16: // BUGBUG this is inefficient Chris@16: typedef typename remove_const::type char_type; Chris@16: Char *end = begin + std::char_traits::length(begin); Chris@16: return detail::regex_match_impl(begin, end, what, re, flags); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_match Chris@16: ( Chris@16: BidiRange &rng Chris@16: , match_results &what Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if >::type * = 0 Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: access::reset(what); Chris@16: return false; Chris@16: } Chris@16: Chris@16: // Note that the result iterator of the range must be convertible Chris@16: // to BidiIter here. Chris@16: BidiIter begin = boost::begin(rng), end = boost::end(rng); Chris@16: return detail::regex_match_impl(begin, end, what, re, flags); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_match Chris@16: ( Chris@16: BidiRange const &rng Chris@16: , match_results &what Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if >::type * = 0 Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: access::reset(what); Chris@16: return false; Chris@16: } Chris@16: Chris@16: // Note that the result iterator of the range must be convertible Chris@16: // to BidiIter here. Chris@16: BidiIter begin = boost::begin(rng), end = boost::end(rng); Chris@16: return detail::regex_match_impl(begin, end, what, re, flags); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_match Chris@16: ( Chris@16: BOOST_XPR_NONDEDUCED_TYPE_(Char) *begin Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: ) Chris@16: { Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: // BUGBUG this is inefficient Chris@16: match_results what; Chris@16: typedef typename remove_const::type char_type; Chris@16: Char *end = begin + std::char_traits::length(begin); Chris@16: return detail::regex_match_impl(begin, end, what, re, flags); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_match Chris@16: ( Chris@16: BidiRange &rng Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if >::type * = 0 Chris@16: ) Chris@16: { Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: // BUGBUG this is inefficient Chris@16: match_results what; Chris@16: // Note that the result iterator of the range must be convertible Chris@16: // to BidiIter here. Chris@16: BidiIter begin = boost::begin(rng), end = boost::end(rng); Chris@16: return detail::regex_match_impl(begin, end, what, re, flags); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_match Chris@16: ( Chris@16: BidiRange const &rng Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if >::type * = 0 Chris@16: ) Chris@16: { Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: // BUGBUG this is inefficient Chris@16: match_results what; Chris@16: // Note that the result iterator of the range must be convertible Chris@16: // to BidiIter here. Chris@16: BidiIter begin = boost::begin(rng), end = boost::end(rng); Chris@16: return detail::regex_match_impl(begin, end, what, re, flags); Chris@16: } Chris@16: Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // regex_search Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // regex_search_impl Chris@16: template Chris@16: inline bool regex_search_impl Chris@16: ( Chris@16: match_state &state Chris@16: , basic_regex const &re Chris@16: , bool not_initial_null = false Chris@16: ) Chris@16: { Chris@16: typedef core_access access; Chris@16: match_results &what = *state.context_.results_ptr_; Chris@16: BOOST_ASSERT(0 != re.regex_id()); Chris@16: Chris@16: bool const partial_ok = state.flags_.match_partial_; Chris@16: save_restore not_null(state.flags_.match_not_null_, state.flags_.match_not_null_ || not_initial_null); Chris@16: state.flags_.match_prev_avail_ = state.flags_.match_prev_avail_ || !state.bos(); Chris@16: Chris@16: regex_impl const &impl = *access::get_regex_impl(re); Chris@16: BidiIter const begin = state.cur_, end = state.end_; Chris@16: BidiIter &sub0begin = state.sub_match(0).begin_; Chris@16: sub0begin = state.cur_; Chris@16: Chris@16: // If match_continuous is set, we only need to check for a match at the current position Chris@16: if(state.flags_.match_continuous_) Chris@16: { Chris@16: if(access::match(re, state)) Chris@16: { Chris@16: access::set_prefix_suffix(what, begin, end); Chris@16: return true; Chris@16: } Chris@16: Chris@16: // handle partial matches Chris@16: else if(partial_ok && state.found_partial_match_) Chris@16: { Chris@16: state.set_partial_match(); Chris@16: return true; Chris@16: } Chris@16: } Chris@16: Chris@16: // If we have a finder, use it to find where a potential match can start Chris@16: else if(impl.finder_ && (!partial_ok || impl.finder_->ok_for_partial_matches())) Chris@16: { Chris@16: finder const &find = *impl.finder_; Chris@16: if(find(state)) Chris@16: { Chris@16: if(state.cur_ != begin) Chris@16: { Chris@16: not_null.restore(); Chris@16: } Chris@16: Chris@16: do Chris@16: { Chris@16: sub0begin = state.cur_; Chris@16: if(access::match(re, state)) Chris@16: { Chris@16: access::set_prefix_suffix(what, begin, end); Chris@16: return true; Chris@16: } Chris@16: Chris@16: // handle partial matches Chris@16: else if(partial_ok && state.found_partial_match_) Chris@16: { Chris@16: state.set_partial_match(); Chris@16: return true; Chris@16: } Chris@16: Chris@16: BOOST_ASSERT(state.cur_ == sub0begin); Chris@16: not_null.restore(); Chris@16: } Chris@16: while(state.cur_ != state.end_ && (++state.cur_, find(state))); Chris@16: } Chris@16: } Chris@16: Chris@16: // Otherwise, use brute force search at every position. Chris@16: else Chris@16: { Chris@16: for(;;) Chris@16: { Chris@16: if(access::match(re, state)) Chris@16: { Chris@16: access::set_prefix_suffix(what, begin, end); Chris@16: return true; Chris@16: } Chris@16: Chris@16: // handle partial matches Chris@16: else if(partial_ok && state.found_partial_match_) Chris@16: { Chris@16: state.set_partial_match(); Chris@16: return true; Chris@16: } Chris@16: Chris@16: else if(end == sub0begin) Chris@16: { Chris@16: break; Chris@16: } Chris@16: Chris@16: BOOST_ASSERT(state.cur_ == sub0begin); Chris@16: state.cur_ = ++sub0begin; Chris@16: not_null.restore(); Chris@16: } Chris@16: } Chris@16: Chris@16: access::reset(what); Chris@16: return false; Chris@16: } Chris@16: } // namespace detail Chris@16: Chris@16: Chris@16: /// \brief Determines whether there is some sub-sequence within [begin,end) Chris@16: /// that matches the regular expression \c re. Chris@16: /// Chris@16: /// Determines whether there is some sub-sequence within [begin,end) that matches Chris@16: /// the regular expression \c re. Chris@16: /// Chris@16: /// \pre Type \c BidiIter meets the requirements of a Bidirectional Iterator (24.1.4). Chris@16: /// \pre [begin,end) denotes a valid iterator range. Chris@16: /// \param begin The beginning of the sequence Chris@16: /// \param end The end of the sequence Chris@16: /// \param what The \c match_results struct into which the sub_matches will be written Chris@16: /// \param re The regular expression object to use Chris@16: /// \param flags Optional match flags, used to control how the expression is matched against Chris@16: /// the sequence. (See \c match_flag_type.) Chris@16: /// \return \c true if a match is found, \c false otherwise Chris@16: /// \throw regex_error on stack exhaustion Chris@16: template Chris@16: inline bool regex_search Chris@16: ( Chris@16: BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin Chris@16: , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end Chris@16: , match_results &what Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: Chris@16: // a default-constructed regex matches nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: access::reset(what); Chris@16: return false; Chris@16: } Chris@16: Chris@16: // the state object holds matching state and Chris@16: // is passed by reference to all the matchers Chris@16: detail::match_state state(begin, end, what, *access::get_regex_impl(re), flags); Chris@16: return detail::regex_search_impl(state, re); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_search Chris@16: ( Chris@16: BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin Chris@16: , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: Chris@16: // a default-constructed regex matches nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: // BUGBUG this is inefficient Chris@16: match_results what; Chris@16: // the state object holds matching state and Chris@16: // is passed by reference to all the matchers Chris@16: detail::match_state state(begin, end, what, *access::get_regex_impl(re), flags); Chris@16: return detail::regex_search_impl(state, re); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_search Chris@16: ( Chris@16: BOOST_XPR_NONDEDUCED_TYPE_(Char) *begin Chris@16: , match_results &what Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: Chris@16: // a default-constructed regex matches nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: access::reset(what); Chris@16: return false; Chris@16: } Chris@16: Chris@16: // BUGBUG this is inefficient Chris@16: typedef typename remove_const::type char_type; Chris@16: Char *end = begin + std::char_traits::length(begin); Chris@16: // the state object holds matching state and Chris@16: // is passed by reference to all the matchers Chris@16: detail::match_state state(begin, end, what, *access::get_regex_impl(re), flags); Chris@16: return detail::regex_search_impl(state, re); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_search Chris@16: ( Chris@16: BidiRange &rng Chris@16: , match_results &what Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if >::type * = 0 Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: Chris@16: // a default-constructed regex matches nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: access::reset(what); Chris@16: return false; Chris@16: } Chris@16: Chris@16: // Note that the result iterator of the range must be convertible Chris@16: // to BidiIter here. Chris@16: BidiIter begin = boost::begin(rng), end = boost::end(rng); Chris@16: // the state object holds matching state and Chris@16: // is passed by reference to all the matchers Chris@16: detail::match_state state(begin, end, what, *access::get_regex_impl(re), flags); Chris@16: return detail::regex_search_impl(state, re); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_search Chris@16: ( Chris@16: BidiRange const &rng Chris@16: , match_results &what Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if >::type * = 0 Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: Chris@16: // a default-constructed regex matches nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: access::reset(what); Chris@16: return false; Chris@16: } Chris@16: Chris@16: // Note that the result iterator of the range must be convertible Chris@16: // to BidiIter here. Chris@16: BidiIter begin = boost::begin(rng), end = boost::end(rng); Chris@16: // the state object holds matching state and Chris@16: // is passed by reference to all the matchers Chris@16: detail::match_state state(begin, end, what, *access::get_regex_impl(re), flags); Chris@16: return detail::regex_search_impl(state, re); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_search Chris@16: ( Chris@16: BOOST_XPR_NONDEDUCED_TYPE_(Char) *begin Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: Chris@16: // a default-constructed regex matches nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: // BUGBUG this is inefficient Chris@16: match_results what; Chris@16: // BUGBUG this is inefficient Chris@16: typedef typename remove_const::type char_type; Chris@16: Char *end = begin + std::char_traits::length(begin); Chris@16: // the state object holds matching state and Chris@16: // is passed by reference to all the matchers Chris@16: detail::match_state state(begin, end, what, *access::get_regex_impl(re), flags); Chris@16: return detail::regex_search_impl(state, re); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_search Chris@16: ( Chris@16: BidiRange &rng Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if >::type * = 0 Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: Chris@16: // a default-constructed regex matches nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: // BUGBUG this is inefficient Chris@16: match_results what; Chris@16: // Note that the result iterator of the range must be convertible Chris@16: // to BidiIter here. Chris@16: BidiIter begin = boost::begin(rng), end = boost::end(rng); Chris@16: // the state object holds matching state and Chris@16: // is passed by reference to all the matchers Chris@16: detail::match_state state(begin, end, what, *access::get_regex_impl(re), flags); Chris@16: return detail::regex_search_impl(state, re); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline bool regex_search Chris@16: ( Chris@16: BidiRange const &rng Chris@16: , basic_regex const &re Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if >::type * = 0 Chris@16: ) Chris@16: { Chris@16: typedef detail::core_access access; Chris@16: Chris@16: // a default-constructed regex matches nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: // BUGBUG this is inefficient Chris@16: match_results what; Chris@16: // Note that the result iterator of the range must be convertible Chris@16: // to BidiIter here. Chris@16: BidiIter begin = boost::begin(rng), end = boost::end(rng); Chris@16: // the state object holds matching state and Chris@16: // is passed by reference to all the matchers Chris@16: detail::match_state state(begin, end, what, *access::get_regex_impl(re), flags); Chris@16: return detail::regex_search_impl(state, re); Chris@16: } Chris@16: Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // regex_replace Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // regex_replace_impl Chris@16: template Chris@16: inline OutIter regex_replace_impl Chris@16: ( Chris@16: OutIter out Chris@16: , BidiIter begin Chris@16: , BidiIter end Chris@16: , basic_regex const &re Chris@16: , Formatter const &format Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: ) Chris@16: { Chris@16: using namespace regex_constants; Chris@16: typedef detail::core_access access; Chris@16: BOOST_ASSERT(0 != re.regex_id()); Chris@16: Chris@16: BidiIter cur = begin; Chris@16: match_results what; Chris@16: detail::match_state state(begin, end, what, *access::get_regex_impl(re), flags); Chris@16: bool const yes_copy = (0 == (flags & format_no_copy)); Chris@16: Chris@16: if(detail::regex_search_impl(state, re)) Chris@16: { Chris@16: if(yes_copy) Chris@16: { Chris@16: out = std::copy(cur, what[0].first, out); Chris@16: } Chris@16: Chris@16: out = what.format(out, format, flags); Chris@16: cur = state.cur_ = state.next_search_ = what[0].second; Chris@16: Chris@16: if(0 == (flags & format_first_only)) Chris@16: { Chris@16: bool not_null = (0 == what.length()); Chris@16: state.reset(what, *access::get_regex_impl(re)); Chris@16: while(detail::regex_search_impl(state, re, not_null)) Chris@16: { Chris@16: if(yes_copy) Chris@16: { Chris@16: out = std::copy(cur, what[0].first, out); Chris@16: } Chris@16: Chris@16: access::set_prefix_suffix(what, begin, end); Chris@16: out = what.format(out, format, flags); Chris@16: cur = state.cur_ = state.next_search_ = what[0].second; Chris@16: not_null = (0 == what.length()); Chris@16: state.reset(what, *access::get_regex_impl(re)); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: if(yes_copy) Chris@16: { Chris@16: out = std::copy(cur, end, out); Chris@16: } Chris@16: Chris@16: return out; Chris@16: } Chris@16: } // namespace detail Chris@16: Chris@16: /// \brief Build an output sequence given an input sequence, a regex, and a format string or Chris@16: /// a formatter object, function, or expression. Chris@16: /// Chris@16: /// Constructs a \c regex_iterator object: regex_iterator\< BidiIter \> i(begin, end, re, flags), Chris@16: /// and uses \c i to enumerate through all of the matches m of type match_results\< BidiIter \> that Chris@16: /// occur within the sequence [begin, end). If no such matches are found and !(flags \& format_no_copy) Chris@16: /// then calls std::copy(begin, end, out). Otherwise, for each match found, if !(flags \& format_no_copy) Chris@16: /// calls std::copy(m.prefix().first, m.prefix().second, out), and then calls m.format(out, format, flags). Chris@16: /// Finally if !(flags \& format_no_copy) calls std::copy(last_m.suffix().first, last_m.suffix().second, out) Chris@16: /// where \c last_m is a copy of the last match found. Chris@16: /// Chris@16: /// If flags \& format_first_only is non-zero then only the first match found is replaced. Chris@16: /// Chris@16: /// \pre Type \c BidiIter meets the requirements of a Bidirectional Iterator (24.1.4). Chris@16: /// \pre Type \c OutIter meets the requirements of an Output Iterator (24.1.2). Chris@16: /// \pre Type \c Formatter models \c ForwardRange, Callable\ \>, Chris@16: /// Callable\, OutIter\>, or Chris@16: /// Callable\, OutIter, regex_constants::match_flag_type\>; Chris@16: /// or else it is a null-terminated format string, or an expression template Chris@16: /// representing a formatter lambda expression. Chris@16: /// \pre [begin,end) denotes a valid iterator range. Chris@16: /// \param out An output iterator into which the output sequence is written. Chris@16: /// \param begin The beginning of the input sequence. Chris@16: /// \param end The end of the input sequence. Chris@16: /// \param re The regular expression object to use. Chris@16: /// \param format The format string used to format the replacement sequence, Chris@16: /// or a formatter function, function object, or expression. Chris@16: /// \param flags Optional match flags, used to control how the expression is matched against Chris@16: /// the sequence. (See \c match_flag_type.) Chris@16: /// \return The value of the output iterator after the output sequence has been written to it. Chris@16: /// \throw regex_error on stack exhaustion or invalid format string. Chris@16: template Chris@16: inline OutIter regex_replace Chris@16: ( Chris@16: OutIter out Chris@16: , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin Chris@16: , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end Chris@16: , basic_regex const &re Chris@16: , Formatter const &format Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if >::type * = 0 Chris@16: ) Chris@16: { Chris@16: // Default-constructed regexes match nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: if((0 == (flags & regex_constants::format_no_copy))) Chris@16: { Chris@16: out = std::copy(begin, end, out); Chris@16: } Chris@16: Chris@16: return out; Chris@16: } Chris@16: Chris@16: return detail::regex_replace_impl(out, begin, end, re, format, flags); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline OutIter regex_replace Chris@16: ( Chris@16: OutIter out Chris@16: , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin Chris@16: , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end Chris@16: , basic_regex const &re Chris@16: , typename iterator_value::type const *format Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: ) Chris@16: { Chris@16: // Default-constructed regexes match nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: if((0 == (flags & regex_constants::format_no_copy))) Chris@16: { Chris@16: out = std::copy(begin, end, out); Chris@16: } Chris@16: Chris@16: return out; Chris@16: } Chris@16: Chris@16: return detail::regex_replace_impl(out, begin, end, re, format, flags); Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline BidiContainer regex_replace Chris@16: ( Chris@16: BidiContainer &str Chris@16: , basic_regex const &re Chris@16: , Formatter const &format Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if, detail::is_char_ptr > >::type * = 0 Chris@16: ) Chris@16: { Chris@16: BidiContainer result; Chris@16: // Note that the result iterator of the range must be convertible Chris@16: // to BidiIter here. Chris@16: BidiIter begin = boost::begin(str), end = boost::end(str); Chris@16: Chris@16: // Default-constructed regexes match nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: if((0 == (flags & regex_constants::format_no_copy))) Chris@16: { Chris@16: std::copy(begin, end, std::back_inserter(result)); Chris@16: } Chris@16: Chris@16: return result; Chris@16: } Chris@16: Chris@16: detail::regex_replace_impl(std::back_inserter(result), begin, end, re, format, flags); Chris@16: return result; Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline BidiContainer regex_replace Chris@16: ( Chris@16: BidiContainer const &str Chris@16: , basic_regex const &re Chris@16: , Formatter const &format Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if, detail::is_char_ptr > >::type * = 0 Chris@16: ) Chris@16: { Chris@16: BidiContainer result; Chris@16: // Note that the result iterator of the range must be convertible Chris@16: // to BidiIter here. Chris@16: BidiIter begin = boost::begin(str), end = boost::end(str); Chris@16: Chris@16: // Default-constructed regexes match nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: if((0 == (flags & regex_constants::format_no_copy))) Chris@16: { Chris@16: std::copy(begin, end, std::back_inserter(result)); Chris@16: } Chris@16: Chris@16: return result; Chris@16: } Chris@16: Chris@16: detail::regex_replace_impl(std::back_inserter(result), begin, end, re, format, flags); Chris@16: return result; Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline std::basic_string::type> regex_replace Chris@16: ( Chris@16: BOOST_XPR_NONDEDUCED_TYPE_(Char) *str Chris@16: , basic_regex const &re Chris@16: , Formatter const &format Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if >::type * = 0 Chris@16: ) Chris@16: { Chris@16: typedef typename remove_const::type char_type; Chris@16: std::basic_string result; Chris@16: Chris@16: // Default-constructed regexes match nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: if((0 == (flags & regex_constants::format_no_copy))) Chris@16: { Chris@16: result = str; Chris@16: } Chris@16: Chris@16: return result; Chris@16: } Chris@16: Chris@16: Char *end = str + std::char_traits::length(str); Chris@16: detail::regex_replace_impl(std::back_inserter(result), str, end, re, format, flags); Chris@16: return result; Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline BidiContainer regex_replace Chris@16: ( Chris@16: BidiContainer &str Chris@16: , basic_regex const &re Chris@16: , typename iterator_value::type const *format Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if >::type * = 0 Chris@16: ) Chris@16: { Chris@16: BidiContainer result; Chris@16: // Note that the result iterator of the range must be convertible Chris@16: // to BidiIter here. Chris@16: BidiIter begin = boost::begin(str), end = boost::end(str); Chris@16: Chris@16: // Default-constructed regexes match nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: if((0 == (flags & regex_constants::format_no_copy))) Chris@16: { Chris@16: std::copy(begin, end, std::back_inserter(result)); Chris@16: } Chris@16: Chris@16: return result; Chris@16: } Chris@16: Chris@16: detail::regex_replace_impl(std::back_inserter(result), begin, end, re, format, flags); Chris@16: return result; Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline BidiContainer regex_replace Chris@16: ( Chris@16: BidiContainer const &str Chris@16: , basic_regex const &re Chris@16: , typename iterator_value::type const *format Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: , typename disable_if >::type * = 0 Chris@16: ) Chris@16: { Chris@16: BidiContainer result; Chris@16: // Note that the result iterator of the range must be convertible Chris@16: // to BidiIter here. Chris@16: BidiIter begin = boost::begin(str), end = boost::end(str); Chris@16: Chris@16: // Default-constructed regexes match nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: if((0 == (flags & regex_constants::format_no_copy))) Chris@16: { Chris@16: std::copy(begin, end, std::back_inserter(result)); Chris@16: } Chris@16: Chris@16: return result; Chris@16: } Chris@16: Chris@16: detail::regex_replace_impl(std::back_inserter(result), begin, end, re, format, flags); Chris@16: return result; Chris@16: } Chris@16: Chris@16: /// \overload Chris@16: /// Chris@16: template Chris@16: inline std::basic_string::type> regex_replace Chris@16: ( Chris@16: BOOST_XPR_NONDEDUCED_TYPE_(Char) *str Chris@16: , basic_regex const &re Chris@16: , typename add_const::type *format Chris@16: , regex_constants::match_flag_type flags = regex_constants::match_default Chris@16: ) Chris@16: { Chris@16: typedef typename remove_const::type char_type; Chris@16: std::basic_string result; Chris@16: Chris@16: // Default-constructed regexes match nothing Chris@16: if(0 == re.regex_id()) Chris@16: { Chris@16: if((0 == (flags & regex_constants::format_no_copy))) Chris@16: { Chris@16: result = str; Chris@16: } Chris@16: Chris@16: return result; Chris@16: } Chris@16: Chris@16: Char *end = str + std::char_traits::length(str); Chris@16: detail::regex_replace_impl(std::back_inserter(result), str, end, re, format, flags); Chris@16: return result; Chris@16: } Chris@16: Chris@16: }} // namespace boost::xpressive Chris@16: Chris@16: #endif