📄 regexpr2.cpp
字号:
// Operator implementations
//
// Evaluates the beginning-of-string condition
template< typename CStringsT >
struct bos_t
{
template< typename IterT >
static bool eval( match_param<IterT> const & param, IterT iter )
{
return param.m_ibufferbegin == iter;
}
};
// Find the beginning of a line, either beginning of a string, or the character
// immediately following a newline
template< typename CStringsT >
struct bol_t
{
template< typename IterT >
static bool eval( match_param<IterT> const & param, IterT iter )
{
typedef typename std::iterator_traits<IterT>::value_type char_type;
typedef std::char_traits<char_type> traits_type;
return param.m_ibufferbegin == iter || traits_type::eq( REGEX_CHAR(char_type,'\n'), *--iter );
}
};
// Evaluates end-of-string condition for string's
template< typename CStringsT >
struct eos_t
{
template< typename IterT >
static bool eval( match_param<IterT> const & param, IterT iter )
{
return param.m_iend == iter;
}
};
template<>
struct eos_t<true_t>
{
template< typename IterT >
static bool eval( match_param<IterT> const &, IterT iter )
{
typedef typename std::iterator_traits<IterT>::value_type char_type;
typedef std::char_traits<char_type> traits_type;
return traits_type::eq( *iter, char_type() );
}
};
// Evaluates end-of-line conditions, either the end of the string, or a
// newline character.
template< typename CStringsT >
struct eol_t
{
template< typename IterT >
static bool eval( match_param<IterT> const & param, IterT iter )
{
typedef typename std::iterator_traits<IterT>::value_type char_type;
typedef std::char_traits<char_type> traits_type;
return param.m_iend == iter
|| traits_type::eq( REGEX_CHAR(char_type,'\n'), *iter );
}
};
template<>
struct eol_t<true_t>
{
template< typename IterT >
static bool eval( match_param<IterT> const &, IterT iter )
{
typedef typename std::iterator_traits<IterT>::value_type char_type;
typedef std::char_traits<char_type> traits_type;
return traits_type::eq( *iter, char_type() )
|| traits_type::eq( *iter, REGEX_CHAR(char_type,'\n') );
}
};
// Evaluates perl's end-of-string conditions, either the end of the string, or a
// newline character followed by end of string. ( Only used by $ and /Z assertions )
template< typename CStringsT >
struct peos_t
{
template< typename IterT >
static bool eval( match_param<IterT> const & param, IterT iter )
{
typedef typename std::iterator_traits<IterT>::value_type char_type;
typedef std::char_traits<char_type> traits_type;
return param.m_iend == iter
|| ( traits_type::eq( REGEX_CHAR(char_type,'\n'), *iter ) && param.m_iend == ++iter );
}
};
template<>
struct peos_t<true_t>
{
template< typename IterT >
static bool eval( match_param<IterT> const &, IterT iter )
{
typedef typename std::iterator_traits<IterT>::value_type char_type;
typedef std::char_traits<char_type> traits_type;
return traits_type::eq( *iter, char_type() )
|| ( traits_type::eq( *iter, REGEX_CHAR(char_type,'\n') )
&& traits_type::eq( *++iter, char_type() ) );
}
};
// compare two characters, case-sensitive
template< typename CharT >
struct ch_neq_t
{
typedef CharT char_type;
typedef std::char_traits<char_type> traits_type;
static bool eval( register CharT ch1, register CharT ch2 )
{
return ! traits_type::eq( ch1, ch2 );
}
};
// Compare two characters, disregarding case
template< typename CharT >
struct ch_neq_nocase_t
{
typedef CharT char_type;
typedef std::char_traits<char_type> traits_type;
static bool eval( register CharT ch1, register CharT ch2 )
{
return ! traits_type::eq( regex_toupper( ch1 ), regex_toupper( ch2 ) );
}
};
//
// helper functions for dealing with widths.
//
inline size_t width_add( size_t a, size_t b )
{
return ( size_t( -1 ) == a || size_t( -1 ) == b ? size_t( -1 ) : a + b );
}
inline size_t width_mult( size_t a, size_t b )
{
if( 0 == a || 0 == b )
return 0;
if( size_t( -1 ) == a || size_t( -1 ) == b )
return size_t( -1 );
return a * b;
}
inline bool operator==( width_type const & rhs, width_type const & lhs )
{
return ( rhs.m_min == lhs.m_min && rhs.m_max == lhs.m_max );
}
inline bool operator!=( width_type const & rhs, width_type const & lhs )
{
return ( rhs.m_min != lhs.m_min || rhs.m_max != lhs.m_max );
}
inline width_type operator+( width_type const & rhs, width_type const & lhs )
{
width_type width = { width_add( rhs.m_min, lhs.m_min ), width_add( rhs.m_max, lhs.m_max ) };
return width;
}
inline width_type operator*( width_type const & rhs, width_type const & lhs )
{
width_type width = { width_mult( rhs.m_min, lhs.m_min ), width_mult( rhs.m_max, lhs.m_max ) };
return width;
}
inline width_type & operator+=( width_type & rhs, width_type const & lhs )
{
rhs.m_min = width_add( rhs.m_min, lhs.m_min );
rhs.m_max = width_add( rhs.m_max, lhs.m_max );
return rhs;
}
inline width_type & operator*=( width_type & rhs, width_type const & lhs )
{
rhs.m_min = width_mult( rhs.m_min, lhs.m_min );
rhs.m_max = width_mult( rhs.m_max, lhs.m_max );
return rhs;
}
namespace
{
width_type const zero_width = { 0, 0 };
width_type const worst_width = { 0, size_t( -1 ) };
}
template< typename IterT >
struct width_param
{
std::vector<match_group_base<IterT>*> & m_rggroups;
std::list<size_t> const & m_invisible_groups;
width_type m_width;
width_param
(
std::vector<match_group_base<IterT>*> & rggroups,
std::list<size_t> const & invisible_groups
)
: m_rggroups( rggroups )
, m_invisible_groups( invisible_groups )
, m_width( zero_width )
{
}
private:
width_param & operator=( width_param const & );
};
template< typename CharT >
struct must_have
{
typedef std::basic_string<CharT> string_type;
typedef typename string_type::const_iterator const_iterator;
bool m_has;
const_iterator m_begin;
const_iterator m_end;
CharT const * m_lower;
};
template< typename CharT >
struct peek_param
{
// "chars" is a list of characters. If every alternate in a group
// begins with a character or string literal, the "chars" list can
// be used to speed up the matching of a group.
size_t m_cchars;
union
{
CharT m_rgchars[2];
CharT const * m_pchars;
};
// "must" is a string that must appear in the match. It is used
// to speed up the search.
must_have<CharT> m_must_have;
};
// --------------------------------------------------------------------------
//
// Class: sub_expr
//
// Description: patterns are "compiled" into a directed graph of sub_expr
// structs. Matching is accomplished by traversing this graph.
//
// Methods: sub_expr - construct a sub_expr
// recursive_match_this - does this sub_expr match at the given location
// width_this - what is the width of this sub_expr
// ~sub_expr - recursively delete the sub_expr graph
// next - pointer to the next node in the graph
// next - pointer to the next node in the graph
// recursive_match_next - match the rest of the graph
// recursive_match_all - recursive_match_this and recursive_match_next
// is_assertion - true if this sub_expr is a zero-width assertion
// get_width - find the width of the graph at this sub_expr
//
// Members: m_pnext - pointer to the next node in the graph
//
// History: 8/14/2000 - ericne - Created
//
// --------------------------------------------------------------------------
template< typename IterT >
class sub_expr : public sub_expr_base<IterT>
{
sub_expr * m_pnext;
protected:
// Only derived classes can instantiate sub_expr's
sub_expr()
: m_pnext( 0 )
{
}
public:
typedef IterT iterator_type;
typedef typename std::iterator_traits<IterT>::value_type char_type;
typedef std::char_traits<char_type> traits_type;
virtual ~sub_expr()
{
delete m_pnext;
}
sub_expr ** pnext()
{
return & m_pnext;
}
sub_expr const * next() const
{
return m_pnext;
}
virtual sub_expr * quantify( size_t, size_t, bool, regex_arena & )
{
throw bad_regexpr( "sub-expression cannot be quantified" );
}
// Match this object and all subsequent objects
// If recursive_match_all returns false, it must not change any of param's state
virtual bool recursive_match_all_s( match_param<IterT> & param, IterT icur ) const
{
return ( recursive_match_this_s( param, icur ) && recursive_match_next( param, icur, false_t() ) );
}
virtual bool recursive_match_all_c( match_param<IterT> & param, IterT icur ) const // for C-style strings
{
return ( recursive_match_this_c( param, icur ) && recursive_match_next( param, icur, true_t() ) );
}
// match this object only
virtual bool recursive_match_this_s( match_param<IterT> &, IterT & ) const
{
return true;
}
virtual bool recursive_match_this_c( match_param<IterT> &, IterT & ) const // for C-style strings
{
return true;
}
// Match all subsequent objects
template< typename CStringsT >
bool recursive_match_next( match_param<IterT> & param, IterT icur, CStringsT ) const
{
return m_pnext->recursive_match_all( param, icur, CStringsT() );
}
virtual bool iterative_match_this_s( match_param<IterT> & param ) const
{
param.m_pnext = next();
return true;
}
virtual bool iterative_match_this_c( match_param<IterT> & param ) const // for C-style strings
{
param.m_pnext = next();
return true;
}
virtual bool iterative_rematch_this_s( match_param<IterT> & ) const
{
return false;
}
virtual bool iterative_rematch_this_c( match_param<IterT> & ) const // for C-style strings
{
return false;
}
virtual bool is_assertion() const
{
return false;
}
width_type get_width( width_param<IterT> & param )
{
width_type temp_width = width_this( param );
if( m_pnext )
temp_width += m_pnext->get_width( param );
return temp_width;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -