Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <regex>
00029
00030 namespace std
00031 {
00032
00033 namespace
00034 {
00035
00036
00037 typedef std::stack<std::__regex::_StateIdT,
00038 std::vector<std::__regex::_StateIdT>
00039 > _StateStack;
00040
00041
00042
00043 inline std::__regex::_StateSet
00044 __move(const std::__regex::_PatternCursor& __p,
00045 const std::__regex::_Nfa& __nfa,
00046 const std::__regex::_StateSet& __s)
00047 {
00048 std::__regex::_StateSet __m;
00049 for (std::__regex::_StateSet::const_iterator __i = __s.begin();
00050 __i != __s.end(); ++__i)
00051 {
00052 if (*__i == std::__regex::_S_invalid_state_id)
00053 continue;
00054
00055 const std::__regex::_State& __state = __nfa[*__i];
00056 if (__state._M_opcode == std::__regex::_S_opcode_match
00057 && __state._M_matches(__p))
00058 __m.insert(__state._M_next);
00059 }
00060 return __m;
00061 }
00062
00063
00064 inline bool
00065 __includes_some(const std::__regex::_StateSet& __s,
00066 const std::__regex::_StateSet& __t)
00067 {
00068 if (__s.size() > 0 && __t.size() > 0)
00069 {
00070 std::__regex::_StateSet::const_iterator __first = __s.begin();
00071 std::__regex::_StateSet::const_iterator __second = __t.begin();
00072 while (__first != __s.end() && __second != __t.end())
00073 {
00074 if (*__first < *__second)
00075 ++__first;
00076 else if (*__second < *__first)
00077 ++__second;
00078 else
00079 return true;
00080 }
00081 }
00082 return false;
00083 }
00084
00085
00086
00087 inline void
00088 __add_visited_state(const std::__regex::_StateIdT __u,
00089 _StateStack& __s,
00090 std::__regex::_StateSet& __e)
00091 {
00092 if (__e.count(__u) == 0)
00093 {
00094 __e.insert(__u);
00095 __s.push(__u);
00096 }
00097 }
00098
00099 }
00100
00101 namespace __regex
00102 {
00103 inline _Grep_matcher::
00104 _Grep_matcher(_PatternCursor& __p, _Results& __r,
00105 const _AutomatonPtr& __nfa,
00106 regex_constants::match_flag_type __flags)
00107 : _M_nfa(static_pointer_cast<_Nfa>(__nfa)), _M_pattern(__p), _M_results(__r)
00108 {
00109 __regex::_StateSet __t = this->_M_e_closure(_M_nfa->_M_start());
00110 for (; !_M_pattern._M_at_end(); _M_pattern._M_next())
00111 __t = this->_M_e_closure(__move(_M_pattern, *_M_nfa, __t));
00112
00113 _M_results._M_set_matched(0,
00114 __includes_some(_M_nfa->_M_final_states(), __t));
00115 }
00116
00117
00118 inline _StateSet _Grep_matcher::
00119 _M_e_closure(_StateIdT __i)
00120 {
00121 _StateSet __s;
00122 __s.insert(__i);
00123 _StateStack __stack;
00124 __stack.push(__i);
00125 return this->_M_e_closure(__stack, __s);
00126 }
00127
00128
00129 inline _StateSet _Grep_matcher::
00130 _M_e_closure(const _StateSet& __s)
00131 {
00132 _StateStack __stack;
00133 for (_StateSet::const_iterator __i = __s.begin(); __i != __s.end(); ++__i)
00134 __stack.push(*__i);
00135 return this->_M_e_closure(__stack, __s);
00136 }
00137
00138 inline _StateSet _Grep_matcher::
00139 _M_e_closure(_StateStack& __stack, const _StateSet& __s)
00140 {
00141 _StateSet __e = __s;
00142 while (!__stack.empty())
00143 {
00144 _StateIdT __t = __stack.top(); __stack.pop();
00145 if (__t == _S_invalid_state_id)
00146 continue;
00147
00148 const _State& __state = _M_nfa->operator[](__t);
00149 switch (__state._M_opcode)
00150 {
00151 case _S_opcode_alternative:
00152 __add_visited_state(__state._M_next, __stack, __e);
00153 __add_visited_state(__state._M_alt, __stack, __e);
00154 break;
00155 case _S_opcode_subexpr_begin:
00156 __add_visited_state(__state._M_next, __stack, __e);
00157 __state._M_tagger(_M_pattern, _M_results);
00158 break;
00159 case _S_opcode_subexpr_end:
00160 __add_visited_state(__state._M_next, __stack, __e);
00161 __state._M_tagger(_M_pattern, _M_results);
00162 _M_results._M_set_matched(__state._M_subexpr, true);
00163 break;
00164 case _S_opcode_accept:
00165 __add_visited_state(__state._M_next, __stack, __e);
00166 break;
00167 default:
00168 break;
00169 }
00170 }
00171 return __e;
00172 }
00173
00174 }
00175 }
00176
00177