15#ifndef BENCHMARK_RE_H_
16#define BENCHMARK_RE_H_
18#include "internal_macros.h"
22#if !defined(HAVE_STD_REGEX) && \
23 !defined(HAVE_GNU_POSIX_REGEX) && \
24 !defined(HAVE_POSIX_REGEX)
26 #if defined(BENCHMARK_OS_LINUX) || defined(BENCHMARK_OS_APPLE)
27 #define HAVE_POSIX_REGEX 1
28 #elif __cplusplus >= 199711L
29 #define HAVE_STD_REGEX 1
35#if defined(BENCHMARK_HAS_NO_EXCEPTIONS) && \
36 defined(HAVE_STD_REGEX) && \
37 (defined(HAVE_GNU_POSIX_REGEX) || defined(HAVE_POSIX_REGEX))
41#if defined(HAVE_STD_REGEX)
43#elif defined(HAVE_GNU_POSIX_REGEX)
45#elif defined(HAVE_POSIX_REGEX)
48#error No regular expression backend was found!
63 Regex() : init_(
false) {}
71 bool Init(
const std::string& spec, std::string* error);
74 bool Match(
const std::string& str);
79#if defined(HAVE_STD_REGEX)
81#elif defined(HAVE_POSIX_REGEX) || defined(HAVE_GNU_POSIX_REGEX)
84#error No regular expression backend implementation available
88#if defined(HAVE_STD_REGEX)
90inline bool Regex::Init(
const std::string& spec, std::string* error) {
91#ifdef BENCHMARK_HAS_NO_EXCEPTIONS
96 re_ = std::regex(spec, std::regex_constants::extended);
98#ifndef BENCHMARK_HAS_NO_EXCEPTIONS
100catch (
const std::regex_error& e) {
109inline Regex::~Regex() {}
111inline bool Regex::Match(
const std::string& str) {
115 return std::regex_search(str, re_);
119inline bool Regex::Init(
const std::string& spec, std::string* error) {
120 int ec = regcomp(&re_, spec.c_str(), REG_EXTENDED | REG_NOSUB);
123 size_t needed = regerror(ec, &re_,
nullptr, 0);
124 char* errbuf =
new char[needed];
125 regerror(ec, &re_, errbuf, needed);
129 BM_CHECK_NE(needed, 0);
130 error->assign(errbuf, needed - 1);
142inline Regex::~Regex() {
148inline bool Regex::Match(
const std::string& str) {
152 return regexec(&re_, str.c_str(), 0,
nullptr, 0) == 0;