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

Sanjay Marreddi via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jan 7 13:15:06 PST 2024


https://github.com/SanjayMarreddi created https://github.com/llvm/llvm-project/pull/77256

Using `regex_search` with the regex_constant `match_default` and a simple regex pattern `$` is expected to match general strings such as _"a", "ab", "abc"..._ at `[last, last)` positions.  But, the current implementation fails to do so.

Fixes #75042


>From bbc98d9c18c7bdf185ad8918fa2bb6f7af039935 Mon Sep 17 00:00:00 2001
From: SanjayMarreddi <sanjay.mareddi at gmail.com>
Date: Sun, 7 Jan 2024 20:55:11 +0000
Subject: [PATCH] [libc++] Fix `regex_search` to match `$` alone with
 `match_default` flag

---
 libcxx/include/regex                                       | 3 +++
 .../std/re/re.const/re.matchflag/match_not_eol.pass.cpp    | 7 +++++++
 2 files changed, 10 insertions(+)

diff --git a/libcxx/include/regex b/libcxx/include/regex
index b575a267583b5f..0761d9de54a95c 100644
--- a/libcxx/include/regex
+++ b/libcxx/include/regex
@@ -1889,6 +1889,9 @@ void __r_anchor_multiline<_CharT>::__exec(__state& __s) const {
   if (__s.__current_ == __s.__last_ && !(__s.__flags_ & regex_constants::match_not_eol)) {
     __s.__do_   = __state::__accept_but_not_consume;
     __s.__node_ = this->first();
+  } else if (__s.__current_ == __s.__first_ && !(__s.__flags_ & regex_constants::match_not_eol)) {
+    __s.__do_   = __state::__accept_but_not_consume;
+    __s.__node_ = this->first();
   } else if (__multiline_ && std::__is_eol(*__s.__current_)) {
     __s.__do_   = __state::__accept_but_not_consume;
     __s.__node_ = this->first();
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 edeea517d2537a..76f8986b2d539f 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,12 @@ 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));
+    }
+
   return 0;
 }



More information about the libcxx-commits mailing list