This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
bytes_from_utf8(): Use memcpy if all invariant
[perl5.git] / utf8.c
diff --git a/utf8.c b/utf8.c
index 3ded4a6..a439c8e 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -2003,6 +2003,8 @@ Perl_bytes_from_utf8(pTHX_ const U8 *s, STRLEN *len, bool *is_utf8)
     *is_utf8 = FALSE;
 
     Newx(d, (*len) - count + 1, U8);
+
+    if (LIKELY(count)) {
     s = start; start = d;
     while (s < send) {
        U8 c = *s++;
@@ -2015,7 +2017,14 @@ Perl_bytes_from_utf8(pTHX_ const U8 *s, STRLEN *len, bool *is_utf8)
     }
     *d = '\0';
     *len = d - start;
+
     return (U8 *)start;
+    }
+    else {
+        Copy(start, d, *len, U8);
+        *(d + *len) = '\0';
+        return (U8 *)d;
+    }
 }
 
 /*