Changes to allow compiler with gcc-2.8.1 in C++ mode,
[p5sagit/p5-mst-13.2.git] / regcomp.c
index aa713bc..8d66f38 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -127,10 +127,10 @@ static regnode *reg_node _((U8));
 static regnode *regpiece _((I32 *));
 static void reginsert _((U8, regnode *));
 static void regoptail _((regnode *, regnode *));
-static void regset _((char *, I32));
 static void regtail _((regnode *, regnode *));
 static char* regwhite _((char *, char *));
 static char* nextchar _((void));
+static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn));
 
 static U32 regseen;
 static I32 seen_zerolen;
@@ -139,7 +139,6 @@ static I32 extralen;
 
 #ifdef DEBUGGING
 static int colorset;
-char *colors[4];
 #endif 
 
 /* Length of a variant. */
@@ -170,8 +169,13 @@ static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 #define SF_FIX_BEFORE_EOL      (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
 #define SF_FL_BEFORE_EOL       (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
 
-#define SF_FIX_SHIFT_EOL       (+2)
-#define SF_FL_SHIFT_EOL                (+4)
+#ifdef NO_UNARY_PLUS
+#  define SF_FIX_SHIFT_EOL     (0+2)
+#  define SF_FL_SHIFT_EOL              (0+4)
+#else
+#  define SF_FIX_SHIFT_EOL     (+2)
+#  define SF_FL_SHIFT_EOL              (+4)
+#endif
 
 #define SF_FIX_BEFORE_SEOL     (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
 #define SF_FIX_BEFORE_MEOL     (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
@@ -182,6 +186,7 @@ static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 #define SF_HAS_PAR             0x80
 #define SF_IN_PAR              0x100
 #define SF_HAS_EVAL            0x200
+#define SCF_DO_SUBSTR          0x400
 
 static void
 scan_commit(scan_data_t *data)
@@ -215,8 +220,6 @@ scan_commit(scan_data_t *data)
     data->flags &= ~SF_BEFORE_EOL;
 }
 
-#define SCF_DO_SUBSTR 1
-
 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
    to the position after last scanned or to NULL. */
 
@@ -340,7 +343,7 @@ study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32
                    scan = next;
                    if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
                        pars++;
-                   if (data_fake.flags & SF_HAS_EVAL)
+                   if (data && (data_fake.flags & SF_HAS_EVAL))
                        data->flags |= SF_HAS_EVAL;
                    if (code == SUSPEND) 
                        break;
@@ -586,7 +589,7 @@ study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32
                        data->longest = &(data->longest_float);
                    }
                }
-               if (fl & SF_HAS_EVAL)
+               if (data && (fl & SF_HAS_EVAL))
                    data->flags |= SF_HAS_EVAL;
              optimize_curly_tail:
 #ifdef REGALIGN
@@ -635,7 +638,7 @@ study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32
            }
            if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
                pars++;
-           if (data_fake.flags & SF_HAS_EVAL)
+           if (data && (data_fake.flags & SF_HAS_EVAL))
                data->flags |= SF_HAS_EVAL;
        } else if (OP(scan) == OPEN) {
            pars++;
@@ -812,6 +815,11 @@ pregcomp(char *exp, char *xend, PMOP *pm)
     r->regstclass = NULL;
     r->naughty = regnaughty >= 10;     /* Probably an expensive pattern. */
     scan = r->program + 1;             /* First BRANCH. */
+
+    /* XXXX To minimize changes to RE engine we always allocate
+       3-units-long substrs field. */
+    Newz(1004, r->substrs, 1, struct reg_substr_data);
+
     if (OP(scan) != BRANCH) {  /* Only one top-level choice. */
        scan_data_t data;
        I32 fake;
@@ -906,7 +914,7 @@ pregcomp(char *exp, char *xend, PMOP *pm)
            r->float_substr = data.longest_float;
            r->float_min_offset = data.offset_float_min;
            r->float_max_offset = data.offset_float_max;
-           fbm_compile(r->float_substr);
+           fbm_compile(r->float_substr, 0);
            BmUSEFUL(r->float_substr) = 100;
            if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
                && (!(data.flags & SF_FL_BEFORE_MEOL)
@@ -926,7 +934,7 @@ pregcomp(char *exp, char *xend, PMOP *pm)
                    || (regflags & PMf_MULTILINE)))) {
            r->anchored_substr = data.longest_fixed;
            r->anchored_offset = data.offset_fixed;
-           fbm_compile(r->anchored_substr);
+           fbm_compile(r->anchored_substr, 0);
            BmUSEFUL(r->anchored_substr) = 100;
            if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
                && (!(data.flags & SF_FIX_BEFORE_MEOL)
@@ -1822,15 +1830,6 @@ regwhite(char *p, char *e)
     return p;
 }
 
-static void
-regset(char *opnd, register I32 c)
-{
-    if (SIZE_ONLY)
-       return;
-    c &= 0xFF;
-    opnd[1 + (c >> 3)] |= (1 << (c & 7));
-}
-
 static regnode *
 regclass(void)
 {
@@ -1866,67 +1865,95 @@ regclass(void)
     while (regparse < regxend && *regparse != ']') {
        skipcond:
        Class = UCHARAT(regparse++);
+       if (Class == '[' && regparse + 1 < regxend &&
+           /* I smell either [: or [= or [. -- POSIX has been here, right? */
+           (*regparse == ':' || *regparse == '=' || *regparse == '.')) {
+           char  posixccc = *regparse;
+           char* posixccs = regparse++;
+           
+           while (regparse < regxend && *regparse != posixccc)
+               regparse++;
+           if (regparse == regxend)
+               /* Grandfather lone [:, [=, [. */
+               regparse = posixccs;
+           else {
+               regparse++; /* skip over the posixccc */
+               if (*regparse == ']') {
+                   /* Not Implemented Yet.
+                    * (POSIX Extended Character Classes, that is)
+                    * The text between e.g. [: and :] would start
+                    * at posixccs + 1 and stop at regparse - 2. */
+                   if (dowarn && !SIZE_ONLY)
+                       warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
+                   regparse++; /* skip over the ending ] */
+               }
+           }
+       }
        if (Class == '\\') {
            Class = UCHARAT(regparse++);
            switch (Class) {
            case 'w':
-               if (regflags & PMf_LOCALE) {
-                   if (!SIZE_ONLY)
+               if (!SIZE_ONLY) {
+                   if (regflags & PMf_LOCALE)
                        *opnd |= ANYOF_ALNUML;
-               }
-               else {
-                   for (Class = 0; Class < 256; Class++)
-                       if (isALNUM(Class))
-                           regset(opnd, Class);
+                   else {
+                       for (Class = 0; Class < 256; Class++)
+                           if (isALNUM(Class))
+                               ANYOF_SET(opnd, Class);
+                   }
                }
                lastclass = 1234;
                continue;
            case 'W':
-               if (regflags & PMf_LOCALE) {
-                   if (!SIZE_ONLY)
+               if (!SIZE_ONLY) {
+                   if (regflags & PMf_LOCALE)
                        *opnd |= ANYOF_NALNUML;
-               }
-               else {
-                   for (Class = 0; Class < 256; Class++)
-                       if (!isALNUM(Class))
-                           regset(opnd, Class);
+                   else {
+                       for (Class = 0; Class < 256; Class++)
+                           if (!isALNUM(Class))
+                               ANYOF_SET(opnd, Class);
+                   }
                }
                lastclass = 1234;
                continue;
            case 's':
-               if (regflags & PMf_LOCALE) {
-                   if (!SIZE_ONLY)
+               if (!SIZE_ONLY) {
+                   if (regflags & PMf_LOCALE)
                        *opnd |= ANYOF_SPACEL;
-               }
-               else {
-                   for (Class = 0; Class < 256; Class++)
-                       if (isSPACE(Class))
-                           regset(opnd, Class);
+                   else {
+                       for (Class = 0; Class < 256; Class++)
+                           if (isSPACE(Class))
+                               ANYOF_SET(opnd, Class);
+                   }
                }
                lastclass = 1234;
                continue;
            case 'S':
-               if (regflags & PMf_LOCALE) {
-                   if (!SIZE_ONLY)
+               if (!SIZE_ONLY) {
+                   if (regflags & PMf_LOCALE)
                        *opnd |= ANYOF_NSPACEL;
-               }
-               else {
-                   for (Class = 0; Class < 256; Class++)
-                       if (!isSPACE(Class))
-                           regset(opnd, Class);
+                   else {
+                       for (Class = 0; Class < 256; Class++)
+                           if (!isSPACE(Class))
+                               ANYOF_SET(opnd, Class);
+                   }
                }
                lastclass = 1234;
                continue;
            case 'd':
-               for (Class = '0'; Class <= '9'; Class++)
-                   regset(opnd, Class);
+               if (!SIZE_ONLY) {
+                   for (Class = '0'; Class <= '9'; Class++)
+                       ANYOF_SET(opnd, Class);
+               }
                lastclass = 1234;
                continue;
            case 'D':
-               for (Class = 0; Class < '0'; Class++)
-                   regset(opnd, Class);
-               for (Class = '9' + 1; Class < 256; Class++)
-                   regset(opnd, Class);
+               if (!SIZE_ONLY) {
+                   for (Class = 0; Class < '0'; Class++)
+                       ANYOF_SET(opnd, Class);
+                   for (Class = '9' + 1; Class < 256; Class++)
+                       ANYOF_SET(opnd, Class);
+               }
                lastclass = 1234;
                continue;
            case 'n':
@@ -1979,13 +2006,31 @@ regclass(void)
                continue;       /* do it next time */
            }
        }
-       for ( ; lastclass <= Class; lastclass++)
-           regset(opnd, lastclass);
+       if (!SIZE_ONLY) {
+           for ( ; lastclass <= Class; lastclass++)
+               ANYOF_SET(opnd, lastclass);
+       }
        lastclass = Class;
     }
     if (*regparse != ']')
        FAIL("unmatched [] in regexp");
     nextchar();
+    /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
+    if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
+       for (Class = 0; Class < 256; ++Class) {
+           if (ANYOF_TEST(opnd, Class)) {
+               I32 cf = fold[Class];
+               ANYOF_SET(opnd, cf);
+           }
+       }
+       *opnd &= ~ANYOF_FOLD;
+    }
+    /* optimize inverted simple patterns (e.g. [^a-z]) */
+    if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
+       for (Class = 0; Class < 32; ++Class)
+           opnd[1 + Class] ^= 0xFF;
+       *opnd = 0;
+    }
     return ret;
 }
 
@@ -2022,12 +2067,7 @@ nextchar(void)
 - reg_node - emit a node
 */
 static regnode *                       /* Location. */
-#ifdef CAN_PROTOTYPE
 reg_node(U8 op)
-#else
-reg_node(op)
-U8 op;
-#endif
 {
     register regnode *ret;
     register regnode *ptr;
@@ -2055,13 +2095,7 @@ U8 op;
 - reganode - emit a node with an argument
 */
 static regnode *                       /* Location. */
-#ifdef CAN_PROTOTYPE
 reganode(U8 op, U32 arg)
-#else
-reganode(op, arg)
-U8 op;
-U32 arg;
-#endif
 {
     register regnode *ret;
     register regnode *ptr;
@@ -2088,15 +2122,8 @@ U32 arg;
 /*
 - regc - emit (if appropriate) a byte of code
 */
-#ifdef CAN_PROTOTYPE
 static void
 regc(U8 b, char* s)
-#else
-static void
-regc(b, s)
-U8 b;
-char *s;
-#endif
 {
     if (!SIZE_ONLY)
        *s = b;
@@ -2107,15 +2134,8 @@ char *s;
 *
 * Means relocating the operand.
 */
-#ifdef CAN_PROTOTYPE
 static void
 reginsert(U8 op, regnode *opnd)
-#else
-static void
-reginsert(op, opnd)
-U8 op;
-regnode *opnd;
-#endif
 {
     register regnode *src;
     register regnode *dst;
@@ -2566,10 +2586,13 @@ pregfree(struct regexp *r)
        Safefree(r->precomp);
     if (r->subbase)
        Safefree(r->subbase);
-    if (r->anchored_substr)
-       SvREFCNT_dec(r->anchored_substr);
-    if (r->float_substr)
-       SvREFCNT_dec(r->float_substr);
+    if (r->substrs) {
+       if (r->anchored_substr)
+           SvREFCNT_dec(r->anchored_substr);
+       if (r->float_substr)
+           SvREFCNT_dec(r->float_substr);
+       Safefree(r->substrs);
+    }
     if (r->data) {
        int n = r->data->count;
        while (--n >= 0) {
@@ -2623,11 +2646,11 @@ regnext(register regnode *p)
 }
 
 #ifdef I_STDARG
-void   
+static void    
 re_croak2(const char* pat1,const char* pat2,...)
 #else
 /*VARARGS0*/
-void   
+static void    
 re_croak2(const char* pat1,const char* pat2, va_alist)
     const char* pat1;
     const char* pat2;
@@ -2662,6 +2685,3 @@ re_croak2(const char* pat1,const char* pat2, va_alist)
     buf[l1] = '\0';                    /* Overwrite \n */
     croak("%s", buf);
 }
-
-
-