regcomp.c: Optimize /[0-9]/ into /\d/a
authorKarl Williamson <public@khwilliamson.com>
Wed, 27 Jun 2012 22:24:43 +0000 (16:24 -0600)
committerKarl Williamson <public@khwilliamson.com>
Sat, 30 Jun 2012 04:22:42 +0000 (22:22 -0600)
The commonly used [0-9] can be optimized into a smaller, faster node
that means the same thing.

regcomp.c

index 22a220f..cfed452 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -11710,8 +11710,13 @@ parseit:
     }
 
     /* [\w] can be optimized into \w, but not if there is anything else in the
-     * brackets (except for an initial '^' which indictes omplementing) */
-    if (element_count == 1 && (has_special_charset_op || has_special_non_charset_op)) {
+     * brackets (except for an initial '^' which indictes omplementing).  We
+     * also can optimize the common special case /[0-9]/ into /\d/a */
+    if (element_count == 1 &&
+        (has_special_charset_op
+         || has_special_non_charset_op
+         || (prevvalue == '0' && value == '9')))
+    {
         U8 op;
         bool invert = ANYOF_FLAGS(ret) & ANYOF_INVERT;
         const char * cur_parse = RExC_parse;
@@ -11783,6 +11788,9 @@ parseit:
                 op++;
             }
         }
+        else {  /* The remaining possibility is [0-9] */
+            op = (invert) ? NDIGITA : DIGITA;
+        }
 
         /* Throw away this ANYOF regnode, and emit the calculated one, which
          * should correspond to the beginning, not current, state of the parse