Partially implement regex_search.

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.

From-SVN: r201113
This commit is contained in:
Tim Shen 2013-07-21 23:34:02 +00:00 committed by Tim Shen
parent 3429db0fb0
commit 603c431f90
5 changed files with 137 additions and 13 deletions

View File

@ -1,3 +1,12 @@
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

View File

@ -2185,6 +2185,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__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;
}
@ -2324,11 +2325,38 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
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.

View File

@ -63,6 +63,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
void
_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; }
@ -111,9 +120,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
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

View File

@ -103,11 +103,8 @@ namespace __detail
{
_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())
@ -117,6 +114,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__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)

View File

@ -0,0 +1,58 @@
// { 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;
}