[patch@26561] utf8.c const issue.
[p5sagit/p5-mst-13.2.git] / utf8.c
diff --git a/utf8.c b/utf8.c
index 813a64f..db81e26 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -1,7 +1,7 @@
 /*    utf8.c
  *
- *    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 by Larry Wall and
- *    others
+ *    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006,
+ *    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.
@@ -1243,7 +1243,7 @@ S_is_utf8_common(pTHX_ const U8 *const p, SV **swash,
     if (!is_utf8_char(p))
        return FALSE;
     if (!*swash)
-       *swash = swash_init("utf8", swashname, &PL_sv_undef, 0, 0);
+       *swash = swash_init("utf8", swashname, &PL_sv_undef, 1, 0);
     return swash_fetch(*swash, p, TRUE) != 0;
 }
 
@@ -1545,8 +1545,12 @@ Perl_to_utf8_fold(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp)
                              &PL_utf8_tofold, "ToFold", "utf8::ToSpecFold");
 }
 
-/* a "swash" is a swatch hash */
-
+/* Note:
+ * A "swash" is a swatch hash.
+ * A "swatch" is a bit vector generated by utf8.c:S_swash_get().
+ * C<pkg> is a pointer to a package name for SWASHNEW, should be "utf8".
+ * For other parameters, see utf8::SWASHNEW in lib/utf8_heavy.pl.
+ */
 SV*
 Perl_swash_init(pTHX_ const char* pkg, const char* name, SV *listsv, I32 minbits, I32 none)
 {
@@ -1622,11 +1626,18 @@ Perl_swash_init(pTHX_ const char* pkg, const char* name, SV *listsv, I32 minbits
  * the lower-level routine, and it is similarly broken for returning
  * multiple values.  --jhi */
 /* Now SWASHGET is recasted into S_swash_get in this file. */
+
+/* Note:
+ * Returns the value of property/mapping C<swash> for the first character
+ * of the string C<ptr>. If C<do_utf8> is true, the string C<ptr> is
+ * assumed to be in utf8. If C<do_utf8> is false, the string C<ptr> is
+ * assumed to be in native 8-bit encoding. Caches the swatch in C<swash>.
+ */
 UV
-Perl_swash_fetch(pTHX_ SV *sv, const U8 *ptr, bool do_utf8)
+Perl_swash_fetch(pTHX_ SV *swash, const U8 *ptr, bool do_utf8)
 {
     dVAR;
-    HV* const hv = (HV*)SvRV(sv);
+    HV* const hv = (HV*)SvRV(swash);
     U32 klen;
     U32 off;
     STRLEN slen;
@@ -1692,7 +1703,7 @@ Perl_swash_fetch(pTHX_ SV *sv, const U8 *ptr, bool do_utf8)
            const UV code_point = utf8n_to_uvuni(ptr, UTF8_MAXBYTES, 0,
                                           ckWARN(WARN_UTF8) ?
                                           0 : UTF8_ALLOW_ANY);
-           swatch = swash_get(sv,
+           swatch = swash_get(swash,
                    /* On EBCDIC & ~(0xA0-1) isn't a useful thing to do */
                                (klen) ? (code_point & ~(needents - 1)) : 0,
                                needents);
@@ -1704,7 +1715,7 @@ Perl_swash_fetch(pTHX_ SV *sv, const U8 *ptr, bool do_utf8)
 
            if (!svp || !(tmps = (U8*)SvPV(*svp, slen))
                     || (slen << 3) < needents)
-               Perl_croak(aTHX_ "The swatch does not have proper length");
+               Perl_croak(aTHX_ "panic: swash_fetch got improper swatch");
        }
 
        PL_last_swash_hv = hv;
@@ -1730,7 +1741,7 @@ Perl_swash_fetch(pTHX_ SV *sv, const U8 *ptr, bool do_utf8)
        off <<= 2;
        return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
     }
-    Perl_croak(aTHX_ "panic: swash_fetch");
+    Perl_croak(aTHX_ "panic: swash_fetch got swatch of unexpected bit width");
     return 0;
 }
 
@@ -1744,24 +1755,25 @@ STATIC SV*
 S_swash_get(pTHX_ SV* swash, UV start, UV span)
 {
     SV *swatch;
-    U8 *l, *lend, *x, *xend, *s, *nl;
+    U8 *l, *lend, *x, *xend, *s;
     STRLEN lcur, xcur, scur;
 
     HV* const hv = (HV*)SvRV(swash);
-    SV** listsvp = hv_fetch(hv, "LIST", 4, FALSE);
-    SV** typesvp = hv_fetch(hv, "TYPE", 4, FALSE);
-    SV** bitssvp = hv_fetch(hv, "BITS", 4, FALSE);
-    SV** nonesvp = hv_fetch(hv, "NONE", 4, FALSE);
-    SV** extssvp = hv_fetch(hv, "EXTRAS", 6, FALSE);
-    U8*  typestr = (U8*)SvPV_nolen(*typesvp);
-    int  typeto  = typestr[0] == 'T' && typestr[1] == 'o';
-    STRLEN bits  = SvUV(*bitssvp);
-    STRLEN octets = bits >> 3; /* if bits == 1, then octets == 0 */
-    UV     none  = SvUV(*nonesvp);
-    UV     end   = start + span;
+    SV** const listsvp = hv_fetch(hv, "LIST", 4, FALSE);
+    SV** const typesvp = hv_fetch(hv, "TYPE", 4, FALSE);
+    SV** const bitssvp = hv_fetch(hv, "BITS", 4, FALSE);
+    SV** const nonesvp = hv_fetch(hv, "NONE", 4, FALSE);
+    SV** const extssvp = hv_fetch(hv, "EXTRAS", 6, FALSE);
+    const U8* const typestr = (U8*)SvPV_nolen(*typesvp);
+    const int  typeto  = typestr[0] == 'T' && typestr[1] == 'o';
+    const STRLEN bits  = SvUV(*bitssvp);
+    const STRLEN octets = bits >> 3; /* if bits == 1, then octets == 0 */
+    const UV     none  = SvUV(*nonesvp);
+    const UV     end   = start + span;
 
     if (bits != 1 && bits != 8 && bits != 16 && bits != 32) {
-       Perl_croak(aTHX_ "swash_get: unknown bits %"UVuf, (UV) bits);
+       Perl_croak(aTHX_ "panic: swash_get doesn't expect bits %"UVuf,
+                                                (UV)bits);
     }
 
     /* create and initialize $swatch */
@@ -1770,7 +1782,7 @@ S_swash_get(pTHX_ SV* swash, UV start, UV span)
     SvGROW(swatch, scur + 1);
     s = (U8*)SvPVX(swatch);
     if (octets && none) {
-       const U8* e = s + scur;
+       const U8* const e = s + scur;
        while (s < e) {
            if (bits == 8)
                *s++ = (U8)(none & 0xff);
@@ -1801,7 +1813,7 @@ S_swash_get(pTHX_ SV* swash, UV start, UV span)
        STRLEN numlen;
        I32 flags = PERL_SCAN_SILENT_ILLDIGIT | PERL_SCAN_DISALLOW_PREFIX;
 
-       nl = (U8*)memchr(l, '\n', lend - l);
+       U8* const nl = (U8*)memchr(l, '\n', lend - l);
 
        numlen = lend - l;
        min = grok_hex((char *)l, &numlen, &flags, NULL);
@@ -1846,6 +1858,8 @@ S_swash_get(pTHX_ SV* swash, UV start, UV span)
                    }
                }
            }
+           else
+               val = 0; /* bits == 1, then val should be ignored */
        }
        else {
            max = min;
@@ -1855,6 +1869,8 @@ S_swash_get(pTHX_ SV* swash, UV start, UV span)
                    Perl_croak(aTHX_ "%s: illegal mapping '%s'", typestr, l);
                }
            }
+           else
+               val = 0; /* bits == 1, then val should be ignored */
        }
 
        if (nl)
@@ -1895,11 +1911,11 @@ S_swash_get(pTHX_ SV* swash, UV start, UV span)
                    ++val;
            }
        }
-       else {
+       else { /* bits == 1, then val should be ignored */
            if (min < start)
                min = start;
            for (key = min; key <= max; key++) {
-               STRLEN offset = (STRLEN)(key - start);
+               const STRLEN offset = (STRLEN)(key - start);
                if (key >= end)
                    goto go_out_list;
                s[offset >> 3] |= 1 << (offset & 7);
@@ -1918,7 +1934,7 @@ S_swash_get(pTHX_ SV* swash, UV start, UV span)
        HV* otherhv;
        STRLEN otherbits;
        SV **otherbitssvp, *other;
-       U8 *s, *o;
+       U8 *s, *o, *nl;
        STRLEN slen, olen;
 
        U8 opc = *x++;
@@ -1949,28 +1965,23 @@ S_swash_get(pTHX_ SV* swash, UV start, UV span)
        }
 
        othersvp = hv_fetch(hv, (char *)namestr, namelen, FALSE);
-       if (*othersvp && SvROK(*othersvp) &&
-                        SvTYPE(SvRV(*othersvp))==SVt_PVHV)
-           otherhv = (HV*)SvRV(*othersvp);
-       else
-           Perl_croak(aTHX_ "otherhv is not a hash reference");
-
+       otherhv = (HV*)SvRV(*othersvp);
        otherbitssvp = hv_fetch(otherhv, "BITS", 4, FALSE);
        otherbits = (STRLEN)SvUV(*otherbitssvp);
        if (bits < otherbits)
-           Perl_croak(aTHX_ "swash_get: swatch size mismatch");
+           Perl_croak(aTHX_ "panic: swash_get found swatch size mismatch");
 
        /* The "other" swatch must be destroyed after. */
        other = swash_get(*othersvp, start, span);
        o = (U8*)SvPV(other, olen);
 
        if (!olen)
-           Perl_croak(aTHX_ "swash_get didn't return valid swatch for other");
+           Perl_croak(aTHX_ "panic: swash_get got improper swatch");
 
        s = (U8*)SvPV(swatch, slen);
        if (bits == 1 && otherbits == 1) {
            if (slen != olen)
-               Perl_croak(aTHX_ "swash_get: swatch length mismatch");
+               Perl_croak(aTHX_ "panic: swash_get found swatch length mismatch");
 
            switch (opc) {
            case '+':
@@ -1993,8 +2004,7 @@ S_swash_get(pTHX_ SV* swash, UV start, UV span)
                break;
            }
        }
-       else { /* bits >= 8 */
-              /* XXX: but weirdly otherval is treated as boolean */
+       else {
            STRLEN otheroctets = otherbits >> 3;
            STRLEN offset = 0;
            U8* send = s + slen;
@@ -2015,8 +2025,8 @@ S_swash_get(pTHX_ SV* swash, UV start, UV span)
                    }
                }
 
-               if      (opc == '+' && otherval)
-                   otherval = 1;
+               if (opc == '+' && otherval)
+                   ; /* replace with otherval */
                else if (opc == '!' && !otherval)
                    otherval = 1;
                else if (opc == '-' && otherval)
@@ -2024,7 +2034,7 @@ S_swash_get(pTHX_ SV* swash, UV start, UV span)
                else if (opc == '&' && !otherval)
                    otherval = 0;
                else {
-                   s += octets; /* not modify orig swatch */
+                   s += octets; /* no replacement */
                    continue;
                }
 
@@ -2141,7 +2151,7 @@ Perl_pv_uni_display(pTHX_ SV *dsv, const U8 *spv, STRLEN len, STRLEN pvlim, UV f
         u = utf8_to_uvchr((U8*)s, 0);
         if (u < 256) {
             const unsigned char c = (unsigned char)u & 0xFF;
-            if (!ok && (flags & UNI_DISPLAY_BACKSLASH)) {
+            if (flags & UNI_DISPLAY_BACKSLASH) {
                 switch (c) {
                 case '\n':
                     ok = 'n'; break;
@@ -2225,9 +2235,12 @@ Perl_ibcmp_utf8(pTHX_ const char *s1, char **pe1, register UV l1, bool u1, const
 {
      register const U8 *p1  = (const U8*)s1;
      register const U8 *p2  = (const U8*)s2;
-     register const U8 *f1 = 0, *f2 = 0;
-     register U8 *e1 = 0, *q1 = 0;
-     register U8 *e2 = 0, *q2 = 0;
+     register const U8 *f1 = NULL;
+     register const U8 *f2 = NULL;
+     register U8 *e1 = NULL;
+     register U8 *q1 = NULL;
+     register U8 *e2 = NULL;
+     register U8 *q2 = NULL;
      STRLEN n1 = 0, n2 = 0;
      U8 foldbuf1[UTF8_MAXBYTES_CASE+1];
      U8 foldbuf2[UTF8_MAXBYTES_CASE+1];