This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/64441] A match_results returns an incorrect sub_match if the sub_match::matched is false


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)
==================================================================


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]