Store C<study>'s data as U32s, instead of I32s.
authorNicholas Clark <nick@ccl4.org>
Tue, 28 Jun 2011 10:17:38 +0000 (12:17 +0200)
committerNicholas Clark <nick@ccl4.org>
Fri, 1 Jul 2011 12:05:41 +0000 (14:05 +0200)
The "no more" condition is now represented as ~0, instead of -1.

pp.c
regexec.c
util.c

diff --git a/pp.c b/pp.c
index 12ab913..f177165 100644 (file)
--- a/pp.c
+++ b/pp.c
@@ -707,9 +707,8 @@ PP(pp_study)
 {
     dVAR; dSP; dPOPss;
     register unsigned char *s;
-    register I32 ch;
-    register I32 *sfirst;
-    register I32 *snext;
+    U32 *sfirst;
+    U32 *snext;
     STRLEN len;
     MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_study) : NULL;
 
@@ -729,7 +728,7 @@ PP(pp_study)
        RETPUSHNO;
     }
 
-    Newx(sfirst, 256 + len, I32);
+    Newx(sfirst, 256 + len, U32);
 
     if (!sfirst)
        DIE(aTHX_ "do_study: out of memory");
@@ -738,19 +737,15 @@ PP(pp_study)
     if (!mg)
        mg = sv_magicext(sv, NULL, PERL_MAGIC_study, &PL_vtbl_regexp, NULL, 0);
     mg->mg_ptr = (char *) sfirst;
-    mg->mg_len = (256 + len) * sizeof(I32);
+    mg->mg_len = (256 + len) * sizeof(U32);
 
-    snext = sfirst;
-    for (ch = 256; ch; --ch)
-       *snext++ = -1;
+    snext = sfirst + 256;
+    memset(sfirst, ~0, 256 * sizeof(U32));
 
     while (len-- > 0) {
        const U8 ch = s[len];
-       if (sfirst[ch] >= 0)
-           snext[len] = sfirst[ch];
-       else
-           snext[len] = -1;
-       sfirst[ch] = (I32)len;
+       snext[len] = sfirst[ch];
+       sfirst[ch] = len;
     }
 
     RETPUSHYES;
index b9677ec..516bf95 100644 (file)
--- a/regexec.c
+++ b/regexec.c
@@ -701,7 +701,7 @@ Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos,
        mg = mg_find(sv, PERL_MAGIC_study);
        assert(mg);
 
-       if (((I32 *)mg->mg_ptr)[BmRARE(check)] != -1
+       if (((U32 *)mg->mg_ptr)[BmRARE(check)] != (U32)~0
            || ( BmRARE(check) == '\n'
                 && (BmPREVIOUS(check) == SvCUR(check) - 1)
                 && SvTAIL(check) ))
diff --git a/util.c b/util.c
index 929c776..4dbd15e 100644 (file)
--- a/util.c
+++ b/util.c
@@ -854,7 +854,7 @@ Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift
 {
     dVAR;
     register const unsigned char *big;
-    register I32 pos;
+    U32 pos;
     register I32 previous;
     register I32 first;
     register const unsigned char *little;
@@ -862,8 +862,9 @@ Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift
     register const unsigned char *littleend;
     bool found = FALSE;
     const MAGIC * mg;
-    I32 *screamfirst;
-    I32 *screamnext;
+    U32 *screamfirst;
+    U32 *screamnext;
+    U32 const nope = ~0;
 
     PERL_ARGS_ASSERT_SCREAMINSTR;
 
@@ -873,12 +874,12 @@ Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift
     assert(SvTYPE(littlestr) == SVt_PVMG);
     assert(SvVALID(littlestr));
 
-    screamfirst = (I32 *)mg->mg_ptr;
+    screamfirst = (U32 *)mg->mg_ptr;
     screamnext = screamfirst + 256;
 
     pos = *old_posp == -1
        ? screamfirst[BmRARE(littlestr)] : screamnext[*old_posp];
-    if (pos == -1) {
+    if (pos == nope) {
       cant_find:
        if ( BmRARE(littlestr) == '\n'
             && BmPREVIOUS(littlestr) == SvCUR(littlestr) - 1) {
@@ -909,14 +910,14 @@ Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift
 #endif
        return NULL;
     }
-    while (pos < previous + start_shift) {
+    while ((I32)pos < previous + start_shift) {
        pos = screamnext[pos];
-       if (pos == -1)
+       if (pos == nope)
            goto cant_find;
     }
     big -= previous;
     do {
-       if (pos >= stop_pos) break;
+       if ((I32)pos >= stop_pos) break;
        if (big[pos] == first) {
            const unsigned char *s = little;
            const unsigned char *x = big + pos + 1;
@@ -926,13 +927,13 @@ Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift
                ++s;
            }
            if (s == littleend) {
-               *old_posp = pos;
+               *old_posp = (I32)pos;
                if (!last) return (char *)(big+pos);
                found = TRUE;
            }
        }
        pos = screamnext[pos];
-    } while (pos != -1);
+    } while (pos != nope);
     if (last && found)
        return (char *)(big+(*old_posp));
   check_tail: