develooper Front page | perl.perl5.porters | Postings from September 2000

[PATCH Encode.xs] Encoded bytes -> UTF8 infrastructure

Thread Next
From:
Simon Cozens
Date:
September 16, 2000 12:24
Subject:
[PATCH Encode.xs] Encoded bytes -> UTF8 infrastructure
Message ID:
20000916202330.A5204@deep-dark-truthful-mirror.perlhacker.org
Adding bytes_to_utf8 encoding infrastructure to Encode

--- perl/ext/Encode/Encode.xs.~1~	Sat Sep 16 20:21:57 2000
+++ perl/ext/Encode/Encode.xs	Sat Sep 16 20:21:57 2000
@@ -2,12 +2,49 @@
 #include "perl.h"
 #include "XSUB.h"
 
+typedef U8 (*map_t) (pTHXo_ U8);
+
 #define UNIMPLEMENTED(x,y) y x (SV *sv, char *encoding) {   \
                          Perl_croak(aTHX_ "panic_unimplemented"); \
 			 return (y)0; /* fool picky compilers */ \
-                         } 
+                         }
+
+map_t _get_map (char *encoding) {
+    return NULL;
+}
+
+I32 _encoded_bytes_to_utf8(SV* sv, char* encoding) {
+    STRLEN len;
+    U8*    s = (U8*)SvPV(sv, len);
+    U8*    send = s + len;
+    U8*    converted;
+    U8*    d;
+    map_t  mapping;
+
+    if (!(mapping = _get_map(encoding)))
+	Perl_croak(aTHX_ "Unknown encoding %s", encoding);
+    
+    Newz(801, converted, len * 2 + 1, U8);
+    d = converted;
+    while (s < send) {
+        if (*s < 0x80)
+            *d++ = *s++;
+        else {
+            UV uv = *s++;
+	    uv = (*mapping)(uv);
+
+            uv_to_utf8(d, uv);
+        }
+    }
+    *d = '\0';
+    sv_setpvn(sv, (char *)converted, len);
+    SvUTF8_on(sv);
+    Safefree(converted); 
+    return d-converted;
+
+}
+
 UNIMPLEMENTED(_encoded_utf8_to_bytes, I32)
-UNIMPLEMENTED(_encoded_bytes_to_utf8, I32)
 
 void call_failure (SV *routine, U8* done, U8* dest, U8* orig) {}
 
End of Patch.

-- 
SM is fun.  ADSM is not.
"Safe, Sane, Consensual"... three words that cannot used to describe
ADSM.
    - Graham Reed, sdm.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About