C++11 regular expressions

This commit is contained in:
Matt Clarkson 2014-07-30 19:08:32 +01:00
parent edfa60a1d1
commit f043826542
4 changed files with 68 additions and 3 deletions

View File

@ -1,6 +1,16 @@
# Define the source files
set(SOURCE_FILES "benchmark.cc" "colorprint.cc" "commandlineflags.cc" "sleep.cc" "sysinfo.cc" "walltime.cc")
set(RE_FILES "re.cc")
# Determine the correct regular expression engine to use
if(HAVE_STD_REGEX)
set(RE_FILES "re_std.cc")
elseif(HAVE_GNU_POSIX_REGEX)
set(RE_FILES "re_posix.cc")
elseif(HAVE_POSIX_REGEX)
set(RE_FILES "re_posix.cc")
else()
message(FATAL_ERROR "Failed to determine the source files for the regular expression backend")
endif()
# Build a regular expression library
add_library(benchmark_re ${RE_FILES})

View File

@ -15,10 +15,14 @@
#ifndef BENCHMARK_RE_H_
#define BENCHMARK_RE_H_
#if defined OS_FREEBSD
#if defined(HAVE_STD_REGEX)
#include <regex>
#elif defined(HAVE_GNU_POSIX_REGEX)
#include <gnuregex.h>
#else
#elif defined(HAVE_POSIX_REGEX)
#include <regex.h>
#else
#error No regular expression backend was found!
#endif
#include <string>
@ -42,7 +46,13 @@ class Regex {
private:
bool init_;
// Underlying regular expression object
#if defined(HAVE_STD_REGEX)
std::regex re_;
#elif defined(HAVE_POSIX_REGEX) || defined(HAVE_GNU_POSIX_REGEX)
regex_t re_;
#else
# error No regular expression backend implementation available
#endif
};
} // end namespace benchmark

45
src/re_std.cc Normal file
View File

@ -0,0 +1,45 @@
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <benchmark/macros.h>
#include "re.h"
namespace benchmark {
Regex::Regex() : init_(false) { }
bool Regex::Init(const std::string& spec, std::string* error) {
try {
re_ = std::regex(spec, std::regex_constants::extended);
init_ = true;
} catch (const std::regex_error& e) {
if (error) {
*error = e.what();
}
}
return init_;
}
Regex::~Regex() { }
bool Regex::Match(const std::string& str) {
if (!init_) {
return false;
}
return std::regex_search(str, re_);
}
} // end namespace benchmark