Bug 64140 - match_results.prefix() returns an incorrect result if regex_iterator holds a zero-length match
Summary: match_results.prefix() returns an incorrect result if regex_iterator holds a ...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: 4.9.3
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-12-01 17:35 UTC by Mitsuru Kariya
Modified: 2023-07-20 11:39 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
g++ -v (1.17 KB, text/plain)
2014-12-01 17:35 UTC, Mitsuru Kariya
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mitsuru Kariya 2014-12-01 17:35:19 UTC
Created attachment 34156 [details]
g++ -v

Please see the following sample.

========================================== sample code ==========================================
#include <iostream>
#include <regex>
#include <string>

void print(const char* t, const std::string& s, const std::ssub_match& sub)
{
    std::cout << "  " << t << ": " << (sub.matched ? "matched  " : "unmatched") << ", "
        "length() = " << sub.length() << ", str() = '" << sub.str() << "\', "
        "pair = (" << sub.first - s.begin() << ", " << sub.second - s.begin() << "), "
        "'" << std::string(sub.first, sub.second) << '\'' << std::endl;
}

int main()
{
    const std::regex e("z*");
    const std::string s("ab");

    int i = 0;
    for (auto&& it = std::sregex_iterator(s.begin(), s.end(), e), end = std::sregex_iterator();
         it != end; ++it) {
        std::cout << i++ << ':' << std::endl;
        print("prefix", s, it->prefix());
        print("match ", s, (*it)[0]);
        std::cout << std::endl;
    }
}
=================================================================================================

============================= output =============================
0:
  prefix: unmatched, length() = 0, str() = '', pair = (0, 0), ''
  match : matched  , length() = 0, str() = '', pair = (0, 0), ''

1:
  prefix: unmatched, length() = 0, str() = '', pair = (0, 1), 'a'
  match : matched  , length() = 0, str() = '', pair = (1, 1), ''

2:
  prefix: unmatched, length() = 0, str() = '', pair = (1, 2), 'b'
  match : matched  , length() = 0, str() = '', pair = (2, 2), ''
==================================================================

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


According to C++11 standard 28.11.3[re.alg.search]/p.3 Table 143, prefix().matched should be true
if prefix().first != prefix().second.

(prefix().first is correct, because 28.12.1.4[re.regiter.incr]/p.5 says "match.prefix().first
shall be equal to the previous value of match[0].second".)

So, I think that the output should be 

============================= output =============================
0:
  prefix: unmatched, length() = 0, str() = '', pair = (0, 0), ''
  match : matched  , length() = 0, str() = '', pair = (0, 0), ''

1:
  prefix: matched  , length() = 1, str() = 'a', pair = (0, 1), 'a'
  match : matched  , length() = 0, str() = '', pair = (1, 1), ''

2:
  prefix: matched  , length() = 1, str() = 'b', pair = (1, 2), 'b'
  match : matched  , length() = 0, str() = '', pair = (2, 2), ''
==================================================================
Comment 1 Tim Shen 2014-12-04 04:25:46 UTC
Author: timshen
Date: Thu Dec  4 04:25:12 2014
New Revision: 218340

URL: https://gcc.gnu.org/viewcvs?rev=218340&root=gcc&view=rev
Log:
	PR libstdc++/64140
	* include/bits/regex.tcc (regex_iterator<>::operator++): Update
	prefix.matched after modifying prefix.first.
	* testsuite/28_regex/iterators/regex_iterator/char/64140.cc: New
	testcase.

Added:
    trunk/libstdc++-v3/testsuite/28_regex/iterators/regex_iterator/char/64140.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/bits/regex.tcc
Comment 2 Tim Shen 2014-12-06 11:32:53 UTC
Author: timshen
Date: Sat Dec  6 11:32:21 2014
New Revision: 218445

URL: https://gcc.gnu.org/viewcvs?rev=218445&root=gcc&view=rev
Log:
	PR libstdc++/64140
	Backport form mainline
	2014-12-04  Tim Shen  <timshen@google.com>

	* include/bits/regex.tcc (regex_iterator<>::operator++): Update
	prefix.matched after modifying prefix.first.
	* testsuite/28_regex/iterators/regex_iterator/char/64140.cc: New
	testcase.

Added:
    branches/gcc-4_9-branch/libstdc++-v3/testsuite/28_regex/iterators/regex_iterator/char/64140.cc
Modified:
    branches/gcc-4_9-branch/libstdc++-v3/ChangeLog
    branches/gcc-4_9-branch/libstdc++-v3/include/bits/regex.tcc
Comment 3 Tim Shen 2015-03-09 06:47:28 UTC
Resolved.