+2013-07-21 Tim Shen <timshen91@gmail.com>
+
+ Partially implement regex_search.
+ * include/bits/regex.h: regex_search.
+ * include/bits/regex_grep_matcher.h: _M_search_from_first.
+ * include/bits/regex_grep_matcher.tcc: Implement it.
+ * testsuite/28_regex/algorithms/regex_search/basic/string_01.cc: New.
+
+
2013-07-21 Jonathan Wakely <jwakely.gcc@gmail.com>
PR libstdc++/54352
__detail::_SpecializedCursor<_Bi_iter> __cs(__s, __e);
__detail::_SpecializedResults<_Bi_iter, _Alloc> __r(__sz, __cs, __m);
__detail::_Grep_matcher __matcher(__cs, __r, __a, __flags);
+ __matcher._M_match();
return __m[0].matched;
}
typename _Ch_type, typename _Rx_traits>
inline bool
regex_search(_Bi_iter __first, _Bi_iter __last,
- match_results<_Bi_iter, _Alloc>& __m,
- const basic_regex<_Ch_type, _Rx_traits>& __re,
- regex_constants::match_flag_type __flags
- = regex_constants::match_default)
- { return false; }
+ match_results<_Bi_iter, _Alloc>& __m,
+ const basic_regex<_Ch_type, _Rx_traits>& __re,
+ regex_constants::match_flag_type __flags
+ = regex_constants::match_default)
+ {
+ __detail::_AutomatonPtr __a = __re._M_get_automaton();
+ __detail::_Automaton::_SizeT __sz = __a->_M_sub_count();
+ __detail::_SpecializedCursor<_Bi_iter> __cs(__first, __last);
+ __detail::_SpecializedResults<_Bi_iter, _Alloc> __r(__sz, __cs, __m);
+ for (auto __cur = __first; __cur != __last; ++__cur) // Any KMP-like algo?
+ {
+ __detail::_SpecializedCursor<_Bi_iter> __curs(__cur, __last);
+ __detail::_Grep_matcher __matcher(__curs, __r, __a, __flags);
+ __matcher._M_search_from_first();
+ if (__m[0].matched)
+ {
+ __r._M_set_range(__m.size(),
+ __detail::_SpecializedCursor<_Bi_iter>
+ {__first, __m[0].first});
+ __r._M_set_range(__m.size()+1,
+ __detail::_SpecializedCursor<_Bi_iter>
+ {__m[0].second, __last});
+ __r._M_set_matched(__m.size(),
+ __m.prefix().first != __m.prefix().second);
+ __r._M_set_matched(__m.size()+1,
+ __m.suffix().first != __m.suffix().second);
+ return true;
+ }
+ }
+ return false;
+ }
+
/**
* Searches for a regular expression within a range.
_M_set_pos(int __i, int __j, const _PatternCursor& __pc);
void
+ _M_set_range(int __i, const _PatternCursor& __pc)
+ {
+ typedef const _SpecializedCursor<_FwdIterT>& _CursorT;
+ _CursorT __c = static_cast<_CursorT>(__pc);
+ _M_results.at(__i).first = __c._M_begin();
+ _M_results.at(__i).second = __c._M_end();
+ }
+
+ void
_M_set_matched(int __i, bool __is_matched)
{ _M_results.at(__i).matched = __is_matched; }
{
public:
_Grep_matcher(_PatternCursor& __p,
- _Results& __r,
- const _AutomatonPtr& __automaton,
- regex_constants::match_flag_type __flags);
+ _Results& __r,
+ const _AutomatonPtr& __automaton,
+ regex_constants::match_flag_type __flags)
+ : _M_nfa(static_pointer_cast<_Nfa>(__automaton)),
+ _M_pattern(__p), _M_results(__r)
+ { }
+
+ void _M_match();
+
+ void _M_search_from_first();
private:
_StateSet
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
- inline _Grep_matcher::
- _Grep_matcher(_PatternCursor& __p, _Results& __r,
- const _AutomatonPtr& __nfa,
- regex_constants::match_flag_type __flags)
- : _M_nfa(static_pointer_cast<_Nfa>(__nfa)), _M_pattern(__p), _M_results(__r)
+ inline void _Grep_matcher::
+ _M_match()
{
__detail::_StateSet __t = this->_M_e_closure(_M_nfa->_M_start());
for (; !_M_pattern._M_at_end(); _M_pattern._M_next())
__includes_some(_M_nfa->_M_final_states(), __t));
}
+ inline void _Grep_matcher::
+ _M_search_from_first()
+ {
+ __detail::_StateSet __t = this->_M_e_closure(_M_nfa->_M_start());
+ for (; !_M_pattern._M_at_end(); _M_pattern._M_next())
+ {
+ if (__includes_some(_M_nfa->_M_final_states(), __t)) // KISS
+ {
+ _M_results._M_set_matched(0, true);
+ return;
+ }
+ __t = this->_M_e_closure(__move(_M_pattern, *_M_nfa, __t));
+ }
+ _M_results._M_set_matched(0, false);
+ }
+
// Creates the e-closure set for the initial state __i.
inline _StateSet _Grep_matcher::
_M_e_closure(_StateIdT __i)
--- /dev/null
+// { dg-options "-std=gnu++11" }
+// { dg-do run { xfail *-*-* } }
+
+//
+// 2013-07-17 Tim Shen <timshen91@gmail.com>
+//
+// Copyright (C) 2013 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// 28.11.3 regex_search
+// Tests BRE against a std::string target.
+
+#include <regex>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ bool test __attribute__((unused)) = true;
+
+ std::regex re("as(df)", std::regex::basic);
+ std::string target("xxasdfyy");
+ std::smatch m;
+
+ VERIFY( std::regex_search(target, m, re) );
+
+ VERIFY( m.size() == re.mark_count()+1 );
+ VERIFY( m.empty() == false );
+ VERIFY( m.prefix().matched == (m.prefix().first != m.prefix().second) );
+ VERIFY( std::string(m.prefix().first, m.prefix().second) == "xx" );
+ VERIFY( m.suffix().matched == (m.suffix().first != m.suffix().second) );
+ VERIFY( std::string(m.suffix().first, m.suffix().second) == "yy" );
+ VERIFY( m[0].matched == true );
+ VERIFY( std::string(m[0].first, m[0].second) == "asdf" );
+ VERIFY( m[1].matched == true );
+ VERIFY( std::string(m[1].first, m[1].second) == "df" );
+}
+
+int
+main()
+{
+ test01();
+ return 0;
+}