| 1 | # ABSTRACT: The character/octet boundary for karr | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package App::karr::Encoding; | ||||||||||||||||||||
| 4 | 47 | our $VERSION = '0.602'; | |||||||||||||||||||
| 5 | 47 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 47 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 47 | ▶ | use Exporter qw( import ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 47 | ▶ | use Encode qw( encode decode FB_CROAK LEAVE_SRC ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 47 | ▶ | use IO::Handle; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 47 | ▶ | use YAML::XS (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 47 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 47 | our @EXPORT_OK = qw( | |||||||||||||||||||
| 15 | BOARD_ENCODING_VERSION | ||||||||||||||||||||
| 16 | decode_argv | ||||||||||||||||||||
| 17 | enable_std_utf8 | ||||||||||||||||||||
| 18 | to_octets | ||||||||||||||||||||
| 19 | from_octets | ||||||||||||||||||||
| 20 | to_octets_for_env | ||||||||||||||||||||
| 21 | from_octets_from_env | ||||||||||||||||||||
| 22 | yaml_dump | ||||||||||||||||||||
| 23 | yaml_load | ||||||||||||||||||||
| 24 | json_encode | ||||||||||||||||||||
| 25 | json_decode | ||||||||||||||||||||
| 26 | repair_mojibake | ||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 47 | ▶ | use constant BOARD_ENCODING_VERSION => 2; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 13 | ▶ | sub to_octets { | ||||||||||||||||||
|
Subroutine
to_octets: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 34 | 13 | my ($chars) = @_; | |||||||||||||||||||
| 35 | 13 | ▶ | return $chars unless defined $chars; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 13 | return encode( 'UTF-8', $chars ); | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 10 | ▶ | sub from_octets { | ||||||||||||||||||
|
Subroutine
from_octets: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||
| 41 | 10 | my ($octets) = @_; | |||||||||||||||||||
| 42 | 10 | ▶ | return $octets unless defined $octets; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 10 | my $chars = eval { decode( 'UTF-8', $octets, FB_CROAK | LEAVE_SRC ) }; | |||||||||||||||||||
| 44 | 10 | ▶ | return defined $chars ? $chars : $octets; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | ▶ | sub to_octets_for_env { | ||||||||||||||||||
|
Subroutine
to_octets_for_env: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 49 | 0 | my ($chars) = @_; | |||||||||||||||||||
| 50 | 0 | return to_octets($chars); | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | ▶ | sub from_octets_from_env { | ||||||||||||||||||
|
Subroutine
from_octets_from_env: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 55 | 0 | my ($octets) = @_; | |||||||||||||||||||
| 56 | 0 | return from_octets($octets); | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | ▶ | sub decode_argv { | ||||||||||||||||||
|
Subroutine
decode_argv: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 61 | 0 | $_ = from_octets($_) for @ARGV; | |||||||||||||||||||
| 62 | 0 | return; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 0 | ▶ | sub enable_std_utf8 { | ||||||||||||||||||
|
Subroutine
enable_std_utf8: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 67 | 0 | binmode STDOUT, ':encoding(UTF-8)'; | |||||||||||||||||||
| 68 | 0 | binmode STDERR, ':encoding(UTF-8)'; | |||||||||||||||||||
| 69 | # After the binmode, and on both handles: see above (#249). The layer takes | ||||||||||||||||||||
| 70 | # STDERR's unbuffered default away, and flushing only STDERR would invert the | ||||||||||||||||||||
| 71 | # commands that print their outcome before they warn. | ||||||||||||||||||||
| 72 | 0 | STDOUT->autoflush(1); | |||||||||||||||||||
| 73 | 0 | STDERR->autoflush(1); | |||||||||||||||||||
| 74 | 0 | return; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 10 | ▶ | sub yaml_dump { | ||||||||||||||||||
|
Subroutine
yaml_dump: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 79 | 10 | my (@data) = @_; | |||||||||||||||||||
| 80 | 10 | return from_octets( YAML::XS::Dump(@data) ); | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 13 | ▶ | sub yaml_load { | ||||||||||||||||||
|
Subroutine
yaml_load: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 85 | 13 | my ($chars) = @_; | |||||||||||||||||||
| 86 | 13 | return YAML::XS::Load( to_octets($chars) ); | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | # One codec for the process. utf8 => 0 is the whole point: the caller gets a | ||||||||||||||||||||
| 90 | # character string back and the output layer encodes it once, at the edge. | ||||||||||||||||||||
| 91 | 47 | my $JSON; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | ▶ | sub _json { | ||||||||||||||||||
|
Subroutine
_json: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 94 | 0 | ▶ | return $JSON //= JSON::MaybeXS->new( | ||||||||||||||||||
|
Condition0%0/3
$JSON //= "JSON::MaybeXS"->new("utf8", 0, "canonical", 1, "convert_blessed", 1)
MC/DC0%0/2
$JSON //= "JSON::MaybeXS"->new("utf8", 0, "canonical", 1, "convert_blessed", 1)
$JSON
"JSON::MaybeXS"->new("utf8", 0, "canonical", 1, "convert_blessed", 1)
Truth table0%0/3
A //= B
A= $JSONB= "JSON::MaybeXS"->new("utf8", 0, "canonical", 1, "convert_blessed", 1)
| |||||||||||||||||||||
| 95 | utf8 => 0, | ||||||||||||||||||||
| 96 | canonical => 1, | ||||||||||||||||||||
| 97 | convert_blessed => 1, | ||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | ▶ | sub json_encode { | ||||||||||||||||||
|
Subroutine
json_encode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 103 | 0 | my ($data) = @_; | |||||||||||||||||||
| 104 | 0 | return _json()->encode($data); | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | ▶ | sub json_decode { | ||||||||||||||||||
|
Subroutine
json_decode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 109 | 0 | my ($chars) = @_; | |||||||||||||||||||
| 110 | 0 | return _json()->decode($chars); | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 0 | ▶ | sub repair_mojibake { | ||||||||||||||||||
|
Subroutine
repair_mojibake: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||
| 115 | 0 | my ($data) = @_; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 0 | my $ref = ref $data; | |||||||||||||||||||
| 118 | 0 | ▶ | return { map { $_ => repair_mojibake( $data->{$_} ) } keys %$data } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | if $ref eq 'HASH'; | ||||||||||||||||||||
| 120 | 0 | ▶ | return [ map { repair_mojibake($_) } @$data ] | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | if $ref eq 'ARRAY'; | ||||||||||||||||||||
| 122 | 0 | ▶ | return $data if $ref; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 0 | ▶ | return $data unless defined $data; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 0 | ▶ | return $data unless $data =~ /[^\x00-\x7F]/; # ASCII: nothing to repair | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 0 | ▶ | return $data if $data =~ /[^\x00-\xFF]/; # real characters: already right | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | # LEAVE_SRC on both calls, and it is not cosmetic: with a CHECK argument and | ||||||||||||||||||||
| 129 | # without it, Encode consumes the source string in place. Omitting it here | ||||||||||||||||||||
| 130 | # emptied $data, so every string that reached the decode and failed it -- all | ||||||||||||||||||||
| 131 | # ordinary Latin-1 text -- came back as "" instead of unchanged. | ||||||||||||||||||||
| 132 | 0 | my $octets = eval { encode( 'ISO-8859-1', $data, FB_CROAK | LEAVE_SRC ) }; | |||||||||||||||||||
| 133 | 0 | ▶ | return $data unless defined $octets; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | 0 | my $decoded = eval { decode( 'UTF-8', $octets, FB_CROAK | LEAVE_SRC ) }; | |||||||||||||||||||
| 135 | 0 | ▶ | return defined $decoded ? $decoded : $data; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 47 | 1; | |||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||||