在https://en.cppreference.com/w/cpp/regex/regex_traits/transform_primary建议使用以下示例片段:
#include <iostream>
#include <regex>
int main()
{
std::locale::global(std::locale("en_US.UTF-8"));
std::wstring str = L"AÀÁÂÃÄÅaàáâãäå";
std::wregex re(L"[[=a=]]*", std::regex::basic);
std::cout << std::boolalpha << std::regex_match(str, re) << '\n';
}
也有人说它应该输出true
。然而,在 Debian 上使用 GCC 8 和 Clang 7 以及 macOS High Sierra 附带的 Clang 尝试它总是给出 false
(您可以使用 cppreference 页面中的“运行”按钮直接测试它) .
有人可能会说 cppreference 页面是错误的,这当然是可能的,但是阅读文档在我看来 true
是正确的输出:字符串 中的所有字符据我了解,str
属于 a
的主要整理类。
所以问题是:谁是对的?编译器还是 cppreference?为什么?
最佳答案
这是 transform_primary
的 g++/libstdc++-9 实现的样子:
template<typename _Fwd_iter>
string_type
transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
{
// TODO : this is not entirely correct.
// This function requires extra support from the platform.
//
// Read http://gcc.gnu.org/ml/libstdc++/2013-09/msg00117.html and
// http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2003/n1429.htm
// for details.
typedef std::ctype<char_type> __ctype_type;
const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
std::vector<char_type> __s(__first, __last);
__fctyp.tolower(__s.data(), __s.data() + __s.size());
return this->transform(__s.data(), __s.data() + __s.size());
}
评论说“不完全正确”;以我的拙见,该评论不太正确。它应该说“这是完全错误的”,因为它是。它根本不起作用。
libc++-8 顶部的注释说:
// transform_primary is very FreeBSD-specific
事实上,它在 Linux 上根本不起作用(它为所有字符返回一个空字符串)。它可能在 macOS 上工作,这是 FreeBSD 的一种变体,但我附近没有要检查的。内部可能潜伏着不同的错误。
所以答案是,至少某些编译器至少在某些时候是错误的。
关于具有主类的 C++ 正则表达式不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55690521/