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