Skip to content

[libc++] regex \b and \B behaviors different from MSVC and libstdc++ #77577

Open
@zufuliu

Description

@zufuliu

https://godbolt.org/z/KGWPj8qjv

#include <string>
#include <regex>
#include <iostream>
using namespace std;

void show_match_result(const char *tag, const sregex_iterator &it) {
	auto match = *it;
    cout << tag << match.position(0) << ", " << match.length(0) << endl;
}

int main() {
    string s = "ab";
    auto start = s.cbegin() + 1;
    auto end = sregex_iterator();
    regex re0("\\b");
    cout << "\\b" << endl;
    auto it = sregex_iterator(s.cbegin(), s.cend(), re0, regex_constants::match_default);
    for (; it != end; ++it) {
        show_match_result("0 match_default: ", it);
    }
    it = sregex_iterator(start, s.cend(), re0, regex_constants::match_default);
    for (; it != end; ++it) {
        show_match_result("1 match_default: ", it);
    }
    it = sregex_iterator(start, s.cend(), re0, regex_constants::match_prev_avail);
    for (; it != end; ++it) {
        show_match_result("1 match_prev_avail: ", it);
    }
    it = sregex_iterator(start, s.cend(), re0, regex_constants::match_not_bow);
    for (; it != end; ++it) {
        show_match_result("1 match_not_bow: ", it);
    }
    it = sregex_iterator(start, s.cend(), re0, regex_constants::match_prev_avail | regex_constants::match_not_bow);
    for (; it != end; ++it) {
        show_match_result("1 match_prev_avail | match_not_bow: ", it);
    }

    regex re1("\\B");
    cout << "\n\\B" << endl;
    it = sregex_iterator(s.cbegin(), s.cend(), re1, regex_constants::match_default);
    for (; it != end; ++it) {
        show_match_result("0 match_default: ", it);
    }
    it = sregex_iterator(start, s.cend(), re1, regex_constants::match_default);
    for (; it != end; ++it) {
        show_match_result("1 match_default: ", it);
    }
    it = sregex_iterator(start, s.cend(), re1, regex_constants::match_prev_avail);
    for (; it != end; ++it) {
        show_match_result("1 match_prev_avail: ", it);
    }
    it = sregex_iterator(start, s.cend(), re1, regex_constants::match_not_bow);
    for (; it != end; ++it) {
        show_match_result("1 match_not_bow: ", it);
    }
    it = sregex_iterator(start, s.cend(), re1, regex_constants::match_prev_avail | regex_constants::match_not_bow);
    for (; it != end; ++it) {
        show_match_result("1 match_prev_avail | match_not_bow: ", it);
    }

    it = sregex_iterator(s.cbegin(), s.cend() - 1, re1, regex_constants::match_default);
    for (; it != end; ++it) {
        show_match_result("-1 match_not_eow: ", it);
    }
    it = sregex_iterator(s.cbegin(), s.cend() - 1, re1, regex_constants::match_not_eow);
    for (; it != end; ++it) {
        show_match_result("-1 match_not_eow: ", it);
    }
    return 0;
}

MSVC 2022 17.8.4:

\b
0 match_default: 0, 0
0 match_default: 2, 0
1 match_default: 0, 0
1 match_default: 1, 0
1 match_prev_avail: 1, 0
1 match_not_bow: 1, 0
1 match_prev_avail | match_not_bow: 1, 0

\B
0 match_default: 1, 0
1 match_prev_avail: 0, 0
1 match_not_bow: 0, 0
1 match_prev_avail | match_not_bow: 0, 0
-1 match_not_eow: 1, 0

mingw-w64 GCC 13.2.0 and GCC trunk:

\b
0 match_default: 0, 0
0 match_default: 2, 0
1 match_default: 0, 0
1 match_default: 1, 0
1 match_prev_avail: 1, 0
1 match_prev_avail | match_not_bow: 1, 0

\B
0 match_default: 1, 0
1 match_prev_avail: 0, 0
1 match_not_bow: 0, 0
1 match_prev_avail | match_not_bow: 0, 0
-1 match_not_eow: 1, 0

llvm-mingw 17.0.6 and libc++ truck:

\b
0 match_default: 0, 0
1 match_default: 0, 0

\B
0 match_default: 1, 0
0 match_default: 2, 0
1 match_prev_avail: 0, 0
1 match_prev_avail: 1, 0
1 match_not_bow: 0, 0
1 match_not_bow: 1, 0
1 match_prev_avail | match_not_bow: 0, 0
1 match_prev_avail | match_not_bow: 1, 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.regexIssues related to regex

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions