C++11 regular expressions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 73992ef..fd3bfc7 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -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})
diff --git a/src/re.h b/src/re.h
index b179bb2..a2e9fdf 100644
--- a/src/re.h
+++ b/src/re.h
@@ -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 @@
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
diff --git a/src/re.cc b/src/re_posix.cc
similarity index 100%
rename from src/re.cc
rename to src/re_posix.cc
diff --git a/src/re_std.cc b/src/re_std.cc
new file mode 100644
index 0000000..30883fd
--- /dev/null
+++ b/src/re_std.cc
@@ -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