libstdc++
regex_grep_matcher.h
Go to the documentation of this file.
00001 // class template regex -*- C++ -*-
00002 
00003 // Copyright (C) 2010, 2011 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /**
00026  *  @file bits/regex_grep_matcher.h
00027  *  This is an internal header file, included by other library headers.
00028  *  Do not attempt to use it directly. @headername{regex}
00029  */
00030 
00031 namespace std _GLIBCXX_VISIBILITY(default)
00032 {
00033 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00034 
00035   template<typename _BiIter>
00036     class sub_match;
00037 
00038   template<typename _Bi_iter, typename _Allocator>
00039     class match_results;
00040 
00041 _GLIBCXX_END_NAMESPACE_VERSION
00042   
00043 namespace __regex
00044 {
00045 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00046 
00047   // A _Results facade specialized for wrapping a templated match_results.
00048   template<typename _FwdIterT, typename _Alloc>
00049     class _SpecializedResults
00050     : public _Results
00051     {
00052     public:
00053       _SpecializedResults(const _Automaton::_SizeT __size,
00054               const _SpecializedCursor<_FwdIterT>& __cursor,
00055               match_results<_FwdIterT, _Alloc>& __m);
00056 
00057       void
00058       _M_set_pos(int __i, int __j, const _PatternCursor& __pc);
00059 
00060       void
00061       _M_set_matched(int __i, bool __is_matched)
00062       { _M_results.at(__i).matched = __is_matched; }
00063 
00064     private:
00065       match_results<_FwdIterT, _Alloc>& _M_results;
00066     };
00067 
00068   template<typename _FwdIterT, typename _Alloc>
00069     _SpecializedResults<_FwdIterT, _Alloc>::
00070     _SpecializedResults(const _Automaton::_SizeT __size,
00071                 const _SpecializedCursor<_FwdIterT>& __cursor,
00072                         match_results<_FwdIterT, _Alloc>& __m)
00073     : _M_results(__m)
00074     {
00075       _M_results.clear();
00076       _M_results.reserve(__size + 2);
00077       _M_results.resize(__size);
00078       typename match_results<_FwdIterT, _Alloc>::value_type __sm;
00079       __sm.first = __sm.second = __cursor._M_begin();
00080       _M_results.push_back(__sm);
00081       __sm.first = __sm.second = __cursor._M_end();
00082       _M_results.push_back(__sm);
00083     }
00084 
00085   template<typename _FwdIterT, typename _Alloc>
00086     void
00087     _SpecializedResults<_FwdIterT, _Alloc>::
00088     _M_set_pos(int __i, int __j, const _PatternCursor& __pc)
00089     { 
00090       typedef const _SpecializedCursor<_FwdIterT>& _CursorT;
00091       _CursorT __c = static_cast<_CursorT>(__pc);
00092       if (__j == 0)
00093     _M_results.at(__i).first = __c._M_pos();
00094       else
00095     _M_results.at(__i).second = __c._M_pos()+1;
00096     }
00097 
00098   // A stack of states used in evaluating the NFA.
00099   typedef std::stack<_StateIdT, std::vector<_StateIdT> > _StateStack;
00100 
00101   // Executes a regular expression NFA/DFA over a range using a variant of
00102   // the parallel execution algorithm featured in the grep utility, modified
00103   // to use Laurikari tags.
00104   class _Grep_matcher
00105   {
00106   public:
00107     _Grep_matcher(_PatternCursor&                   __p,
00108           _Results&                         __r,
00109           const _AutomatonPtr&              __automaton,
00110           regex_constants::match_flag_type  __flags);
00111 
00112   private:
00113     _StateSet
00114     _M_e_closure(_StateIdT __i);
00115 
00116     _StateSet
00117     _M_e_closure(const _StateSet& __s);
00118 
00119     _StateSet
00120     _M_e_closure(_StateStack& __stack, const _StateSet& __s);
00121 
00122   private:
00123     const std::shared_ptr<_Nfa>        _M_nfa;
00124     _PatternCursor&                    _M_pattern;
00125     _Results&                          _M_results;
00126   };
00127 
00128 _GLIBCXX_END_NAMESPACE_VERSION
00129 } // namespace __regex
00130 } // namespace
00131 
00132 #include <bits/regex_grep_matcher.tcc>