表示为std :: basic_string_view的迭代正则表达式子匹配 [英] Iterating regex submatches represented as std::basic_string_view
问题描述
是否有直接有效的方法来转换 std :: sub_match
到 std :: basic_string_view
(无需构造中间 std :: basic_string
且无需中间堆分配)?还是一个抽象级别,是否可以替代 std :: regex_token_iterator
用于迭代表示为> $的正则表达式子匹配c $ c> std :: basic_string_view 而不是 std :: sub_match
使用 std
(C ++ 17)?
Is there a direct efficient way to convert 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
using the std
(C++17)?
我更喜欢使用 <$的原因c $ c> std :: basic_string_view 通过 std :: sub_match
是:
The reasons why I rather like to use std::basic_string_view
over std::sub_match
are:
-
std :: bas ic_string_view
表示字符状对象的连续连续序列,该序列的第一个元素位于零位置。这样可以使用charconv
的std :: from_chars
(令人惊讶的是,没有使用ForwardIterator
s实现)。std :: sub_match
,因为它表示为一对BidirectionalIterator
s。 -
std :: basic_string_view
有很多更丰富的类似字符串的界面,在某些特殊情况下,对于某些文件格式,可以促进其他上下文相关的标记化。
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
, since it is represented as a pair ofBidirectionalIterator
s.std::basic_string_view
has a much richer string-like interface facilitating additional context-sensitive tokenization in some exceptional cases for some file formats.
推荐答案
没有检测迭代器是否连续的通用方法。我们仍然可以处理已知的连续迭代器,例如 std :: string
:
There's no general way to detect whether an iterator is contiguous. We can still handle known contiguous iterators - such as those of std::string
:
std::string_view as_sv(std::ssub_match m) {
if(!m.matched) return {};
return { &*m.first, m.second - m.first };
}
处理 sub_match $的其余命名专业化
留给读者练习。
这篇关于表示为std :: basic_string_view的迭代正则表达式子匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!