Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove PP stubs and reformat predefine_encodings() #104

Merged
merged 2 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 64 additions & 114 deletions Encode.pm
Original file line number Diff line number Diff line change
Expand Up @@ -290,128 +290,78 @@ sub decode_utf8($;$) {
return $string;
}

# sub decode_utf8($;$) {
# my ( $str, $check ) = @_;
# return $str if is_utf8($str);
# if ($check) {
# return decode( "utf8", $str, $check );
# }
# else {
# return decode( "utf8", $str );
# return $str;
# }
# }

onBOOT;
predefine_encodings(1);

#
# This is to restore %Encoding if really needed;
#

sub predefine_encodings {
require 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 ( undef, $str, $chk ) = @_;
my $res = '';
for ( my $i = 0 ; $i < length($str) ; $i++ ) {
$res .=
chr(
utf8::unicode_to_native( ord( substr( $str, $i, 1 ) ) )
);
}
$_[1] = '' if $chk;
return $res;
};
*encode = sub {
my ( undef, $str, $chk ) = @_;
my $res = '';
for ( my $i = 0 ; $i < length($str) ; $i++ ) {
$res .=
chr(
utf8::native_to_unicode( ord( substr( $str, $i, 1 ) ) )
);
}
$_[1] = '' if $chk;
return $res;
};
my $obj = bless { Name => "UTF_EBCDIC" } => "Encode::UTF_EBCDIC";
Encode::define_encoding($obj, 'Unicode');
if ($ON_EBCDIC) {
package Encode::UTF_EBCDIC;
use parent 'Encode::Encoding';
my $obj = bless { Name => "UTF_EBCDIC" } => "Encode::UTF_EBCDIC";
Encode::define_encoding($obj, 'Unicode');
sub decode {
my ( undef, $str, $chk ) = @_;
my $res = '';
for ( my $i = 0 ; $i < length($str) ; $i++ ) {
$res .=
chr(
utf8::unicode_to_native( ord( substr( $str, $i, 1 ) ) )
);
}
$_[1] = '' if $chk;
return $res;
}
else {

package Encode::Internal;
push @Encode::Internal::ISA, 'Encode::Encoding';
*decode = sub {
my ( undef, $str, $chk ) = @_;
utf8::upgrade($str);
$_[1] = '' if $chk;
return $str;
};
*encode = \&decode;
my $obj = bless { Name => "Internal" } => "Encode::Internal";
Encode::define_encoding($obj, 'Unicode');
sub encode {
my ( undef, $str, $chk ) = @_;
my $res = '';
for ( my $i = 0 ; $i < length($str) ; $i++ ) {
$res .=
chr(
utf8::native_to_unicode( ord( substr( $str, $i, 1 ) ) )
);
}
$_[1] = '' if $chk;
return $res;
}
{
# https://rt.cpan.org/Public/Bug/Display.html?id=103253
package Encode::XS;
push @Encode::XS::ISA, 'Encode::Encoding';
} else {
package Encode::Internal;
use parent 'Encode::Encoding';
my $obj = bless { Name => "Internal" } => "Encode::Internal";
Encode::define_encoding($obj, 'Unicode');
sub decode {
my ( undef, $str, $chk ) = @_;
utf8::upgrade($str);
$_[1] = '' if $chk;
return $str;
}
{
*encode = \&decode;
}

# was in Encode::utf8
package Encode::utf8;
push @Encode::utf8::ISA, 'Encode::Encoding';
{
# https://rt.cpan.org/Public/Bug/Display.html?id=103253
package Encode::XS;
use parent 'Encode::Encoding';
}

#
if ($use_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 ( undef, $octets, $chk ) = @_;
my $str = Encode::decode_utf8($octets);
if ( defined $str ) {
$_[1] = '' if $chk;
return $str;
}
return undef;
};
*encode = sub {
my ( undef, $string, $chk ) = @_;
my $octets = Encode::encode_utf8($string);
$_[1] = '' if $chk;
return $octets;
};
{
package Encode::utf8;
use parent 'Encode::Encoding';
__PACKAGE__->Define('utf8');
my $strict_obj = bless { Name => "utf-8-strict", strict_utf8 => 1 } => "Encode::utf8";
Encode::define_encoding($strict_obj, 'utf-8-strict');
sub cat_decode {
# ($obj, $dst, $src, $pos, $trm, $chk)
# currently ignores $chk
my ( undef, undef, undef, $pos, $trm ) = @_;
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;
}
*cat_decode = sub { # ($obj, $dst, $src, $pos, $trm, $chk)
# currently ignores $chk
my ( undef, undef, undef, $pos, $trm ) = @_;
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 '';
};
__PACKAGE__->Define('utf8');
my $strict_obj = bless { Name => "utf-8-strict", strict_utf8 => 1 } => "Encode::utf8";
Encode::define_encoding($strict_obj, 'utf-8-strict');
$$rdst .= substr( $$rsrc, $pos );
$$rpos = length($$rsrc);
return '';
}
}

Expand Down
4 changes: 2 additions & 2 deletions Encode.xs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ MODULE = Encode PACKAGE = Encode::utf8 PREFIX = Method_
PROTOTYPES: DISABLE

void
Method_decode_xs(obj,src,check_sv = &PL_sv_no)
Method_decode(obj,src,check_sv = &PL_sv_no)
SV * obj
SV * src
SV * check_sv
Expand Down Expand Up @@ -655,7 +655,7 @@ CODE:
}

void
Method_encode_xs(obj,src,check_sv = &PL_sv_no)
Method_encode(obj,src,check_sv = &PL_sv_no)
SV * obj
SV * src
SV * check_sv
Expand Down
6 changes: 0 additions & 6 deletions Unicode/Unicode.pm
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ sub renew {
return $clone;
}

# There used to be a perl implementation of (en|de)code but with
# XS version is ripe, perl version is zapped for optimal speed

*decode = \&decode_xs;
*encode = \&encode_xs;

1;
__END__

Expand Down
4 changes: 2 additions & 2 deletions Unicode/Unicode.xs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ PROTOTYPES: DISABLE
*hv_fetch((HV *)SvRV(obj),k,l,0) : &PL_sv_undef)

void
decode_xs(obj, str, check = 0)
decode(obj, str, check = 0)
SV * obj
SV * str
IV check
Expand Down Expand Up @@ -345,7 +345,7 @@ CODE:
}

void
encode_xs(obj, utf8, check = 0)
encode(obj, utf8, check = 0)
SV * obj
SV * utf8
IV check
Expand Down