[llvm] [Support][NFC] Add test documenting that empty `Regex` pattern matches nothing. (PR #83849)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 4 06:01:30 PST 2024


https://github.com/martinboehme created https://github.com/llvm/llvm-project/pull/83849

I was wondering about this when I recently used `Regex`, and I thought it would
be nice to have a test documenting this behavior.

>From f853ccf30fd64b27ad0883e51e25b851a50c6f96 Mon Sep 17 00:00:00 2001
From: Martin Braenne <mboehme at google.com>
Date: Mon, 4 Mar 2024 08:57:55 +0000
Subject: [PATCH] [Support][NFC] Add test documenting that empty `Regex`
 pattern matches nothing.

I was wondering about this when I recently used `Regex`, and I thought it would
be nice to have a test documenting this behavior.
---
 llvm/unittests/Support/RegexTest.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/llvm/unittests/Support/RegexTest.cpp b/llvm/unittests/Support/RegexTest.cpp
index 09f674bb209c07..c6ac42591d1fc7 100644
--- a/llvm/unittests/Support/RegexTest.cpp
+++ b/llvm/unittests/Support/RegexTest.cpp
@@ -60,6 +60,14 @@ TEST_F(RegexTest, Basics) {
   EXPECT_TRUE(r5.match(String));
 }
 
+TEST_F(RegexTest, EmptyPattern) {
+  // The empty pattern doesn't match anything -- not even the empty string.
+  // (This is different from some other regex implementations.)
+  Regex r("");
+  EXPECT_FALSE(r.match("123"));
+  EXPECT_FALSE(r.match(""));
+}
+
 TEST_F(RegexTest, Backreferences) {
   Regex r1("([a-z]+)_\\1");
   SmallVector<StringRef, 4> Matches;



More information about the llvm-commits mailing list