File Coverage

lib/Badger/Codec/Unicode.pm
Criterion Covered Total %
statement 28 30 93.3
branch 4 6 66.6
condition n/a
subroutine 10 10 100.0
pod 4 4 100.0
total 46 50 92.0


line stmt bran cond sub pod time code
1             #========================================================================
2             #
3             # Badger::Codec::Unicode
4             #
5             # DESCRIPTION
6             # Codec module for encoding/decoding Unicode via Encode
7             #
8             # AUTHOR
9             # Andy Wardley
10             #
11             #========================================================================
12              
13             package Badger::Codec::Unicode;
14              
15 1     1   1819 use 5.008; # Unicode not fully supported prior to 5.8
  1         9  
16             use Badger::Class
17 1         8 version => 0.01,
18 1     1   746 base => 'Badger::Codec::Encode';
  1         2  
19              
20 1     1   6 use Encode qw();
  1         2  
  1         15  
21 1     1   5 use bytes;
  1         3  
  1         4  
22              
23             # Default encoding
24             our $ENCODING = 'UTF-8';
25              
26             # Byte Order Markers for different UTF encodings
27             our $UTFBOMS = [
28             'UTF-8' => "\x{ef}\x{bb}\x{bf}",
29             'UTF-32BE' => "\x{0}\x{0}\x{fe}\x{ff}",
30             'UTF-32LE' => "\x{ff}\x{fe}\x{0}\x{0}",
31             'UTF-16BE' => "\x{fe}\x{ff}",
32             'UTF-16LE' => "\x{ff}\x{fe}",
33             ];
34              
35             sub encode {
36 10 50   10 1 44 my ($self, $enc, $data) = @_ == 3 ? @_ : (shift, $ENCODING, shift);
37 10         24 Encode::encode($enc, $data);
38             }
39              
40             sub decode {
41 10     10 1 120 my $self = shift;
42 10 50       22 if (@_ >= 2) {
43 0         0 goto &Encode::decode; # not a real GOTO - more like a magic
44             } # subroutine call - see perldoc -f goto
45             else {
46 10         14 my $data = shift;
47 10         12 my $count = 0;
48            
49             # try all the BOMs in order looking for one (order is important
50             # 32bit BOMs look like 16bit BOMs)
51 10         22 while ($count < @$UTFBOMS) {
52 30         40 my $enc = $UTFBOMS->[$count++];
53 30         38 my $bom = $UTFBOMS->[$count++];
54            
55             # does the string start with the bom?
56 30 100       64 if ($bom eq substr($data, 0, length($bom))) {
57             # decode it and hand it back
58             # return Encode::decode($enc, $data);
59 10         33 return Encode::decode($enc, substr($data, length($bom)), 1);
60             }
61             }
62 0         0 return $data;
63             }
64             }
65              
66             sub encoder {
67 1     1 1 2 my $self = shift;
68 1     5   5 return sub { $self->encode(@_) };
  5         24  
69             }
70              
71             sub decoder {
72 1     1 1 2 my $self = shift;
73 1     5   3 return sub { $self->decode(@_) };
  5         38  
74             }
75              
76             1;
77              
78             __END__