Fix String.endsWith for overlapping regex matches (#1880)

This commit is contained in:
Alexandre Kohler authored and GitHub committed 2026-09-29 21:52:12 -07:00
1 parent 1271da2741
commit 8331570aaf
3 files changed
+15 -6

No files matched your search

@@ -333,14 +333,13 @@ public final class StringNodes {
@TruffleBoundary
@Specialization
// inefficient but at least correct
protected boolean eval(String self, VmRegex regex) {
var matcher = regex.matcher(self);
var end = -1;
while (matcher.find()) {
end = matcher.end();
// try every suffix; `find()` would skip matches that overlap an earlier one
var matcher = regex.matcher(self).useTransparentBounds(true).useAnchoringBounds(false);
for (var start = self.length(); ; start = self.offsetByCodePoints(start, -1)) {
if (matcher.region(start, self.length()).matches()) return true;
if (start == 0) return false;
}
return end == self.length();
}
}
@@ -76,7 +76,12 @@ facts {
["endsWith()"] {
str1.endsWith("")
str1.endsWith("efg")
str1.endsWith(Regex(#"\w"#))
"aaa".endsWith(Regex("aa"))
!str1.endsWith("efx")
!str1.endsWith(Regex(#"\d"#))
!str1.endsWith(Regex(#"\bfg"#))
!str1.endsWith(Regex("^fg"))
}
["isRegex"] {
@@ -59,6 +59,11 @@ facts {
true
true
true
true
true
true
true
true
}
["isRegex"] {
true