This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Relax some restrictions on optimizations for locale
authorKarl Williamson <public@khwilliamson.com>
Tue, 10 Jul 2012 04:00:16 +0000 (22:00 -0600)
committerKarl Williamson <public@khwilliamson.com>
Wed, 25 Jul 2012 03:13:48 +0000 (21:13 -0600)
Prior to this commit, we didn't do any inversions for bracketed
character classes running under locale.  However, this is more strict
than necessary.  If there is no folding, and everything else is known at
compile time, then what is matched when the result is complemented is
well-defined, and can be done now.  (Also clarifies one of the affected
comments)

regcomp.c

index c237c64..5a87e9c 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -12397,15 +12397,14 @@ parseit:
      * Now we can see about various optimizations.  Fold calculation (which we
      * did above) needs to take place before inversion.  Otherwise /[^k]/i
      * would invert to include K, which under /i would match k, which it
-     * shouldn't. */
+     * shouldn't.  Therefore we can't invert folded locale now, as it won't be
+     * folded until runtime */
 
-    /* Optimize inverted simple patterns (e.g. [^a-z]).  Note that we haven't
-     * set the FOLD flag yet, so this does optimize those.  It doesn't
-     * optimize locale.  Doing so perhaps could be done as long as there is
-     * nothing like \w in it; some thought also would have to be given to the
-     * interaction with above 0x100 chars */
+    /* Optimize inverted simple patterns (e.g. [^a-z]) when everything is known
+     * at compile time.  Besides not inverting folded locale now, we can't invert
+     * if there are things such as \w, which aren't known until runtime */
     if (invert
-        && ! LOC
+        && ! (LOC && (FOLD || (ANYOF_FLAGS(ret) & ANYOF_CLASS)))
        && ! depends_list
        && ! unicode_alternate
        && ! HAS_NONLOCALE_RUNTIME_PROPERTY_DEFINITION)