`<regex>`: How should multi-character collating elements in character classes be handled?
メンテナーはふだん 2 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
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 の本文から書いたものです。
説明
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
collatemode or non-collatemode, 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
- Keep things as-is, with the unfortunate consequence that
[c]|[[.ch.]]does not match the same character sequences as[c[.ch.]]. - 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.]]. - Make
[c[.ch.]]matchcorch(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"). - Make
[c]hnot matchch, 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 calllookup_collatename()first to find the collating element (by essentially doinglookup_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
collateflag 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 whencollateis 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.
- 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
- 主要言語
- C++
- スター
- 11.2k
- フォーク
- 1.7k
- 平均マージ
- 4日 4時間
- マージ済み PR(30日)
- 14
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/STL のほかの issue
-
LWG
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 2 日以内に返信
-
generator LWG
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 2 日以内に返信
-
LWG
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 2 日以内に返信
-
`<__msvc_math.hpp>`: `_USE_MATH_DEFINES` is ignored after the first `<math.h>` include under `/Zc:cmath`対応中かも @tiagomacarios が 10 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 2 日以内に返信
似ている issue
-
bug data-types
難易度 2/5 1〜3時間 初心者へのやさしさ 71/100
ClickHouse/clickhouse-odbc#680 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
ionut-mihalache/aqua#31 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
needs-kind
難易度 2/5 1〜3時間 初心者へのやさしさ 63/100
villagesql/villagesql-server#1702 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
microsoft/vcpkg-tool#2191 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信