This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
S_pmtrans(): add assert and simplify conditional
authorDavid Mitchell <davem@iabyn.com>
Mon, 8 Jan 2018 16:14:17 +0000 (16:14 +0000)
committerDavid Mitchell <davem@iabyn.com>
Fri, 19 Jan 2018 11:24:55 +0000 (11:24 +0000)
in tr/search/replace/c, the number of 'paired' replacement chars
will always be <= length(replace). Assert this, and thus simplify a couple
of conditionals from >= to ==.

It should make no difference to execution, but reduces the cognitive
load.

op.c

diff --git a/op.c b/op.c
index 6504ef0..5652858 100644 (file)
--- a/op.c
+++ b/op.c
@@ -6644,7 +6644,7 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
            tbl[t[i]] = -1;
        for (i = 0, j = 0; i < 256; i++) {
            if (!tbl[i]) {
            tbl[t[i]] = -1;
        for (i = 0, j = 0; i < 256; i++) {
            if (!tbl[i]) {
-               if (j >= (I32)rlen) {
+               if (j == (I32)rlen) {
                    if (del)
                        tbl[i] = -2;
                    else if (rlen)
                    if (del)
                        tbl[i] = -2;
                    else if (rlen)
@@ -6659,6 +6659,8 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
                }
            }
        }
                }
            }
        }
+
+        assert(j <= (I32)rlen);
        if (!del) {
            if (!rlen) {
                 /* empty replacement list */
        if (!del) {
            if (!rlen) {
                 /* empty replacement list */
@@ -6666,7 +6668,7 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
                if (!squash)
                    o->op_private |= OPpTRANS_IDENTICAL;
            }
                if (!squash)
                    o->op_private |= OPpTRANS_IDENTICAL;
            }
-           else if (j >= (I32)rlen)
+           else if (j == (I32)rlen)
                 /* no more replacement chars than search chars */
                j = rlen - 1;
            else {
                 /* no more replacement chars than search chars */
                j = rlen - 1;
            else {