Commit bc273845 by Tim Shen Committed by Tim Shen

re PR libstdc++/64239 (regex_iterator::operator= should copy match_results::position)

	PR libstdc++/64239
	* include/bits/regex.h (match_results<>::match_results,
	match_results<>::operator=, match_results<>::position,
	match_results<>::swap): Remove match_results::_M_in_iterator.
	Fix ctor/assign/swap.
	* include/bits/regex.tcc: (__regex_algo_impl<>,
	regex_iterator<>::operator++): Set match_results::_M_begin as
	"start position".
	* testsuite/28_regex/iterators/regex_iterator/char/
	string_position_01.cc: Test cases.

From-SVN: r218710
parent 65545817
2014-12-13 Tim Shen <timshen@google.com>
PR libstdc++/64239
* include/bits/regex.h (match_results<>::match_results,
match_results<>::operator=, match_results<>::position,
match_results<>::swap): Remove match_results::_M_in_iterator.
Fix ctor/assign/swap.
* include/bits/regex.tcc: (__regex_algo_impl<>,
regex_iterator<>::operator++): Set match_results::_M_begin as
"start position".
* testsuite/28_regex/iterators/regex_iterator/char/
string_position_01.cc: Test cases.
2014-12-13 Jonathan Wakely <jwakely@redhat.com> 2014-12-13 Jonathan Wakely <jwakely@redhat.com>
* include/experimental/any (any): Remove allocator support and update * include/experimental/any (any): Remove allocator support and update
......
...@@ -1563,42 +1563,30 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1563,42 +1563,30 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/ */
explicit explicit
match_results(const _Alloc& __a = _Alloc()) match_results(const _Alloc& __a = _Alloc())
: _Base_type(__a), _M_in_iterator(false) : _Base_type(__a)
{ } { }
/** /**
* @brief Copy constructs a %match_results. * @brief Copy constructs a %match_results.
*/ */
match_results(const match_results& __rhs) match_results(const match_results& __rhs) = default;
: _Base_type(__rhs), _M_in_iterator(false)
{ }
/** /**
* @brief Move constructs a %match_results. * @brief Move constructs a %match_results.
*/ */
match_results(match_results&& __rhs) noexcept match_results(match_results&& __rhs) noexcept = default;
: _Base_type(std::move(__rhs)), _M_in_iterator(false)
{ }
/** /**
* @brief Assigns rhs to *this. * @brief Assigns rhs to *this.
*/ */
match_results& match_results&
operator=(const match_results& __rhs) operator=(const match_results& __rhs) = default;
{
match_results(__rhs).swap(*this);
return *this;
}
/** /**
* @brief Move-assigns rhs to *this. * @brief Move-assigns rhs to *this.
*/ */
match_results& match_results&
operator=(match_results&& __rhs) operator=(match_results&& __rhs) = default;
{
match_results(std::move(__rhs)).swap(*this);
return *this;
}
/** /**
* @brief Destroys a %match_results object. * @brief Destroys a %match_results object.
...@@ -1685,13 +1673,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1685,13 +1673,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
difference_type difference_type
position(size_type __sub = 0) const position(size_type __sub = 0) const
{ {
// [28.12.1.4.5] return __sub < size() ? std::distance(_M_begin,
if (_M_in_iterator) (*this)[__sub].first) : -1;
return __sub < size() ? std::distance(_M_begin,
(*this)[__sub].first) : -1;
else
return __sub < size() ? std::distance(this->prefix().first,
(*this)[__sub].first) : -1;
} }
/** /**
...@@ -1876,7 +1859,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1876,7 +1859,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/ */
void void
swap(match_results& __that) swap(match_results& __that)
{ _Base_type::swap(__that); } {
_Base_type::swap(__that);
swap(_M_begin, __that._M_begin);
}
//@} //@}
private: private:
...@@ -1894,7 +1880,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1894,7 +1880,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
regex_constants::match_flag_type); regex_constants::match_flag_type);
_Bi_iter _M_begin; _Bi_iter _M_begin;
bool _M_in_iterator;
}; };
typedef match_results<const char*> cmatch; typedef match_results<const char*> cmatch;
......
...@@ -62,6 +62,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -62,6 +62,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return false; return false;
typename match_results<_BiIter, _Alloc>::_Base_type& __res = __m; typename match_results<_BiIter, _Alloc>::_Base_type& __res = __m;
__m._M_begin = __s;
__res.resize(__re._M_automaton->_M_sub_count() + 2); __res.resize(__re._M_automaton->_M_sub_count() + 2);
for (auto& __it : __res) for (auto& __it : __res)
__it.matched = false; __it.matched = false;
...@@ -572,7 +573,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -572,7 +573,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
auto& __prefix = _M_match.at(_M_match.size()); auto& __prefix = _M_match.at(_M_match.size());
__prefix.first = __prefix_first; __prefix.first = __prefix_first;
__prefix.matched = __prefix.first != __prefix.second; __prefix.matched = __prefix.first != __prefix.second;
_M_match._M_in_iterator = true; // [28.12.1.4.5]
_M_match._M_begin = _M_begin; _M_match._M_begin = _M_begin;
return *this; return *this;
} }
...@@ -587,7 +588,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -587,7 +588,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
auto& __prefix = _M_match.at(_M_match.size()); auto& __prefix = _M_match.at(_M_match.size());
__prefix.first = __prefix_first; __prefix.first = __prefix_first;
__prefix.matched = __prefix.first != __prefix.second; __prefix.matched = __prefix.first != __prefix.second;
_M_match._M_in_iterator = true; // [28.12.1.4.5]
_M_match._M_begin = _M_begin; _M_match._M_begin = _M_begin;
} }
else else
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
// Tests iter->position() behavior // Tests iter->position() behavior
#include <regex> #include <regex>
#include <tuple>
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
void void
...@@ -41,9 +42,53 @@ test01() ...@@ -41,9 +42,53 @@ test01()
} }
} }
// PR libstdc++/64239
void
test02()
{
bool test __attribute__((unused)) = true;
std::regex re("\\w+");
std::string s("-a-b-c-");
std::tuple<int, int, const char*> expected[] =
{
std::make_tuple(1, 1, "a"),
std::make_tuple(3, 1, "b"),
std::make_tuple(5, 1, "c"),
};
int i = 0;
for (auto it1 = std::sregex_iterator(s.begin(), s.end(), re),
end = std::sregex_iterator(); it1 != end; ++it1, i++)
{
auto it2 = it1;
VERIFY(it1->position() == std::get<0>(expected[i]));
VERIFY(it1->length() == std::get<1>(expected[i]));
VERIFY(it1->str() == std::get<2>(expected[i]));
VERIFY(it2->position() == std::get<0>(expected[i]));
VERIFY(it2->length() == std::get<1>(expected[i]));
VERIFY(it2->str() == std::get<2>(expected[i]));
}
}
void
test03()
{
bool test __attribute__((unused)) = true;
std::smatch m;
std::string s = "abcde";
std::regex_search(s, m, std::regex("bcd"));
VERIFY(m.position() == 1);
VERIFY(m.position() == m.prefix().length());
}
int int
main() main()
{ {
test01(); test01();
test02();
test03();
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment