[libcxx-commits] [libcxx] [libc++] Fix `regex_search` to match `$` alone with `match_default` flag (PR #78845)

Sanjay Marreddi via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jan 20 09:27:05 PST 2024


https://github.com/SanjayMarreddi updated https://github.com/llvm/llvm-project/pull/78845

>From 9536e7b22f92ce855ab22fdb5395b11f6f559eb2 Mon Sep 17 00:00:00 2001
From: SanjayMarreddi <sanjay.mareddi at gmail.com>
Date: Sat, 20 Jan 2024 13:01:10 +0000
Subject: [PATCH] [libc++] Fix `regex_search` to match `$` alone with
 `match_default` flag

---
 libcxx/include/regex                          |  8 ++++
 .../re.matchflag/match_not_eol.pass.cpp       | 39 +++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/libcxx/include/regex b/libcxx/include/regex
index b575a267583b5fe..48af5b8b57fd649 100644
--- a/libcxx/include/regex
+++ b/libcxx/include/regex
@@ -5124,6 +5124,14 @@ bool basic_regex<_CharT, _Traits>::__search(
       }
       __m.__matches_.assign(__m.size(), __m.__unmatched_);
     }
+    __m.__matches_.assign(__m.size(), __m.__unmatched_);
+    if (__match_at_start(__first, __last, __m, __flags, false)) {
+      __m.__prefix_.second  = __m[0].first;
+      __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
+      __m.__suffix_.first   = __m[0].second;
+      __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
+      return true;
+    }
   }
   __m.__matches_.clear();
   return false;
diff --git a/libcxx/test/std/re/re.const/re.matchflag/match_not_eol.pass.cpp b/libcxx/test/std/re/re.const/re.matchflag/match_not_eol.pass.cpp
index edeea517d2537a3..89d939eaf809f4b 100644
--- a/libcxx/test/std/re/re.const/re.matchflag/match_not_eol.pass.cpp
+++ b/libcxx/test/std/re/re.const/re.matchflag/match_not_eol.pass.cpp
@@ -47,5 +47,44 @@ int main(int, char**)
     assert( std::regex_search(target, re, std::regex_constants::match_not_eol));
     }
 
+    {
+      std::string target = "foo";
+      std::regex re("$");
+      assert(std::regex_search(target, re));
+      assert(!std::regex_search(target, re, std::regex_constants::match_not_eol));
+
+      std::smatch match;
+      assert(std::regex_search(target, match, re));
+      assert(match.position(0) == 3);
+      assert(match.length(0) == 0);
+      assert(!std::regex_search(target, match, re, std::regex_constants::match_not_eol));
+      assert(match.length(0) == 0);
+    }
+
+    {
+      std::string target = "foo";
+      std::regex re("$", std::regex::multiline);
+      std::smatch match;
+      assert(std::regex_search(target, match, re));
+      assert(match.position(0) == 3);
+      assert(match.length(0) == 0);
+      assert(!std::regex_search(target, match, re, std::regex_constants::match_not_eol));
+      assert(match.length(0) == 0);
+    }
+
+    {
+      std::string target = "foo";
+      std::regex re("$");
+      assert(!std::regex_match(target, re));
+      assert(!std::regex_match(target, re, std::regex_constants::match_not_eol));
+    }
+
+    {
+      std::string target = "a";
+      std::regex re("^b*$");
+      assert(!std::regex_search(target, re));
+      assert(!std::regex_search(target, re, std::regex_constants::match_not_eol));
+    }
+
   return 0;
 }



More information about the libcxx-commits mailing list