diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index f6d8a2483a7..6083b5b3f89 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,12 @@ +2013-07-21 Tim Shen + + 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 PR libstdc++/54352 diff --git a/libstdc++-v3/include/bits/regex.h b/libstdc++-v3/include/bits/regex.h index 6b6484679ad..58d4e72be9d 100644 --- a/libstdc++-v3/include/bits/regex.h +++ b/libstdc++-v3/include/bits/regex.h @@ -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. diff --git a/libstdc++-v3/include/bits/regex_grep_matcher.h b/libstdc++-v3/include/bits/regex_grep_matcher.h index 8397fc3ce42..da9264d5c17 100644 --- a/libstdc++-v3/include/bits/regex_grep_matcher.h +++ b/libstdc++-v3/include/bits/regex_grep_matcher.h @@ -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 diff --git a/libstdc++-v3/include/bits/regex_grep_matcher.tcc b/libstdc++-v3/include/bits/regex_grep_matcher.tcc index 11382d5a32d..2d4a9a60faa 100644 --- a/libstdc++-v3/include/bits/regex_grep_matcher.tcc +++ b/libstdc++-v3/include/bits/regex_grep_matcher.tcc @@ -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) diff --git a/libstdc++-v3/testsuite/28_regex/algorithms/regex_search/basic/string_01.cc b/libstdc++-v3/testsuite/28_regex/algorithms/regex_search/basic/string_01.cc new file mode 100644 index 00000000000..f2a7f2104c1 --- /dev/null +++ b/libstdc++-v3/testsuite/28_regex/algorithms/regex_search/basic/string_01.cc @@ -0,0 +1,58 @@ +// { dg-options "-std=gnu++11" } +// { dg-do run { xfail *-*-* } } + +// +// 2013-07-17 Tim Shen +// +// 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 +// . + +// 28.11.3 regex_search +// Tests BRE against a std::string target. + +#include +#include + +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; +}