https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64441

            Bug ID: 64441
           Summary: A match_results returns an incorrect sub_match if the
                    sub_match::matched is false
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kariya_mitsuru at hotmail dot com

Created attachment 34362
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=34362&action=edit
g++ -v

Please see the following sample.

====================================== 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 = [-140734305427376, -140734305427376)
2:true, str = 'abc', range = [0, 3)
==================================================================

cf. http://melpon.org/wandbox/permlink/SBoMF5UKYYa38Y4N


The C++11 standard 28.10[re.results]/p.4 says, "Otherwise matched is false, and
members first and second point to the end of the sequence that was searched."

So, I think that 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)
==================================================================

Reply via email to