`<regex>`: How should multi-character collating elements in character classes be handled?

Đang mở
#6,552 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
28/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
cpp
Lĩnh vực
backend

Hướng nghiên cứu

Start with the <regex> implementation and the lookup_collatename() behavior, then reproduce the three patterns from the issue against the standard-library matcher. Compare the alternatives with the provided table and establish semantics for positive and negated character classes in both collate and non-collate modes. Done means one coherent rule is selected, documented, and covered by tests that verify the union behavior and multi-character matches.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

LWG issue needed question regex

As mentioned in #6540, the loop subtype for negated character classes in collate mode is missing because such character classes aren't marked as simple, because there is a reasonable set of semantics where such character classes could match character sequences of varying length if there are multi-character collating elements. (Specifically, this will be a consequence of option 4 below.) The question is: What are the appropriate semantics we should implement?

Unfortunately, the POSIX standard, which originally added collating elements to regular expressions, isn't of much help in the version referenced by the C++ standard (if interested, expand below).

POSIX (un-)specification

The POSIX standard (see https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/basedefs/V1_chap09.html) states:

  • For the list of expressions inside a positive character class:

It is unspecified whether a matching list expression matches a multi-character collating element that is matched by one of the expressions.

  • For the list of expressions inside a negated character class:

It is unspecified whether a non-matching list expression matches a multi-character collating element that is not matched by any of the expressions.

  • For named character classes (e.g., [:alpha:]):

An unspecified set of multi-character collating elements.

  • For character ranges (note that the C locale doesn't have multi-character collating elements):

In [locales other than the C locale], a range expression has unspecified behavior: strictly conforming applications shall not rely on whether the range expression is valid, or on the set of collating elements matched.

So nowadays, the POSIX standard leaves basically everything about multi-character collating elements in character classes unspecified and thus is of very little help.

In turn, the C++ standard effectively specifies at least some of this:

  • Given that isctype() in the traits class is only defined for matching single characters, named character classes can't match multi-character collating elements.
  • In [re.grammar], it defines how range expressions are to be interpreted in collate mode or non-collate mode, at least for single characters.

The current implementation in MSVC STL has at least one unfortunate inconsistency: It breaks the property that the combination of two positive character classes actually represents the union of the matched character sequences, so [c[.ch.]]h does not match everything that [c]h or [[.ch.]]h match (or, expressed differently, [c[.ch.]]h does not match the same sequences as ([c]|[[.ch.]])h).

Test case

#include <iostream>
#include <locale>
#include <regex>
#include <string>

using namespace std;

class custom_traits : public regex_traits<char> {
public:
    custom_traits() {
        regex_traits<char>::imbue(locale("cs_CZ"));
    }
    template <class FwdIt>
    string_type lookup_collatename(FwdIt first, FwdIt last) const {
        string_type name(first, last);
        if (name == "ch" || name == "Ch") {
            return name;
        }

        return regex_traits::lookup_collatename(first, last);
    }
};

int main() {
    for (string pattern : {"[c]h", "[[.ch.]]h", "[c[.ch.]]h"}) {
        basic_regex<char, custom_traits> re(pattern);
        cout << "pattern '" << pattern
                  << "' matches ch: " << std::regex_match("ch", re)
                  << " matches chh: " << std::regex_match("chh", re) << '\n';
    }

    return 0;
}

https://godbolt.org/z/qe1YTM63T

Equivalent test case for Boost:

#include <boost/regex.hpp>
#include <iostream>
#include <locale>
#include <string>

int main() {
    boost::regex re;
    re.imbue(std::locale("cs_CZ"));
    for (std::string pattern : {"[c]h", "[[.ch.]]h", "[c[.ch.]]h"}) {
        re.assign(pattern);
        std::cout << "pattern '" << pattern
                  << "' matches ch: " << boost::regex_match("ch", re)
                  << " matches chh: " << boost::regex_match("chh", re) << '\n';
    }

    return 0;
}

https://godbolt.org/z/bKEK5nz84

Results of the test case (x/y means: regex_match returns x on "ch" and y on "chh"):

Library [c]h [[.ch.]]h [c[.ch.]]h
MSVC STL 1/0 0/1 0/1
libstdc++ 1/0 1/0 (???) 1/0
libc++ 0/1 (???) 0/1 0/1
Boost.Regex 1/0 0/1 1/0
GNU grep 3.11 1/0 0/1 1/1

Disagreement is rampant. MSVC STL and Boost.Regex fail the property that the combination of two character classes should match the set union. The other three have that property, but libstdc++ and libc++ only have it due to obvious bugs as highlighted in the table above.

Options

  1. Keep things as-is, with the unfortunate consequence that [c]|[[.ch.]] does not match the same character sequences as [c[.ch.]].
  2. Align with Boost.Regex, so switch the order when multi-character collating elements are checked compared to single characters. The only advantage is that we behave like Boost.Regex now, but it's still the case that [c]|[[.ch.]] does not match the same character sequences as [c[.ch.]].
  3. Make [c[.ch.]] match c or ch (i.e., follow GNU grep). This will complicate the matcher: It must be taught to partially backtrack the matching of a character class (i.e., if matching "ch" for character class "[c[.ch.]]" succeeded, but the match failed later, then backtrack to the character class and retry with "c").
  4. Make [c]h not match ch, i.e., the character class must always match the longest collating element and not any prefix of it. This means that processing a character class will have to call lookup_collatename() first to find the collating element (by essentially doing lookup_collatename(cur, cur+x) == string(cur, cur + x)), then check that this collating element is in the character class. Problems:
    • There is no traits API to find the longest collating element that is a prefix of a character sequence, so we would have to impose an arbitrary maximum length that is checked using lookup_collatename(). 3 appears reasonable, though, because that is the length of the longest collating element in Hungarian. We could double-check using ICU, though, if there is a longer collating element in some language.
    • The additional lookup_collatename() calls make character matching more expensive, especially after #5393 is addressed.
    • This approach only seems reasonable when the collate flag is set: It results in behavior people do not usually expect, so we can only do this when users explicitly ask for behavior consistent with locale collation. So even if we go with this option, we still have to decide the appropriate semantics when collate is not set.
    • libc++ kind of tries to implement this, but the implementation is very buggy (see, e.g., llvm/llvm-project#39399).
    • I think this is closest to the way collating elements were supposed to behave when they were added to POSIX, but I'm not aware that any actively maintained regex engine properly implements this anymore.
    • For consistent behavior, a negated character class might also match multi-character collating elements when this option is implemented.
Ngôn ngữ chính
C++
Star
11.2k
Fork
1.7k
Merge trung bình
4 ngày 4 giờ
Pull request đã merge (30 ngày)
14

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoft/STL

Tất cả issue của microsoft/STL

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.