]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/regex_executor.tcc
[AArch64] Fix size of memory store for the vst<n>_lane intrinsics
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / regex_executor.tcc
CommitLineData
6cb784b6
TS
1// class template regex -*- C++ -*-
2
3// Copyright (C) 2013 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/**
26 * @file bits/regex_executor.tcc
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{regex}
29 */
30
31namespace std _GLIBCXX_VISIBILITY(default)
32{
33namespace __detail
34{
35_GLIBCXX_BEGIN_NAMESPACE_VERSION
36
9f0d9611
TS
37 template<typename _BiIter, typename _Alloc, typename _TraitsT,
38 bool __dfs_mode>
39 bool _Executor<_BiIter, _Alloc, _TraitsT, __dfs_mode>::
18971f1f
TS
40 _M_search()
41 {
42 if (_M_flags & regex_constants::match_continuous)
43 return _M_search_from_first();
44 auto __cur = _M_begin;
45 do
46 {
9f0d9611
TS
47 _M_current = __cur;
48 if (_M_main<false>())
18971f1f
TS
49 return true;
50 }
51 // Continue when __cur == _M_end
52 while (__cur++ != _M_end);
53 return false;
54 }
55
caaf33fa
TS
56 // This function operates in different modes, DFS mode or BFS mode, indicated
57 // by template parameter __dfs_mode. See _M_main for details.
58 //
59 // ------------------------------------------------------------
60 //
61 // DFS mode:
62 //
63 // It applies a Depth-First-Search (aka backtracking) on given NFA and input
64 // string.
65 // At the very beginning the executor stands in the start state, then it tries
66 // every possible state transition in current state recursively. Some state
67 // transitions consume input string, say, a single-char-matcher or a
68 // back-reference matcher; some don't, like assertion or other anchor nodes.
69 // When the input is exhausted and/or the current state is an accepting state,
70 // the whole executor returns true.
71 //
72 // TODO: This approach is exponentially slow for certain input.
73 // Try to compile the NFA to a DFA.
74 //
75 // Time complexity: o(match_length), O(2^(_M_nfa.size()))
76 // Space complexity: \theta(match_results.size() + match_length)
77 //
78 // ------------------------------------------------------------
79 //
80 // BFS mode:
81 //
82 // Russ Cox's article (http://swtch.com/~rsc/regexp/regexp1.html)
83 // explained this algorithm clearly.
84 //
85 // It first computes epsilon closure for every state that's still matching,
86 // using the same DFS algorithm, but doesn't reenter states (set true in
87 // _M_visited), nor follows _S_opcode_match.
88 //
89 // Then apply DFS using every _S_opcode_match (in _M_match_queue) as the start
90 // state.
91 //
92 // It significantly reduces potential duplicate states, so has a better
93 // upper bound; but it requires more overhead.
94 //
95 // Time complexity: o(match_length * match_results.size())
96 // O(match_length * _M_nfa.size() * match_results.size())
97 // Space complexity: o(_M_nfa.size() + match_results.size())
98 // O(_M_nfa.size() * match_results.size())
9f0d9611
TS
99 template<typename _BiIter, typename _Alloc, typename _TraitsT,
100 bool __dfs_mode>
101 template<bool __match_mode>
102 bool _Executor<_BiIter, _Alloc, _TraitsT, __dfs_mode>::
103 _M_main()
104 {
105 if (__dfs_mode)
106 {
107 _M_has_sol = false;
108 _M_cur_results = _M_results;
109 _M_dfs<__match_mode>(_M_start_state);
110 return _M_has_sol;
111 }
112 else
113 {
9f0d9611
TS
114 _M_match_queue->push(make_pair(_M_start_state, _M_results));
115 bool __ret = false;
116 while (1)
117 {
118 _M_has_sol = false;
119 if (_M_match_queue->empty())
120 break;
121 _M_visited->assign(_M_visited->size(), false);
122 auto _M_old_queue = std::move(*_M_match_queue);
123 while (!_M_old_queue.empty())
124 {
125 auto __task = _M_old_queue.front();
126 _M_old_queue.pop();
127 _M_cur_results = __task.second;
128 _M_dfs<__match_mode>(__task.first);
129 }
130 if (!__match_mode)
131 __ret |= _M_has_sol;
132 if (_M_current == _M_end)
133 break;
134 ++_M_current;
135 }
136 if (__match_mode)
137 __ret = _M_has_sol;
138 return __ret;
139 }
140 }
141
142 // Return whether now match the given sub-NFA.
143 template<typename _BiIter, typename _Alloc, typename _TraitsT,
144 bool __dfs_mode>
145 bool _Executor<_BiIter, _Alloc, _TraitsT, __dfs_mode>::
146 _M_lookahead(_State<_Executor<_BiIter, _Alloc, _TraitsT, __dfs_mode>::
147 _CharT, _TraitsT> __state)
148 {
149 _ResultsVec __what(_M_cur_results.size());
150 auto __sub = std::unique_ptr<_Executor>(new _Executor(_M_current,
151 _M_end,
152 __what,
153 _M_re,
154 _M_flags));
155 __sub->_M_start_state = __state._M_alt;
156 if (__sub->_M_search_from_first())
157 {
158 for (size_t __i = 0; __i < __what.size(); __i++)
159 if (__what[__i].matched)
160 _M_cur_results[__i] = __what[__i];
161 return true;
162 }
163 return false;
164 }
165
9f0d9611
TS
166 template<typename _BiIter, typename _Alloc, typename _TraitsT,
167 bool __dfs_mode>
168 template<bool __match_mode>
169 void _Executor<_BiIter, _Alloc, _TraitsT, __dfs_mode>::
6cb784b6
TS
170 _M_dfs(_StateIdT __i)
171 {
9f0d9611
TS
172 if (!__dfs_mode)
173 {
174 if ((*_M_visited)[__i])
175 return;
176 (*_M_visited)[__i] = true;
177 }
178
179 const auto& __state = _M_nfa[__i];
caaf33fa
TS
180 // Every change on _M_cur_results and _M_current will be rolled back after
181 // finishing the recursion step.
6cb784b6 182 switch (__state._M_opcode)
e280b6ff 183 {
caaf33fa
TS
184 // _M_alt branch is "match once more", while _M_next is "get me out
185 // of this quantifier". Executing _M_next first or _M_alt first don't
186 // mean the same thing, and we need to choose the correct order under
187 // given greedy mode.
e280b6ff 188 case _S_opcode_alternative:
caaf33fa 189 // Greedy.
7b86458e 190 if (!__state._M_neg)
9f0d9611 191 {
caaf33fa 192 // "Once more" is preferred in greedy mode.
9f0d9611 193 _M_dfs<__match_mode>(__state._M_alt);
caaf33fa 194 // If it's DFS executor and already accepted, we're done.
9f0d9611
TS
195 if (!__dfs_mode || !_M_has_sol)
196 _M_dfs<__match_mode>(__state._M_next);
197 }
caaf33fa 198 else // Non-greedy mode
9f0d9611
TS
199 {
200 if (__dfs_mode)
201 {
caaf33fa 202 // vice-versa.
9f0d9611
TS
203 _M_dfs<__match_mode>(__state._M_next);
204 if (!_M_has_sol)
205 _M_dfs<__match_mode>(__state._M_alt);
206 }
207 else
208 {
caaf33fa
TS
209 // DON'T attempt anything, because there's already another
210 // state with higher priority accepted. This state cannot be
211 // better by attempting its next node.
9f0d9611
TS
212 if (!_M_has_sol)
213 {
214 _M_dfs<__match_mode>(__state._M_next);
caaf33fa
TS
215 // DON'T attempt anything if it's already accepted. An
216 // accepted state *must* be better than a solution that
217 // matches a non-greedy quantifier one more time.
9f0d9611
TS
218 if (!_M_has_sol)
219 _M_dfs<__match_mode>(__state._M_alt);
220 }
221 }
222 }
e280b6ff
TS
223 break;
224 case _S_opcode_subexpr_begin:
caaf33fa
TS
225 // If there's nothing changed since last visit, do NOT continue.
226 // This prevents the executor from get into infinite loop when using
227 // "()*" to match "".
b21abcee 228 if (!_M_cur_results[__state._M_subexpr].matched
9f0d9611 229 || _M_cur_results[__state._M_subexpr].first != _M_current)
e280b6ff 230 {
9f0d9611
TS
231 auto& __res = _M_cur_results[__state._M_subexpr];
232 auto __back = __res.first;
233 __res.first = _M_current;
234 _M_dfs<__match_mode>(__state._M_next);
235 __res.first = __back;
e280b6ff
TS
236 }
237 break;
238 case _S_opcode_subexpr_end:
9f0d9611 239 if (_M_cur_results[__state._M_subexpr].second != _M_current
b21abcee 240 || _M_cur_results[__state._M_subexpr].matched != true)
e280b6ff 241 {
9f0d9611
TS
242 auto& __res = _M_cur_results[__state._M_subexpr];
243 auto __back = __res;
244 __res.second = _M_current;
245 __res.matched = true;
246 _M_dfs<__match_mode>(__state._M_next);
247 __res = __back;
e280b6ff
TS
248 }
249 else
9f0d9611 250 _M_dfs<__match_mode>(__state._M_next);
e280b6ff 251 break;
7b86458e 252 case _S_opcode_line_begin_assertion:
9f0d9611
TS
253 if (_M_at_begin())
254 _M_dfs<__match_mode>(__state._M_next);
7b86458e
TS
255 break;
256 case _S_opcode_line_end_assertion:
9f0d9611
TS
257 if (_M_at_end())
258 _M_dfs<__match_mode>(__state._M_next);
7b86458e 259 break;
7b86458e 260 case _S_opcode_word_boundry:
9f0d9611
TS
261 if (_M_word_boundry(__state) == !__state._M_neg)
262 _M_dfs<__match_mode>(__state._M_next);
7b86458e 263 break;
caaf33fa
TS
264 // Here __state._M_alt offers a single start node for a sub-NFA.
265 // We recursively invoke our algorithm to match the sub-NFA.
7b86458e 266 case _S_opcode_subexpr_lookahead:
9f0d9611
TS
267 if (_M_lookahead(__state) == !__state._M_neg)
268 _M_dfs<__match_mode>(__state._M_next);
7b86458e 269 break;
e280b6ff 270 case _S_opcode_match:
9f0d9611 271 if (__dfs_mode)
e280b6ff 272 {
9f0d9611
TS
273 if (_M_current != _M_end && __state._M_matches(*_M_current))
274 {
275 ++_M_current;
276 _M_dfs<__match_mode>(__state._M_next);
277 --_M_current;
278 }
e280b6ff 279 }
9f0d9611
TS
280 else
281 if (__state._M_matches(*_M_current))
282 _M_match_queue->push(make_pair(__state._M_next, _M_cur_results));
e280b6ff 283 break;
b21abcee 284 // First fetch the matched result from _M_cur_results as __submatch;
e280b6ff 285 // then compare it with
caaf33fa
TS
286 // (_M_current, _M_current + (__submatch.second - __submatch.first)).
287 // If matched, keep going; else just return and try another state.
e280b6ff
TS
288 case _S_opcode_backref:
289 {
9f0d9611 290 _GLIBCXX_DEBUG_ASSERT(__dfs_mode);
b21abcee 291 auto& __submatch = _M_cur_results[__state._M_backref_index];
e280b6ff
TS
292 if (!__submatch.matched)
293 break;
9f0d9611 294 auto __last = _M_current;
e280b6ff 295 for (auto __tmp = __submatch.first;
9f0d9611 296 __last != _M_end && __tmp != __submatch.second;
e280b6ff
TS
297 ++__tmp)
298 ++__last;
9f0d9611 299 if (_M_re._M_traits.transform(__submatch.first,
b21abcee 300 __submatch.second)
9f0d9611 301 == _M_re._M_traits.transform(_M_current, __last))
ab1c993b 302 {
9f0d9611 303 if (__last != _M_current)
ab1c993b 304 {
9f0d9611
TS
305 auto __backup = _M_current;
306 _M_current = __last;
307 _M_dfs<__match_mode>(__state._M_next);
308 _M_current = __backup;
ab1c993b
TS
309 }
310 else
9f0d9611 311 _M_dfs<__match_mode>(__state._M_next);
ab1c993b 312 }
e280b6ff
TS
313 }
314 break;
315 case _S_opcode_accept:
9f0d9611 316 if (__dfs_mode)
e280b6ff 317 {
9f0d9611
TS
318 _GLIBCXX_DEBUG_ASSERT(!_M_has_sol);
319 if (__match_mode)
320 _M_has_sol = _M_current == _M_end;
321 else
322 _M_has_sol = true;
323 if (_M_current == _M_begin
324 && (_M_flags & regex_constants::match_not_null))
325 _M_has_sol = false;
326 if (_M_has_sol)
327 _M_results = _M_cur_results;
e280b6ff 328 }
9f0d9611 329 else
18971f1f 330 {
9f0d9611
TS
331 if (_M_current == _M_begin
332 && (_M_flags & regex_constants::match_not_null))
333 break;
334 if (!__match_mode || _M_current == _M_end)
335 if (!_M_has_sol)
336 {
337 _M_has_sol = true;
338 _M_results = _M_cur_results;
339 }
18971f1f 340 }
9f0d9611
TS
341 break;
342 default:
343 _GLIBCXX_DEBUG_ASSERT(false);
e280b6ff 344 }
6cb784b6
TS
345 }
346
b21abcee 347 // Return whether now is at some word boundry.
9f0d9611
TS
348 template<typename _BiIter, typename _Alloc, typename _TraitsT,
349 bool __dfs_mode>
350 bool _Executor<_BiIter, _Alloc, _TraitsT, __dfs_mode>::
b21abcee
TS
351 _M_word_boundry(_State<_CharT, _TraitsT> __state) const
352 {
353 // By definition.
354 bool __ans = false;
355 auto __pre = _M_current;
356 --__pre;
357 if (!(_M_at_begin() && _M_at_end()))
ab1c993b
TS
358 {
359 if (_M_at_begin())
360 __ans = _M_is_word(*_M_current)
361 && !(_M_flags & regex_constants::match_not_bow);
362 else if (_M_at_end())
363 __ans = _M_is_word(*__pre)
364 && !(_M_flags & regex_constants::match_not_eow);
365 else
366 __ans = _M_is_word(*_M_current)
367 != _M_is_word(*__pre);
368 }
b21abcee
TS
369 return __ans;
370 }
371
6cb784b6
TS
372_GLIBCXX_END_NAMESPACE_VERSION
373} // namespace __detail
374} // namespace