libstdc++
regex_executor.tcc
Go to the documentation of this file.
1// class template regex -*- C++ -*-
2
3// Copyright (C) 2013-2026 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{
33_GLIBCXX_BEGIN_NAMESPACE_VERSION
34
35#pragma GCC diagnostic push
36#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
37namespace __detail
38{
39_GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2)
40 template<typename _BiIter, typename _Alloc, typename _TraitsT,
41 bool __dfs_mode>
44 {
45 if (_M_search_from_first())
46 return true;
48 return false;
50 while (_M_begin != _M_end)
51 {
52 ++_M_begin;
53 if (_M_search_from_first())
54 return true;
55 }
56 return false;
57 }
58
59 enum _ExecutorFrameOpcode : unsigned char
60 {
61 _S_fopcode_next,
62 _S_fopcode_fallback_next,
63 _S_fopcode_rep_once_more,
64 _S_fopcode_fallback_rep_once_more,
65 _S_fopcode_posix_alternative,
66 _S_fopcode_merge_sol,
67 _S_fopcode_restore_cur_results,
68 _S_fopcode_restore_rep_count,
69 _S_fopcode_decrement_rep_count,
70 };
71
72#pragma GCC diagnostic push
73#pragma GCC diagnostic ignored "-Wpedantic" // anon struct
74 struct _ExecutorFrameBase
75 {
76 _ExecutorFrameBase(_ExecutorFrameOpcode __op, _StateIdT __i)
77 : _M_op(__op), _M_state_id(__i)
78 { }
79
80 _ExecutorFrameOpcode _M_op;
81 union {
82 unsigned char _M_byte0;
83 struct { // Used by restore_rep_count frame
84 unsigned char _M_count : 2;
85 };
86 struct { // Used by restore_cur_results frame
87 unsigned char _M_end : 1;
88 unsigned char _M_matched : 1;
89 };
90 };
91 unsigned char _M_bytes[6];
92 _StateIdT _M_state_id;
93 };
94#pragma GCC diagnostic pop
95
96 template<typename _BiIter, bool _Trivial /* = is_trivially_copyable<_BiIter>::value */>
97 struct _ExecutorFrame : _ExecutorFrameBase
98 {
99 _ExecutorFrame(_ExecutorFrameOpcode __op, _StateIdT __i)
100 : _ExecutorFrameBase(__op, __i)
101 { }
102
103 _ExecutorFrame(_ExecutorFrameOpcode __op, _StateIdT __i, _BiIter __p)
104 : _ExecutorFrameBase(__op, __i), _M_pos(__p)
105 { }
106
107 _ExecutorFrame(_ExecutorFrameOpcode __op, _StateIdT __i, long __v)
108 : _ExecutorFrameBase(__op, __i), _M_val(__v)
109 { }
110
111 // _M_pos and _M_val are mutually exclusive, which the optimized
112 // partial specialization below depends on.
113 _BiIter _M_pos = _BiIter();
114 long _M_val = 0;
115 };
116
117 // Space-optimized partial specialization for when the input iterator is
118 // trivially copyable.
119 template<typename _BiIter>
120 struct _ExecutorFrame<_BiIter, true> : _ExecutorFrameBase
121 {
122 _ExecutorFrame(_ExecutorFrameOpcode __op, _StateIdT __i)
123 : _ExecutorFrameBase(__op, __i)
124 { }
125
126 _ExecutorFrame(_ExecutorFrameOpcode __op, _StateIdT __i, _BiIter __p)
127 : _ExecutorFrameBase(__op, __i), _M_pos(__p)
128 { }
129
130 _ExecutorFrame(_ExecutorFrameOpcode __op, _StateIdT __i, long __v)
131 : _ExecutorFrameBase(__op, __i), _M_val(__v)
132 { }
133
134 union {
135 _BiIter _M_pos;
136 long _M_val;
137 };
138 };
139
140 // The _M_main function operates in different modes, DFS mode or BFS mode,
141 // indicated by template parameter __dfs_mode, and dispatches to one of the
142 // _M_main_dispatch overloads.
143 //
144 // ------------------------------------------------------------
145 //
146 // DFS mode:
147 //
148 // It applies a Depth-First-Search (aka backtracking) on given NFA and input
149 // string.
150 // At the very beginning the executor stands in the start state, then it
151 // tries every possible state transition in current state recursively. Some
152 // state transitions consume input string, say, a single-char-matcher or a
153 // back-reference matcher; some don't, like assertion or other anchor nodes.
154 // When the input is exhausted and/or the current state is an accepting
155 // state, the whole executor returns true.
156 //
157 // TODO: This approach is exponentially slow for certain input.
158 // Try to compile the NFA to a DFA.
159 //
160 // Time complexity: \Omega(match_length), O(2^(_M_nfa.size()))
161 // Space complexity: \theta(match_results.size() + match_length)
162 //
163 template<typename _BiIter, typename _Alloc, typename _TraitsT,
164 bool __dfs_mode>
166 _M_main_dispatch(_Match_mode __match_mode, __dfs)
167 {
168 _M_has_sol = false;
169 *_M_states._M_get_sol_pos() = _BiIter();
170 _M_cur_results = _M_results;
171 _M_dfs(__match_mode, _M_states._M_start);
172 return _M_has_sol;
173 }
174
175 // ------------------------------------------------------------
176 //
177 // BFS mode:
178 //
179 // Russ Cox's article (http://swtch.com/~rsc/regexp/regexp1.html)
180 // explained this algorithm clearly.
181 //
182 // It first computes epsilon closure (states that can be achieved without
183 // consuming characters) for every state that's still matching,
184 // using the same DFS algorithm, but doesn't re-enter states (using
185 // _M_states._M_visited to check), nor follow _S_opcode_match.
186 //
187 // Then apply DFS using every _S_opcode_match (in _M_states._M_match_queue)
188 // as the start state.
189 //
190 // It significantly reduces potential duplicate states, so has a better
191 // upper bound; but it requires more overhead.
192 //
193 // Time complexity: \Omega(match_length * match_results.size())
194 // O(match_length * _M_nfa.size() * match_results.size())
195 // Space complexity: \Omega(_M_nfa.size() + match_results.size())
196 // O(_M_nfa.size() * match_results.size())
197 template<typename _BiIter, typename _Alloc, typename _TraitsT,
198 bool __dfs_mode>
200 _M_main_dispatch(_Match_mode __match_mode, __bfs)
201 {
202 _M_states._M_queue(_M_states._M_start, _M_results);
203 bool __ret = false;
204 while (1)
205 {
206 _M_has_sol = false;
207 if (_M_states._M_match_queue.empty())
208 break;
209 std::fill_n(_M_states._M_visited_states, _M_nfa.size(), false);
210 auto __old_queue = std::move(_M_states._M_match_queue);
211 auto __alloc = _M_cur_results.get_allocator();
212 for (auto& __task : __old_queue)
213 {
214 _M_cur_results = _ResultsVec(std::move(__task.second), __alloc);
215 _M_dfs(__match_mode, __task.first);
216 }
217 if (__match_mode == _Match_mode::_Prefix)
218 __ret |= _M_has_sol;
219 if (_M_current == _M_end)
220 break;
221 ++_M_current;
222 }
223 if (__match_mode == _Match_mode::_Exact)
224 __ret = _M_has_sol;
225 _M_states._M_match_queue.clear();
226 return __ret;
227 }
228
229 // Return whether now match the given sub-NFA.
230 template<typename _BiIter, typename _Alloc, typename _TraitsT,
231 bool __dfs_mode>
233 _M_lookahead(_StateIdT __next)
234 {
235 // Backreferences may refer to captured content.
236 // We may want to make this faster by not copying,
237 // but let's not be clever prematurely.
238 _ResultsVec __what(_M_cur_results);
239 _Executor __sub(_M_current, _M_end, __what, _M_re, _M_flags);
240 __sub._M_states._M_start = __next;
241 if (__sub._M_search_from_first())
242 {
243 for (size_t __i = 0; __i < __what.size(); __i++)
244 if (__what[__i].matched)
245 _M_cur_results[__i] = __what[__i];
246 return true;
247 }
248 return false;
249 }
250
251 // __rep_count records how many times (__rep_count.second)
252 // this node is visited under certain input iterator
253 // (__rep_count.first). This prevent the executor from entering
254 // infinite loop by refusing to continue when it's already been
255 // visited more than twice. It's `twice` instead of `once` because
256 // we need to spare one more time for potential group capture.
257 template<typename _BiIter, typename _Alloc, typename _TraitsT,
258 bool __dfs_mode>
260 _M_rep_once_more(_Match_mode __match_mode, _StateIdT __i)
261 {
262 const auto& __state = _M_nfa[__i];
263 auto& __rep_count = _M_rep_count[__i];
264 if (__rep_count.second == 0 || __rep_count.first != _M_current)
265 {
266 _M_frames.emplace_back(_S_fopcode_restore_rep_count,
267 __i, __rep_count.first);
268 _M_frames.back()._M_count = __rep_count.second;
269 __rep_count.first = _M_current;
270 __rep_count.second = 1;
271 _M_frames.emplace_back(_S_fopcode_next, __state._M_alt);
272 }
273 else
274 {
275 if (__rep_count.second < 2)
276 {
277 __rep_count.second++;
278 _M_frames.emplace_back(_S_fopcode_decrement_rep_count, __i);
279 _M_frames.emplace_back(_S_fopcode_next, __state._M_alt);
280 }
281 }
282 }
283
284 // _M_alt branch is "match once more", while _M_next is "get me out
285 // of this quantifier". Executing _M_next first or _M_alt first don't
286 // mean the same thing, and we need to choose the correct order under
287 // given greedy mode.
288 template<typename _BiIter, typename _Alloc, typename _TraitsT,
289 bool __dfs_mode>
291 _M_handle_repeat(_Match_mode __match_mode, _StateIdT __i)
292 {
293 const auto& __state = _M_nfa[__i];
294 // Greedy.
295 if (!__state._M_neg)
296 {
297 if constexpr (__dfs_mode)
298 // If it's DFS executor and already accepted, we're done.
299 _M_frames.emplace_back(_S_fopcode_fallback_next, __state._M_next,
300 _M_current);
301 else
302 _M_frames.emplace_back(_S_fopcode_next, __state._M_next);
303 _M_frames.emplace_back(_S_fopcode_rep_once_more, __i);
304 }
305 else // Non-greedy mode
306 {
307 if constexpr (__dfs_mode)
308 {
309 // vice-versa.
310 _M_frames.emplace_back(_S_fopcode_fallback_rep_once_more, __i,
311 _M_current);
312 _M_frames.emplace_back(_S_fopcode_next, __state._M_next);
313 }
314 else
315 {
316 // DON'T attempt anything, because there's already another
317 // state with higher priority accepted. This state cannot
318 // be better by attempting its next node.
319 if (!_M_has_sol)
320 {
321 // DON'T attempt anything if it's already accepted. An
322 // accepted state *must* be better than a solution that
323 // matches a non-greedy quantifier one more time.
324 _M_frames.emplace_back(_S_fopcode_fallback_rep_once_more, __i);
325 _M_frames.emplace_back(_S_fopcode_next, __state._M_next);
326 }
327 }
328 }
329 }
330
331 template<typename _BiIter, typename _Alloc, typename _TraitsT,
332 bool __dfs_mode>
334 _M_handle_subexpr_begin(_Match_mode __match_mode, _StateIdT __i)
335 {
336 const auto& __state = _M_nfa[__i];
337 auto& __res = _M_cur_results[__state._M_subexpr];
338 _M_frames.emplace_back(_S_fopcode_restore_cur_results,
339 static_cast<_StateIdT>(__state._M_subexpr),
340 __res.first);
341 _M_frames.back()._M_end = false;
342 __res.first = _M_current;
343 _M_frames.emplace_back(_S_fopcode_next, __state._M_next);
344 }
345
346 template<typename _BiIter, typename _Alloc, typename _TraitsT,
347 bool __dfs_mode>
349 _M_handle_subexpr_end(_Match_mode __match_mode, _StateIdT __i)
350 {
351 const auto& __state = _M_nfa[__i];
352 auto& __res = _M_cur_results[__state._M_subexpr];
353 _M_frames.emplace_back(_S_fopcode_restore_cur_results,
354 static_cast<_StateIdT>(__state._M_subexpr),
355 __res.second);
356 _M_frames.back()._M_end = true;
357 _M_frames.back()._M_matched = __res.matched;
358 __res.second = _M_current;
359 __res.matched = true;
360 _M_frames.emplace_back(_S_fopcode_next, __state._M_next);
361 }
362
363 template<typename _BiIter, typename _Alloc, typename _TraitsT,
364 bool __dfs_mode>
366 _M_handle_line_begin_assertion(_Match_mode __match_mode, _StateIdT __i)
367 {
368 const auto& __state = _M_nfa[__i];
369 if (_M_at_begin())
370 _M_frames.emplace_back(_S_fopcode_next, __state._M_next);
371 }
372
373 template<typename _BiIter, typename _Alloc, typename _TraitsT,
374 bool __dfs_mode>
376 _M_handle_line_end_assertion(_Match_mode __match_mode, _StateIdT __i)
377 {
378 const auto& __state = _M_nfa[__i];
379 if (_M_at_end())
380 _M_frames.emplace_back(_S_fopcode_next, __state._M_next);
381 }
382
383 template<typename _BiIter, typename _Alloc, typename _TraitsT,
384 bool __dfs_mode>
386 _M_handle_word_boundary(_Match_mode __match_mode, _StateIdT __i)
387 {
388 const auto& __state = _M_nfa[__i];
389 if (_M_word_boundary() == !__state._M_neg)
390 _M_frames.emplace_back(_S_fopcode_next, __state._M_next);
391 }
392
393 // Here __state._M_alt offers a single start node for a sub-NFA.
394 // We recursively invoke our algorithm to match the sub-NFA.
395 template<typename _BiIter, typename _Alloc, typename _TraitsT,
396 bool __dfs_mode>
398 _M_handle_subexpr_lookahead(_Match_mode __match_mode, _StateIdT __i)
399 {
400 const auto& __state = _M_nfa[__i];
401 if (_M_lookahead(__state._M_alt) == !__state._M_neg)
402 _M_frames.emplace_back(_S_fopcode_next, __state._M_next);
403 }
404
405 template<typename _BiIter, typename _Alloc, typename _TraitsT,
406 bool __dfs_mode>
408 _M_handle_match(_Match_mode __match_mode, _StateIdT __i)
409 {
410 const auto& __state = _M_nfa[__i];
411 if (_M_current == _M_end)
412 return;
413 if constexpr (__dfs_mode)
414 {
415 if (__state._M_matches(*_M_current))
416 {
417 ++_M_current;
418 _M_frames.emplace_back(_S_fopcode_next, __state._M_next);
419 }
420 }
421 else
422 if (__state._M_matches(*_M_current))
423 _M_states._M_queue(__state._M_next, _M_cur_results);
424 }
425
426 template<typename _BiIter, typename _TraitsT>
427 struct _Backref_matcher
428 {
429 _Backref_matcher(bool /* __icase */, const _TraitsT& __traits)
430 : _M_traits(__traits) { }
431
432 bool
433 _M_apply(_BiIter __expected_begin,
434 _BiIter __expected_end, _BiIter __actual_begin,
435 _BiIter __actual_end)
436 {
437 return _M_traits.transform(__expected_begin, __expected_end)
438 == _M_traits.transform(__actual_begin, __actual_end);
439 }
440
441 const _TraitsT& _M_traits;
442 };
443
444 template<typename _BiIter, typename _CharT>
445 struct _Backref_matcher<_BiIter, std::regex_traits<_CharT>>
446 {
447 using _TraitsT = std::regex_traits<_CharT>;
448 _Backref_matcher(bool __icase, const _TraitsT& __traits)
449 : _M_icase(__icase), _M_traits(__traits) { }
450
451 bool
452 _M_apply(_BiIter __expected_begin,
453 _BiIter __expected_end, _BiIter __actual_begin,
454 _BiIter __actual_end)
455 {
456 if (!_M_icase)
457 return _GLIBCXX_STD_A::__equal4(__expected_begin, __expected_end,
458 __actual_begin, __actual_end);
459 typedef std::ctype<_CharT> __ctype_type;
460 const auto& __fctyp = use_facet<__ctype_type>(_M_traits.getloc());
461 return _GLIBCXX_STD_A::__equal4(__expected_begin, __expected_end,
462 __actual_begin, __actual_end,
463 [this, &__fctyp](_CharT __lhs, _CharT __rhs)
464 {
465 return __fctyp.tolower(__lhs)
466 == __fctyp.tolower(__rhs);
467 });
468 }
469
470 bool _M_icase;
471 const _TraitsT& _M_traits;
472 };
473
474 // First fetch the matched result from _M_cur_results as __submatch;
475 // then compare it with
476 // (_M_current, _M_current + (__submatch.second - __submatch.first)).
477 // If matched, keep going; else just return and try another state.
478 template<typename _BiIter, typename _Alloc, typename _TraitsT,
479 bool __dfs_mode>
481 _M_handle_backref(_Match_mode __match_mode, _StateIdT __i)
482 {
483 static_assert(__dfs_mode, "this should never be instantiated");
484
485 const auto& __state = _M_nfa[__i];
486 auto& __submatch = _M_cur_results[__state._M_backref_index];
487 if (!__submatch.matched)
488 return;
489 auto __last = _M_current;
490 for (auto __tmp = __submatch.first;
491 __last != _M_end && __tmp != __submatch.second;
492 ++__tmp)
493 ++__last;
494 if (_Backref_matcher<_BiIter, _TraitsT>(
495 _M_re.flags() & regex_constants::icase,
496 _M_re._M_automaton->_M_traits)._M_apply(
497 __submatch.first, __submatch.second, _M_current, __last))
498 {
499 _M_current = __last;
500 _M_frames.emplace_back(_S_fopcode_next, __state._M_next);
501 }
502 }
503
504 template<typename _BiIter, typename _Alloc, typename _TraitsT,
505 bool __dfs_mode>
507 _M_handle_accept(_Match_mode __match_mode, _StateIdT)
508 {
509 if constexpr (__dfs_mode)
510 {
511 __glibcxx_assert(!_M_has_sol);
512 if (__match_mode == _Match_mode::_Exact)
513 _M_has_sol = _M_current == _M_end;
514 else
515 _M_has_sol = true;
516 if (_M_current == _M_begin
517 && (_M_flags & regex_constants::match_not_null))
518 _M_has_sol = false;
519 if (_M_has_sol)
520 {
521 if (_M_nfa._M_flags & regex_constants::ECMAScript)
522 _M_results = _M_cur_results;
523 else // POSIX
524 {
525 __glibcxx_assert(_M_states._M_get_sol_pos());
526 // Here's POSIX's logic: match the longest one. However
527 // we never know which one (lhs or rhs of "|") is longer
528 // unless we try both of them and compare the results.
529 // The member variable _M_sol_pos records the end
530 // position of the last successful match. It's better
531 // to be larger, because POSIX regex is always greedy.
532 // TODO: This could be slow.
533 if (*_M_states._M_get_sol_pos() == _BiIter()
534 || std::distance(_M_begin,
535 *_M_states._M_get_sol_pos())
536 < std::distance(_M_begin, _M_current))
537 {
538 *_M_states._M_get_sol_pos() = _M_current;
539 _M_results = _M_cur_results;
540 }
541 }
542 }
543 }
544 else
545 {
546 if (_M_current == _M_begin
547 && (_M_flags & regex_constants::match_not_null))
548 return;
549 if (__match_mode == _Match_mode::_Prefix || _M_current == _M_end)
550 if (!_M_has_sol)
551 {
552 _M_has_sol = true;
553 _M_results = _M_cur_results;
554 }
555 }
556 }
557
558 template<typename _BiIter, typename _Alloc, typename _TraitsT,
559 bool __dfs_mode>
561 _M_handle_alternative(_Match_mode __match_mode, _StateIdT __i)
562 {
563 const auto& __state = _M_nfa[__i];
564 if (_M_nfa._M_flags & regex_constants::ECMAScript)
565 {
566 // TODO: Fix BFS support. It is wrong.
567 // Pick lhs if it matches. Only try rhs if it doesn't.
568 _M_frames.emplace_back(_S_fopcode_fallback_next, __state._M_next,
569 _M_current);
570 _M_frames.emplace_back(_S_fopcode_next, __state._M_alt);
571 }
572 else
573 {
574 // Try both and compare the result.
575 // See "case _S_opcode_accept:" handling above.
576 _M_frames.emplace_back(_S_fopcode_posix_alternative, __state._M_next,
577 _M_current);
578 _M_frames.emplace_back(_S_fopcode_next, __state._M_alt);
579 }
580 }
581
582 template<typename _BiIter, typename _Alloc, typename _TraitsT,
583 bool __dfs_mode>
584#ifdef __OPTIMIZE__
585 [[__gnu__::__always_inline__]]
586#endif
588 _M_node(_Match_mode __match_mode, _StateIdT __i)
589 {
590 if (_M_states._M_visited(__i))
591 return;
592
593 switch (_M_nfa[__i]._M_opcode())
594 {
595 case _S_opcode_repeat:
596 _M_handle_repeat(__match_mode, __i); break;
597 case _S_opcode_subexpr_begin:
598 _M_handle_subexpr_begin(__match_mode, __i); break;
599 case _S_opcode_subexpr_end:
600 _M_handle_subexpr_end(__match_mode, __i); break;
601 case _S_opcode_line_begin_assertion:
602 _M_handle_line_begin_assertion(__match_mode, __i); break;
603 case _S_opcode_line_end_assertion:
604 _M_handle_line_end_assertion(__match_mode, __i); break;
605 case _S_opcode_word_boundary:
606 _M_handle_word_boundary(__match_mode, __i); break;
607 case _S_opcode_subexpr_lookahead:
608 _M_handle_subexpr_lookahead(__match_mode, __i); break;
609 case _S_opcode_match:
610 _M_handle_match(__match_mode, __i); break;
611 case _S_opcode_backref:
612 if constexpr (__dfs_mode)
613 _M_handle_backref(__match_mode, __i);
614 else
615 __builtin_unreachable();
616 break;
617 case _S_opcode_accept:
618 _M_handle_accept(__match_mode, __i); break;
619 case _S_opcode_alternative:
620 _M_handle_alternative(__match_mode, __i); break;
621 default:
622 __glibcxx_assert(false);
623 }
624 }
625
626 template<typename _BiIter, typename _Alloc, typename _TraitsT,
627 bool __dfs_mode>
629 _M_dfs(_Match_mode __match_mode, _StateIdT __start)
630 {
631 _M_frames.emplace_back(_S_fopcode_next, __start);
632
633 while (!_M_frames.empty())
634 {
635 _ExecutorFrame<_BiIter> __frame = std::move(_M_frames.back());
636 _M_frames.pop_back();
637
638 switch (__frame._M_op)
639 {
640 case _S_fopcode_fallback_next:
641 if (_M_has_sol)
642 break;
643 if constexpr (__dfs_mode)
644 _M_current = __frame._M_pos;
645 [[__fallthrough__]];
646 case _S_fopcode_next:
647 _M_node(__match_mode, __frame._M_state_id);
648 break;
649
650 case _S_fopcode_fallback_rep_once_more:
651 if (_M_has_sol)
652 break;
653 if constexpr (__dfs_mode)
654 _M_current = __frame._M_pos;
655 [[__fallthrough__]];
656 case _S_fopcode_rep_once_more:
657 _M_rep_once_more(__match_mode, __frame._M_state_id);
658 break;
659
660 case _S_fopcode_posix_alternative:
661 _M_frames.emplace_back(_S_fopcode_merge_sol, 0, _M_has_sol);
662 _M_frames.emplace_back(_S_fopcode_next, __frame._M_state_id);
663 if constexpr (__dfs_mode)
664 _M_current = __frame._M_pos;
665 _M_has_sol = false;
666 break;
667
668 case _S_fopcode_merge_sol:
669 _M_has_sol |= __frame._M_val;
670 break;
671
672 case _S_fopcode_restore_cur_results:
673 if (!__frame._M_end)
674 _M_cur_results[__frame._M_state_id].first = __frame._M_pos;
675 else
676 {
677 _M_cur_results[__frame._M_state_id].second = __frame._M_pos;
678 _M_cur_results[__frame._M_state_id].matched = __frame._M_matched;
679 }
680 break;
681
682 case _S_fopcode_restore_rep_count:
683 _M_rep_count[__frame._M_state_id].first = __frame._M_pos;
684 _M_rep_count[__frame._M_state_id].second = __frame._M_count;
685 break;
686
687 case _S_fopcode_decrement_rep_count:
688 _M_rep_count[__frame._M_state_id].second--;
689 break;
690 }
691 }
692 }
693
694 // Return whether now is at some word boundary.
695 template<typename _BiIter, typename _Alloc, typename _TraitsT,
696 bool __dfs_mode>
698 _M_word_boundary() const
699 {
700 if (_M_current == _M_begin && (_M_flags & regex_constants::match_not_bow))
701 return false;
702 if (_M_current == _M_end && (_M_flags & regex_constants::match_not_eow))
703 return false;
704
705 bool __left_is_word = false;
706 if (_M_current != _M_begin
707 || (_M_flags & regex_constants::match_prev_avail))
708 {
709 auto __prev = _M_current;
710 if (_M_is_word(*std::prev(__prev)))
711 __left_is_word = true;
712 }
713 bool __right_is_word =
714 _M_current != _M_end && _M_is_word(*_M_current);
715
716 return __left_is_word != __right_is_word;
717 }
718_GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)
719} // namespace __detail
720#pragma GCC diagnostic pop
721
722_GLIBCXX_END_NAMESPACE_VERSION
723} // namespace
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
const _Facet & use_facet(const locale &__loc)
Return a facet.
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Implementation details not part of the namespace std interface.
constexpr match_flag_type match_not_bow
constexpr syntax_option_type ECMAScript
constexpr match_flag_type match_continuous
constexpr syntax_option_type icase
constexpr match_flag_type match_prev_avail
constexpr match_flag_type match_not_eow
constexpr match_flag_type match_not_null
Takes a regex and an input string and does the matching.