| 1 | package POE::XUL::Encode; | ||||||||||||
| 2 | # Copyright Philip Gwyn 2007-2010. All rights reserved. | ||||||||||||
| 3 | |||||||||||||
| 4 | # | ||||||||||||
| 5 | # This was an attempt at a faster binary format for transmitting data | ||||||||||||
| 6 | # to the browser. But JavaScript fails when it comes to binary formats | ||||||||||||
| 7 | # so JSON is used | ||||||||||||
| 8 | # | ||||||||||||
| 9 | |||||||||||||
| 10 | 19 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 19 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 19 | ▶ | use Encode (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 0 | ▶ | sub content_type { return "application/vnd.poe-xul" } | ||||||||||
|
Subroutine
content_type: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 15 | |||||||||||||
| 16 | 19 | my $utf8 = Encode::find_encoding( "utf-8" ); | |||||||||||
| 17 | |||||||||||||
| 18 | 19 | our $VERSION = '0.0600'; | |||||||||||
| 19 | |||||||||||||
| 20 | 19 | my $ETB = "\x17"; # HASH/ARRAY end | |||||||||||
| 21 | 19 | my $FS = "\x1C"; # field sep | |||||||||||
| 22 | 19 | my $GS = "\x1D"; # HASH begin | |||||||||||
| 23 | 19 | my $RS = "\x1E"; # record sep | |||||||||||
| 24 | 19 | my $US = "\x1F"; # ARRAY begin | |||||||||||
| 25 | |||||||||||||
| 26 | sub encode | ||||||||||||
| 27 | { | ||||||||||||
| 28 | 0 | ▶ | my( $package, $AofA ) = @_; | ||||||||||
|
Subroutine
encode: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 29 | 0 | my @ret; | |||||||||||
| 30 | 0 | foreach my $A ( @$AofA ) { | |||||||||||
| 31 | 0 | push @ret, join $FS, map { $package->encode_S( $_ ) } @$A; | |||||||||||
| 32 | } | ||||||||||||
| 33 | 0 | return Encode::encode $utf8, join $RS, @ret; | |||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | sub encode_S | ||||||||||||
| 37 | { | ||||||||||||
| 38 | 0 | ▶ | my( $package, $T ) = @_; | ||||||||||
|
Subroutine
encode_S: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 39 | 0 | my $r = ref $T; | |||||||||||
| 40 | 0 | ▶ | return $T unless $r; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 0 | ▶ | return $GS.join( $FS, %$T ).$ETB if 'HASH' eq $r; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 0 | ▶ | return $US.join( $FS, @$T ).$ETB if 'ARRAY' eq $r; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | 0 | return $T; | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | ########## Following is deprecated because JS can't handle binary data | ||||||||||||
| 47 | sub pack_S | ||||||||||||
| 48 | { | ||||||||||||
| 49 | 0 | ▶ | my( $package, $scalar ) = @_; | ||||||||||
|
Subroutine
pack_S: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 50 | 0 | my $s = '' . $scalar; | |||||||||||
| 51 | 0 | return join '', $package->pack_number( length $s ), $s; | |||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | sub pack_AofS | ||||||||||||
| 55 | { | ||||||||||||
| 56 | 0 | ▶ | my( $package, $array ) = @_; | ||||||||||
|
Subroutine
pack_AofS: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 57 | 0 | ▶ | $array ||= []; | ||||||||||
|
Condition0%0/2
$array ||= []
MC/DC0%0/1
$array ||= []
$array
Truth table0%0/2
A
A= $array
| |||||||||||||
| 58 | 0 | my @ret = $package->pack_number( 0+@$array ); | |||||||||||
| 59 | 0 | foreach my $el ( @$array ) { | |||||||||||
| 60 | 0 | push @ret, $package->pack_S( $el ); | |||||||||||
| 61 | } | ||||||||||||
| 62 | 0 | return join '', @ret; | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | sub pack_AofA | ||||||||||||
| 66 | { | ||||||||||||
| 67 | 0 | ▶ | my( $package, $array ) = @_; | ||||||||||
|
Subroutine
pack_AofA: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 68 | 0 | ▶ | $array ||= []; | ||||||||||
|
Condition0%0/2
$array ||= []
MC/DC0%0/1
$array ||= []
$array
Truth table0%0/2
A
A= $array
| |||||||||||||
| 69 | 0 | my @ret = $package->pack_number( 0+@$array ); | |||||||||||
| 70 | 0 | foreach my $el ( @$array ) { | |||||||||||
| 71 | 0 | push @ret, $package->pack_AofS( $el ); | |||||||||||
| 72 | } | ||||||||||||
| 73 | 0 | return join '', @ret; | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | sub pack_number | ||||||||||||
| 77 | { | ||||||||||||
| 78 | 0 | ▶ | my( $package, $num ) = @_; | ||||||||||
|
Subroutine
pack_number: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 79 | 0 | ▶ | if( $num < 255 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | 0 | return pack "C", 0+$num; | |||||||||||
| 81 | } | ||||||||||||
| 82 | else { | ||||||||||||
| 83 | 0 | return pack "CN", 255, 0+$num; | |||||||||||
| 84 | } | ||||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | 19 | 1; | |||||||||||