This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
utf8.c: Reorder some tests
authorKarl Williamson <khw@cpan.org>
Mon, 7 Dec 2015 04:32:28 +0000 (21:32 -0700)
committerKarl Williamson <khw@cpan.org>
Wed, 9 Dec 2015 02:01:28 +0000 (19:01 -0700)
When converting a code point to its UTF-8 form, the code point is
checked to be sure it is not problematic.  It turns out that all
problematic code points require at least 3 UTF-8 bytes to represent.
Already, the single-byte characters are handled before the problematic
tests.  This changes so that the two-byte ones are as well.  Further, it
now uses the canned macros that do this which are valid on both EBCDIC
and ASCII systems, instead of the previous code, which was
ASCII-specific.  This means one less test when the input code point is
representable by 2 bytes.

utf8.c

diff --git a/utf8.c b/utf8.c
index 5185dd4..722a7bf 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -112,6 +112,11 @@ Perl_uvoffuni_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags)
        *d++ = LATIN1_TO_NATIVE(uv);
        return d;
     }
+    if (uv <= MAX_UTF8_TWO_BYTE) {
+        *d++ = UTF8_TWO_BYTE_HI(uv);
+        *d++ = UTF8_TWO_BYTE_LO(uv);
+        return d;
+    }
 
     /* The first problematic code point is the first surrogate */
     if (uv >= UNICODE_SURROGATE_FIRST) {
@@ -172,11 +177,6 @@ Perl_uvoffuni_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags)
        return d+len;
     }
 #else /* Non loop style */
-    if (uv < 0x800) {
-       *d++ = (U8)(( uv >>  6)         | 0xc0);
-       *d++ = (U8)(( uv        & 0x3f) | 0x80);
-       return d;
-    }
     if (uv < 0x10000) {
        *d++ = (U8)(( uv >> 12)         | 0xe0);
        *d++ = (U8)(((uv >>  6) & 0x3f) | 0x80);