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

未关闭
#6,552 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
28/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
cpp
领域
backend

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.
主要语言
C++
星标
11.2k
派生
1.7k
平均合并
4 天 4 小时
30 天内合并 PR
14

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/STL 的其他 Issue

查看 microsoft/STL 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。