libstdc++
regex_executor.h
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.h
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 // FIXME convert comments to doxygen format.
32 
33 namespace std _GLIBCXX_VISIBILITY(default)
34 {
35 namespace __detail
36 {
37 _GLIBCXX_BEGIN_NAMESPACE_VERSION
38 
39  /**
40  * @addtogroup regex-detail
41  * @{
42  */
43 
44  template<typename _BiIter, typename _Alloc, typename _TraitsT,
45  bool __dfs_mode>
46  class _Executor
47  {
48  public:
49  typedef typename iterator_traits<_BiIter>::value_type _CharT;
50  typedef basic_regex<_CharT, _TraitsT> _RegexT;
51  typedef std::vector<sub_match<_BiIter>, _Alloc> _ResultsVec;
52  typedef regex_constants::match_flag_type _FlagT;
53  typedef typename _TraitsT::char_class_type _ClassT;
54  typedef _NFA<_TraitsT> _NFAT;
55 
56  public:
57  _Executor(_BiIter __begin,
58  _BiIter __end,
59  _ResultsVec& __results,
60  const _RegexT& __re,
61  _FlagT __flags)
62  : _M_begin(__begin),
63  _M_end(__end),
64  _M_re(__re),
65  _M_nfa(*__re._M_automaton),
66  _M_results(__results),
67  _M_match_queue(__dfs_mode ? nullptr
68  : new vector<pair<_StateIdT, _ResultsVec>>()),
69  _M_visited(__dfs_mode ? nullptr : new vector<bool>(_M_nfa.size())),
70  _M_flags((__flags & regex_constants::match_prev_avail)
71  ? (__flags
72  & ~regex_constants::match_not_bol
73  & ~regex_constants::match_not_bow)
74  : __flags),
75  _M_start_state(_M_nfa._M_start())
76  { }
77 
78  // Set matched when string exactly match the pattern.
79  bool
80  _M_match()
81  {
82  _M_current = _M_begin;
83  return _M_main<true>();
84  }
85 
86  // Set matched when some prefix of the string matches the pattern.
87  bool
88  _M_search_from_first()
89  {
90  _M_current = _M_begin;
91  return _M_main<false>();
92  }
93 
94  bool
95  _M_search();
96 
97  private:
98  template<bool __match_mode>
99  void
100  _M_dfs(_StateIdT __start);
101 
102  template<bool __match_mode>
103  bool
104  _M_main();
105 
106  bool
107  _M_is_word(_CharT __ch) const
108  {
109  static const _CharT __s[2] = { 'w' };
110  return _M_re._M_traits.isctype
111  (__ch, _M_re._M_traits.lookup_classname(__s, __s+1));
112  }
113 
114  bool
115  _M_at_begin() const
116  {
117  return _M_current == _M_begin
118  && !(_M_flags & (regex_constants::match_not_bol
120  }
121 
122  bool
123  _M_at_end() const
124  {
125  return _M_current == _M_end
126  && !(_M_flags & regex_constants::match_not_eol);
127  }
128 
129  bool
130  _M_word_boundary(_State<_TraitsT> __state) const;
131 
132  bool
133  _M_lookahead(_State<_TraitsT> __state);
134 
135  public:
136  _ResultsVec _M_cur_results;
137  _BiIter _M_current;
138  const _BiIter _M_begin;
139  const _BiIter _M_end;
140  const _RegexT& _M_re;
141  const _NFAT& _M_nfa;
142  _ResultsVec& _M_results;
143  // Used in BFS, saving states that need to be considered for the next
144  // character.
146  // Used in BFS, indicating that which state is already visited.
147  std::unique_ptr<vector<bool>> _M_visited;
148  _FlagT _M_flags;
149  // To record current solution.
150  _StateIdT _M_start_state;
151  // Do we have a solution so far?
152  bool _M_has_sol;
153  };
154 
155  //@} regex-detail
156 _GLIBCXX_END_NAMESPACE_VERSION
157 } // namespace __detail
158 } // namespace std
159 
160 #include <bits/regex_executor.tcc>
A standard container which offers fixed time access to individual elements in any order...
Definition: stl_vector.h:214
20.7.1.2 unique_ptr for single objects.
Definition: unique_ptr.h:129
match_flag_type
This is a bitmask type indicating regex matching rules.
constexpr size_t size() const noexcept
Returns the total number of bits.
Definition: bitset:1293