Iterating regex submatches represented as std::basic_string_view

只谈情不闲聊 提交于 2020-01-15 10:32:14

问题


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)?

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.

回答1:


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 };
}

Handling the remaining named specializations of sub_match is left as an exercise for the reader.



来源:https://stackoverflow.com/questions/50676058/iterating-regex-submatches-represented-as-stdbasic-string-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!