From: Nick Ing-Simmons Date: Sun, 25 Feb 2001 11:30:22 +0000 (+0000) Subject: Change method names in Encode implementation classes to something X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=50d2698546d7dba5ed48b29bf1c13887b041a4ba;p=p5sagit%2Fp5-mst-13.2.git Change method names in Encode implementation classes to something less confusing (preparing to "go public"). p4raw-id: //depot/perlio@8927 --- diff --git a/ext/Encode/Encode.pm b/ext/Encode/Encode.pm index eb07f1f..f17cc1a 100644 --- a/ext/Encode/Encode.pm +++ b/ext/Encode/Encode.pm @@ -113,7 +113,7 @@ sub encode my ($name,$string,$check) = @_; my $enc = find_encoding($name); croak("Unknown encoding '$name'") unless defined $enc; - my $octets = $enc->fromUnicode($string,$check); + my $octets = $enc->encode($string,$check); return undef if ($check && length($string)); return $octets; } @@ -123,7 +123,7 @@ sub decode my ($name,$octets,$check) = @_; my $enc = find_encoding($name); croak("Unknown encoding '$name'") unless defined $enc; - my $string = $enc->toUnicode($octets,$check); + my $string = $enc->decode($octets,$check); return undef if ($check && length($octets)); return $string; } @@ -135,9 +135,9 @@ sub from_to croak("Unknown encoding '$from'") unless defined $f; my $t = find_encoding($to); croak("Unknown encoding '$to'") unless defined $t; - my $uni = $f->toUnicode($string,$check); + my $uni = $f->decode($string,$check); return undef if ($check && length($string)); - $string = $t->fromUnicode($uni,$check); + $string = $t->encode($uni,$check); return undef if ($check && length($uni)); return length($_[0] = $string); } @@ -156,14 +156,24 @@ sub decode_utf8 return $str; } +package Encode::Encoding; +# Base class for classes which implement encodings +# Temporary legacy methods +sub toUnicode { shift->decode(@_) } +sub fromUnicode { shift->encode(@_) } + +package Encode::XS; +use base 'Encode::Encoding'; + package Encode::Unicode; +use base 'Encode::Encoding'; # Dummy package that provides the encode interface but leaves data # as UTF-8 encoded. It is here so that from_to() works. sub name { 'Unicode' } -sub toUnicode +sub decode { my ($obj,$str,$chk) = @_; Encode::utf8_upgrade($str); @@ -171,9 +181,10 @@ sub toUnicode return $str; } -*fromUnicode = \&toUnicode; +*encode = \&decode; package Encode::utf8; +use base 'Encode::Encoding'; # package to allow long-hand # $octets = encode( utf8 => $string ); @@ -181,7 +192,7 @@ package Encode::utf8; sub name { 'utf8' } -sub toUnicode +sub decode { my ($obj,$octets,$chk) = @_; my $str = Encode::decode_utf8($octets); @@ -193,7 +204,7 @@ sub toUnicode return undef; } -sub fromUnicode +sub encode { my ($obj,$string,$chk) = @_; my $octets = Encode::encode_utf8($string); @@ -202,6 +213,7 @@ sub fromUnicode } package Encode::Table; +use base 'Encode::Encoding'; sub read { @@ -266,7 +278,7 @@ sub representation $obj-{'Rep'}->($ch); } -sub toUnicode +sub decode { my ($obj,$str,$chk) = @_; my $rep = $obj->{'Rep'}; @@ -296,7 +308,7 @@ sub toUnicode return $uni; } -sub fromUnicode +sub encode { my ($obj,$uni,$chk) = @_; my $fmuni = $obj->{'FmUni'}; @@ -319,12 +331,14 @@ sub fromUnicode } package Encode::iso10646_1; +use base 'Encode::Encoding'; + # Encoding is 16-bit network order Unicode # Used for X font encodings sub name { 'iso10646-1' } -sub toUnicode +sub decode { my ($obj,$str,$chk) = @_; my $uni = ''; @@ -338,7 +352,7 @@ sub toUnicode return $uni; } -sub fromUnicode +sub encode { my ($obj,$uni,$chk) = @_; my $str = ''; @@ -359,6 +373,8 @@ sub fromUnicode package Encode::Escape; +use base 'Encode::Encoding'; + use Carp; sub read @@ -377,12 +393,12 @@ sub read sub name { shift->{'Name'} } -sub toUnicode +sub decode { croak("Not implemented yet"); } -sub fromUnicode +sub encode { croak("Not implemented yet"); } diff --git a/ext/Encode/Encode.xs b/ext/Encode/Encode.xs index 3913faf..40c3dc7 100644 --- a/ext/Encode/Encode.xs +++ b/ext/Encode/Encode.xs @@ -167,7 +167,7 @@ PerlIOEncode_fill(PerlIO *f) XPUSHs(e->bufsv); XPUSHs(&PL_sv_yes); PUTBACK; - if (perl_call_method("toUnicode",G_SCALAR) != 1) + if (perl_call_method("decode",G_SCALAR) != 1) code = -1; SPAGAIN; uni = POPs; @@ -223,7 +223,7 @@ PerlIOEncode_flush(PerlIO *f) XPUSHs(e->bufsv); XPUSHs(&PL_sv_yes); PUTBACK; - if (perl_call_method("fromUnicode",G_SCALAR) != 1) + if (perl_call_method("encode",G_SCALAR) != 1) code = -1; SPAGAIN; str = POPs; @@ -462,12 +462,12 @@ sv_utf8_downgrade(sv,failok=0) SV * sv bool failok -MODULE = Encode PACKAGE = Encode::XS PREFIX = Encode_ +MODULE = Encode PACKAGE = Encode::XS PREFIX = Method_ PROTOTYPES: ENABLE void -Encode_toUnicode(obj,src,check = 0) +Method_decode(obj,src,check = 0) SV * obj SV * src int check @@ -480,7 +480,7 @@ CODE: } void -Encode_fromUnicode(obj,src,check = 0) +Method_encode(obj,src,check = 0) SV * obj SV * src int check diff --git a/t/lib/encode.t b/t/lib/encode.t index 5c911f0..da4d1b8 100644 --- a/t/lib/encode.t +++ b/t/lib/encode.t @@ -30,9 +30,9 @@ $cpy = $str; ok(length($str),from_to($cpy,'iso8859-1','Unicode'),"Length Wrong"); my $sym = Encode->getEncoding('symbol'); -my $uni = $sym->toUnicode('a'); +my $uni = $sym->decode('a'); ok("\N{alpha}",substr($uni,0,1),"alpha does not map to symbol 'a'"); -$str = $sym->fromUnicode("\N{Beta}"); +$str = $sym->encode("\N{Beta}"); ok("B",substr($str,0,1),"Symbol 'B' does not map to Beta"); foreach my $enc (qw(symbol dingbats ascii),@encodings) @@ -40,8 +40,8 @@ foreach my $enc (qw(symbol dingbats ascii),@encodings) my $tab = Encode->getEncoding($enc); ok(1,defined($tab),"Could not load $enc"); $str = join('',map(chr($_),0x20..0x7E)); - $uni = $tab->toUnicode($str); - $cpy = $tab->fromUnicode($uni); + $uni = $tab->decode($str); + $cpy = $tab->encode($uni); ok($cpy,$str,"$enc mangled translating to Unicode and back"); }