regex_automaton.h (__detail::_State): Split non-dependent parts into new _State_base.

* include/bits/regex_automaton.h (__detail::_State): Split
	non-dependent parts into new _State_base.
	(__detail::_NFA): Likewise for _NFA_base. Use std::move() to avoid
	copies when inserting _MatcherT and _StateT objects.
	* include/bits/regex_automaton.tcc: Move member definitions to base
	class. Qualify dependent names.
	* include/bits/regex_compiler.h (__detail::_Compiler::_M_get_nfa): Make
	non-const and use std::move to avoid copying.
	* include/bits/regex_compiler.tcc: Likewise.
	* include/bits/regex_executor.h (__detail::_Executor::_M_is_word): Use
	array, so past-the-end iterator is valid.

From-SVN: r204571
This commit is contained in:
Jonathan Wakely 2013-11-08 14:30:22 +00:00 committed by Jonathan Wakely
parent cb3d1e3e87
commit 7d9d218516
6 changed files with 231 additions and 195 deletions

View File

@ -1,3 +1,17 @@
2013-11-08 Jonathan Wakely <jwakely.gcc@gmail.com>
* include/bits/regex_automaton.h (__detail::_State): Split
non-dependent parts into new _State_base.
(__detail::_NFA): Likewise for _NFA_base. Use std::move() to avoid
copies when inserting _MatcherT and _StateT objects.
* include/bits/regex_automaton.tcc: Move member definitions to base
class. Qualify dependent names.
* include/bits/regex_compiler.h (__detail::_Compiler::_M_get_nfa): Make
non-const and use std::move to avoid copying.
* include/bits/regex_compiler.tcc: Likewise.
* include/bits/regex_executor.h (__detail::_Executor::_M_is_word): Use
array, so past-the-end iterator is valid.
2013-11-06 Jonathan Wakely <jwakely.gcc@gmail.com>
* include/bits/regex_automaton.h (_S_opcode_word_boundry): Rename to

View File

@ -65,81 +65,114 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_S_opcode_accept,
};
template<typename _CharT, typename _TraitsT>
class _State
struct _State_base
{
_Opcode _M_opcode; // type of outgoing transition
_StateIdT _M_next; // outgoing transition
union // Since they are mutually exclusive.
{
public:
typedef _Matcher<_CharT> _MatcherT;
_Opcode _M_opcode; // type of outgoing transition
_StateIdT _M_next; // outgoing transition
union // Since they are mutually exclusive.
size_t _M_subexpr; // for _S_opcode_subexpr_*
size_t _M_backref_index; // for _S_opcode_backref
struct
{
size_t _M_subexpr; // for _S_opcode_subexpr_*
size_t _M_backref_index; // for _S_opcode_backref
struct
{
// for _S_opcode_alternative.
_StateIdT _M_quant_index;
// for _S_opcode_alternative or _S_opcode_subexpr_lookahead
_StateIdT _M_alt;
// for _S_opcode_word_boundary or _S_opcode_subexpr_lookahead or
// quantifiers(ungreedy if set true)
bool _M_neg;
};
// for _S_opcode_alternative.
_StateIdT _M_quant_index;
// for _S_opcode_alternative or _S_opcode_subexpr_lookahead
_StateIdT _M_alt;
// for _S_opcode_word_boundary or _S_opcode_subexpr_lookahead or
// quantifiers (ungreedy if set true)
bool _M_neg;
};
_MatcherT _M_matches; // for _S_opcode_match
explicit _State(_Opcode __opcode)
: _M_opcode(__opcode), _M_next(_S_invalid_state_id)
{ }
#ifdef _GLIBCXX_DEBUG
std::ostream&
_M_print(std::ostream& ostr) const;
// Prints graphviz dot commands for state.
std::ostream&
_M_dot(std::ostream& __ostr, _StateIdT __id) const;
#endif
};
explicit _State_base(_Opcode __opcode)
: _M_opcode(__opcode), _M_next(_S_invalid_state_id)
{ }
protected:
~_State_base() = default;
public:
#ifdef _GLIBCXX_DEBUG
std::ostream&
_M_print(std::ostream& ostr) const;
// Prints graphviz dot commands for state.
std::ostream&
_M_dot(std::ostream& __ostr, _StateIdT __id) const;
#endif
};
template<typename _CharT, typename _TraitsT>
class _NFA
: public std::vector<_State<_CharT, _TraitsT>>
struct _State : _State_base
{
public:
typedef _State<_CharT, _TraitsT> _StateT;
typedef const _Matcher<_CharT>& _MatcherT;
typedef size_t _SizeT;
typedef regex_constants::syntax_option_type _FlagT;
typedef _Matcher<_CharT> _MatcherT;
_NFA(_FlagT __f)
: _M_flags(__f), _M_start_state(0), _M_subexpr_count(0),
_M_quant_count(0), _M_has_backref(false)
{ }
_MatcherT _M_matches; // for _S_opcode_match
_FlagT
_M_options() const
{ return _M_flags; }
explicit _State(_Opcode __opcode) : _State_base(__opcode) { }
};
_StateIdT
_M_start() const
{ return _M_start_state; }
struct _NFA_base
{
typedef size_t _SizeT;
typedef regex_constants::syntax_option_type _FlagT;
const _StateSet&
_M_final_states() const
{ return _M_accepting_states; }
explicit
_NFA_base(_FlagT __f)
: _M_flags(__f), _M_start_state(0), _M_subexpr_count(0),
_M_quant_count(0), _M_has_backref(false)
{ }
_SizeT
_M_sub_count() const
{ return _M_subexpr_count; }
_NFA_base(_NFA_base&&) = default;
protected:
~_NFA_base() = default;
public:
_FlagT
_M_options() const
{ return _M_flags; }
_StateIdT
_M_start() const
{ return _M_start_state; }
const _StateSet&
_M_final_states() const
{ return _M_accepting_states; }
_SizeT
_M_sub_count() const
{ return _M_subexpr_count; }
std::vector<size_t> _M_paren_stack;
_StateSet _M_accepting_states;
_FlagT _M_flags;
_StateIdT _M_start_state;
_SizeT _M_subexpr_count;
_SizeT _M_quant_count;
bool _M_has_backref;
};
template<typename _CharT, typename _TraitsT>
struct _NFA
: _NFA_base, std::vector<_State<_CharT, _TraitsT>>
{
typedef _State<_CharT, _TraitsT> _StateT;
typedef _Matcher<_CharT> _MatcherT;
using _NFA_base::_NFA_base;
// for performance reasons _NFA objects should only be moved not copied
_NFA(const _NFA&) = delete;
_NFA(_NFA&&) = default;
_StateIdT
_M_insert_accept()
{
auto __ret = _M_insert_state(_StateT(_S_opcode_accept));
_M_accepting_states.insert(__ret);
this->_M_accepting_states.insert(__ret);
return __ret;
}
@ -149,38 +182,38 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_StateT __tmp(_S_opcode_alternative);
// It labels every quantifier to make greedy comparison easier in BFS
// approach.
__tmp._M_quant_index = _M_quant_count++;
__tmp._M_quant_index = this->_M_quant_count++;
__tmp._M_next = __next;
__tmp._M_alt = __alt;
__tmp._M_neg = __neg;
return _M_insert_state(__tmp);
return _M_insert_state(std::move(__tmp));
}
_StateIdT
_M_insert_matcher(_MatcherT __m)
{
_StateT __tmp(_S_opcode_match);
__tmp._M_matches = __m;
return _M_insert_state(__tmp);
__tmp._M_matches = std::move(__m);
return _M_insert_state(std::move(__tmp));
}
_StateIdT
_M_insert_subexpr_begin()
{
auto __id = _M_subexpr_count++;
_M_paren_stack.push_back(__id);
auto __id = this->_M_subexpr_count++;
this->_M_paren_stack.push_back(__id);
_StateT __tmp(_S_opcode_subexpr_begin);
__tmp._M_subexpr = __id;
return _M_insert_state(__tmp);
return _M_insert_state(std::move(__tmp));
}
_StateIdT
_M_insert_subexpr_end()
{
_StateT __tmp(_S_opcode_subexpr_end);
__tmp._M_subexpr = _M_paren_stack.back();
_M_paren_stack.pop_back();
return _M_insert_state(__tmp);
__tmp._M_subexpr = this->_M_paren_stack.back();
this->_M_paren_stack.pop_back();
return _M_insert_state(std::move(__tmp));
}
_StateIdT
@ -199,7 +232,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
_StateT __tmp(_S_opcode_word_boundary);
__tmp._M_neg = __neg;
return _M_insert_state(__tmp);
return _M_insert_state(std::move(__tmp));
}
_StateIdT
@ -208,7 +241,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_StateT __tmp(_S_opcode_subexpr_lookahead);
__tmp._M_alt = __alt;
__tmp._M_neg = __neg;
return _M_insert_state(__tmp);
return _M_insert_state(std::move(__tmp));
}
_StateIdT
@ -218,7 +251,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_StateIdT
_M_insert_state(_StateT __s)
{
this->push_back(__s);
this->push_back(std::move(__s));
return this->size()-1;
}
@ -230,14 +263,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
std::ostream&
_M_dot(std::ostream& __ostr) const;
#endif
std::vector<size_t> _M_paren_stack;
_StateSet _M_accepting_states;
_FlagT _M_flags;
_StateIdT _M_start_state;
_SizeT _M_subexpr_count;
_SizeT _M_quant_count;
bool _M_has_backref;
};
/// Describes a sequence of one or more %_State, its current start
@ -251,7 +276,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
public:
_StateSeq(_RegexT& __nfa, _StateIdT __s)
: _StateSeq(__nfa, __s, __s)
: _M_nfa(__nfa), _M_start(__s), _M_end(__s)
{ }
_StateSeq(_RegexT& __nfa, _StateIdT __s, _StateIdT __end)

View File

@ -35,118 +35,116 @@ namespace __detail
_GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifdef _GLIBCXX_DEBUG
template<typename _CharT, typename _TraitsT>
std::ostream& _State<_CharT, _TraitsT>::
_M_print(std::ostream& ostr) const
std::ostream&
_State_base::_M_print(std::ostream& ostr) const
{
switch (_M_opcode)
{
switch (_M_opcode)
{
case _S_opcode_alternative:
ostr << "alt next=" << _M_next << " alt=" << _M_alt;
break;
case _S_opcode_subexpr_begin:
ostr << "subexpr begin next=" << _M_next << " index=" << _M_subexpr;
break;
case _S_opcode_subexpr_end:
ostr << "subexpr end next=" << _M_next << " index=" << _M_subexpr;
break;
case _S_opcode_backref:
ostr << "backref next=" << _M_next << " index=" << _M_backref_index;
break;
case _S_opcode_match:
ostr << "match next=" << _M_next;
break;
case _S_opcode_accept:
ostr << "accept next=" << _M_next;
break;
default:
ostr << "unknown next=" << _M_next;
break;
}
return ostr;
case _S_opcode_alternative:
ostr << "alt next=" << _M_next << " alt=" << _M_alt;
break;
case _S_opcode_subexpr_begin:
ostr << "subexpr begin next=" << _M_next << " index=" << _M_subexpr;
break;
case _S_opcode_subexpr_end:
ostr << "subexpr end next=" << _M_next << " index=" << _M_subexpr;
break;
case _S_opcode_backref:
ostr << "backref next=" << _M_next << " index=" << _M_backref_index;
break;
case _S_opcode_match:
ostr << "match next=" << _M_next;
break;
case _S_opcode_accept:
ostr << "accept next=" << _M_next;
break;
default:
ostr << "unknown next=" << _M_next;
break;
}
return ostr;
}
// Prints graphviz dot commands for state.
template<typename _CharT, typename _TraitsT>
std::ostream& _State<_CharT, _TraitsT>::
_M_dot(std::ostream& __ostr, _StateIdT __id) const
std::ostream&
_State_base::_M_dot(std::ostream& __ostr, _StateIdT __id) const
{
switch (_M_opcode)
{
switch (_M_opcode)
{
case _S_opcode_alternative:
__ostr << __id << " [label=\"" << __id << "\\nALT\"];\n"
<< __id << " -> " << _M_next
<< " [label=\"epsilon\", tailport=\"s\"];\n"
<< __id << " -> " << _M_alt
<< " [label=\"epsilon\", tailport=\"n\"];\n";
break;
case _S_opcode_backref:
__ostr << __id << " [label=\"" << __id << "\\nBACKREF "
<< _M_subexpr << "\"];\n"
<< __id << " -> " << _M_next << " [label=\"<match>\"];\n";
break;
case _S_opcode_line_begin_assertion:
__ostr << __id << " [label=\"" << __id << "\\nLINE_BEGIN \"];\n"
<< __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
break;
case _S_opcode_line_end_assertion:
__ostr << __id << " [label=\"" << __id << "\\nLINE_END \"];\n"
<< __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
break;
case _S_opcode_word_boundary:
__ostr << __id << " [label=\"" << __id << "\\nWORD_BOUNDRY "
<< _M_neg << "\"];\n"
<< __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
break;
case _S_opcode_subexpr_lookahead:
__ostr << __id << " [label=\"" << __id << "\\nLOOK_AHEAD\"];\n"
<< __id << " -> " << _M_next
<< " [label=\"epsilon\", tailport=\"s\"];\n"
<< __id << " -> " << _M_alt
<< " [label=\"<assert>\", tailport=\"n\"];\n";
break;
case _S_opcode_subexpr_begin:
__ostr << __id << " [label=\"" << __id << "\\nSBEGIN "
<< _M_subexpr << "\"];\n"
<< __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
break;
case _S_opcode_subexpr_end:
__ostr << __id << " [label=\"" << __id << "\\nSEND "
<< _M_subexpr << "\"];\n"
<< __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
break;
case _S_opcode_dummy:
break;
case _S_opcode_match:
__ostr << __id << " [label=\"" << __id << "\\nMATCH\"];\n"
<< __id << " -> " << _M_next << " [label=\"<match>\"];\n";
break;
case _S_opcode_accept:
__ostr << __id << " [label=\"" << __id << "\\nACC\"];\n" ;
break;
default:
_GLIBCXX_DEBUG_ASSERT(false);
break;
}
return __ostr;
case _S_opcode_alternative:
__ostr << __id << " [label=\"" << __id << "\\nALT\"];\n"
<< __id << " -> " << _M_next
<< " [label=\"epsilon\", tailport=\"s\"];\n"
<< __id << " -> " << _M_alt
<< " [label=\"epsilon\", tailport=\"n\"];\n";
break;
case _S_opcode_backref:
__ostr << __id << " [label=\"" << __id << "\\nBACKREF "
<< _M_subexpr << "\"];\n"
<< __id << " -> " << _M_next << " [label=\"<match>\"];\n";
break;
case _S_opcode_line_begin_assertion:
__ostr << __id << " [label=\"" << __id << "\\nLINE_BEGIN \"];\n"
<< __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
break;
case _S_opcode_line_end_assertion:
__ostr << __id << " [label=\"" << __id << "\\nLINE_END \"];\n"
<< __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
break;
case _S_opcode_word_boundary:
__ostr << __id << " [label=\"" << __id << "\\nWORD_BOUNDRY "
<< _M_neg << "\"];\n"
<< __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
break;
case _S_opcode_subexpr_lookahead:
__ostr << __id << " [label=\"" << __id << "\\nLOOK_AHEAD\"];\n"
<< __id << " -> " << _M_next
<< " [label=\"epsilon\", tailport=\"s\"];\n"
<< __id << " -> " << _M_alt
<< " [label=\"<assert>\", tailport=\"n\"];\n";
break;
case _S_opcode_subexpr_begin:
__ostr << __id << " [label=\"" << __id << "\\nSBEGIN "
<< _M_subexpr << "\"];\n"
<< __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
break;
case _S_opcode_subexpr_end:
__ostr << __id << " [label=\"" << __id << "\\nSEND "
<< _M_subexpr << "\"];\n"
<< __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
break;
case _S_opcode_dummy:
break;
case _S_opcode_match:
__ostr << __id << " [label=\"" << __id << "\\nMATCH\"];\n"
<< __id << " -> " << _M_next << " [label=\"<match>\"];\n";
break;
case _S_opcode_accept:
__ostr << __id << " [label=\"" << __id << "\\nACC\"];\n" ;
break;
default:
_GLIBCXX_DEBUG_ASSERT(false);
break;
}
return __ostr;
}
template<typename _CharT, typename _TraitsT>
std::ostream& _NFA<_CharT, _TraitsT>::
_M_dot(std::ostream& __ostr) const
std::ostream&
_NFA<_CharT, _TraitsT>::_M_dot(std::ostream& __ostr) const
{
__ostr << "digraph _Nfa {\n"
<< " rankdir=LR;\n";
" rankdir=LR;\n";
for (size_t __i = 0; __i < this->size(); ++__i)
{ this->at(__i)._M_dot(__ostr, __i); }
(*this)[__i]._M_dot(__ostr, __i);
__ostr << "}\n";
return __ostr;
}
#endif
template<typename _CharT, typename _TraitsT>
_StateIdT _NFA<_CharT, _TraitsT>::
_M_insert_backref(size_t __index)
_StateIdT
_NFA<_CharT, _TraitsT>::_M_insert_backref(size_t __index)
{
// To figure out whether a backref is valid, a stack is used to store
// unfinished sub-expressions. For example, when parsing
@ -157,18 +155,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// time, "\\2" is valid, but "\\1" and "\\3" are not.
if (__index >= _M_subexpr_count)
__throw_regex_error(regex_constants::error_backref);
for (auto __it : _M_paren_stack)
for (auto __it : this->_M_paren_stack)
if (__index == __it)
__throw_regex_error(regex_constants::error_backref);
_M_has_backref = true;
this->_M_has_backref = true;
_StateT __tmp(_S_opcode_backref);
__tmp._M_backref_index = __index;
return _M_insert_state(__tmp);
return _M_insert_state(std::move(__tmp));
}
template<typename _CharT, typename _TraitsT>
void _NFA<_CharT, _TraitsT>::
_M_eliminate_dummy()
void
_NFA<_CharT, _TraitsT>::_M_eliminate_dummy()
{
for (auto& __it : *this)
{
@ -185,8 +183,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Just apply DFS on the sequence and re-link their links.
template<typename _CharT, typename _TraitsT>
_StateSeq<_CharT, _TraitsT> _StateSeq<_CharT, _TraitsT>::
_M_clone()
_StateSeq<_CharT, _TraitsT>
_StateSeq<_CharT, _TraitsT>::_M_clone()
{
std::map<_StateIdT, _StateIdT> __m;
std::stack<_StateIdT> __stack;

View File

@ -55,8 +55,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const _TraitsT& __traits, _FlagT __flags);
std::shared_ptr<_RegexT>
_M_get_nfa() const
{ return make_shared<_RegexT>(_M_nfa); }
_M_get_nfa()
{ return make_shared<_RegexT>(std::move(_M_nfa)); }
private:
typedef _Scanner<_FwdIter> _ScannerT;

View File

@ -147,11 +147,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_assertion()
{
if (_M_match_token(_ScannerT::_S_token_line_begin))
_M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
_M_insert_line_begin()));
_M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_line_begin()));
else if (_M_match_token(_ScannerT::_S_token_line_end))
_M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
_M_insert_line_end()));
_M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_line_end()));
else if (_M_match_token(_ScannerT::_S_token_word_bound))
// _M_value[0] == 'n' means it's negtive, say "not word boundary".
_M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
@ -305,7 +303,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_traits, _M_flags);
__matcher._M_add_character_class(_M_value);
_M_stack.push(_StateSeqT(_M_nfa,
_M_nfa._M_insert_matcher(__matcher)));
_M_nfa._M_insert_matcher(std::move(__matcher))));
}
else if (_M_match_token(_ScannerT::_S_token_subexpr_no_group_begin))
{
@ -343,7 +341,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_BMatcherT __matcher(__neg, _M_traits, _M_flags);
while (!_M_match_token(_ScannerT::_S_token_bracket_end))
_M_expression_term(__matcher);
_M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_matcher(__matcher)));
_M_stack.push(_StateSeqT(_M_nfa,
_M_nfa._M_insert_matcher(std::move(__matcher))));
return true;
}
@ -432,8 +431,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _CharT, typename _TraitsT>
bool _BracketMatcher<_CharT, _TraitsT>::
operator()(_CharT __ch) const
bool
_BracketMatcher<_CharT, _TraitsT>::operator()(_CharT __ch) const
{
bool __ret = false;
if (_M_traits.isctype(__ch, _M_class_set)

View File

@ -53,7 +53,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/
template<typename _BiIter, typename _Alloc, typename _TraitsT,
bool __dfs_mode>
bool __dfs_mode>
class _Executor
{
public:
@ -117,9 +117,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
bool
_M_is_word(_CharT __ch) const
{
static const _CharT __s = 'w';
static const _CharT __s[2] = { 'w' };
return _M_re._M_traits.isctype
(__ch, _M_re._M_traits.lookup_classname(&__s, &__s+1));
(__ch, _M_re._M_traits.lookup_classname(__s, __s+1));
}
bool