A proper, working, stable optimisation for sort {$b cmp $a}
[p5sagit/p5-mst-13.2.git] / utf8.c
diff --git a/utf8.c b/utf8.c
index 3ad3a95..6155fab 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -1,6 +1,6 @@
 /*    utf8.c
  *
- *    Copyright (c) 1998-2002, Larry Wall
+ *    Copyright (C) 2000, 2001, 2002, 2003, 2004, by Larry Wall and others
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
@@ -31,7 +31,7 @@ static char unees[] = "Malformed UTF-8 character (unexpected end of string)";
 
 =for apidoc A|U8 *|uvuni_to_utf8_flags|U8 *d|UV uv|UV flags
 
-Adds the UTF8 representation of the Unicode codepoint C<uv> to the end
+Adds the UTF-8 representation of the Unicode codepoint C<uv> to the end
 of the string C<d>; C<d> should be have at least C<UTF8_MAXLEN+1> free
 bytes available. The return value is the pointer to the byte after the
 end of the new character. In other words,
@@ -75,7 +75,7 @@ Perl_uvuni_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags)
                         "Unicode character 0x%04"UVxf" is illegal", uv);
     }
     if (UNI_IS_INVARIANT(uv)) {
-       *d++ = UTF_TO_NATIVE(uv);
+       *d++ = (U8)UTF_TO_NATIVE(uv);
        return d;
     }
 #if defined(EBCDIC)
@@ -83,76 +83,76 @@ Perl_uvuni_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags)
        STRLEN len  = UNISKIP(uv);
        U8 *p = d+len-1;
        while (p > d) {
-           *p-- = UTF_TO_NATIVE((uv & UTF_CONTINUATION_MASK) | UTF_CONTINUATION_MARK);
+           *p-- = (U8)UTF_TO_NATIVE((uv & UTF_CONTINUATION_MASK) | UTF_CONTINUATION_MARK);
            uv >>= UTF_ACCUMULATION_SHIFT;
        }
-       *p = UTF_TO_NATIVE((uv & UTF_START_MASK(len)) | UTF_START_MARK(len));
+       *p = (U8)UTF_TO_NATIVE((uv & UTF_START_MASK(len)) | UTF_START_MARK(len));
        return d+len;
     }
 #else /* Non loop style */
     if (uv < 0x800) {
-       *d++ = (( uv >>  6)         | 0xc0);
-       *d++ = (( uv        & 0x3f) | 0x80);
+       *d++ = (U8)(( uv >>  6)         | 0xc0);
+       *d++ = (U8)(( uv        & 0x3f) | 0x80);
        return d;
     }
     if (uv < 0x10000) {
-       *d++ = (( uv >> 12)         | 0xe0);
-       *d++ = (((uv >>  6) & 0x3f) | 0x80);
-       *d++ = (( uv        & 0x3f) | 0x80);
+       *d++ = (U8)(( uv >> 12)         | 0xe0);
+       *d++ = (U8)(((uv >>  6) & 0x3f) | 0x80);
+       *d++ = (U8)(( uv        & 0x3f) | 0x80);
        return d;
     }
     if (uv < 0x200000) {
-       *d++ = (( uv >> 18)         | 0xf0);
-       *d++ = (((uv >> 12) & 0x3f) | 0x80);
-       *d++ = (((uv >>  6) & 0x3f) | 0x80);
-       *d++ = (( uv        & 0x3f) | 0x80);
+       *d++ = (U8)(( uv >> 18)         | 0xf0);
+       *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >>  6) & 0x3f) | 0x80);
+       *d++ = (U8)(( uv        & 0x3f) | 0x80);
        return d;
     }
     if (uv < 0x4000000) {
-       *d++ = (( uv >> 24)         | 0xf8);
-       *d++ = (((uv >> 18) & 0x3f) | 0x80);
-       *d++ = (((uv >> 12) & 0x3f) | 0x80);
-       *d++ = (((uv >>  6) & 0x3f) | 0x80);
-       *d++ = (( uv        & 0x3f) | 0x80);
+       *d++ = (U8)(( uv >> 24)         | 0xf8);
+       *d++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >>  6) & 0x3f) | 0x80);
+       *d++ = (U8)(( uv        & 0x3f) | 0x80);
        return d;
     }
     if (uv < 0x80000000) {
-       *d++ = (( uv >> 30)         | 0xfc);
-       *d++ = (((uv >> 24) & 0x3f) | 0x80);
-       *d++ = (((uv >> 18) & 0x3f) | 0x80);
-       *d++ = (((uv >> 12) & 0x3f) | 0x80);
-       *d++ = (((uv >>  6) & 0x3f) | 0x80);
-       *d++ = (( uv        & 0x3f) | 0x80);
+       *d++ = (U8)(( uv >> 30)         | 0xfc);
+       *d++ = (U8)(((uv >> 24) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >>  6) & 0x3f) | 0x80);
+       *d++ = (U8)(( uv        & 0x3f) | 0x80);
        return d;
     }
 #ifdef HAS_QUAD
     if (uv < UTF8_QUAD_MAX)
 #endif
     {
-       *d++ =                        0xfe;     /* Can't match U+FEFF! */
-       *d++ = (((uv >> 30) & 0x3f) | 0x80);
-       *d++ = (((uv >> 24) & 0x3f) | 0x80);
-       *d++ = (((uv >> 18) & 0x3f) | 0x80);
-       *d++ = (((uv >> 12) & 0x3f) | 0x80);
-       *d++ = (((uv >>  6) & 0x3f) | 0x80);
-       *d++ = (( uv        & 0x3f) | 0x80);
+       *d++ =                            0xfe; /* Can't match U+FEFF! */
+       *d++ = (U8)(((uv >> 30) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 24) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >>  6) & 0x3f) | 0x80);
+       *d++ = (U8)(( uv        & 0x3f) | 0x80);
        return d;
     }
 #ifdef HAS_QUAD
     {
-       *d++ =                        0xff;     /* Can't match U+FFFE! */
-       *d++ =                        0x80;     /* 6 Reserved bits */
-       *d++ = (((uv >> 60) & 0x0f) | 0x80);    /* 2 Reserved bits */
-       *d++ = (((uv >> 54) & 0x3f) | 0x80);
-       *d++ = (((uv >> 48) & 0x3f) | 0x80);
-       *d++ = (((uv >> 42) & 0x3f) | 0x80);
-       *d++ = (((uv >> 36) & 0x3f) | 0x80);
-       *d++ = (((uv >> 30) & 0x3f) | 0x80);
-       *d++ = (((uv >> 24) & 0x3f) | 0x80);
-       *d++ = (((uv >> 18) & 0x3f) | 0x80);
-       *d++ = (((uv >> 12) & 0x3f) | 0x80);
-       *d++ = (((uv >>  6) & 0x3f) | 0x80);
-       *d++ = (( uv        & 0x3f) | 0x80);
+       *d++ =                            0xff;         /* Can't match U+FFFE! */
+       *d++ =                            0x80;         /* 6 Reserved bits */
+       *d++ = (U8)(((uv >> 60) & 0x0f) | 0x80);        /* 2 Reserved bits */
+       *d++ = (U8)(((uv >> 54) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 48) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 42) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 36) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 30) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 24) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
+       *d++ = (U8)(((uv >>  6) & 0x3f) | 0x80);
+       *d++ = (U8)(( uv        & 0x3f) | 0x80);
        return d;
     }
 #endif
@@ -208,7 +208,7 @@ Perl_is_utf8_char(pTHX_ U8 *s)
        s++;
     }
 
-    if (UNISKIP(uv) < len)
+    if ((STRLEN)UNISKIP(uv) < len)
        return 0;
 
     return len;
@@ -217,10 +217,10 @@ Perl_is_utf8_char(pTHX_ U8 *s)
 /*
 =for apidoc A|bool|is_utf8_string|U8 *s|STRLEN len
 
-Returns true if first C<len> bytes of the given string form a valid UTF8
-string, false otherwise.  Note that 'a valid UTF8 string' does not mean
-'a string that contains UTF8' because a valid ASCII string is a valid
-UTF8 string.
+Returns true if first C<len> bytes of the given string form a valid
+UTF-8 string, false otherwise.  Note that 'a valid UTF-8 string' does
+not mean 'a string that contains code points above 0x7F encoded in UTF-8'
+because a valid ASCII string is a valid UTF-8 string.
 
 =cut
 */
@@ -232,14 +232,22 @@ Perl_is_utf8_string(pTHX_ U8 *s, STRLEN len)
     U8* send;
     STRLEN c;
 
-    if (!len)
+    if (!len && s)
        len = strlen((char *)s);
     send = s + len;
 
     while (x < send) {
-        c = is_utf8_char(x);
-       if (!c)
-           return FALSE;
+        /* Inline the easy bits of is_utf8_char() here for speed... */
+        if (UTF8_IS_INVARIANT(*x))
+             c = 1;
+        else if (!UTF8_IS_START(*x))
+             return FALSE;
+        else {
+             /* ... and call is_utf8_char() only if really needed. */
+             c = is_utf8_char(x);
+             if (!c)
+                  return FALSE;
+        }
         x += c;
     }
     if (x != send)
@@ -249,14 +257,63 @@ Perl_is_utf8_string(pTHX_ U8 *s, STRLEN len)
 }
 
 /*
+=for apidoc A|bool|is_utf8_string_loc|U8 *s|STRLEN len|U8 **p
+
+Like is_ut8_string but store the location of the failure in
+the last argument.
+
+=cut
+*/
+
+bool
+Perl_is_utf8_string_loc(pTHX_ U8 *s, STRLEN len, U8 **p)
+{
+    U8* x = s;
+    U8* send;
+    STRLEN c;
+
+    if (!len && s)
+       len = strlen((char *)s);
+    send = s + len;
+
+    while (x < send) {
+        /* Inline the easy bits of is_utf8_char() here for speed... */
+        if (UTF8_IS_INVARIANT(*x))
+             c = 1;
+        else if (!UTF8_IS_START(*x)) {
+             if (p)
+                 *p = x;
+             return FALSE;
+        }
+        else {
+             /* ... and call is_utf8_char() only if really needed. */
+             c = is_utf8_char(x);
+             if (!c) {
+                  if (p)
+                     *p = x;
+                  return FALSE;
+             }
+        }
+        x += c;
+    }
+    if (x != send) {
+       if (p)
+          *p = x;
+       return FALSE;
+    }
+
+    return TRUE;
+}
+
+/*
 =for apidoc A|UV|utf8n_to_uvuni|U8 *s|STRLEN curlen|STRLEN *retlen|U32 flags
 
 Bottom level UTF-8 decode routine.
 Returns the unicode code point value of the first character in the string C<s>
-which is assumed to be in UTF8 encoding and no longer than C<curlen>;
+which is assumed to be in UTF-8 encoding and no longer than C<curlen>;
 C<retlen> will be set to the length, in bytes, of that character.
 
-If C<s> does not point to a well-formed UTF8 character, the behaviour
+If C<s> does not point to a well-formed UTF-8 character, the behaviour
 is dependent on the value of C<flags>: if it contains UTF8_CHECK_ONLY,
 it is assumed that the caller will raise a warning, and this function
 will silently just set C<retlen> to C<-1> and return zero.  If the
@@ -388,7 +445,7 @@ Perl_utf8n_to_uvuni(pTHX_ U8 *s, STRLEN curlen, STRLEN *retlen, U32 flags)
        !(flags & UTF8_ALLOW_SURROGATE)) {
        warning = UTF8_WARN_SURROGATE;
        goto malformed;
-    } else if ((expectlen > UNISKIP(uv)) &&
+    } else if ((expectlen > (STRLEN)UNISKIP(uv)) &&
               !(flags & UTF8_ALLOW_LONG)) {
        warning = UTF8_WARN_LONG;
        goto malformed;
@@ -476,10 +533,10 @@ malformed:
 =for apidoc A|UV|utf8_to_uvchr|U8 *s|STRLEN *retlen
 
 Returns the native character value of the first character in the string C<s>
-which is assumed to be in UTF8 encoding; C<retlen> will be set to the
+which is assumed to be in UTF-8 encoding; C<retlen> will be set to the
 length, in bytes, of that character.
 
-If C<s> does not point to a well-formed UTF8 character, zero is
+If C<s> does not point to a well-formed UTF-8 character, zero is
 returned and retlen is set, if possible, to -1.
 
 =cut
@@ -496,13 +553,13 @@ Perl_utf8_to_uvchr(pTHX_ U8 *s, STRLEN *retlen)
 =for apidoc A|UV|utf8_to_uvuni|U8 *s|STRLEN *retlen
 
 Returns the Unicode code point of the first character in the string C<s>
-which is assumed to be in UTF8 encoding; C<retlen> will be set to the
+which is assumed to be in UTF-8 encoding; C<retlen> will be set to the
 length, in bytes, of that character.
 
 This function should only be used when returned UV is considered
 an index into the Unicode semantic tables (e.g. swashes).
 
-If C<s> does not point to a well-formed UTF8 character, zero is
+If C<s> does not point to a well-formed UTF-8 character, zero is
 returned and retlen is set, if possible, to -1.
 
 =cut
@@ -568,7 +625,7 @@ Perl_utf8_length(pTHX_ U8 *s, U8 *e)
 /*
 =for apidoc A|IV|utf8_distance|U8 *a|U8 *b
 
-Returns the number of UTF8 characters between the UTF-8 pointers C<a>
+Returns the number of UTF-8 characters between the UTF-8 pointers C<a>
 and C<b>.
 
 WARNING: use only if you *know* that the pointers point inside the
@@ -663,7 +720,7 @@ Perl_utf8_hop(pTHX_ U8 *s, I32 off)
 /*
 =for apidoc A|U8 *|utf8_to_bytes|U8 *s|STRLEN *len
 
-Converts a string C<s> of length C<len> from UTF8 into byte encoding.
+Converts a string C<s> of length C<len> from UTF-8 into byte encoding.
 Unlike C<bytes_to_utf8>, this over-writes the original string, and
 updates len to contain the new length.
 Returns zero on failure, setting C<len> to -1.
@@ -678,7 +735,7 @@ Perl_utf8_to_bytes(pTHX_ U8 *s, STRLEN *len)
     U8 *d;
     U8 *save = s;
 
-    /* ensure valid UTF8 and chars < 256 before updating string */
+    /* ensure valid UTF-8 and chars < 256 before updating string */
     for (send = s + *len; s < send; ) {
         U8 c = *s++;
 
@@ -704,7 +761,7 @@ Perl_utf8_to_bytes(pTHX_ U8 *s, STRLEN *len)
 /*
 =for apidoc A|U8 *|bytes_from_utf8|U8 *s|STRLEN *len|bool *is_utf8
 
-Converts a string C<s> of length C<len> from UTF8 into byte encoding.
+Converts a string C<s> of length C<len> from UTF-8 into byte encoding.
 Unlike <utf8_to_bytes> but like C<bytes_to_utf8>, returns a pointer to
 the newly-created string, and updates C<len> to contain the new
 length.  Returns the original string if no conversion occurs, C<len>
@@ -725,7 +782,7 @@ Perl_bytes_from_utf8(pTHX_ U8 *s, STRLEN *len, bool *is_utf8)
     if (!*is_utf8)
        return start;
 
-    /* ensure valid UTF8 and chars < 256 before converting string */
+    /* ensure valid UTF-8 and chars < 256 before converting string */
     for (send = s + *len; s < send;) {
        U8 c = *s++;
        if (!UTF8_IS_INVARIANT(c)) {
@@ -758,10 +815,13 @@ Perl_bytes_from_utf8(pTHX_ U8 *s, STRLEN *len, bool *is_utf8)
 /*
 =for apidoc A|U8 *|bytes_to_utf8|U8 *s|STRLEN *len
 
-Converts a string C<s> of length C<len> from ASCII into UTF8 encoding.
+Converts a string C<s> of length C<len> from ASCII into UTF-8 encoding.
 Returns a pointer to the newly-created string, and sets C<len> to
 reflect the new length.
 
+If you want to convert to UTF-8 from other encodings than ASCII,
+see sv_recode_to_utf8().
+
 =cut
 */
 
@@ -779,10 +839,10 @@ Perl_bytes_to_utf8(pTHX_ U8 *s, STRLEN *len)
     while (s < send) {
         UV uv = NATIVE_TO_ASCII(*s++);
         if (UNI_IS_INVARIANT(uv))
-            *d++ = UTF_TO_NATIVE(uv);
+            *d++ = (U8)UTF_TO_NATIVE(uv);
         else {
-            *d++ = UTF8_EIGHT_BIT_HI(uv);
-            *d++ = UTF8_EIGHT_BIT_LO(uv);
+            *d++ = (U8)UTF8_EIGHT_BIT_HI(uv);
+            *d++ = (U8)UTF8_EIGHT_BIT_LO(uv);
         }
     }
     *d = '\0';
@@ -811,31 +871,32 @@ Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
        UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */
        p += 2;
        if (uv < 0x80) {
-           *d++ = uv;
+           *d++ = (U8)uv;
            continue;
        }
        if (uv < 0x800) {
-           *d++ = (( uv >>  6)         | 0xc0);
-           *d++ = (( uv        & 0x3f) | 0x80);
+           *d++ = (U8)(( uv >>  6)         | 0xc0);
+           *d++ = (U8)(( uv        & 0x3f) | 0x80);
            continue;
        }
        if (uv >= 0xd800 && uv < 0xdbff) {      /* surrogates */
-           UV low = *p++;
+           UV low = (p[0] << 8) + p[1];
+           p += 2;
            if (low < 0xdc00 || low >= 0xdfff)
                Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
            uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
        }
        if (uv < 0x10000) {
-           *d++ = (( uv >> 12)         | 0xe0);
-           *d++ = (((uv >>  6) & 0x3f) | 0x80);
-           *d++ = (( uv        & 0x3f) | 0x80);
+           *d++ = (U8)(( uv >> 12)         | 0xe0);
+           *d++ = (U8)(((uv >>  6) & 0x3f) | 0x80);
+           *d++ = (U8)(( uv        & 0x3f) | 0x80);
            continue;
        }
        else {
-           *d++ = (( uv >> 18)         | 0xf0);
-           *d++ = (((uv >> 12) & 0x3f) | 0x80);
-           *d++ = (((uv >>  6) & 0x3f) | 0x80);
-           *d++ = (( uv        & 0x3f) | 0x80);
+           *d++ = (U8)(( uv >> 18)         | 0xf0);
+           *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
+           *d++ = (U8)(((uv >>  6) & 0x3f) | 0x80);
+           *d++ = (U8)(( uv        & 0x3f) | 0x80);
            continue;
        }
     }
@@ -1127,13 +1188,13 @@ Perl_is_utf8_alnum(pTHX_ U8 *p)
         * descendant of isalnum(3), in other words, it doesn't
         * contain the '_'. --jhi */
        PL_utf8_alnum = swash_init("utf8", "IsWord", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_alnum, p, TRUE);
+    return swash_fetch(PL_utf8_alnum, p, TRUE) != 0;
 /*    return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
 #ifdef SURPRISINGLY_SLOWER  /* probably because alpha is usually true */
     if (!PL_utf8_alnum)
        PL_utf8_alnum = swash_init("utf8", "",
            sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
-    return swash_fetch(PL_utf8_alnum, p, TRUE);
+    return swash_fetch(PL_utf8_alnum, p, TRUE) != 0;
 #endif
 }
 
@@ -1144,13 +1205,13 @@ Perl_is_utf8_alnumc(pTHX_ U8 *p)
        return FALSE;
     if (!PL_utf8_alnum)
        PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_alnum, p, TRUE);
+    return swash_fetch(PL_utf8_alnum, p, TRUE) != 0;
 /*    return is_utf8_alpha(p) || is_utf8_digit(p); */
 #ifdef SURPRISINGLY_SLOWER  /* probably because alpha is usually true */
     if (!PL_utf8_alnum)
        PL_utf8_alnum = swash_init("utf8", "",
            sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
-    return swash_fetch(PL_utf8_alnum, p, TRUE);
+    return swash_fetch(PL_utf8_alnum, p, TRUE) != 0;
 #endif
 }
 
@@ -1163,7 +1224,7 @@ Perl_is_utf8_idfirst(pTHX_ U8 *p) /* The naming is historical. */
        return FALSE;
     if (!PL_utf8_idstart) /* is_utf8_idstart would be more logical. */
        PL_utf8_idstart = swash_init("utf8", "IdStart", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_idstart, p, TRUE);
+    return swash_fetch(PL_utf8_idstart, p, TRUE) != 0;
 }
 
 bool
@@ -1175,7 +1236,7 @@ Perl_is_utf8_idcont(pTHX_ U8 *p)
        return FALSE;
     if (!PL_utf8_idcont)
        PL_utf8_idcont = swash_init("utf8", "IdContinue", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_idcont, p, TRUE);
+    return swash_fetch(PL_utf8_idcont, p, TRUE) != 0;
 }
 
 bool
@@ -1185,7 +1246,7 @@ Perl_is_utf8_alpha(pTHX_ U8 *p)
        return FALSE;
     if (!PL_utf8_alpha)
        PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_alpha, p, TRUE);
+    return swash_fetch(PL_utf8_alpha, p, TRUE) != 0;
 }
 
 bool
@@ -1195,7 +1256,7 @@ Perl_is_utf8_ascii(pTHX_ U8 *p)
        return FALSE;
     if (!PL_utf8_ascii)
        PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_ascii, p, TRUE);
+    return swash_fetch(PL_utf8_ascii, p, TRUE) != 0;
 }
 
 bool
@@ -1205,7 +1266,7 @@ Perl_is_utf8_space(pTHX_ U8 *p)
        return FALSE;
     if (!PL_utf8_space)
        PL_utf8_space = swash_init("utf8", "IsSpacePerl", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_space, p, TRUE);
+    return swash_fetch(PL_utf8_space, p, TRUE) != 0;
 }
 
 bool
@@ -1215,7 +1276,7 @@ Perl_is_utf8_digit(pTHX_ U8 *p)
        return FALSE;
     if (!PL_utf8_digit)
        PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_digit, p, TRUE);
+    return swash_fetch(PL_utf8_digit, p, TRUE) != 0;
 }
 
 bool
@@ -1224,8 +1285,8 @@ Perl_is_utf8_upper(pTHX_ U8 *p)
     if (!is_utf8_char(p))
        return FALSE;
     if (!PL_utf8_upper)
-       PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_upper, p, TRUE);
+       PL_utf8_upper = swash_init("utf8", "IsUppercase", &PL_sv_undef, 0, 0);
+    return swash_fetch(PL_utf8_upper, p, TRUE) != 0;
 }
 
 bool
@@ -1234,8 +1295,8 @@ Perl_is_utf8_lower(pTHX_ U8 *p)
     if (!is_utf8_char(p))
        return FALSE;
     if (!PL_utf8_lower)
-       PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_lower, p, TRUE);
+       PL_utf8_lower = swash_init("utf8", "IsLowercase", &PL_sv_undef, 0, 0);
+    return swash_fetch(PL_utf8_lower, p, TRUE) != 0;
 }
 
 bool
@@ -1245,7 +1306,7 @@ Perl_is_utf8_cntrl(pTHX_ U8 *p)
        return FALSE;
     if (!PL_utf8_cntrl)
        PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_cntrl, p, TRUE);
+    return swash_fetch(PL_utf8_cntrl, p, TRUE) != 0;
 }
 
 bool
@@ -1255,7 +1316,7 @@ Perl_is_utf8_graph(pTHX_ U8 *p)
        return FALSE;
     if (!PL_utf8_graph)
        PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_graph, p, TRUE);
+    return swash_fetch(PL_utf8_graph, p, TRUE) != 0;
 }
 
 bool
@@ -1265,7 +1326,7 @@ Perl_is_utf8_print(pTHX_ U8 *p)
        return FALSE;
     if (!PL_utf8_print)
        PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_print, p, TRUE);
+    return swash_fetch(PL_utf8_print, p, TRUE) != 0;
 }
 
 bool
@@ -1275,7 +1336,7 @@ Perl_is_utf8_punct(pTHX_ U8 *p)
        return FALSE;
     if (!PL_utf8_punct)
        PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_punct, p, TRUE);
+    return swash_fetch(PL_utf8_punct, p, TRUE) != 0;
 }
 
 bool
@@ -1285,7 +1346,7 @@ Perl_is_utf8_xdigit(pTHX_ U8 *p)
        return FALSE;
     if (!PL_utf8_xdigit)
        PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_xdigit, p, TRUE);
+    return swash_fetch(PL_utf8_xdigit, p, TRUE) != 0;
 }
 
 bool
@@ -1295,7 +1356,7 @@ Perl_is_utf8_mark(pTHX_ U8 *p)
        return FALSE;
     if (!PL_utf8_mark)
        PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
-    return swash_fetch(PL_utf8_mark, p, TRUE);
+    return swash_fetch(PL_utf8_mark, p, TRUE) != 0;
 }
 
 /*
@@ -1340,21 +1401,19 @@ Perl_to_utf8_case(pTHX_ U8 *p, U8* ustrp, STRLEN *lenp, SV **swashp, char *norma
     if (!*swashp) /* load on-demand */
          *swashp = swash_init("utf8", normal, &PL_sv_undef, 4, 0);
 
-    if (special) {
+    /* The 0xDF is the only special casing Unicode code point below 0x100. */
+    if (special && (uv1 == 0xDF || uv1 > 0xFF)) {
          /* It might be "special" (sometimes, but not always,
          * a multicharacter mapping) */
         HV *hv;
-        SV *keysv;
-        HE *he;
-        SV *val;
-       
-        if ((hv    = get_hv(special, FALSE)) &&
-            (keysv = sv_2mortal(Perl_newSVpvf(aTHX_ "%04"UVXf, uv1))) &&
-            (he    = hv_fetch_ent(hv, keysv, FALSE, 0)) &&
-            (val   = HeVAL(he))) {
-            char *s;
+        SV **svp;
+
+        if ((hv  = get_hv(special, FALSE)) &&
+            (svp = hv_fetch(hv, (const char*)tmpbuf, UNISKIP(uv1), FALSE)) &&
+            (*svp)) {
+             char *s;
 
-             s = SvPV(val, len);
+             s = SvPV(*svp, len);
              if (len == 1)
                   len = uvuni_to_utf8(ustrp, NATIVE_TO_UNI(*(U8*)s)) - ustrp;
              else {
@@ -1365,7 +1424,7 @@ Perl_to_utf8_case(pTHX_ U8 *p, U8* ustrp, STRLEN *lenp, SV **swashp, char *norma
                   U8 *t = (U8*)s, *tend = t + len, *d;
                
                   d = tmpbuf;
-                  if (SvUTF8(val)) {
+                  if (SvUTF8(*svp)) {
                        STRLEN tlen = 0;
                        
                        while (t < tend) {
@@ -1505,13 +1564,16 @@ Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
     SV* retval;
     SV* tokenbufsv = sv_2mortal(NEWSV(0,0));
     dSP;
-    HV *stash = gv_stashpvn(pkg, strlen(pkg), FALSE);
+    size_t pkg_len = strlen(pkg);
+    size_t name_len = strlen(name);
+    HV *stash = gv_stashpvn(pkg, pkg_len, FALSE);
     SV* errsv_save;
 
     if (!gv_fetchmeth(stash, "SWASHNEW", 8, -1)) {     /* demand load utf8 */
        ENTER;
        errsv_save = newSVsv(ERRSV);
-       Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpv(pkg,0), Nullsv);
+       Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpvn(pkg,pkg_len),
+                        Nullsv);
        if (!SvTRUE(ERRSV))
            sv_setsv(ERRSV, errsv_save);
        SvREFCNT_dec(errsv_save);
@@ -1521,8 +1583,8 @@ Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
     PUSHSTACKi(PERLSI_MAGIC);
     PUSHMARK(SP);
     EXTEND(SP,5);
-    PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
-    PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
+    PUSHs(sv_2mortal(newSVpvn(pkg, pkg_len)));
+    PUSHs(sv_2mortal(newSVpvn(name, name_len)));
     PUSHs(listsv);
     PUSHs(sv_2mortal(newSViv(minbits)));
     PUSHs(sv_2mortal(newSViv(none)));
@@ -1531,9 +1593,10 @@ Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
     SAVEI32(PL_hints);
     PL_hints = 0;
     save_re_context();
-    if (PL_curcop == &PL_compiling) {
+    if (IN_PERL_COMPILETIME) {
        /* XXX ought to be handled by lex_start */
        SAVEI32(PL_in_my);
+       PL_in_my = 0;
        sv_setpv(tokenbufsv, PL_tokenbuf);
     }
     errsv_save = newSVsv(ERRSV);
@@ -1546,17 +1609,17 @@ Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
     SvREFCNT_dec(errsv_save);
     LEAVE;
     POPSTACK;
-    if (PL_curcop == &PL_compiling) {
+    if (IN_PERL_COMPILETIME) {
        STRLEN len;
        char* pv = SvPV(tokenbufsv, len);
 
        Copy(pv, PL_tokenbuf, len+1, char);
-       PL_curcop->op_private = PL_hints;
+       PL_curcop->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
     }
     if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV) {
         if (SvPOK(retval))
-           Perl_croak(aTHX_ "Can't find Unicode property definition \"%s\"",
-                      SvPV_nolen(retval));
+           Perl_croak(aTHX_ "Can't find Unicode property definition \"%"SVf"\"",
+                      retval);
        Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
     }
     return retval;
@@ -1583,8 +1646,8 @@ Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr, bool do_utf8)
     UV c = NATIVE_TO_ASCII(*ptr);
 
     if (!do_utf8 && !UNI_IS_INVARIANT(c)) {
-        tmputf8[0] = UTF8_EIGHT_BIT_HI(c);
-        tmputf8[1] = UTF8_EIGHT_BIT_LO(c);
+        tmputf8[0] = (U8)UTF8_EIGHT_BIT_HI(c);
+        tmputf8[1] = (U8)UTF8_EIGHT_BIT_LO(c);
         ptr = tmputf8;
     }
     /* Given a UTF-X encoded char 0xAA..0xYY,0xZZ
@@ -1598,7 +1661,7 @@ Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr, bool do_utf8)
     if (klen == 0)
      {
       /* If char in invariant then swatch is for all the invariant chars
-       * In both UTF-8 and UTF8-MOD that happens to be UTF_CONTINUATION_MARK
+       * In both UTF-8 and UTF-8-MOD that happens to be UTF_CONTINUATION_MARK
        */
       needents = UTF_CONTINUATION_MARK;
       off      = NATIVE_TO_UTF(ptr[klen]);
@@ -1663,8 +1726,8 @@ Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr, bool do_utf8)
            POPSTACK;
            FREETMPS;
            LEAVE;
-           if (PL_curcop == &PL_compiling)
-               PL_curcop->op_private = PL_hints;
+           if (IN_PERL_COMPILETIME)
+               PL_curcop->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
 
            svp = hv_store(hv, (char*)ptr, klen, retval, 0);
 
@@ -1702,7 +1765,7 @@ Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr, bool do_utf8)
 /*
 =for apidoc A|U8 *|uvchr_to_utf8|U8 *d|UV uv
 
-Adds the UTF8 representation of the Native codepoint C<uv> to the end
+Adds the UTF-8 representation of the Native codepoint C<uv> to the end
 of the string C<d>; C<d> should be have at least C<UTF8_MAXLEN+1> free
 bytes available. The return value is the pointer to the byte after the
 end of the new character. In other words,
@@ -1736,7 +1799,7 @@ Perl_uvchr_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags)
 =for apidoc A|UV|utf8n_to_uvchr|U8 *s|STRLEN curlen|STRLEN *retlen|U32 flags
 
 Returns the native character value of the first character in the string C<s>
-which is assumed to be in UTF8 encoding; C<retlen> will be set to the
+which is assumed to be in UTF-8 encoding; C<retlen> will be set to the
 length, in bytes, of that character.
 
 Allows length and flags to be passed to low level routine.
@@ -1880,11 +1943,11 @@ Perl_ibcmp_utf8(pTHX_ const char *s1, char **pe1, register UV l1, bool u1, const
      
      if (pe1)
          e1 = *(U8**)pe1;
-     if (e1 == 0 || (l1 && l1 < e1 - (U8*)s1))
+     if (e1 == 0 || (l1 && l1 < (UV)(e1 - (U8*)s1)))
          f1 = (U8*)s1 + l1;
      if (pe2)
          e2 = *(U8**)pe2;
-     if (e2 == 0 || (l2 && l2 < e2 - (U8*)s2))
+     if (e2 == 0 || (l2 && l2 < (UV)(e2 - (U8*)s2)))
          f2 = (U8*)s2 + l2;
 
      if ((e1 == 0 && f1 == 0) || (e2 == 0 && f2 == 0) || (f1 == 0 && f2 == 0))