Use Perl_sv_catpvf to shorten the code (source and object) needed for
[p5sagit/p5-mst-13.2.git] / utf8.c
diff --git a/utf8.c b/utf8.c
index 5a80bfd..b26d5a6 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -191,7 +191,7 @@ the "Perl extended UTF-8" (the infamous 'v-strings') will encode into
 five bytes or more.
 
 =cut */
-STRLEN
+STATIC STRLEN
 S_is_utf8_char_slow(pTHX_ const U8 *s, const STRLEN len)
 {
     U8 u = *s;
@@ -240,10 +240,9 @@ will be returned if it is valid, otherwise 0.
 STRLEN
 Perl_is_utf8_char(pTHX_ const U8 *s)
 {
-    STRLEN len;
+    STRLEN len = UTF8SKIP(s);
 #ifdef IS_UTF8_CHAR
-    len = UTF8SKIP(s);
-    if (len <= 4)
+    if (IS_UTF8_CHAR_FAST(len))
         return IS_UTF8_CHAR(s, len) ? len : 0;
 #endif /* #ifdef IS_UTF8_CHAR */
     return is_utf8_char_slow(s, len);
@@ -257,6 +256,8 @@ 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.
 
+See also is_utf8_string_loclen() and is_utf8_string_loc().
+
 =cut
 */
 
@@ -276,26 +277,26 @@ Perl_is_utf8_string(pTHX_ const U8 *s, STRLEN len)
         if (UTF8_IS_INVARIANT(*x))
              c = 1;
         else if (!UTF8_IS_START(*x))
-             return FALSE;
+            goto out;
         else {
              /* ... and call is_utf8_char() only if really needed. */
 #ifdef IS_UTF8_CHAR
             c = UTF8SKIP(x);
-            if (c <= 4) {
-                if (!IS_UTF8_CHAR(x, c))
-                    return FALSE;
-            } else {
-                if (!is_utf8_char_slow(x, c))
-                    return FALSE;
-            }
+            if (IS_UTF8_CHAR_FAST(c)) {
+                if (!IS_UTF8_CHAR(x, c))
+                    goto out;
+            } else if (!is_utf8_char_slow(x, c))
+                goto out;
 #else
             c = is_utf8_char(x);
 #endif /* #ifdef IS_UTF8_CHAR */
              if (!c)
-                  return FALSE;
+                 goto out;
         }
         x += c;
     }
+
+ out:
     if (x != send)
        return FALSE;
 
@@ -303,16 +304,20 @@ Perl_is_utf8_string(pTHX_ const U8 *s, STRLEN len)
 }
 
 /*
-=for apidoc A|bool|is_utf8_string_loc|const U8 *s|STRLEN len|const U8 **p
+=for apidoc A|bool|is_utf8_string_loclen|const U8 *s|STRLEN len|const U8 **ep|const STRLEN *el
+
+Like is_ut8_string() but stores the location of the failure (in the
+case of "utf8ness failure") or the location s+len (in the case of
+"utf8ness success") in the C<ep>, and the number of UTF-8
+encoded characters in the C<el>.
 
-Like is_ut8_string but store the location of the failure in
-the last argument.
+See also is_utf8_string_loc() and is_utf8_string().
 
 =cut
 */
 
 bool
-Perl_is_utf8_string_loc(pTHX_ const U8 *s, STRLEN len, const U8 **p)
+Perl_is_utf8_string_loclen(pTHX_ const U8 *s, STRLEN len, const U8 **ep, STRLEN *el)
 {
     const U8* x = s;
     const U8* send;
@@ -321,37 +326,63 @@ Perl_is_utf8_string_loc(pTHX_ const U8 *s, STRLEN len, const U8 **p)
     if (!len && s)
         len = strlen((const char *)s);
     send = s + len;
+    if (el)
+        *el = 0;
 
     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;
-        }
+            c = 1;
+        else if (!UTF8_IS_START(*x))
+            goto out;
         else {
-             /* ... and call is_utf8_char() only if really needed. */
-             c = is_utf8_char(x);
-             if (!c) {
-                  if (p)
-                     *p = x;
-                  return FALSE;
-             }
+            /* ... and call is_utf8_char() only if really needed. */
+#ifdef IS_UTF8_CHAR
+            c = UTF8SKIP(x);
+            if (IS_UTF8_CHAR_FAST(c)) {
+                if (!IS_UTF8_CHAR(x, c))
+                    c = 0;
+            } else
+                c = is_utf8_char_slow(x, c);
+#else
+            c = is_utf8_char(x);
+#endif /* #ifdef IS_UTF8_CHAR */
+            if (!c)
+                goto out;
         }
-        x += c;
+         x += c;
+        if (el)
+            (*el)++;
     }
-    if (x != send) {
-       if (p)
-          *p = x;
+
+ out:
+    if (ep)
+        *ep = x;
+    if (x != send)
        return FALSE;
-    }
 
     return TRUE;
 }
 
 /*
+=for apidoc A|bool|is_utf8_string_loc|const U8 *s|STRLEN len|const U8 **ep|const STRLEN *el
+
+Like is_ut8_string() but stores the location of the failure (in the
+case of "utf8ness failure") or the location s+len (in the case of
+"utf8ness success") in the C<ep>.
+
+See also is_utf8_string_loclen() and is_utf8_string().
+
+=cut
+*/
+
+bool
+Perl_is_utf8_string_loc(pTHX_ const U8 *s, STRLEN len, const U8 **ep)
+{
+    return is_utf8_string_loclen(s, len, ep, 0);
+}
+
+/*
 =for apidoc A|UV|utf8n_to_uvuni|const U8 *s|STRLEN curlen|STRLEN *retlen|U32 flags
 
 Bottom level UTF-8 decode routine.
@@ -1445,9 +1476,9 @@ Perl_to_utf8_case(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp, SV **swashp, const
         if ((hv  = get_hv(special, FALSE)) &&
             (svp = hv_fetch(hv, (const char*)tmpbuf, UNISKIP(uv1), FALSE)) &&
             (*svp)) {
-             char *s;
+            const char *s;
 
-             s = SvPV(*svp, len);
+             s = SvPV_const(*svp, len);
              if (len == 1)
                   len = uvuni_to_utf8(ustrp, NATIVE_TO_UNI(*(U8*)s)) - ustrp;
              else {
@@ -1643,7 +1674,7 @@ Perl_swash_init(pTHX_ const char* pkg, const char* name, SV *listsv, I32 minbits
     POPSTACK;
     if (IN_PERL_COMPILETIME) {
        STRLEN len;
-        const char* pv = SvPV(tokenbufsv, len);
+        const char* pv = SvPV_const(tokenbufsv, len);
 
        Copy(pv, PL_tokenbuf, len+1, char);
        PL_curcop->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
@@ -1672,7 +1703,7 @@ Perl_swash_fetch(pTHX_ SV *sv, const U8 *ptr, bool do_utf8)
     U32 off;
     STRLEN slen;
     STRLEN needents;
-    U8 *tmps = NULL;
+    const U8 *tmps = NULL;
     U32 bit;
     SV *retval;
     U8 tmputf8[2];
@@ -1727,7 +1758,7 @@ Perl_swash_fetch(pTHX_ SV *sv, const U8 *ptr, bool do_utf8)
        SV** svp = hv_fetch(hv, (const char*)ptr, klen, FALSE);
 
        /* If not cached, generate it via utf8::SWASHGET */
-       if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
+       if (!svp || !SvPOK(*svp) || !(tmps = (const U8*)SvPV_const(*svp, slen))) {
            dSP;
            /* We use utf8n_to_uvuni() as we want an index into
               Unicode tables, not a native character number.
@@ -1770,7 +1801,8 @@ Perl_swash_fetch(pTHX_ SV *sv, const U8 *ptr, bool do_utf8)
 
        PL_last_swash_hv = hv;
        PL_last_swash_klen = klen;
-       PL_last_swash_tmps = tmps;
+       /* FIXME change interpvar.h?  */
+       PL_last_swash_tmps = (U8 *) tmps;
        PL_last_swash_slen = slen;
        if (klen)
            Copy(ptr, PL_last_swash_key, klen, U8);
@@ -1938,8 +1970,8 @@ The pointer to the PV of the dsv is returned.
 char *
 Perl_sv_uni_display(pTHX_ SV *dsv, SV *ssv, STRLEN pvlim, UV flags)
 {
-     return Perl_pv_uni_display(aTHX_ dsv, (U8*)SvPVX(ssv), SvCUR(ssv),
-                               pvlim, flags);
+     return Perl_pv_uni_display(aTHX_ dsv, (const U8*)SvPVX_const(ssv),
+                               SvCUR(ssv), pvlim, flags);
 }
 
 /*