ENCODE original code

xiaoxiao2021-03-06  64

## $ ID: Encode.pm, V 2.8 2004/10/24 12:32:06 Dankogai EXP $ # package encode; use strict; @r = (q $ revision: 2.8 $ = ~ // D / g); sprintf "% d.". "% 02d" x $ # r, @R}; sub debug () {0} Use xsloader (); xsloader :: load (__ package__, $ version);

Require exporter; Use base qw / exporter /;

# Public, encouraged API is exported by Default

Our @Export = QW (decode decode_utf8 encode encode_utf8 encodings find_encoding clone_encoding);

our @FB_FLAGS = qw (DIE_ON_ERR WARN_ON_ERR RETURN_ON_ERR LEAVE_SRC PERLQQ HTMLCREF XMLCREF); our @FB_CONSTS = qw (FB_DEFAULT FB_CROAK FB_QUIET FB_WARN FB_PERLQQ FB_HTMLCREF FB_XMLCREF);

Our @Export_ok = (qw (_UTF8_OFF _UTF8_ON Define_Encoding from_to is_16bit is_8bit is_utf8 perlio_ok resolve_alias utf8_downgrade uTF8_UPGRADE), @fb_flags, @fb_consts,

OUR% export_tags = (all => [@Export, @export_ok], fallbacks => [@fb_consts], falboard_all => [@fb_consts, @fb_flags],);

# Documentation Moved After __END__ for Speed ​​- Ni-S

Our $ on_ebcdic = (ORD ("a") == 193);

Use encode :: alias;

# Make a% Encoding package variable to allow a ceertin amount of cheatingour% encoding; ot% extmodule; request eXCode :: config; eval {required eNCode :: configLocal}

Sub Encodings {MY $ Class = Shift; MY% ENC; IF (@_ and $ _ [0] EQ ": all") {% ENC = (% encoding,% extModule);} else {% ENC =% encoding; For my $ mod (map {m / :: / o? $ _: "Encode :: $ _"} @_) {debug and warn $ mod; for my $ enc (keys% extModule) {$ extModule {$ ENC } EQ $ mod and $ enc {$ enc} = $ mod;}}} RETURN SORT {LC $ A CMP LC $ B} GREP {! / ^ (?: Internal | Unicode | Guess $ / O} Keys% ENC } SUB Perlio_ok {MY $ OBJ = Ref ($ _ [0])? $ _ [0]: Find_Encoding ($ _ [0]); $ OBJ-> CAN ("Perlio_ok") and return $ OBJ-> Perlio_ok (); Return 0; # safty net}

Sub define_encoding {MY $ OBJ = Shift; My $ Name = Shift; $ Encoding {$ Name} = $ OBJ; MY $ LC = LC ($ Name); Define_Alias ​​($ lc => $ OBJ) Uns $ LC EQ $ Name WHILE (@_) {MY $ alias = Shift; Define_Alias ​​($ alias, $ obj);} returnit $ obj;

Sub getencoding {MY ($ Class, $ Name, $ Skip_external) = @_;

Ref ($ name) && $ name-> can ('renew') and return $ name; EXISTS $ encoding {$ name} and return $ encoding {$ name}; my $ lc = lc $ name; EXISTS $ encoding {$ LC} and return $ encoding {$ lc};

MY $ OC = $ Class-> Find_alias ($ al); defined ($ o) and return $ OC; $ lc NE $ name and $ = $ Class-> Find_alias ($ lc); defined ($ o) and return $ OC;

Unless ($ MOD = $ EXTMODULE {$ Name} || $ extModule {$ lc}) {$ mod = ~ s, ::, /, g; $ mod. = '.pm'; Eval {Require $ mod;}; exists $ encoding {$ name} and return $ encoding {$ name};

Sub find_encoding ($; $) {MY ($ Name, $ Skip_external) = @_; return __package __-> getENCoding ($ name, $ SKIP_EXTERNAL);

Sub Resolve_Alias ​​($) {MY $ OBJ = FIND_ENCODING (Shift); defined $ obj and return $ obj-> name; return;} sub clone_encoding ($) {My $ OBJ = FIND_ENCODING (Shift); Ref $ obj or return; Eval {require storable}; $ @ and return; return stORABLE :: Dclone ($ obj);

Sub Encode ($$) {MY ($ Name, $ String, $ Check) = @_; return undef unless $ string; return undef if ref $ string; $ check || = 0; MY $ ENC = FIND_ENCODING STRING ($ name); unsS (Defined $ ENC) {Require Carp; Carp :: Croak ("Unknown Encoding '$ Name');} my $ octets = $ ENC-> ENCODE ($ string, $ check); $ _ [1] = $ string if $ check; return $ octets;}

Sub Decode ($$; $) {MY ($ Name, $ OCTS, $ Check) = @_; return undef unless $ o c c i r o o $ @ CHECK || = 0; MY $ ENC = FIND_ENCODING ($ Name); unless (Defined $ ENC) {Require Carp; Carp :: Croak ("Unknown Encoding '$ Name');} MY $ String = $ ENC-> Decode ($ OCTETS, $ Check); $ _ [1] = $ octets if $ check; return $ string;}

Sub from_to ($$$) {MY ($ String, $ from, $ To, $ Check) = @_; return undef unless $ string; $ check || = 0; MY $ f = find_encoding ($ from Unless (defined $ f) {Require CARP; CARP :: Croak ("Unknown Encoding '$ from');} my $ t = find_encoding ($ to); unless (Defined $ T) {Require CARP; Carp: : Croak ("Unknown Encoding '$ To');} My $ UNI = $ F-> Decode ($ String, $ Check); Return Undef IF ($ Check && Length); $ String = $ T -> ENCODE ($ UNI, $ Check); RETURN Undef IF ($ CHECK & & LENGTH); RETURN Defined ($ _ [0] = $ String)? Length ($ String): undef;} Sub Encode_utf8 $) {MY ($ STR) = @_; uTF8 :: Encode ($ Str); Return $ Str;}

Sub Decode_utf8 ($) {MY ($ STR, $ Check) = @_; if ($ Check) {Return Decode ("UTF8", $ Str, $ Check);} else {return undef unless 9 :: decode ($ STR); RETURN $ STR;}}

PREDEFINE_ENCODINGS (1);

## this is to restore% encoding if really needed; #

sub predefine_encodings {use Encode :: Encoding; no warnings 'redefine'; my $ use_xs = shift; if ($ ON_EBCDIC) {# was in Encode :: UTF_EBCDIC package Encode :: UTF_EBCDIC; push @Encode :: UTF_EBCDIC :: ISA, 'Encode :: Encoding'; * Decode = Sub {MY ($ OBJ, $ STR, $ CHK) = @_; my $ res = '; for (My $ I = 0; $ I "inload"} => "eNCode :: inload";

{# Was in encode :: UTF8 Package Encode :: UTF8; Push @Encode :: UTF8 :: ISA, 'Encode :: Encoding'; # i ($ us_xs) {eNCode :: debug and warn __package__, "xs ON" ; * Decode = / & decode_xs; * encode = / & eNCode_xs;} else {eNCode :: debug and warn __package__, "xs off"; * decode = Sub {MY ($ OBJ, $ OCTS, $ CHK) = @_; my $ Str = Encode :: Decode_utf8 ($ OCTETS); if (Defined $ Str) {$ _ [1] = '' IF $ Chk; Return $ Str;} Return Undef;}; * ENCODE = SUB {MY ($ OBJ) $ String, $ chk) = @_; my $ octets = Encode :: Encode_utf8 ($ String); $ _ [1] = '' IF $ chk; return $ c c}};} * cat_decode = Sub {# # $ OBJ, $ DST, $ SRC, $ POS, $ TRM, $ CHK) My ($ OBJ, UNDEF, UNDEF, $ POS, $ TRM) = @_; # currently ignores $ chk my ($ RDST, $ RSRC, $ rpos) = / @ _ [1, 2, 3]; use bytes; if ((MY $ NPOS = Index ($$ RSRC, $ TRM, $ POS))> = 0) {$$ RDST. = SUBSTR $$ RSRC, $ POS, $ NPOS - $ POS Length ($ TRM)); $$ RPOS = $ NPOS Length ($ TRM); Return 1;} $$ RDST. = Substr ($$ RSRC, $ POS $$ RPOS = Length ($ $ RSRC); return '' ';}; $ encode :: encoding {utf8} = bless {name => "uTF8"} => "eNCode :: UTF8";}} 1;

转载请注明原文地址:https://www.9cbs.com/read-85036.html

New Post(0)