Integrate perlio:
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / Guess.pm
CommitLineData
af1f55d9 1package Encode::Guess;
2use strict;
7e19fb92 3
af1f55d9 4use Encode qw(:fallbacks find_encoding);
e8c86ba6 5our $VERSION = do { my @r = (q$Revision: 1.4 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
af1f55d9 6
7my $Canon = 'Guess';
af1f55d9 8our $DEBUG = 0;
7e19fb92 9our %DEF_SUSPECTS = map { $_ => find_encoding($_) } qw(ascii utf8);
10$Encode::Encoding{$Canon} =
11 bless {
12 Name => $Canon,
13 Suspects => { %DEF_SUSPECTS },
14 } => __PACKAGE__;
15
10c5ecbb 16use base qw(Encode::Encoding);
7e19fb92 17sub needs_lines { 1 }
18sub perlio_ok { 0 }
7e19fb92 19
20our @EXPORT = qw(guess_encoding);
21
22sub import { # Exporter not used so we do it on our own
23 my $callpkg = caller;
24 for my $item (@EXPORT){
25 no strict 'refs';
26 *{"$callpkg\::$item"} = \&{"$item"};
27 }
28 set_suspects(@_);
29}
af1f55d9 30
7e19fb92 31sub set_suspects{
32 my $class = shift;
33 my $self = ref($class) ? $class : $Encode::Encoding{$Canon};
34 $self->{Suspects} = { %DEF_SUSPECTS };
35 $self->add_suspects(@_);
36}
af1f55d9 37
7e19fb92 38sub add_suspects{
af1f55d9 39 my $class = shift;
7e19fb92 40 my $self = ref($class) ? $class : $Encode::Encoding{$Canon};
af1f55d9 41 for my $c (@_){
42 my $e = find_encoding($c) or die "Unknown encoding: $c";
7e19fb92 43 $self->{Suspects}{$e->name} = $e;
af1f55d9 44 $DEBUG and warn "Added: ", $e->name;
45 }
46}
47
af1f55d9 48sub decode($$;$){
49 my ($obj, $octet, $chk) = @_;
7e19fb92 50 my $guessed = guess($obj, $octet);
10c5ecbb 51 unless (ref($guessed)){
52 require Carp;
53 Carp::croak($guessed);
54 }
7e19fb92 55 my $utf8 = $guessed->decode($octet, $chk);
af1f55d9 56 $_[1] = $octet if $chk;
57 return $utf8;
58}
59
7e19fb92 60sub guess_encoding{
61 guess($Encode::Encoding{$Canon}, @_);
af1f55d9 62}
63
64sub guess {
7e19fb92 65 my $class = shift;
66 my $obj = ref($class) ? $class : $Encode::Encoding{$Canon};
67 my $octet = shift;
68 # cheat 0: utf8 flag;
af1f55d9 69 Encode::is_utf8($octet) and return find_encoding('utf8');
7e19fb92 70 # cheat 1: BOM
71 use Encode::Unicode;
72 my $BOM = unpack('n', $octet);
73 return find_encoding('UTF-16')
74 if ($BOM == 0xFeFF or $BOM == 0xFFFe);
75 $BOM = unpack('N', $octet);
76 return find_encoding('UTF-32')
77 if ($BOM == 0xFeFF or $BOM == 0xFFFe0000);
78
79 my %try = %{$obj->{Suspects}};
80 for my $c (@_){
81 my $e = find_encoding($c) or die "Unknown encoding: $c";
82 $try{$e->name} = $e;
83 $DEBUG and warn "Added: ", $e->name;
84 }
af1f55d9 85 my $nline = 1;
86 for my $line (split /\r|\n|\r\n/, $octet){
7e19fb92 87 # cheat 2 -- \e in the string
af1f55d9 88 if ($line =~ /\e/o){
89 my @keys = keys %try;
90 delete @try{qw/utf8 ascii/};
91 for my $k (@keys){
92 ref($try{$k}) eq 'Encode::XS' and delete $try{$k};
93 }
94 }
95 my %ok = %try;
96 # warn join(",", keys %try);
97 for my $k (keys %try){
98 my $scratch = $line;
99 $try{$k}->decode($scratch, FB_QUIET);
100 if ($scratch eq ''){
101 $DEBUG and warn sprintf("%4d:%-24s ok\n", $nline, $k);
102 }else{
103 use bytes ();
104 $DEBUG and
105 warn sprintf("%4d:%-24s not ok; %d bytes left\n",
106 $nline, $k, bytes::length($scratch));
107 delete $ok{$k};
108
109 }
110 }
7e19fb92 111 %ok or return "No appropriate encodings found!";
af1f55d9 112 if (scalar(keys(%ok)) == 1){
113 my ($retval) = values(%ok);
114 return $retval;
115 }
116 %try = %ok; $nline++;
117 }
7e19fb92 118 $try{ascii} or
119 return "Encodings too ambiguous: ", join(" or ", keys %try);
af1f55d9 120 return $try{ascii};
121}
122
123
7e19fb92 124
af1f55d9 1251;
126__END__
127
128=head1 NAME
129
7e19fb92 130Encode::Guess -- Guesses encoding from data
131
132=head1 SYNOPSIS
133
134 # if you are sure $data won't contain anything bogus
135
e8c86ba6 136 use Encode;
7e19fb92 137 use Encode::Guess qw/euc-jp shiftjis 7bit-jis/;
138 my $utf8 = decode("Guess", $data);
139 my $data = encode("Guess", $utf8); # this doesn't work!
140
141 # more elaborate way
142 use Encode::Guess,
143 my $enc = guess_encoding($data, qw/euc-jp shiftjis 7bit-jis/);
144 ref($enc) or die "Can't guess: $enc"; # trap error this way
145 $utf8 = $enc->decode($data);
146 # or
147 $utf8 = decode($enc->name, $data)
148
149=head1 ABSTRACT
150
151Encode::Guess enables you to guess in what encoding a given data is
152encoded, or at least tries to.
153
154=head1 DESCRIPTION
155
156By default, it checks only ascii, utf8 and UTF-16/32 with BOM.
157
158 use Encode::Guess; # ascii/utf8/BOMed UTF
159
160To use it more practically, you have to give the names of encodings to
161check (I<suspects> as follows). The name of suspects can either be
162canonical names or aliases.
163
164 # tries all major Japanese Encodings as well
165 use Encode::Guess qw/euc-jp shiftjis 7bit-jis/;
166
167=over 4
168
169=item Encode::Guess->set_suspects
170
171You can also change the internal suspects list via C<set_suspects>
172method.
173
174 use Encode::Guess;
175 Encode::Guess->set_suspects(qw/euc-jp shiftjis 7bit-jis/);
176
177=item Encode::Guess->add_suspects
178
179Or you can use C<add_suspects> method. The difference is that
180C<set_suspects> flushes the current suspects list while
181C<add_suspects> adds.
182
183 use Encode::Guess;
184 Encode::Guess->add_suspects(qw/euc-jp shiftjis 7bit-jis/);
185 # now the suspects are euc-jp,shiftjis,7bit-jis, AND
186 # euc-kr,euc-cn, and big5-eten
187 Encode::Guess->add_suspects(qw/euc-kr euc-cn big5-eten/);
188
189=item Encode::decode("Guess" ...)
190
191When you are content with suspects list, you can now
192
193 my $utf8 = Encode::decode("Guess", $data);
194
195=item Encode::Guess->guess($data)
196
197But it will croak if Encode::Guess fails to eliminate all other
198suspects but the right one or no suspect was good. So you should
199instead try this;
200
201 my $decoder = Encode::Guess->guess($data);
202
203On success, $decoder is an object that is documented in
204L<Encode::Encoding>. So you can now do this;
205
206 my $utf8 = $decoder->decode($data);
207
208On failure, $decoder now contains an error message so the whole thing
209would be as follows;
210
211 my $decoder = Encode::Guess->guess($data);
212 die $decoder unless ref($decoder);
213 my $utf8 = $decoder->decode($data);
214
215=item guess_encoding($data, [, I<list of suspects>])
216
217You can also try C<guess_encoding> function which is exported by
218default. It takes $data to check and it also takes the list of
219suspects by option. The optional suspect list is I<not reflected> to
220the internal suspects list.
221
222 my $decoder = guess_encoding($data, qw/euc-jp euc-kr euc-cn/);
223 die $decoder unless ref($decoder);
224 my $utf8 = $decoder->decode($data);
225 # check only ascii and utf8
226 my $decoder = guess_encoding($data);
227
228=back
229
230=head1 CAVEATS
231
232=over 4
233
234=item *
235
236Because of the algorithm used, ISO-8859 series and other single-byte
237encodings do not work well unless either one of ISO-8859 is the only
238one suspect (besides ascii and utf8).
239
240 use Encode::Guess;
241 # perhaps ok
242 my $decoder = guess_encoding($data, 'latin1');
243 # definitely NOT ok
244 my $decoder = guess_encoding($data, qw/latin1 greek/);
245
246The reason is that Encode::Guess guesses encoding by trial and error.
247It first splits $data into lines and tries to decode the line for each
248suspect. It keeps it going until all but one encoding was eliminated
249out of suspects list. ISO-8859 series is just too successful for most
250cases (because it fills almost all code points in \x00-\xff).
251
252=item *
253
254Do not mix national standard encodings and the corresponding vendor
255encodings.
256
257 # a very bad idea
258 my $decoder
259 = guess_encoding($data, qw/shiftjis MacJapanese cp932/);
260
261The reason is that vendor encoding is usually a superset of national
262standard so it becomes too ambiguous for most cases.
263
264=item *
265
266On the other hand, mixing various national standard encodings
267automagically works unless $data is too short to allow for guessing.
268
269 # This is ok if $data is long enough
270 my $decoder =
271 guess_encoding($data, qw/euc-cn
272 euc-jp shiftjis 7bit-jis
273 euc-kr
274 big5-eten/);
275
276=item *
277
278DO NOT PUT TOO MANY SUSPECTS! Don't you try something like this!
279
280 my $decoder = guess_encoding($data,
281 Encode->encodings(":all"));
282
283=back
284
285It is, after all, just a guess. You should alway be explicit when it
286comes to encodings. But there are some, especially Japanese,
287environment that guess-coding is a must. Use this module with care.
288
289=head1 SEE ALSO
290
291L<Encode>, L<Encode::Encoding>
af1f55d9 292
293=cut
294