summaryrefslogtreecommitdiffstats
path: root/googletest/include/gtest/internal
diff options
context:
space:
mode:
authorDerek Mauro <dmauro@google.com>2022-04-26 20:26:45 (GMT)
committerCopybara-Service <copybara-worker@google.com>2022-04-26 20:27:20 (GMT)
commite33c2b24ca3e13df961ed369f7ed21e4cfcf9eec (patch)
treef5ee5044093047f2f02d200f54e1fd3649dcba2c /googletest/include/gtest/internal
parent238e4745c62258ca1d2f3c514789205e8419eeee (diff)
downloadgoogletest-e33c2b24ca3e13df961ed369f7ed21e4cfcf9eec.zip
googletest-e33c2b24ca3e13df961ed369f7ed21e4cfcf9eec.tar.gz
googletest-e33c2b24ca3e13df961ed369f7ed21e4cfcf9eec.tar.bz2
Use RE2 for the regex implementation when building with Bazel and using Abseil
bazel build --define=absl=1 ... A dependency on RE2 is now required when building GoogleTest with Abseil. Using RE2 will provide a consistent cross-platform regex experience. Users will need to add the com_googlesource_code_re2, bazel_skylib, and platforms repository to their WORKSPACE files. See our WORKSPACE file in the root directory of this project for an example of how to add the dependencies. Please note that the com_googlesource_code_re2 dependency must use a commit from the `abseil` branch of the project: https://github.com/google/re2/tree/abseil PiperOrigin-RevId: 444650118 Change-Id: I45c55b26684c0c50d721a05b81c5f8a0c092400f
Diffstat (limited to 'googletest/include/gtest/internal')
-rw-r--r--googletest/include/gtest/internal/gtest-port.h70
1 files changed, 42 insertions, 28 deletions
diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h
index 5d9220b..b1cae90 100644
--- a/googletest/include/gtest/internal/gtest-port.h
+++ b/googletest/include/gtest/internal/gtest-port.h
@@ -170,6 +170,7 @@
// GTEST_HAS_TYPED_TEST - typed tests
// GTEST_HAS_TYPED_TEST_P - type-parameterized tests
// GTEST_IS_THREADSAFE - Google Test is thread-safe.
+// GTEST_USES_RE2 - the RE2 regular expression library is used
// GTEST_USES_POSIX_RE - enhanced POSIX regex is used. Do not confuse with
// GTEST_HAS_POSIX_RE (see above) which users can
// define themselves.
@@ -215,10 +216,13 @@
// - synchronization primitives.
//
// Regular expressions:
-// RE - a simple regular expression class using the POSIX
-// Extended Regular Expression syntax on UNIX-like platforms
-// or a reduced regular exception syntax on other
-// platforms, including Windows.
+// RE - a simple regular expression class using
+// 1) the RE2 syntax on all platforms when built with RE2
+// and Abseil as dependencies
+// 2) the POSIX Extended Regular Expression syntax on
+// UNIX-like platforms,
+// 3) A reduced regular exception syntax on other platforms,
+// including Windows.
// Logging:
// GTEST_LOG_() - logs messages at the specified severity level.
// LogToStderr() - directs all log messages to stderr.
@@ -385,32 +389,19 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#endif
#endif
-#if GTEST_USES_PCRE
-// The appropriate headers have already been included.
-
+// Select the regular expression implementation.
+#if GTEST_HAS_ABSL
+// When using Abseil, RE2 is required.
+#include "absl/strings/string_view.h"
+#include "re2/re2.h"
+#define GTEST_USES_RE2 1
#elif GTEST_HAS_POSIX_RE
-
-// On some platforms, <regex.h> needs someone to define size_t, and
-// won't compile otherwise. We can #include it here as we already
-// included <stdlib.h>, which is guaranteed to define size_t through
-// <stddef.h>.
#include <regex.h> // NOLINT
-
#define GTEST_USES_POSIX_RE 1
-
-#elif GTEST_OS_WINDOWS
-
-// <regex.h> is not available on Windows. Use our own simple regex
-// implementation instead.
-#define GTEST_USES_SIMPLE_RE 1
-
#else
-
-// <regex.h> may not be available on this platform. Use our own
-// simple regex implementation instead.
+// Use our own simple regex implementation.
#define GTEST_USES_SIMPLE_RE 1
-
-#endif // GTEST_USES_PCRE
+#endif
#ifndef GTEST_HAS_EXCEPTIONS
// The user didn't tell us whether exceptions are enabled, so we need
@@ -868,8 +859,31 @@ GTEST_API_ bool IsTrue(bool condition);
// Defines RE.
-#if GTEST_USES_PCRE
-// if used, PCRE is injected by custom/gtest-port.h
+#if GTEST_USES_RE2
+
+// This is almost `using RE = ::RE2`, except it is copy-constructible, and it
+// needs to disambiguate the `std::string`, `absl::string_view`, and `const
+// char*` constructors.
+class GTEST_API_ RE {
+ public:
+ RE(absl::string_view regex) : regex_(regex) {} // NOLINT
+ RE(const char* regex) : RE(absl::string_view(regex)) {} // NOLINT
+ RE(const std::string& regex) : RE(absl::string_view(regex)) {} // NOLINT
+ RE(const RE& other) : RE(other.pattern()) {}
+
+ const std::string& pattern() const { return regex_.pattern(); }
+
+ static bool FullMatch(absl::string_view str, const RE& re) {
+ return RE2::FullMatch(str, re.regex_);
+ }
+ static bool PartialMatch(absl::string_view str, const RE& re) {
+ return RE2::PartialMatch(str, re.regex_);
+ }
+
+ private:
+ RE2 regex_;
+};
+
#elif GTEST_USES_POSIX_RE || GTEST_USES_SIMPLE_RE
// A simple C++ wrapper for <regex.h>. It uses the POSIX Extended
@@ -920,7 +934,7 @@ class GTEST_API_ RE {
#endif
};
-#endif // GTEST_USES_PCRE
+#endif // ::testing::internal::RE implementation
// Formats a source file path and a line number as they would appear
// in an error message from the compiler used to compile this code.