std::regex匹配std::string_view返回子串乱值的零拷贝实现问题
问题根因
对std::string_view执行正则匹配返回子串失效的核心原因是取值方式错误:
std::match_results中存储的匹配项为std::sub_match类型,本身仅持有匹配区间的起止迭代器,直接指向传入的原始字符序列,默认不会产生内容拷贝。- 调用
.str()方法时,该方法会构造并返回一个临时std::string对象,将匹配到的子串拷贝到新分配的内存中。用这个临时对象构造std::string_view时,临时string会在当前完整表达式执行结束后立刻销毁,最终得到的string_view会指向已释放的内存,出现悬空失效问题,这也是clang-tidy警告的触发原因。
零拷贝修复方法
不需要为子串做额外内存分配,直接使用std::sub_match保存的起止迭代器构造std::string_view即可。由于原始字符串(示例中为全局有效的字符串字面量)的底层内存始终合法,迭代器指向的地址全程有效,整个过程不会产生任何字符串拷贝或临时对象。
只需将原代码中获取子串的两行替换为以下实现即可:
std::string_view config(matchResults[1].first, matchResults[1].second); std::string_view platform(matchResults[2].first, matchResults[2].second);
std::sub_match继承自迭代器对类型,其中first成员为匹配子串的起始迭代器,second为匹配子串的尾后迭代器,完全符合std::string_view迭代器构造函数的参数要求。
完整可运行代码
#include <tuple> #include <regex> #include <string_view> #include <iostream> using configuration_str = std::string_view; using platform_str = std::string_view; std::tuple<configuration_str, platform_str> parse_condition_str(std::string_view conditionValue) { constexpr const auto ®exStr = R"((?:\'\$\(Configuration\)\s*\|\s*\$\(Platform\)\s*\'==\'\s*)(.+)\|(.+)')"; static std::regex regex{ regexStr }; std::match_results<typename decltype(conditionValue)::const_iterator> matchResults{}; bool matched = std::regex_match(conditionValue.cbegin(), conditionValue.cend(), matchResults, regex); (void)matched; // 错误写法:会产生临时string导致悬空 // std::string_view config = matchResults[1].str(); // std::string_view platform = matchResults[2].str(); // 零拷贝正确写法 std::string_view config(matchResults[1].first, matchResults[1].second); std::string_view platform(matchResults[2].first, matchResults[2].second); return { config, platform }; } int main() { const auto &stringLiteralThatIsALIVE = "'$(Configuration)|$(Platform)'=='Release|x64'"; const auto&[config, platform] = parse_condition_str(stringLiteralThatIsALIVE); std::cout << "config: " << config << "\nplatform: " << platform << std::endl; return 0; }
原代码触发clang-tidy警告的行为:执行
std::string_view platform = matchResults[2].str();时,clang-tidy提示指针指向的底层对象将在完整表达式结束时被销毁,该警告指向的就是.str()返回的临时std::string对象。
内容的提问来源于stack exchange,提问作者Sergey Kolesnik
相关产品推荐
相关产品推荐

