c++ - std::regex 的不一致行为

标签 c++ boost std boost-filesystem

我遇到以下问题:

    如果我传递 boost::filesystem::path::string() 的结果与将结果存储在中间字符串变量中,
  • std::regex 的行为会有所不同.第一个将返回一个被截断的匹配项,该匹配项后来不被 std::stoull 接受(抛出 invalid_argument 异常),而第二个工作正常。

请参阅以下命令来解释更多问题:

[nix-shell:~]$ ls -l foo
total 0
-rw-r--r-- 1 amine users 0 Aug 10 16:55 008
-rw-r--r-- 1 amine users 0 Aug 10 15:47 2530047398992289207

[nix-shell:~]$ cat test-1.cpp
#include <iostream>
#include <regex>
#include <string>
#include <boost/filesystem.hpp>

int main() {
  std::regex expression{R"(([0-9]+))"};
  boost::filesystem::path cacheDir("/home/amine/foo");
  for (const auto& entry : boost::filesystem::directory_iterator{cacheDir})
  {
      std::smatch match;
      auto result = std::regex_match(entry.path().filename().string(), match, expression);
      std::cout << "Result: " << result << std::endl
        << "Length: " << match[1].length() << std::endl
        << "Match: " << match[1] << std::endl
        << "Filename: " << entry.path().filename().string() << std::endl
        << std::endl;

      std::stoull(match[1], 0);
  }
  return 0;
}
[nix-shell:~]$ g++ -o test1 test-1.cpp -lboost_filesystem -O0 -g

[nix-shell:~]$ ./test1
Result: 1
Length: 19
Match: 98992289207
Filename: 2530047398992289207

terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoull
Aborted

[nix-shell:~]$ cat test-2.cpp
#include <iostream>
#include <regex>
#include <string>
#include <boost/filesystem.hpp>

int main() {
  std::regex expression{R"(([0-9]+))"};
  boost::filesystem::path cacheDir("/home/amine/foo");
  for (const auto& entry : boost::filesystem::directory_iterator{cacheDir})
  {
      std::smatch match;
      auto what = entry.path().filename().string();
      auto result = std::regex_match(what, match, expression);
      std::cout << "Result: " << result << std::endl
        << "Length: " << match[1].length() << std::endl
        << "Match: " << match[1] << std::endl
        << "Filename: " << entry.path().filename().string() << std::endl
        << std::endl;

      std::stoull(match[1], 0);
  }
  return 0;
}
[nix-shell:~]$ g++ -o test2 test-2.cpp -lboost_filesystem -O0 -g

[nix-shell:~]$ ./test2
Result: 1
Length: 19
Match: 2530047398992289207
Filename: 2530047398992289207

Result: 1
Length: 3
Match: 008
Filename: 008

所以我的问题是:

  • 为什么直接使用boost::filesystem::path::string()std::regex的结果会被截断。
  • 假设匹配变量中的结果被截断没有问题,为什么 std::stoull 会抛出异常?

最佳答案

不幸的是,你掉进了陷阱。在 C++11 中,您调用的 std::regex_match 的重载是

template< class STraits, class SAlloc, 
          class Alloc, class CharT, class Traits >
bool regex_match( const std::basic_string<CharT,STraits,SAlloc>& s,
                  std::match_results<
                      typename std::basic_string<CharT,STraits,SAlloc>::const_iterator,
                      Alloc
                  >& m,
                  const std::basic_regex<CharT,Traits>& e,
                  std::regex_constants::match_flag_type flags = 
                      std::regex_constants::match_default );

因为它需要一个 const& 到一个 std::string 你可以给它传递一个临时字符串。不幸的是,std::regex_match 并非设计用于处理临时字符串。这就是为什么你会得到意想不到的行为。您尝试引用超出范围的数据。

C++14 通过添加修复了这个问题

template< class STraits, class SAlloc, 
          class Alloc, class CharT, class Traits >
bool regex_match( const std::basic_string<CharT,STraits,SAlloc>&&,
                  std::match_results<
                      typename std::basic_string<CharT,STraits,SAlloc>::const_iterator,
                      Alloc
                  >&,
                  const std::basic_regex<CharT,Traits>&,
                  std::regex_constants::match_flag_type flags = 
                      std::regex_constants::match_default ) = delete;

因此您不能再传递临时字符串。

如果您不能使用 C++14,那么您需要确保没有将临时字符串传递给 std::regex_match

关于c++ - std::regex 的不一致行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51792370/

相关文章:

c++ - 动态分配的 std::vector 似乎有内存泄漏

c++ - 如何使用其他类作为一个类的成员?

c++ - boost::function 运行时性能

c++ - 使用 boost 迭代 xml 文件

c++ - 预处理器定义在 VS2019 C++ 中不起作用

c++ - c++中读取函数的一个问题

c++ - union 中的 boost::optional<>?

c++ - "set with class member as key"的最佳数据结构?

C++ std::sort 具有相同的参数

c++ - 在整数 std::chrono::durations 之间转换