📄 regexpr2.h
字号:
MODE_MIXED, // Uses a heuristic to automatically determine which algorithm
// is the most appropriate for this pattern.
// MS VC++ has structured exception handling, which makes the
// consequences of a stack overflow much less severe. Because of this,
// it is possible to use the "fast" algorithm always on MS platforms,
#ifdef _MSC_VER
MODE_DEFAULT = MODE_FAST
#else
MODE_DEFAULT = MODE_MIXED
#endif
};
template< typename CH >
void reset_intrinsic_charsets( CH ch = CH( 0 ) );
// This is for implementation details that really belong in the
// cpp file, but can't go there because of template strangeness.
#include "reimpl2.h"
// --------------------------------------------------------------------------
//
// Class: basic_rpattern_base
//
// Description:
//
// Methods: basic_rpattern_base - c'tor
// basic_rpattern_base -
// basic_rpattern_base -
// init - ( re )initialize the pattern
// init -
// set_substitution - set the substitution string
// _find_next_group - parse the next group of the pattern
// _find_next - parse the next sub_expr of the pattern
// _find_atom - parse the next atom of the pattern
// _quantify - quantify the sub_expr
// _common_init - perform some common initialization tasks
// _parse_subst - parse the substitution string
// _add_subst_backref - add a backref node to the subst list
//
// Members: m_invisible_groups - list of hidden groups
//
// Typedefs: syntax_type -
// backref_type -
// backref_vector -
// string_type -
// size_type -
//
// History: 8/14/2000 - ericne - Created
// 8/5/2001 - ericne - complete overhaul
//
// --------------------------------------------------------------------------
template< typename CI, typename SY >
class basic_rpattern_base : protected detail::basic_rpattern_base_impl<CI>
{
typedef detail::basic_rpattern_base_impl<CI> impl;
public:
typedef SY syntax_type;
typedef typename detail::basic_rpattern_base_impl<CI>::char_type char_type;
typedef typename detail::basic_rpattern_base_impl<CI>::traits_type traits_type;
typedef typename detail::basic_rpattern_base_impl<CI>::string_type string_type;
typedef typename detail::basic_rpattern_base_impl<CI>::size_type size_type;
typedef typename detail::basic_rpattern_base_impl<CI>::backref_type backref_type;
typedef typename detail::basic_rpattern_base_impl<CI>::backref_vector backref_vector;
// Work-around for an apparant bug in gcc
struct iter_wrap
{
typename string_type::iterator & ipat;
iter_wrap( typename string_type::iterator & i ) : ipat(i) {}
private:
iter_wrap( iter_wrap const & );
iter_wrap & operator=( iter_wrap const & );
};
void init(
string_type const & pat,
REGEX_FLAGS flags = NOFLAGS,
REGEX_MODE mode = MODE_DEFAULT ); //throw( bad_regexpr, std::bad_alloc );
void init(
string_type const & pat,
string_type const & subst,
REGEX_FLAGS flags = NOFLAGS,
REGEX_MODE mode = MODE_DEFAULT ); //throw( bad_regexpr, std::bad_alloc );
void set_substitution(
string_type const & subst ); //throw( bad_regexpr, std::bad_alloc );
REGEX_FLAGS flags() const { return impl::flags(); }
REGEX_MODE mode() const { return impl::mode(); }
width_type get_width() const { return impl::get_width(); }
size_t cgroups() const { return impl::cgroups(); }
string_type const & get_pat() const { return impl::get_pat(); }
string_type const & get_subst() const { return impl::get_subst(); }
void swap( basic_rpattern_base<CI, SY> & that ) { impl::swap( that ); }
static size_t const npos;
protected:
basic_rpattern_base() //throw()
: detail::basic_rpattern_base_impl<CI>()
{
}
basic_rpattern_base(
basic_rpattern_base<CI, SY> const & that ) //throw()
: detail::basic_rpattern_base_impl<CI>( that.flags(), that.mode(), that.get_pat(), that.get_subst() )
{
// Don't call _normalize_string(). If that.flags()&NORMALIZE,
// then subst has already been normalized.
basic_rpattern_base<CI, SY>::_common_init( m_flags );
basic_rpattern_base<CI, SY>::_parse_subst( *m_subst, m_fuses_backrefs, m_subst_list ); // must come after _common_init
}
explicit basic_rpattern_base(
string_type const & pat,
REGEX_FLAGS flags = NOFLAGS,
REGEX_MODE mode = MODE_DEFAULT ) //throw( bad_regexpr, std::bad_alloc )
: detail::basic_rpattern_base_impl<CI>( flags, mode, pat )
{
basic_rpattern_base<CI, SY>::_common_init( m_flags );
}
basic_rpattern_base(
string_type const & pat,
string_type const & subst,
REGEX_FLAGS flags = NOFLAGS,
REGEX_MODE mode = MODE_DEFAULT ) //throw( bad_regexpr, std::bad_alloc )
: detail::basic_rpattern_base_impl<CI>( flags, mode, pat, subst )
{
basic_rpattern_base<CI, SY>::_common_init( m_flags );
_normalize_string( *m_subst );
basic_rpattern_base<CI, SY>::_parse_subst( *m_subst, m_fuses_backrefs, m_subst_list ); // must come after _common_init
}
basic_rpattern_base & operator=(
basic_rpattern_base<CI, SY> const & that ) //throw( bad_regexpr, std::bad_alloc )
{
basic_rpattern_base<CI, SY> temp( that );
swap( temp );
return *this;
}
detail::match_group_base<CI> * _find_next_group(
iter_wrap & iw,
detail::match_group_base<CI> * pgroup, syntax_type & sy,
std::vector<detail::match_group_base<CI>*> & rggroups );
bool _find_next(
iter_wrap & iw,
detail::match_group_base<CI> * pgroup, syntax_type & sy,
std::vector<detail::match_group_base<CI>*> & rggroups );
void _find_atom(
iter_wrap & iw,
detail::match_group_base<CI> * pgroup,
syntax_type & sy );
void _quantify(
std::auto_ptr<detail::sub_expr<CI> > & pnew,
iter_wrap & iw,
bool is_group,
syntax_type & sy );
void _add_subst_backref(
detail::subst_node & snode,
size_t nbackref,
size_t rstart,
bool & uses_backrefs,
detail::subst_list_type & subst_list ) const;
virtual void _parse_subst(
string_type & subst,
bool & uses_backrefs,
detail::subst_list_type & subst_list ) const;
virtual void _common_init( REGEX_FLAGS flags );
};
template< typename CI, typename SY >
size_t const basic_rpattern_base<CI, SY>::npos = size_t( -1 );
// VC6 is buggy in its handling of the typename keyword
#if defined(_MSC_VER) & _MSC_VER < 1300
# define REGEX_TYPENAME
#else
# define REGEX_TYPENAME typename
#endif
// --------------------------------------------------------------------------
//
// Class: basic_rpattern
//
// Description: generic regex pattern object
//
// Methods: basic_rpattern - c'tor
// basic_rpattern -
// basic_rpattern -
// match - match from begin iter to end iter
// match - match a null-terminated string
// match - match a std::string
// count - count matches from begin iter to end iter
// count - count matches in a null-terminated string
// count - count matches in a std::string
// substitute - do substitutions in a std::string
// _do_match - internal implementation
// _do_count - internal implementation
//
// History: 8/13/2001 - ericne - Created
//
// --------------------------------------------------------------------------
template< typename CI, typename SY = perl_syntax<REGEX_TYPENAME std::iterator_traits<CI>::value_type> >
class basic_rpattern : public basic_rpattern_base<CI, SY>
{
typedef detail::basic_rpattern_base_impl<CI> impl;
public:
typedef typename basic_rpattern_base<CI, SY>::syntax_type syntax_type;
typedef typename basic_rpattern_base<CI, SY>::char_type char_type;
typedef typename basic_rpattern_base<CI, SY>::traits_type traits_type;
typedef typename basic_rpattern_base<CI, SY>::string_type string_type;
typedef typename basic_rpattern_base<CI, SY>::size_type size_type;
typedef typename basic_rpattern_base<CI, SY>::backref_type backref_type;
typedef typename basic_rpattern_base<CI, SY>::backref_vector backref_vector;
basic_rpattern() //throw()
: basic_rpattern_base<CI, SY>()
{
}
basic_rpattern( basic_rpattern const & that )
: basic_rpattern_base<CI, SY>( that )
{
}
explicit basic_rpattern(
string_type const & pat,
REGEX_FLAGS flags = NOFLAGS,
REGEX_MODE mode = MODE_DEFAULT ) //throw( bad_regexpr, std::bad_alloc )
: basic_rpattern_base<CI, SY>( pat, flags, mode )
{
}
basic_rpattern(
string_type const & pat,
string_type const & subst,
REGEX_FLAGS flags = NOFLAGS,
REGEX_MODE mode = MODE_DEFAULT ) //throw( bad_regexpr, std::bad_alloc )
: basic_rpattern_base<CI, SY>( pat, subst, flags, mode )
{
}
basic_rpattern & operator=( basic_rpattern<CI, SY> const & that ) //throw( bad_regexpr, std::bad_alloc )
{
basic_rpattern_base<CI, SY>::operator=( that );
return *this;
}
// CI2 must be convertible to type CI
template< typename CI2 >
backref_type const & match(
CI2 ibegin,
CI2 iend,
basic_match_results<CI> & results ) const
{
// If your compile breaks here, it is because CI2 is not
// convertible to type CI. Check the declaration of your rpattern object.
detail::static_assert< detail::is_convertible<CI2,CI>::value > const iterator_types_are_not_convertible;
( void ) iterator_types_are_not_convertible;
detail::match_param<CI> param = detail::matcher_helper<CI>::init_param( ibegin, iend, results );
return impl::_do_match( param, false ) ? results.backref(0) : detail::static_init<backref_type>::value;
}
template< typename CH >
backref_type const & match(
CH * szbegin,
basic_match_results<CI> & results ) const
{
// If your compile breaks here, it is because CI2 is not
// convertible to type CI. Check the declaration of your rpattern object.
typedef CH * CI2;
detail::static_assert< detail::is_convertible<CI2,CI>::value > const iterator_types_are_not_convertible;
( void ) iterator_types_are_not_convertible;
detail::match_param<CI> param = detail::matcher_helper<CI>::init_param( szbegin, (CH*)0, results );
return impl::_do_match( param, szbegin ) ? results.backref(0) : detail::static_init<backref_type>::value;
}
template< typename CH, typename TR, typename AL >
backref_type const & match(
std::basic_string<CH, TR, AL> const & str,
basic_match_results<CI> & results,
size_type pos,
size_type len ) const
{
// If your compile breaks here, it is because CI2 is not
// convertible to type CI. Check the declaration of your rpattern object.
typedef typename std::basic_string<CH, TR, AL>::const_iterator CI2;
detail::static_assert< detail::is_convertible<CI2,CI>::value > const iterator_types_are_not_convertible;
( void ) iterator_types_are_not_convertible;
detail::match_param<CI> param = detail::matcher_helper<CI>::init_param( str.begin(), str.begin(), results );
if( len == npos || pos + len >= str.size() )
param.istop = CI(str.end());
else
std::advance( param.istop, pos + len );
std::advance( param.istart, pos );
param.ibegin = param.istart;
return impl::_do_match( param, false ) ? results.backref(0) : detail::static_init<backref_type>::value;
}
template< typename CH, typename TR, typename AL >
backref_type const & match(
std::basic_string<CH, TR, AL> const & str,
basic_match_results<CI> & results ) const
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -