File Coverage

blib/lib/XML/SAX/PurePerl/EncodingDetect.pm
Criterion Covered Total %
statement 15 49 30.6
branch 20 38 52.6
condition n/a
subroutine 2 2 100.0
pod 0 1 0.0
total 37 90 41.1


line stmt bran cond sub pod time code
1             # $Id$
2              
3             package XML::SAX::PurePerl; # NB, not ::EncodingDetect!
4              
5 12     12   70 use strict;
  12         25  
  12         8132  
6              
7             sub encoding_detect {
8 22     22 0 45 my ($parser, $reader) = @_;
9            
10 22         57 my $error = "Invalid byte sequence at start of file";
11            
12 22         133 my $data = $reader->data;
13 22 50       868 if ($data =~ /^\x00\x00\xFE\xFF/) {
    50          
    50          
    50          
    50          
    50          
    50          
    100          
    50          
    100          
    50          
    50          
    50          
    100          
    50          
    50          
    50          
    0          
    0          
14             # BO-UCS4-be
15 0         0 $reader->move_along(4);
16 0         0 $reader->set_encoding('UCS-4BE');
17 0         0 return;
18             }
19             elsif ($data =~ /^\x00\x00\xFF\xFE/) {
20             # BO-UCS-4-2143
21 0         0 $reader->move_along(4);
22 0         0 $reader->set_encoding('UCS-4-2143');
23 0         0 return;
24             }
25             elsif ($data =~ /^\x00\x00\x00\x3C/) {
26 0         0 $reader->set_encoding('UCS-4BE');
27 0         0 return;
28             }
29             elsif ($data =~ /^\x00\x00\x3C\x00/) {
30 0         0 $reader->set_encoding('UCS-4-2143');
31 0         0 return;
32             }
33             elsif ($data =~ /^\x00\x3C\x00\x00/) {
34 0         0 $reader->set_encoding('UCS-4-3412');
35 0         0 return;
36             }
37             elsif ($data =~ /^\x00\x3C\x00\x3F/) {
38 0         0 $reader->set_encoding('UTF-16BE');
39 0         0 return;
40             }
41             elsif ($data =~ /^\xFF\xFE\x00\x00/) {
42             # BO-UCS-4LE
43 0         0 $reader->move_along(4);
44 0         0 $reader->set_encoding('UCS-4LE');
45 0         0 return;
46             }
47             elsif ($data =~ /^\xFF\xFE/) {
48 1         5 $reader->move_along(2);
49 1         4 $reader->set_encoding('UTF-16LE');
50 1         3 return;
51             }
52             elsif ($data =~ /^\xFE\xFF\x00\x00/) {
53 0         0 $reader->move_along(4);
54 0         0 $reader->set_encoding('UCS-4-3412');
55 0         0 return;
56             }
57             elsif ($data =~ /^\xFE\xFF/) {
58 1         7 $reader->move_along(2);
59 1         4 $reader->set_encoding('UTF-16BE');
60 1         6 return;
61             }
62             elsif ($data =~ /^\xEF\xBB\xBF/) { # UTF-8 BOM
63 0         0 $reader->move_along(3);
64 0         0 $reader->set_encoding('UTF-8');
65 0         0 return;
66             }
67             elsif ($data =~ /^\x3C\x00\x00\x00/) {
68 0         0 $reader->set_encoding('UCS-4LE');
69 0         0 return;
70             }
71             elsif ($data =~ /^\x3C\x00\x3F\x00/) {
72 0         0 $reader->set_encoding('UTF-16LE');
73 0         0 return;
74             }
75             elsif ($data =~ /^\x3C\x3F\x78\x6D/) {
76             # $reader->set_encoding('UTF-8');
77 9         59 return;
78             }
79             elsif ($data =~ /^\x3C\x3F\x78/) {
80             # $reader->set_encoding('UTF-8');
81 0         0 return;
82             }
83             elsif ($data =~ /^\x3C\x3F/) {
84             # $reader->set_encoding('UTF-8');
85 0         0 return;
86             }
87             elsif ($data =~ /^\x3C/) {
88             # $reader->set_encoding('UTF-8');
89 11         51 return;
90             }
91             elsif ($data =~ /^[\x20\x09\x0A\x0D]+\x3C[^\x3F]/) {
92             # $reader->set_encoding('UTF-8');
93 0           return;
94             }
95             elsif ($data =~ /^\x4C\x6F\xA7\x94/) {
96 0           $reader->set_encoding('EBCDIC');
97 0           return;
98             }
99            
100 0           warn("Unable to recognise encoding of this document");
101 0           return;
102             }
103              
104             1;
105