This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
multiconcat: use append_utf8_from_native_byte()
authorDavid Mitchell <davem@iabyn.com>
Wed, 1 Nov 2017 18:45:36 +0000 (18:45 +0000)
committerDavid Mitchell <davem@iabyn.com>
Thu, 2 Nov 2017 08:58:27 +0000 (08:58 +0000)
This small inline function does what my code was doing manually in a
couple of places. Should be no functional difference, just makes the code
tidier.

Suggested by Karl.

op.c
pp_hot.c

diff --git a/op.c b/op.c
index c2f0a3a..333e5b1 100644 (file)
--- a/op.c
+++ b/op.c
@@ -3237,19 +3237,13 @@ S_maybe_multiconcat(pTHX_ OP *o)
         aux[PERL_MULTICONCAT_IX_UTF8_LEN].size = ulen;
 
         for (n = 0; n < (nargs + 1); n++) {
-            SSize_t l, ul, i;
-            l = ul = (lens++)->size;
-            for (i = 0; i < l; i++) {
+            SSize_t i;
+            char * orig_up = up;
+            for (i = (lens++)->size; i > 0; i--) {
                 U8 c = *p++;
-                if (UTF8_IS_INVARIANT(c))
-                    *up++ = c;
-                else {
-                    *up++ = UTF8_EIGHT_BIT_HI(c);
-                    *up++ = UTF8_EIGHT_BIT_LO(c);
-                    ul++;
-                }
+                append_utf8_from_native_byte(c, (U8**)&up);
             }
-            (ulens++)->size = ul;
+            (ulens++)->size = (i < 0) ? i : up - orig_up;
         }
     }
 
index fff9139..2ce77b3 100644 (file)
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -969,16 +969,11 @@ PP(pp_multiconcat)
                 len = -len;
                 if (UNLIKELY(p)) {
                     /* copy plain-but-variant pv to a utf8 targ */
+                    char * end_pv = dsv_pv + len;
                     assert(dst_utf8);
-                    while (len--) {
+                    while (dsv_pv < end_pv) {
                         U8 c = (U8) *p++;
-                        if (UTF8_IS_INVARIANT(c))
-                            *dsv_pv++ = c;
-                        else {
-                            *dsv_pv++ = UTF8_EIGHT_BIT_HI(c);
-                            *dsv_pv++ = UTF8_EIGHT_BIT_LO(c);
-                            len--;
-                        }
+                        append_utf8_from_native_byte(c, (U8**)&dsv_pv);
                     }
                 }
                 else