This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/64441] A match_results returns an incorrect sub_match if the sub_match::matched is false
- From: "kariya_mitsuru at hotmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 31 Dec 2014 14:20:59 +0000
- Subject: [Bug libstdc++/64441] A match_results returns an incorrect sub_match if the sub_match::matched is false
- Auto-submitted: auto-generated
- References: <bug-64441-4 at http dot gcc dot gnu dot org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64441
--- Comment #2 from Mitsuru Kariya <kariya_mitsuru at hotmail dot com> ---
The rev.219121 seems to satisfy the sample code above, but does not satisfy
another sample code like below.
====================================== sample code
======================================
#include <iostream>
#include <regex>
int main()
{
const char s[] = "abc";
const std::regex re("(\\d+)|(\\w+)");
std::cmatch m;
std::regex_search(s, m, re);
std::cout << std::boolalpha;
for (size_t i = 0, n = m.size(); i <= n; ++i) {
auto&& sub = m[i];
std::cout << i << ":" << sub.matched << ", str = '" << sub.str() << "',
"
"range = [" << sub.first - s << ", " << sub.second - s << ")" <<
std::endl;
}
}
=========================================================================================
============================= output =============================
0:true, str = 'abc', range = [0, 3)
1:false, str = '', range = [3, 3)
2:true, str = 'abc', range = [0, 3)
3:false, str = '', range = [-140735878146800, -140735878146800)
==================================================================
According to the C++11 standard 28.10.4[re.results.acc]/p.8,
If n >= size() then returns a sub_match object representing an unmatched
sub-expression.
So, I think that n = 1 and n = 3 should be an identical result, and the output
should be
============================= output =============================
0:true, str = 'abc', range = [0, 3)
1:false, str = '', range = [3, 3)
2:true, str = 'abc', range = [0, 3)
3:false, str = '', range = [3, 3)
==================================================================