| 1 | #======================================================================== | ||||||||||
| 2 | # | ||||||||||
| 3 | # Badger::Codec::Unicode | ||||||||||
| 4 | # | ||||||||||
| 5 | # DESCRIPTION | ||||||||||
| 6 | # Codec module for encoding/decoding Unicode via Encode | ||||||||||
| 7 | # | ||||||||||
| 8 | # AUTHOR | ||||||||||
| 9 | # Andy Wardley <abw@wardley.org> | ||||||||||
| 10 | # | ||||||||||
| 11 | #======================================================================== | ||||||||||
| 12 | |||||||||||
| 13 | package Badger::Codec::Unicode; | ||||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | use 5.008; # Unicode not fully supported prior to 5.8 | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | use Badger::Class | ||||||||||
| 17 | 1 | ▶ | version => 0.01, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | base => 'Badger::Codec::Encode'; | |||||||||
| 19 | |||||||||||
| 20 | 1 | ▶ | use Encode qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 1 | ▶ | use bytes; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | # Default encoding | ||||||||||
| 24 | 1 | our $ENCODING = 'UTF-8'; | |||||||||
| 25 | |||||||||||
| 26 | # Byte Order Markers for different UTF encodings | ||||||||||
| 27 | 1 | 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 | 10 | ▶ | sub encode { | ||||||||
|
Subroutine
encode: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 36 | 10 | ▶ | my ($self, $enc, $data) = @_ == 3 ? @_ : (shift, $ENCODING, shift); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 10 | Encode::encode($enc, $data); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 10 | ▶ | sub decode { | ||||||||
|
Subroutine
decode: called CC=4 SCAR=14.4
Pod
documented
| |||||||||||
| 41 | 10 | my $self = shift; | |||||||||
| 42 | 10 | ▶ | if (@_ >= 2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | goto &Encode::decode; # not a real GOTO - more like a magic | |||||||||
| 44 | } # subroutine call - see perldoc -f goto | ||||||||||
| 45 | else { | ||||||||||
| 46 | 10 | my $data = shift; | |||||||||
| 47 | 10 | 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 | ▶ | while ($count < @$UTFBOMS) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 30 | my $enc = $UTFBOMS->[$count++]; | |||||||||
| 53 | 30 | my $bom = $UTFBOMS->[$count++]; | |||||||||
| 54 | |||||||||||
| 55 | # does the string start with the bom? | ||||||||||
| 56 | 30 | ▶ | if ($bom eq substr($data, 0, length($bom))) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | # decode it and hand it back | ||||||||||
| 58 | # return Encode::decode($enc, $data); | ||||||||||
| 59 | 10 | return Encode::decode($enc, substr($data, length($bom)), 1); | |||||||||
| 60 | } | ||||||||||
| 61 | } | ||||||||||
| 62 | 0 | return $data; | |||||||||
| 63 | } | ||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 1 | ▶ | sub encoder { | ||||||||
|
Subroutine
encoder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 67 | 1 | my $self = shift; | |||||||||
| 68 | 1 | ▶ | return sub { $self->encode(@_) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 1 | ▶ | sub decoder { | ||||||||
|
Subroutine
decoder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 72 | 1 | my $self = shift; | |||||||||
| 73 | 1 | ▶ | return sub { $self->decode(@_) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 1 | 1; | |||||||||
| 77 | |||||||||||
| 78 | __END__ | ||||||||||