表示为std :: basic_string_view的迭代正则表达式子匹配 [英] Iterating regex submatches represented as std::basic_string_view

查看:58
本文介绍了表示为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::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 ForwardIterators). This does not seem to be the case for std::sub_match, since it is represented as a pair of BidirectionalIterators.
  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆