Commit fa164531b8 for perl
commit fa164531b898f6392f5d3c99cef21f90887e4bae
Author: Karl Williamson <khw@cpan.org>
Date: Sat Nov 21 09:36:26 2026 -0700
regcomp.c: Don't optimize /[]/ containing multi-char fold
Fixes #25080
Here we are trying to see if a character class, which usually is
implemented by an ANYOF-type node, can be optimized into an EXACTFU-type
node. This can happen even without /i being in effect if it so happens
that the members of the class all fold to the same code point, which is
also a member of the class. EXACTFU is preferred to ANYOF because it
doesn't require converting UTF-8 to its code point at runtime, and
presents possibilities of being combined with adjacent EXACTFish nodes.
We know immediately that the fold isn't a member of the class if the
fold of the first character we try is to multiple characters. That's
because the class consists of single characters, so multiple ones can't
exist in it.
diff --git a/regcomp.c b/regcomp.c
index 0aac1d625e..97ec4e771f 100644
--- a/regcomp.c
+++ b/regcomp.c
@@ -12172,12 +12172,25 @@ S_optimize_regclass(pTHX_
* points) in the ASCII range, so we can't use it here to
* artificially restrict the fold domain, so we check if the
* class does or does not match some EXACTFish node. Further,
- * if we aren't under /i, and and the folded-to character is
- * part of a multi-character fold, we can't do this
- * optimization, as the sequence around it could be that
- * multi-character fold, and we don't here know the context, so
- * we have to assume it is that multi-char fold, to prevent
- * potential bugs.
+ * if we aren't under /i, both:
+ * 1) the folds of all characters in the class must be
+ * included in the class. Since the class consists of
+ * single characters, that means the folds must all be
+ * single characters. If A folds to B which folds to C,
+ * the Unicode database skips the intermediate B, and says
+ * that A folds to C. That means the folds of all
+ * characters in the class must be the same.
+ * 2) the fold-to character can't be part of a
+ * multi-character fold. That is if A folds to Z, we
+ * can't have anywhere in Unicode another character B,
+ * such that B folds to any of
+ * C Z
+ * Z D
+ * C Z D
+ * The reason for this as the sequence around Z could be
+ * that multi-character fold, and we don't here know the
+ * context, so we'd have to assume it is that multi-char
+ * fold, to prevent potential bugs.
*
* To do the general case, we first find the fold of the lowest
* code point (which may be higher than that lowest unfolded
@@ -12187,7 +12200,10 @@ S_optimize_regclass(pTHX_
Size_t foldlen;
U8 foldbuf[UTF8_MAXBYTES_CASE];
- UV folded = to_uni_fold_flags_(lowest_cp, foldbuf, &foldlen, 0);
+ UV folded = to_uni_fold_flags_(lowest_cp, foldbuf, &foldlen,
+ FOLD_FLAGS_FULL);
+ /* Fold can't be multi-char */
+ if (foldlen == UNISKIP(folded)) {
U32 first_fold;
const U32 * remaining_folds;
Size_t folds_to_this_cp_count = inverse_folds_(
@@ -12295,6 +12311,7 @@ S_optimize_regclass(pTHX_
SvREFCNT_dec_NN(fold_list);
SvREFCNT_dec(all_cp_list);
+ }
}
}
diff --git a/t/re/re_tests b/t/re/re_tests
index 9cb2610821..61bedd77b4 100644
--- a/t/re/re_tests
+++ b/t/re/re_tests
@@ -1643,8 +1643,8 @@ a(.)\4294967298 ab\o{42}94967298 ya $1 b \d not converted to native; \o{} is
(?{})[\x{100}] \x{100} y $& \x{100}
# GH #25080
-/[\x{390}\x{1FD3}]/ \x{390} Ty $& \x{390}
-/[\x{390}\x{1FD3}]/ \x{1FD3} Ty $& \x{1FD3}
+/[\x{390}\x{1FD3}]/ \x{390} y $& \x{390}
+/[\x{390}\x{1FD3}]/ \x{1FD3} y $& \x{1FD3}
# RT #85964
^m?(\S)(.*)\1$ aba y $1 a