迭代表示为 std::basic_string_view 的正则表达式子匹配
Iterating regex submatches represented as std::basic_string_view
是否有使用 std
(C++17) 转换 std::sub_match
to std::basic_string_view
(without constructing an intermediate std::basic_string
and without intermediate heap allocation)? Or one abstraction level further, is there an alternative to std::regex_token_iterator
for iterating regex submatches represented as std::basic_string_view
instead of std::sub_match
的直接有效方法?
我比较喜欢用std::basic_string_view
over std::sub_match
的原因是:
std::basic_string_view
refers to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero. This enables the usage of charconv
's std::from_chars
(which surprisingly is not implemented using ForwardIterator
s). This does not seem to be the case for std::sub_match
,因为它表示为一对BidirectionalIterator
。
std::basic_string_view
有一个更丰富的类似字符串的接口,在某些文件格式的特殊情况下有助于额外的上下文敏感标记化。
没有通用的方法来检测迭代器是否连续。我们仍然可以处理已知的连续迭代器——例如 std::string
:
std::string_view as_sv(std::ssub_match m) {
if(!m.matched) return {};
return { &*m.first, m.second - m.first };
}
处理 sub_match
的其余命名特化留作 reader 的练习。
是否有使用 std
(C++17) 转换 std::sub_match
to std::basic_string_view
(without constructing an intermediate std::basic_string
and without intermediate heap allocation)? Or one abstraction level further, is there an alternative to std::regex_token_iterator
for iterating regex submatches represented as std::basic_string_view
instead of std::sub_match
的直接有效方法?
我比较喜欢用std::basic_string_view
over std::sub_match
的原因是:
std::basic_string_view
refers to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero. This enables the usage ofcharconv
'sstd::from_chars
(which surprisingly is not implemented usingForwardIterator
s). This does not seem to be the case forstd::sub_match
,因为它表示为一对BidirectionalIterator
。std::basic_string_view
有一个更丰富的类似字符串的接口,在某些文件格式的特殊情况下有助于额外的上下文敏感标记化。
没有通用的方法来检测迭代器是否连续。我们仍然可以处理已知的连续迭代器——例如 std::string
:
std::string_view as_sv(std::ssub_match m) {
if(!m.matched) return {};
return { &*m.first, m.second - m.first };
}
处理 sub_match
的其余命名特化留作 reader 的练习。