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;
}
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;
}
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);
}
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);
return $str;
}
-*fromUnicode = \&toUnicode;
+*encode = \&decode;
package Encode::utf8;
+use base 'Encode::Encoding';
# package to allow long-hand
# $octets = encode( utf8 => $string );
sub name { 'utf8' }
-sub toUnicode
+sub decode
{
my ($obj,$octets,$chk) = @_;
my $str = Encode::decode_utf8($octets);
return undef;
}
-sub fromUnicode
+sub encode
{
my ($obj,$string,$chk) = @_;
my $octets = Encode::encode_utf8($string);
}
package Encode::Table;
+use base 'Encode::Encoding';
sub read
{
$obj-{'Rep'}->($ch);
}
-sub toUnicode
+sub decode
{
my ($obj,$str,$chk) = @_;
my $rep = $obj->{'Rep'};
return $uni;
}
-sub fromUnicode
+sub encode
{
my ($obj,$uni,$chk) = @_;
my $fmuni = $obj->{'FmUni'};
}
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 = '';
return $uni;
}
-sub fromUnicode
+sub encode
{
my ($obj,$uni,$chk) = @_;
my $str = '';
package Encode::Escape;
+use base 'Encode::Encoding';
+
use Carp;
sub read
sub name { shift->{'Name'} }
-sub toUnicode
+sub decode
{
croak("Not implemented yet");
}
-sub fromUnicode
+sub encode
{
croak("Not implemented yet");
}
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;
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;
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
}
void
-Encode_fromUnicode(obj,src,check = 0)
+Method_encode(obj,src,check = 0)
SV * obj
SV * src
int check
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)
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");
}