summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortimshen <timshen@138bc75d-0d04-0410-961f-82ee72b054a4>2013-07-21 23:34:02 +0000
committertimshen <timshen@138bc75d-0d04-0410-961f-82ee72b054a4>2013-07-21 23:34:02 +0000
commit68b61c93f7284cf9d423233072dbe798d9debfc3 (patch)
tree048f4c3c258490bce321bc913432437d27f68791
parent429d379090af06b3f1dd9a027beb080b8ae68241 (diff)
downloadgcc-68b61c93f7284cf9d423233072dbe798d9debfc3.tar.gz
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. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@201113 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r--libstdc++-v3/ChangeLog9
-rw-r--r--libstdc++-v3/include/bits/regex.h38
-rw-r--r--libstdc++-v3/include/bits/regex_grep_matcher.h22
-rw-r--r--libstdc++-v3/include/bits/regex_grep_matcher.tcc23
-rw-r--r--libstdc++-v3/testsuite/28_regex/algorithms/regex_search/basic/string_01.cc58
5 files changed, 137 insertions, 13 deletions
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 <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
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
@@ -64,6 +64,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_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 <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;
+}