Down with C++ reserved names
[p5sagit/p5-mst-13.2.git] / regcomp.c
index 12bd96b..30c492b 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -268,7 +268,7 @@ static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  * "...".
  */
 #define        FAIL(msg) STMT_START {                                          \
-    char *ellipses = "";                                               \
+    const char *ellipses = "";                                         \
     IV len = RExC_end - RExC_precomp;                                  \
                                                                        \
     if (!SIZE_ONLY)                                                    \
@@ -288,7 +288,7 @@ static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  * "...".
  */
 #define        FAIL2(pat,msg) STMT_START {                                     \
-    char *ellipses = "";                                               \
+    const char *ellipses = "";                                         \
     IV len = RExC_end - RExC_precomp;                                  \
                                                                        \
     if (!SIZE_ONLY)                                                    \
@@ -478,8 +478,8 @@ static void clear_re(pTHX_ void *r);
 STATIC void
 S_scan_commit(pTHX_ RExC_state_t *pRExC_state, scan_data_t *data)
 {
-    STRLEN l = CHR_SVLEN(data->last_found);
-    STRLEN old_l = CHR_SVLEN(*data->longest);
+    const STRLEN l = CHR_SVLEN(data->last_found);
+    const STRLEN old_l = CHR_SVLEN(*data->longest);
 
     if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
        SvSetMagicSV(*data->longest, data->last_found);
@@ -793,13 +793,13 @@ and would end up looking like:
               scan += len;                                                   \
               len = 0;                                                       \
            } else {                                                          \
-               uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags);  \
+               uvc = utf8n_to_uvuni( (const U8*)uc, UTF8_MAXLEN, &len, uniflags);\
                uvc = to_uni_fold( uvc, foldbuf, &foldlen );                  \
                foldlen -= UNISKIP( uvc );                                    \
                scan = foldbuf + UNISKIP( uvc );                              \
            }                                                                 \
        } else {                                                              \
-           uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags);      \
+           uvc = utf8n_to_uvuni( (const U8*)uc, UTF8_MAXLEN, &len, uniflags);\
        }                                                                     \
     } else {                                                                  \
        uvc = (U32)*uc;                                                       \
@@ -837,13 +837,13 @@ S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *firs
     /* first pass, loop through and scan words */
     reg_trie_data *trie;
     regnode *cur;
-    U32 uniflags = ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY;
+    const U32 uniflags = ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY;
     STRLEN len = 0;
     UV uvc = 0;
     U16 curword = 0;
     U32 next_alloc = 0;
     /* we just use folder as a flag in utf8 */
-    const U8 *folder=( flags == EXACTF
+    const U8 * const folder = ( flags == EXACTF
                        ? PL_fold
                        : ( flags == EXACTFL
                            ? PL_fold_locale
@@ -851,7 +851,7 @@ S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *firs
                          )
                      );
 
-    U32 data_slot = add_data( pRExC_state, 1, "t" );
+    const U32 data_slot = add_data( pRExC_state, 1, "t" );
     SV *re_trie_maxbuff;
 
     GET_RE_DEBUG_FLAGS_DECL;
@@ -866,9 +866,9 @@ S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *firs
     });
 
 
-    re_trie_maxbuff=get_sv(RE_TRIE_MAXBUFF, 1);
+    re_trie_maxbuff = get_sv(RE_TRIE_MAXBUF_NAME, 1);
     if (!SvIOK(re_trie_maxbuff)) {
-        sv_setiv(re_trie_maxbuff, TRIE_SIMPLE_MAX_BUFF);
+        sv_setiv(re_trie_maxbuff, RE_TRIE_MAXBUF_INIT);
     }
 
     /*  -- First loop and Setup --
@@ -897,11 +897,11 @@ S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *firs
 
     for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
         regnode *noper = NEXTOPER( cur );
-        U8 *uc  = (U8*)STRING( noper );
-        U8 *e   = uc + STR_LEN( noper );
+        const U8 *uc = (U8*)STRING( noper );
+        const U8 *e  = uc + STR_LEN( noper );
         STRLEN foldlen = 0;
         U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
-        U8 *scan;
+        const U8 *scan;
 
         for ( ; uc < e ; uc += len ) {
             trie->charcount++;
@@ -941,8 +941,8 @@ S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *firs
     /*
         We now know what we are dealing with in terms of unique chars and
         string sizes so we can calculate how much memory a naive
-        representation using a flat table  will take. If its over a reasonable
-        limit (as specified by $^RE_TRIE_MAXBUFF) we use a more memory
+        representation using a flat table  will take. If it's over a reasonable
+        limit (as specified by ${^RE_TRIE_MAXBUF}) we use a more memory
         conservative but potentially much slower representation using an array
         of lists.
 
@@ -1160,8 +1160,7 @@ S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *firs
                 */
                 trie->states[ state ].trans.base=base;
             }
-            Renew( trie->trans, tp + 1, reg_trie_trans );
-
+            trie->lasttrans = tp + 1;
         }
     } else {
         /*
@@ -1409,7 +1408,7 @@ S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *firs
                 }
             }
         }
-        Renew( trie->trans, pos + 1, reg_trie_trans);
+        trie->lasttrans = pos + 1;
         Renew( trie->states, laststate + 1, reg_trie_state);
         DEBUG_TRIE_COMPILE_MORE_r(
                 PerlIO_printf( Perl_debug_log, " Alloc: %d Orig: %d elements, Final:%d. Savings of %%%5.2f\n",
@@ -1419,6 +1418,8 @@ S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *firs
 
         } /* end table compress */
     }
+    /* resize the trans array to remove unused space */
+    Renew( trie->trans, trie->lasttrans, reg_trie_trans);
 
     DEBUG_TRIE_COMPILE_r({
         U32 state;
@@ -1436,9 +1437,11 @@ S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *firs
             }
         }
         PerlIO_printf( Perl_debug_log, "\n-----:-----------------------");
+
         for( state = 0 ; state < trie->uniquecharcount ; state++ )
             PerlIO_printf( Perl_debug_log, "-----");
         PerlIO_printf( Perl_debug_log, "\n");
+
         for( state = 1 ; state < trie->laststate ; state++ ) {
             U32 base = trie->states[ state ].trans.base;
 
@@ -1455,14 +1458,16 @@ S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *firs
             if ( base ) {
                 U32 ofs = 0;
 
-                while( ( base + ofs - trie->uniquecharcount ) >=0 &&
-                      trie->trans[ base + ofs - trie->uniquecharcount ].check != state )
+                while( ( base + ofs  < trie->uniquecharcount ) ||
+                       ( base + ofs - trie->uniquecharcount < trie->lasttrans
+                         && trie->trans[ base + ofs - trie->uniquecharcount ].check != state))
                         ofs++;
 
                 PerlIO_printf( Perl_debug_log, "+%02X[ ", ofs);
 
                 for ( ofs = 0 ; ofs < trie->uniquecharcount ; ofs++ ) {
-                    if ( ( base + ofs - trie->uniquecharcount>=0) &&
+                    if ( ( base + ofs >= trie->uniquecharcount ) &&
+                         ( base + ofs - trie->uniquecharcount < trie->lasttrans ) &&
                          trie->trans[ base + ofs - trie->uniquecharcount ].check == state )
                     {
                        PerlIO_printf( Perl_debug_log, "%04X ",
@@ -1646,8 +1651,8 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
 */
                 char *s0 = STRING(scan), *s, *t;
                 char *s1 = s0 + STR_LEN(scan) - 1, *s2 = s1 - 4;
-                char *t0 = "\xcc\x88\xcc\x81";
-                char *t1 = t0 + 3;
+                 const char *t0 = "\xcc\x88\xcc\x81";
+                 const char *t1 = t0 + 3;
                 
                 for (s = s0 + 2;
                      s < s2 && (t = ninstr(s, s1, t0, t1));
@@ -1829,13 +1834,12 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
                   it would just call its tail, no WHILEM/CURLY needed.
 
                */
-                if (DO_TRIE) {
-                    if (!re_trie_maxbuff) {
-                        re_trie_maxbuff=get_sv(RE_TRIE_MAXBUFF, 1);
-                        if (!SvIOK(re_trie_maxbuff))
-                            sv_setiv(re_trie_maxbuff, TRIE_SIMPLE_MAX_BUFF);
-
-           }
+               if (DO_TRIE) {
+                   if (!re_trie_maxbuff) {
+                       re_trie_maxbuff = get_sv(RE_TRIE_MAXBUF_NAME, 1);
+                       if (!SvIOK(re_trie_maxbuff))
+                           sv_setiv(re_trie_maxbuff, RE_TRIE_MAXBUF_INIT);
+                   }
                     if ( SvIV(re_trie_maxbuff)>=0 && OP( startbranch )==BRANCH ) {
                         regnode *cur;
                         regnode *first = (regnode *)NULL;
@@ -1897,7 +1901,6 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
                             regnode *noper = NEXTOPER( cur );
                             regnode *noper_next = regnext( noper );
 
-
                             DEBUG_OPTIMISE_r({
                                 regprop( mysv, cur);
                                 PerlIO_printf( Perl_debug_log, "%*s%s",
@@ -2097,7 +2100,7 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
            }
            flags &= ~SCF_DO_STCLASS;
        }
-       else if (strchr((char*)PL_varies,OP(scan))) {
+       else if (strchr((const char*)PL_varies,OP(scan))) {
            I32 mincount, maxcount, minnext, deltanext, fl = 0;
            I32 f = flags, pos_before = 0;
            regnode *oscan = scan;
@@ -2241,7 +2244,7 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
 
                    /* Skip open. */
                    nxt = regnext(nxt);
-                   if (!strchr((char*)PL_simple,OP(nxt))
+                   if (!strchr((const char*)PL_simple,OP(nxt))
                        && !(PL_regkind[(U8)OP(nxt)] == EXACT
                             && STR_LEN(nxt) == 1))
                        goto nogo;
@@ -2446,7 +2449,7 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
                break;
            }
        }
-       else if (strchr((char*)PL_simple,OP(scan))) {
+       else if (strchr((const char*)PL_simple,OP(scan))) {
            int value = 0;
 
            if (flags & SCF_DO_SUBSTR) {
@@ -2754,7 +2757,7 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
 }
 
 STATIC I32
-S_add_data(pTHX_ RExC_state_t *pRExC_state, I32 n, char *s)
+S_add_data(pTHX_ RExC_state_t *pRExC_state, I32 n, const char *s)
 {
     if (RExC_rx->data) {
        Renewc(RExC_rx->data,
@@ -2788,11 +2791,11 @@ Perl_reginitcolors(pTHX)
                PL_colors[i] = ++s;
            }
            else
-               PL_colors[i] = s = "";
+               PL_colors[i] = s = (char *)"";
        }
     } else {
        while (i < 6)
-           PL_colors[i++] = "";
+           PL_colors[i++] = (char *)"";
     }
     PL_colorset = 1;
 }
@@ -2975,7 +2978,7 @@ Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
            else if ((OP(first) == EXACTF || OP(first) == EXACTFL))
                r->regstclass = first;
        }
-       else if (strchr((char*)PL_simple,OP(first)))
+       else if (strchr((const char*)PL_simple,OP(first)))
            r->regstclass = first;
        else if (PL_regkind[(U8)OP(first)] == BOUND ||
                 PL_regkind[(U8)OP(first)] == NBOUND)
@@ -3588,8 +3591,8 @@ S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
     }
 
     {
-       char *p;
-       static char parens[] = "=!<,>";
+        const char *p;
+        static const char parens[] = "=!<,>";
 
        if (paren && (p = strchr(parens, paren))) {
            U8 node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
@@ -5707,10 +5710,10 @@ S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
            node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
        }
        else if ( PL_regkind[(U8)op]  == TRIE ) {
-           I32 n = ARG(node);
-           reg_trie_data *trie=(reg_trie_data*)PL_regdata->data[n];
+            const I32 n = ARG(node);
+            const reg_trie_data *trie = (reg_trie_data*)PL_regdata->data[n];
+            const I32 arry_len = av_len(trie->words)+1;
            I32 word_idx;
-           I32 arry_len=av_len(trie->words)+1;
            PerlIO_printf(Perl_debug_log,
                       "%*s[Words:%d Chars Stored:%d Unique Chars:%d States:%d%s]\n",
                       (int)(2*(l+3)), "",
@@ -5867,7 +5870,7 @@ Perl_regdump(pTHX_ regexp *r)
     PerlIO_printf(Perl_debug_log, "\n");
     if (r->offsets) {
       U32 i;
-      U32 len = r->offsets[0];
+      const U32 len = r->offsets[0];
         GET_RE_DEBUG_FLAGS_DECL;
         DEBUG_OFFSETS_r({
       PerlIO_printf(Perl_debug_log, "Offsets: [%"UVuf"]\n\t", (UV)r->offsets[0]);
@@ -5911,7 +5914,7 @@ Perl_regprop(pTHX_ SV *sv, regnode *o)
        /* It would be nice to FAIL() here, but this may be called from
           regexec.c, and it would be hard to supply pRExC_state. */
        Perl_croak(aTHX_ "Corrupted regexp opcode");
-    sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
+    sv_catpv(sv, reg_name[OP(o)]); /* Take off const! */
 
     k = PL_regkind[(U8)OP(o)];
 
@@ -5925,7 +5928,7 @@ Perl_regprop(pTHX_ SV *sv, regnode *o)
          pv_uni_display(dsv, (U8*)STRING(o), STR_LEN(o), 60,
                         UNI_DISPLAY_REGEX) :
          STRING(o);
-       int len = do_utf8 ?
+       const int len = do_utf8 ?
          strlen(s) :
          STR_LEN(o);
        Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>",
@@ -6128,13 +6131,10 @@ Perl_pregfree(pTHX_ struct regexp *r)
     if (!r || (--r->refcnt > 0))
        return;
     DEBUG_r(if (re_debug_flags && (SvIV(re_debug_flags) & RE_DEBUG_COMPILE)) {
-        int len;
-         char *s;
-
-        s = (r->reganch & ROPT_UTF8) ? pv_uni_display(dsv, (U8*)r->precomp,
-               r->prelen, 60, UNI_DISPLAY_REGEX)
+        const char *s = (r->reganch & ROPT_UTF8)
+            ? pv_uni_display(dsv, (U8*)r->precomp, r->prelen, 60, UNI_DISPLAY_REGEX)
             : pv_display(dsv, r->precomp, r->prelen, 0, 60);
-        len = SvCUR(dsv);
+        const int len = SvCUR(dsv);
         if (!PL_colorset)
              reginitcolors();
         PerlIO_printf(Perl_debug_log,
@@ -6267,7 +6267,7 @@ S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
     STRLEN l2 = strlen(pat2);
     char buf[512];
     SV *msv;
-    char *message;
+    const char *message;
 
     if (l1 > 510)
        l1 = 510;