This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add memBEGINPs() to core and use it
authorKarl Williamson <khw@cpan.org>
Tue, 25 Apr 2017 02:27:24 +0000 (20:27 -0600)
committerKarl Williamson <khw@cpan.org>
Mon, 6 Nov 2017 19:50:06 +0000 (12:50 -0700)
This macro is like memBEGINs(), but the 'P' signifies we want a proper
substring, meaning that the 2nd string parameter must not be the entire
first parameter.

ext/attributes/attributes.xs
handy.h
toke.c

index fa0b72a..c14407c 100644 (file)
@@ -85,8 +85,9 @@ modify_SV_attributes(pTHX_ SV *sv, SV **retlist, SV **attrlist, int numattrs)
                }
                break;
            default:
-               if (len > 10 && _memEQs(name, "prototype(")) {
-                   SV * proto = newSVpvn(name+10,len-11);
+               if (memBEGINPs(name, len, "prototype(")) {
+                    const STRLEN proto_len = sizeof("prototype(") - 1;
+                   SV * proto = newSVpvn(name + proto_len, len - proto_len - 1);
                    HEK *const hek = CvNAME_HEK((CV *)sv);
                    SV *subname;
                    if (name[len-1] != ')')
diff --git a/handy.h b/handy.h
index 06db1e2..7b5aeb8 100644 (file)
--- a/handy.h
+++ b/handy.h
@@ -495,6 +495,9 @@ Returns zero if non-equal, or non-zero if equal.
 #define memBEGINs(s1, l, s2)                                                \
             (   (l) >= sizeof(s2) - 1                                       \
              && memEQ(s1, "" s2 "", sizeof(s2)-1))
+#define memBEGINPs(s1, l, s2)                                               \
+            (   (l) > sizeof(s2) - 1                                        \
+             && memEQ(s1, "" s2 "", sizeof(s2)-1))
 #define memENDs(s1, l, s2)                                                  \
             (   (l) >= sizeof(s2) - 1                                       \
              && memEQ(s1 + (l) - (sizeof(s2) - 1), "" s2 "", sizeof(s2)-1))
diff --git a/toke.c b/toke.c
index 5593b6f..734b02c 100644 (file)
--- a/toke.c
+++ b/toke.c
@@ -2103,8 +2103,10 @@ S_force_word(pTHX_ char *start, int token, int check_keyword, int allow_pack)
        if (check_keyword) {
          char *s2 = PL_tokenbuf;
          STRLEN len2 = len;
-         if (allow_pack && len > 6 && strBEGINs(s2, "CORE::"))
-           s2 += 6, len2 -= 6;
+         if (allow_pack && memBEGINPs(s2, len, "CORE::")) {
+           s2 += sizeof("CORE::") - 1;
+            len2 -= sizeof("CORE::") - 1;
+          }
          if (keyword(s2, len2, 0))
            return start;
        }
@@ -5369,7 +5371,9 @@ Perl_yylex(pTHX)
            }
            if (PL_parser->in_pod) {
                /* Incest with pod. */
-               if (*s == '=' && strBEGINs(s, "=cut") && !isALPHA(s[4])) {
+                if (    memBEGINPs(s, (STRLEN) (PL_bufend - s), "=cut")
+                    && !isALPHA(s[4]))
+                {
                     SvPVCLEAR(PL_linestr);
                    PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
                    PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
@@ -7950,14 +7954,17 @@ Perl_yylex(pTHX)
                char *p = s;
                 SSize_t s_off = s - SvPVX(PL_linestr);
 
-               if ((PL_bufend - p) >= 3
-                    && strBEGINs(p, "my") && isSPACE(*(p + 2)))
+                if (   memBEGINPs(p, (STRLEN) (PL_bufend - p), "my")
+                    && isSPACE(*(p + 2)))
                 {
-                   p += 2;
+                    p += 2;
                 }
-               else if ((PL_bufend - p) >= 4
-                         && strBEGINs(p, "our") && isSPACE(*(p + 3)))
-                   p += 3;
+                else if (   memBEGINPs(p, (STRLEN) (PL_bufend - p), "our")
+                         && isSPACE(*(p + 3)))
+                {
+                    p += 3;
+                }
+
                p = skipspace(p);
                 /* skip optional package name, as in "for my abc $x (..)" */
                if (isIDFIRST_lazy_if_safe(p, PL_bufend, UTF)) {