15#ifndef BENCHMARK_RE_H_
16#define BENCHMARK_RE_H_
20#include "internal_macros.h"
24#if !defined(HAVE_STD_REGEX) && \
25 !defined(HAVE_GNU_POSIX_REGEX) && \
26 !defined(HAVE_POSIX_REGEX)
28 #if defined(BENCHMARK_OS_LINUX) || defined(BENCHMARK_OS_APPLE)
29 #define HAVE_POSIX_REGEX 1
30 #elif __cplusplus >= 199711L
31 #define HAVE_STD_REGEX 1
37#if defined(BENCHMARK_HAS_NO_EXCEPTIONS) && \
38 defined(HAVE_STD_REGEX) && \
39 (defined(HAVE_GNU_POSIX_REGEX) || defined(HAVE_POSIX_REGEX))
43#if defined(HAVE_STD_REGEX)
45#elif defined(HAVE_GNU_POSIX_REGEX)
47#elif defined(HAVE_POSIX_REGEX)
50#error No regular expression backend was found!
65 Regex() : init_(
false) {}
73 bool Init(
const std::string& spec, std::string* error);
76 bool Match(
const std::string& str);
81#if defined(HAVE_STD_REGEX)
83#elif defined(HAVE_POSIX_REGEX) || defined(HAVE_GNU_POSIX_REGEX)
86#error No regular expression backend implementation available
90#if defined(HAVE_STD_REGEX)
92inline bool Regex::Init(
const std::string& spec, std::string* error) {
93#ifdef BENCHMARK_HAS_NO_EXCEPTIONS
98 re_ = std::regex(spec, std::regex_constants::extended);
100#ifndef BENCHMARK_HAS_NO_EXCEPTIONS
102catch (
const std::regex_error& e) {
111inline Regex::~Regex() {}
113inline bool Regex::Match(
const std::string& str) {
117 return std::regex_search(str, re_);
121inline bool Regex::Init(
const std::string& spec, std::string* error) {
122 int ec = regcomp(&re_, spec.c_str(), REG_EXTENDED | REG_NOSUB);
125 size_t needed = regerror(ec, &re_,
nullptr, 0);
126 std::vector<char> errbuf(needed);
127 regerror(ec, &re_, errbuf.data(), needed);
131 BM_CHECK_NE(needed, 0);
132 error->assign(errbuf.data(), needed - 1);
142inline Regex::~Regex() {
148inline bool Regex::Match(
const std::string& str) {
152 return regexec(&re_, str.c_str(), 0,
nullptr, 0) == 0;