Skip to content

Commit

Permalink
Merge pull request #102 from pali/master
Browse files Browse the repository at this point in the history
Use Encode::define_encoding and propagate carp/croak messages
  • Loading branch information
dankogai authored May 29, 2017
2 parents e52dd56 + 3c86047 commit 20ed8f3
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 22 deletions.
21 changes: 12 additions & 9 deletions Encode.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ XSLoader::load( __PACKAGE__, $VERSION );

use Exporter 5.57 'import';

our @CARP_NOT = qw(Encode::Encoder);

# Public, encouraged API is exported by default

our @EXPORT = qw(
Expand Down Expand Up @@ -96,6 +98,9 @@ sub define_encoding {
my $alias = shift;
define_alias( $alias, $obj );
}
my $class = ref($obj);
push @Encode::CARP_NOT, $class unless grep { $_ eq $class } @Encode::CARP_NOT;
push @Encode::Encoding::CARP_NOT, $class unless grep { $_ eq $class } @Encode::Encoding::CARP_NOT;
return $obj;
}

Expand Down Expand Up @@ -333,8 +338,8 @@ sub predefine_encodings {
$_[1] = '' if $chk;
return $res;
};
$Encode::Encoding{Unicode} =
bless { Name => "UTF_EBCDIC" } => "Encode::UTF_EBCDIC";
my $obj = bless { Name => "UTF_EBCDIC" } => "Encode::UTF_EBCDIC";
Encode::define_encoding($obj, 'Unicode');
}
else {

Expand All @@ -347,8 +352,8 @@ sub predefine_encodings {
return $str;
};
*encode = \&decode;
$Encode::Encoding{Unicode} =
bless { Name => "Internal" } => "Encode::Internal";
my $obj = bless { Name => "Internal" } => "Encode::Internal";
Encode::define_encoding($obj, 'Unicode');
}
{
# https://rt.cpan.org/Public/Bug/Display.html?id=103253
Expand Down Expand Up @@ -400,11 +405,9 @@ sub predefine_encodings {
$$rpos = length($$rsrc);
return '';
};
$Encode::Encoding{utf8} =
bless { Name => "utf8" } => "Encode::utf8";
$Encode::Encoding{"utf-8-strict"} =
bless { Name => "utf-8-strict", strict_utf8 => 1 }
=> "Encode::utf8";
__PACKAGE__->Define('utf8');
my $strict_obj = bless { Name => "utf-8-strict", strict_utf8 => 1 } => "Encode::utf8";
Encode::define_encoding($strict_obj, 'utf-8-strict');
}
}

Expand Down
3 changes: 2 additions & 1 deletion Unicode/Unicode.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ for my $name (
$endian = ( $3 eq 'BE' ) ? 'n' : ( $3 eq 'LE' ) ? 'v' : '';
$size == 4 and $endian = uc($endian);

$Encode::Encoding{$name} = bless {
my $obj = bless {
Name => $name,
size => $size,
endian => $endian,
ucs2 => $ucs2,
} => __PACKAGE__;
Encode::define_encoding($obj, $name);
}

use parent qw(Encode::Encoding);
Expand Down
2 changes: 2 additions & 0 deletions lib/Encode/Encoding.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use strict;
use warnings;
our $VERSION = do { my @r = ( q$Revision: 2.7 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r };

our @CARP_NOT = qw(Encode Encode::Encoder);

require Encode;

sub DEBUG { 0 }
Expand Down
3 changes: 2 additions & 1 deletion lib/Encode/Guess.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ our $VERSION = do { my @r = ( q$Revision: 2.6 $ =~ /\d+/g ); sprintf "%d." . "%0
my $Canon = 'Guess';
use constant DEBUG => !!$ENV{PERL_ENCODE_DEBUG};
our %DEF_SUSPECTS = map { $_ => find_encoding($_) } qw(ascii utf8);
$Encode::Encoding{$Canon} = bless {
my $obj = bless {
Name => $Canon,
Suspects => {%DEF_SUSPECTS},
} => __PACKAGE__;
Encode::define_encoding($obj, $Canon);

use parent qw(Encode::Encoding);
sub needs_lines { 1 }
Expand Down
3 changes: 2 additions & 1 deletion lib/Encode/JP/JIS7.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ for my $name ( '7bit-jis', 'iso-2022-jp', 'iso-2022-jp-1' ) {
my $h2z = ( $name eq '7bit-jis' ) ? 0 : 1;
my $jis0212 = ( $name eq 'iso-2022-jp' ) ? 0 : 1;

$Encode::Encoding{$name} = bless {
my $obj = bless {
Name => $name,
h2z => $h2z,
jis0212 => $jis0212,
} => __PACKAGE__;
Encode::define_encoding($obj, $name);
}

use parent qw(Encode::Encoding);
Expand Down
17 changes: 8 additions & 9 deletions lib/Encode/MIME/Header.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,28 @@ my %seed = (
bpl => 75, # bytes per line
);

$Encode::Encoding{'MIME-Header'} = bless {
my @objs;

push @objs, bless {
%seed,
Name => 'MIME-Header',
} => __PACKAGE__;

$Encode::Encoding{'MIME-B'} = bless {
push @objs, bless {
%seed,
decode_q => 0,
Name => 'MIME-B',
} => __PACKAGE__;

$Encode::Encoding{'MIME-Q'} = bless {
push @objs, bless {
%seed,
decode_b => 0,
encode => 'Q',
Name => 'MIME-Q',
} => __PACKAGE__;

Encode::define_encoding($_, $_->{Name}) foreach @objs;

use parent qw(Encode::Encoding);

sub needs_lines { 1 }
Expand Down Expand Up @@ -195,7 +199,6 @@ sub _decode_q {
sub _decode_octets {
my ($enc, $octets, $chk) = @_;
$chk &= ~Encode::LEAVE_SRC if not ref $chk and $chk;
local $Carp::CarpLevel = $Carp::CarpLevel + 1; # propagate Carp messages back to caller
my $output = $enc->decode($octets, $chk);
return undef if not ref $chk and $chk and $octets ne '';
return $output;
Expand Down Expand Up @@ -239,11 +242,7 @@ sub _encode_string {
my @result = ();
my $octets = '';
while ( length( my $chr = substr($str, 0, 1, '') ) ) {
my $seq;
{
local $Carp::CarpLevel = $Carp::CarpLevel + 1; # propagate Carp messages back to caller
$seq = $enc->encode($chr, $enc_chk);
}
my $seq = $enc->encode($chr, $enc_chk);
if ( not length($seq) ) {
substr($str, 0, 0, $chr);
last;
Expand Down
3 changes: 2 additions & 1 deletion lib/Encode/MIME/Header/ISO_2022_JP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use warnings;

use parent qw(Encode::MIME::Header);

$Encode::Encoding{'MIME-Header-ISO_2022_JP'} =
my $obj =
bless { decode_b => '1', decode_q => '1', encode => 'B', bpl => 76, Name => 'MIME-Header-ISO_2022_JP' } =>
__PACKAGE__;
Encode::define_encoding($obj, 'MIME-Header-ISO_2022_JP');

use constant HEAD => '=?ISO-2022-JP?B?';
use constant TAIL => '?=';
Expand Down

0 comments on commit 20ed8f3

Please sign in to comment.