Commit f0438265 by Matt Clarkson

C++11 regular expressions

parent edfa60a1
# Define the source files # Define the source files
set(SOURCE_FILES "benchmark.cc" "colorprint.cc" "commandlineflags.cc" "sleep.cc" "sysinfo.cc" "walltime.cc") 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 # Build a regular expression library
add_library(benchmark_re ${RE_FILES}) add_library(benchmark_re ${RE_FILES})
......
...@@ -15,10 +15,14 @@ ...@@ -15,10 +15,14 @@
#ifndef BENCHMARK_RE_H_ #ifndef BENCHMARK_RE_H_
#define 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> #include <gnuregex.h>
#else #elif defined(HAVE_POSIX_REGEX)
#include <regex.h> #include <regex.h>
#else
#error No regular expression backend was found!
#endif #endif
#include <string> #include <string>
...@@ -42,7 +46,13 @@ class Regex { ...@@ -42,7 +46,13 @@ class Regex {
private: private:
bool init_; bool init_;
// Underlying regular expression object // 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_; regex_t re_;
#else
# error No regular expression backend implementation available
#endif
}; };
} // end namespace benchmark } // end namespace benchmark
......
// 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment