libstdc++
regex_executor.tcc
Go to the documentation of this file.
1 // class template regex -*- C++ -*-
2 
3 // Copyright (C) 2013-2014 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 
31 namespace std _GLIBCXX_VISIBILITY(default)
32 {
33 namespace __detail
34 {
35 _GLIBCXX_BEGIN_NAMESPACE_VERSION
36 
37  template<typename _BiIter, typename _Alloc, typename _TraitsT,
38  bool __dfs_mode>
39  bool _Executor<_BiIter, _Alloc, _TraitsT, __dfs_mode>::
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  {
47  _M_current = __cur;
48  if (_M_main<false>())
49  return true;
50  }
51  // Continue when __cur == _M_end
52  while (__cur++ != _M_end);
53  return false;
54  }
55 
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())
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  {
114  _M_match_queue->push_back(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  for (auto __task : _M_old_queue)
124  {
125  _M_cur_results = __task.second;
126  _M_dfs<__match_mode>(__task.first);
127  }
128  if (!__match_mode)
129  __ret |= _M_has_sol;
130  if (_M_current == _M_end)
131  break;
132  ++_M_current;
133  }
134  if (__match_mode)
135  __ret = _M_has_sol;
136  return __ret;
137  }
138  }
139 
140  // Return whether now match the given sub-NFA.
141  template<typename _BiIter, typename _Alloc, typename _TraitsT,
142  bool __dfs_mode>
143  bool _Executor<_BiIter, _Alloc, _TraitsT, __dfs_mode>::
144  _M_lookahead(_State<_TraitsT> __state)
145  {
146  _ResultsVec __what(_M_cur_results.size());
147  auto __sub = std::unique_ptr<_Executor>(new _Executor(_M_current,
148  _M_end,
149  __what,
150  _M_re,
151  _M_flags));
152  __sub->_M_start_state = __state._M_alt;
153  if (__sub->_M_search_from_first())
154  {
155  for (size_t __i = 0; __i < __what.size(); __i++)
156  if (__what[__i].matched)
157  _M_cur_results[__i] = __what[__i];
158  return true;
159  }
160  return false;
161  }
162 
163  // TODO: Use a function vector to dispatch, instead of using switch-case.
164  template<typename _BiIter, typename _Alloc, typename _TraitsT,
165  bool __dfs_mode>
166  template<bool __match_mode>
167  void _Executor<_BiIter, _Alloc, _TraitsT, __dfs_mode>::
168  _M_dfs(_StateIdT __i)
169  {
170  if (!__dfs_mode)
171  {
172  if ((*_M_visited)[__i])
173  return;
174  (*_M_visited)[__i] = true;
175  }
176 
177  const auto& __state = _M_nfa[__i];
178  // Every change on _M_cur_results and _M_current will be rolled back after
179  // finishing the recursion step.
180  switch (__state._M_opcode)
181  {
182  // _M_alt branch is "match once more", while _M_next is "get me out
183  // of this quantifier". Executing _M_next first or _M_alt first don't
184  // mean the same thing, and we need to choose the correct order under
185  // given greedy mode.
186  case _S_opcode_alternative:
187  // Greedy.
188  if (!__state._M_neg)
189  {
190  // "Once more" is preferred in greedy mode.
191  _M_dfs<__match_mode>(__state._M_alt);
192  // If it's DFS executor and already accepted, we're done.
193  if (!__dfs_mode || !_M_has_sol)
194  _M_dfs<__match_mode>(__state._M_next);
195  }
196  else // Non-greedy mode
197  {
198  if (__dfs_mode)
199  {
200  // vice-versa.
201  _M_dfs<__match_mode>(__state._M_next);
202  if (!_M_has_sol)
203  _M_dfs<__match_mode>(__state._M_alt);
204  }
205  else
206  {
207  // DON'T attempt anything, because there's already another
208  // state with higher priority accepted. This state cannot be
209  // better by attempting its next node.
210  if (!_M_has_sol)
211  {
212  _M_dfs<__match_mode>(__state._M_next);
213  // DON'T attempt anything if it's already accepted. An
214  // accepted state *must* be better than a solution that
215  // matches a non-greedy quantifier one more time.
216  if (!_M_has_sol)
217  _M_dfs<__match_mode>(__state._M_alt);
218  }
219  }
220  }
221  break;
222  case _S_opcode_subexpr_begin:
223  // If there's nothing changed since last visit, do NOT continue.
224  // This prevents the executor from get into infinite loop when using
225  // "()*" to match "".
226  if (!_M_cur_results[__state._M_subexpr].matched
227  || _M_cur_results[__state._M_subexpr].first != _M_current)
228  {
229  auto& __res = _M_cur_results[__state._M_subexpr];
230  auto __back = __res.first;
231  __res.first = _M_current;
232  _M_dfs<__match_mode>(__state._M_next);
233  __res.first = __back;
234  }
235  break;
236  case _S_opcode_subexpr_end:
237  if (_M_cur_results[__state._M_subexpr].second != _M_current
238  || _M_cur_results[__state._M_subexpr].matched != true)
239  {
240  auto& __res = _M_cur_results[__state._M_subexpr];
241  auto __back = __res;
242  __res.second = _M_current;
243  __res.matched = true;
244  _M_dfs<__match_mode>(__state._M_next);
245  __res = __back;
246  }
247  else
248  _M_dfs<__match_mode>(__state._M_next);
249  break;
250  case _S_opcode_line_begin_assertion:
251  if (_M_at_begin())
252  _M_dfs<__match_mode>(__state._M_next);
253  break;
254  case _S_opcode_line_end_assertion:
255  if (_M_at_end())
256  _M_dfs<__match_mode>(__state._M_next);
257  break;
258  case _S_opcode_word_boundary:
259  if (_M_word_boundary(__state) == !__state._M_neg)
260  _M_dfs<__match_mode>(__state._M_next);
261  break;
262  // Here __state._M_alt offers a single start node for a sub-NFA.
263  // We recursively invoke our algorithm to match the sub-NFA.
264  case _S_opcode_subexpr_lookahead:
265  if (_M_lookahead(__state) == !__state._M_neg)
266  _M_dfs<__match_mode>(__state._M_next);
267  break;
268  case _S_opcode_match:
269  if (__dfs_mode)
270  {
271  if (_M_current != _M_end && __state._M_matches(*_M_current))
272  {
273  ++_M_current;
274  _M_dfs<__match_mode>(__state._M_next);
275  --_M_current;
276  }
277  }
278  else
279  if (__state._M_matches(*_M_current))
280  _M_match_queue->push_back(make_pair(__state._M_next,
281  _M_cur_results));
282  break;
283  // First fetch the matched result from _M_cur_results as __submatch;
284  // then compare it with
285  // (_M_current, _M_current + (__submatch.second - __submatch.first)).
286  // If matched, keep going; else just return and try another state.
287  case _S_opcode_backref:
288  {
289  _GLIBCXX_DEBUG_ASSERT(__dfs_mode);
290  auto& __submatch = _M_cur_results[__state._M_backref_index];
291  if (!__submatch.matched)
292  break;
293  auto __last = _M_current;
294  for (auto __tmp = __submatch.first;
295  __last != _M_end && __tmp != __submatch.second;
296  ++__tmp)
297  ++__last;
298  if (_M_re._M_traits.transform(__submatch.first,
299  __submatch.second)
300  == _M_re._M_traits.transform(_M_current, __last))
301  {
302  if (__last != _M_current)
303  {
304  auto __backup = _M_current;
305  _M_current = __last;
306  _M_dfs<__match_mode>(__state._M_next);
307  _M_current = __backup;
308  }
309  else
310  _M_dfs<__match_mode>(__state._M_next);
311  }
312  }
313  break;
314  case _S_opcode_accept:
315  if (__dfs_mode)
316  {
317  _GLIBCXX_DEBUG_ASSERT(!_M_has_sol);
318  if (__match_mode)
319  _M_has_sol = _M_current == _M_end;
320  else
321  _M_has_sol = true;
322  if (_M_current == _M_begin
323  && (_M_flags & regex_constants::match_not_null))
324  _M_has_sol = false;
325  if (_M_has_sol)
326  _M_results = _M_cur_results;
327  }
328  else
329  {
330  if (_M_current == _M_begin
331  && (_M_flags & regex_constants::match_not_null))
332  break;
333  if (!__match_mode || _M_current == _M_end)
334  if (!_M_has_sol)
335  {
336  _M_has_sol = true;
337  _M_results = _M_cur_results;
338  }
339  }
340  break;
341  default:
342  _GLIBCXX_DEBUG_ASSERT(false);
343  }
344  }
345 
346  // Return whether now is at some word boundary.
347  template<typename _BiIter, typename _Alloc, typename _TraitsT,
348  bool __dfs_mode>
349  bool _Executor<_BiIter, _Alloc, _TraitsT, __dfs_mode>::
350  _M_word_boundary(_State<_TraitsT> __state) const
351  {
352  // By definition.
353  bool __ans = false;
354  auto __pre = _M_current;
355  --__pre;
356  if (!(_M_at_begin() && _M_at_end()))
357  {
358  if (_M_at_begin())
359  __ans = _M_is_word(*_M_current)
360  && !(_M_flags & regex_constants::match_not_bow);
361  else if (_M_at_end())
362  __ans = _M_is_word(*__pre)
363  && !(_M_flags & regex_constants::match_not_eow);
364  else
365  __ans = _M_is_word(*_M_current)
366  != _M_is_word(*__pre);
367  }
368  return __ans;
369  }
370 
371 _GLIBCXX_END_NAMESPACE_VERSION
372 } // namespace __detail
373 } // namespace
20.7.1.2 unique_ptr for single objects.
Definition: unique_ptr.h:129
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
Definition: stl_pair.h:276