std::regex_traits<CharT>::isctype
来自cppreference.com
| |
||
确定字符 c 是否属于 f 所标识的字符类,即 lookup_classname() 所返回的值或几个这种值的逐位或的结果。
std::regex_traits 的标准库特化中提供的此函数版本进行下列操作:
1) 首先将
f 转换成 std::ctype_base::mask 类型的值 m。 对于 lookup_classname() 页面中表格列出的每个 std::ctype 分类,如果
f 中设置了该分类对应的位,那么 m 也会设置对应位。2) 然后尝试通过调用
std::use_facet<std::ctype<CharT>>(getloc()).is(m, c) 在浸染的本地环境中分类字符。
- 如果调用返回
true,那么isctype()也返回true。 - 否则,如果
c等于'_',并且f包含对字符类[:w:]调用 lookup_classname() 的结果,那么就会返回true,否则返回false。
参数
| c | - | 要分类的字符 |
| f | - | 从一或多次调用 lookup_classname() 获得的位掩码 |
返回值
如果 c 以 f 分类就会返回 true,否则返回 false。
示例
Run this code
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::regex_traits<char> t;
std::string str_alnum = "alnum";
auto a = t.lookup_classname(str_alnum.begin(), str_alnum.end());
std::string str_w = "w"; // [:w:] 是 [:alnum:] 加上 '_'
auto w = t.lookup_classname(str_w.begin(), str_w.end());
std::cout << std::boolalpha
<< t.isctype('A', w) << ' ' << t.isctype('A', a) << '\n'
<< t.isctype('_', w) << ' ' << t.isctype('_', a) << '\n'
<< t.isctype(' ', w) << ' ' << t.isctype(' ', a) << '\n';
}
输出:
true true
true false
false false
演示自定义正则表达式特性的 lookup_classname() / isctype() 实现:
Run this code
#include <cwctype>
#include <iostream>
#include <locale>
#include <regex>
// 此定制正则表达式特性以 wctype/iswctype 实现 lookup_classname/isctype
struct wctype_traits : std::regex_traits<wchar_t>
{
using char_class_type = std::wctype_t;
template<class It>
char_class_type lookup_classname(It first, It last, bool = false) const
{
return std::wctype(std::string(first, last).c_str());
}
bool isctype(wchar_t c, char_class_type f) const
{
return std::iswctype(c, f);
}
};
int main()
{
std::locale::global(std::locale("ja_JP.utf8"));
std::wcout.sync_with_stdio(false);
std::wcout.imbue(std::locale());
std::wsmatch m;
std::wstring in = L"風の谷のナウシカ";
// 匹配所有字符(它们被分类为 alnum )
std::regex_search(in, m, std::wregex(L"([[:alnum:]]+)"));
std::wcout << "alnums:" << m[1] << '\n'; // 打印 "風の谷のナウシカ"
// 只匹配片假名
std::regex_search(in, m,
std::basic_regex<wchar_t, wctype_traits>(L"([[:jkata:]]+)"));
std::wcout << "片假名:" << m[1] << '\n'; // 打印 "ナウシカ"
}
输出:
alnum:風の谷のナウシカ
片假名:ナウシカ
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2018 | C++11 | 未指定 m 的值
|
与 lookup_classname() 的最低限度支持相匹配 |
参阅
| 按名称获得字符类 (公开成员函数) | |
[虚] |
分类字符或字符序列 ( std::ctype<CharT> 的虚受保护成员函数)
|
按照指定的 LC_CTYPE 类别分类宽字符 (函数) |