Tidy code in pp_study and Perl_screaminstr()
authorNicholas Clark <nick@ccl4.org>
Mon, 27 Jun 2011 19:13:39 +0000 (21:13 +0200)
committerNicholas Clark <nick@ccl4.org>
Fri, 1 Jul 2011 12:05:41 +0000 (14:05 +0200)
In pp_study eliminate the variable pos, which duplicates len. ch should be U8,
not I32.

In Perl_screaminstr(), move the declarations of s and x to their point of use,
convert a for loop to a while loop, and avoid incrementing and decrementing s.
found is a boolean.

pp.c
util.c

diff --git a/pp.c b/pp.c
index 229e1fa..12ab913 100644 (file)
--- a/pp.c
+++ b/pp.c
@@ -707,7 +707,6 @@ PP(pp_study)
 {
     dVAR; dSP; dPOPss;
     register unsigned char *s;
-    register I32 pos;
     register I32 ch;
     register I32 *sfirst;
     register I32 *snext;
@@ -729,9 +728,8 @@ PP(pp_study)
        */
        RETPUSHNO;
     }
-    pos = len;
 
-    Newx(sfirst, 256 + pos, I32);
+    Newx(sfirst, 256 + len, I32);
 
     if (!sfirst)
        DIE(aTHX_ "do_study: out of memory");
@@ -746,13 +744,13 @@ PP(pp_study)
     for (ch = 256; ch; --ch)
        *snext++ = -1;
 
-    while (--pos >= 0) {
-       register const I32 ch = s[pos];
+    while (len-- > 0) {
+       const U8 ch = s[len];
        if (sfirst[ch] >= 0)
-           snext[pos] = sfirst[ch];
+           snext[len] = sfirst[ch];
        else
-           snext[pos] = -1;
-       sfirst[ch] = pos;
+           snext[len] = -1;
+       sfirst[ch] = (I32)len;
     }
 
     RETPUSHYES;
diff --git a/util.c b/util.c
index 4d03933..929c776 100644 (file)
--- a/util.c
+++ b/util.c
@@ -860,7 +860,7 @@ Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift
     register const unsigned char *little;
     register I32 stop_pos;
     register const unsigned char *littleend;
-    I32 found = 0;
+    bool found = FALSE;
     const MAGIC * mg;
     I32 *screamfirst;
     I32 *screamnext;
@@ -916,19 +916,19 @@ Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift
     }
     big -= previous;
     do {
-       register const unsigned char *s, *x;
        if (pos >= stop_pos) break;
        if (big[pos] == first) {
-           for (x=big+pos+1,s=little; s < littleend; /**/ ) {
-               if (*s++ != *x++) {
-                   s--;
+           const unsigned char *s = little;
+           const unsigned char *x = big + pos + 1;
+           while (s < littleend) {
+               if (*s != *x++)
                    break;
-               }
+               ++s;
            }
            if (s == littleend) {
                *old_posp = pos;
                if (!last) return (char *)(big+pos);
-               found = 1;
+               found = TRUE;
            }
        }
        pos = screamnext[pos];