line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Encode::Simple; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
500
|
use strict; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
5
|
1
|
|
|
1
|
|
4
|
use Carp (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
12
|
|
6
|
1
|
|
|
1
|
|
556
|
use Encode (); |
|
1
|
|
|
|
|
10051
|
|
|
1
|
|
|
|
|
25
|
|
7
|
1
|
|
|
1
|
|
7
|
use Exporter 'import'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
87
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.003'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @EXPORT = qw(encode decode); |
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw(encode_lax decode_lax); |
13
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK], strict => \@EXPORT, lax => \@EXPORT_OK); |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
6
|
use constant MASK_STRICT => Encode::FB_CROAK | Encode::LEAVE_SRC; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
57
|
|
16
|
1
|
|
|
1
|
|
6
|
use constant MASK_LAX => Encode::FB_DEFAULT | Encode::LEAVE_SRC; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
92
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my %ENCODINGS; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub encode { |
21
|
5
|
|
|
5
|
1
|
1569
|
my ($encoding, $input) = @_; |
22
|
5
|
50
|
|
|
|
15
|
return undef unless defined $input; |
23
|
5
|
|
66
|
|
|
18
|
my $obj = $ENCODINGS{$encoding} || _find_encoding($encoding); |
24
|
5
|
|
|
|
|
10
|
my ($output, $error); |
25
|
1
|
|
|
1
|
|
7
|
{ local $@; use warnings FATAL => 'utf8'; # Encode::Unicode throws warnings in this category |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
167
|
|
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
8
|
|
26
|
5
|
100
|
50
|
|
|
9
|
unless (eval { $output = $obj->encode("$input", MASK_STRICT); 1 }) { $error = $@ || 'Error' } |
|
5
|
|
|
|
|
58
|
|
|
1
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
15
|
|
27
|
|
|
|
|
|
|
} |
28
|
5
|
100
|
|
|
|
17
|
_rethrow($error) if defined $error; |
29
|
1
|
|
|
|
|
5
|
return $output; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub encode_lax { |
33
|
4
|
|
|
4
|
1
|
1232
|
my ($encoding, $input) = @_; |
34
|
4
|
50
|
|
|
|
12
|
return undef unless defined $input; |
35
|
4
|
|
33
|
|
|
9
|
my $obj = $ENCODINGS{$encoding} || _find_encoding($encoding); |
36
|
4
|
|
|
|
|
9
|
my ($output, $error); |
37
|
1
|
|
|
1
|
|
7
|
{ local $@; no warnings 'utf8'; # Encode::Unicode throws warnings in this category |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
167
|
|
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
7
|
|
38
|
4
|
50
|
0
|
|
|
6
|
unless (eval { $output = $obj->encode("$input", MASK_LAX); 1 }) { $error = $@ || 'Error' } |
|
4
|
|
|
|
|
23
|
|
|
4
|
|
|
|
|
13
|
|
|
0
|
|
|
|
|
0
|
|
39
|
|
|
|
|
|
|
} |
40
|
4
|
50
|
|
|
|
10
|
_rethrow($error) if defined $error; |
41
|
4
|
|
|
|
|
20
|
return $output; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub decode { |
45
|
2
|
|
|
2
|
1
|
5
|
my ($encoding, $input) = @_; |
46
|
2
|
50
|
|
|
|
8
|
return undef unless defined $input; |
47
|
2
|
|
33
|
|
|
7
|
my $obj = $ENCODINGS{$encoding} || _find_encoding($encoding); |
48
|
2
|
|
|
|
|
4
|
my ($output, $error); |
49
|
1
|
|
|
1
|
|
7
|
{ local $@; use warnings FATAL => 'utf8'; # Encode::Unicode throws warnings in this category |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
160
|
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
4
|
|
50
|
2
|
100
|
50
|
|
|
16
|
unless (eval { $output = $obj->decode("$input", MASK_STRICT); 1 }) { $error = $@ || 'Error' } |
|
2
|
|
|
|
|
17
|
|
|
1
|
|
|
|
|
9
|
|
|
1
|
|
|
|
|
15
|
|
51
|
|
|
|
|
|
|
} |
52
|
2
|
100
|
|
|
|
8
|
_rethrow($error) if defined $error; |
53
|
1
|
|
|
|
|
4
|
return $output; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub decode_lax { |
57
|
2
|
|
|
2
|
1
|
6
|
my ($encoding, $input) = @_; |
58
|
2
|
50
|
|
|
|
7
|
return undef unless defined $input; |
59
|
2
|
|
33
|
|
|
7
|
my $obj = $ENCODINGS{$encoding} || _find_encoding($encoding); |
60
|
2
|
|
|
|
|
4
|
my ($output, $error); |
61
|
1
|
|
|
1
|
|
7
|
{ local $@; no warnings 'utf8'; # Encode::Unicode throws warnings in this category |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
293
|
|
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
3
|
|
62
|
2
|
50
|
0
|
|
|
4
|
unless (eval { $output = $obj->decode("$input", MASK_LAX); 1 }) { $error = $@ || 'Error' } |
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
13
|
|
|
0
|
|
|
|
|
0
|
|
63
|
|
|
|
|
|
|
} |
64
|
2
|
50
|
|
|
|
6
|
_rethrow($error) if defined $error; |
65
|
2
|
|
|
|
|
15
|
return $output; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _find_encoding { |
69
|
3
|
|
|
3
|
|
5
|
my ($encoding) = @_; |
70
|
3
|
50
|
|
|
|
7
|
Carp::croak('Encoding name should not be undef') unless defined $encoding; |
71
|
3
|
|
|
|
|
11
|
my $obj = Encode::find_encoding($encoding); |
72
|
3
|
50
|
|
|
|
3728
|
Carp::croak("Unknown encoding '$encoding'") unless defined $obj; |
73
|
3
|
|
|
|
|
16
|
return $ENCODINGS{$encoding} = $obj; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub _rethrow { |
77
|
5
|
|
|
5
|
|
11
|
my ($error) = @_; |
78
|
5
|
50
|
33
|
|
|
36
|
die $error if ref $error or $error =~ m/\n(?!\z)/; |
79
|
5
|
|
|
|
|
34
|
$error =~ s/ at .+? line [0-9]+\.\n\z//; |
80
|
5
|
|
|
|
|
649
|
Carp::croak($error); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
1; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 NAME |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Encode::Simple - Encode and decode text, simply |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 SYNOPSIS |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
use Encode::Simple qw(encode decode encode_lax decode_lax); |
92
|
|
|
|
|
|
|
my $characters = decode 'cp1252', $bytes; |
93
|
|
|
|
|
|
|
my $characters = decode_lax 'UTF-8', $bytes; |
94
|
|
|
|
|
|
|
my $bytes = encode 'Shift_JIS', $characters; |
95
|
|
|
|
|
|
|
my $bytes = encode_lax 'ASCII', $characters; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 DESCRIPTION |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
This module is a simple wrapper around L that presents L"encode"> and |
100
|
|
|
|
|
|
|
L"decode"> functions with straightforward behavior and error handling. See |
101
|
|
|
|
|
|
|
L for a list of supported encodings. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 FUNCTIONS |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
All functions are exported by name, as well as via the tags C<:all>, |
106
|
|
|
|
|
|
|
C<:strict>, and C<:lax>. By default, L"encode"> and L"decode"> are |
107
|
|
|
|
|
|
|
exported. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head2 encode |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
my $bytes = encode $encoding, $characters; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Encodes the input string of characters into a byte string using C<$encoding>. |
114
|
|
|
|
|
|
|
Throws an exception if the input string contains characters that are not valid |
115
|
|
|
|
|
|
|
or possible to represent in C<$encoding>. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 encode_lax |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my $bytes = encode_lax $encoding, $characters; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Encodes the input string of characters as in L"encode">, but instead of |
122
|
|
|
|
|
|
|
throwing an exception on invalid input, any invalid characters are encoded as a |
123
|
|
|
|
|
|
|
substitution character (the substitution character used depends on the |
124
|
|
|
|
|
|
|
encoding). Note that some encoders do not respect this option and may throw an |
125
|
|
|
|
|
|
|
exception anyway, this notably includes L (but not UTF-8). |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 decode |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $characters = decode $encoding, $bytes; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Decodes the input byte string into a string of characters using C<$encoding>. |
132
|
|
|
|
|
|
|
Throws an exception if the input bytes are not valid for C<$encoding>. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 decode_lax |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
my $characters = decode_lax $encoding, $bytes; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Decodes the input byte string as in L"decode">, but instead of throwing an |
139
|
|
|
|
|
|
|
exception on invalid input, any malformed bytes will be decoded to the Unicode |
140
|
|
|
|
|
|
|
replacement character (U+FFFD). Note that some encoders do not respect this |
141
|
|
|
|
|
|
|
option and may throw an exception anyway, this notably includes |
142
|
|
|
|
|
|
|
L (but not UTF-8). |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 BUGS |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Report any issues on the public bugtracker. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 AUTHOR |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Dan Book |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
This software is Copyright (c) 2018 by Dan Book. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
This is free software, licensed under: |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 SEE ALSO |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
L |