Gettext/Storage/MO.pm

filter: Statementstmt 97.8% 93 / 95 Branchbran 52.9% 18 / 34 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 85.3% 128 / 150 CC 23 SCAR 28.8
CC
23
Cov
85%
__ANON__
25.4
__ANON__
2.0
BEGIN
1.0
CRAP 17.9
↑ Summary Gettext.pm »
1 package File::Gettext::Storage::MO;
2  
3 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Encode qw( decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::DataClass::Constants qw( NUL );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::DataClass::Functions qw( extension_map throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Gettext::Constants qw( MAGIC_N MAGIC_V PLURAL_SEP );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 extension_map '+File::Gettext::Storage::MO' => '.mo';
12  
13 1 extends q(File::DataClass::Storage);
14  
15 1 has '+extn' => default => '.mo';
16  
17 # Private subroutines
18 45 ▶my $_decode = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
19 45 ▶ my ($charset, $text) = @_; defined $text or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $text or return 45 0 45
20  
21 45 $text = decode( $charset, $text );
22 45 $text =~ s{ [\\][\'] }{\'}gmsx; $text =~ s{ [\\][\"] }{\"}gmsx;
23 45 return $text;
24 1 };
25  
26 # Private methods
27 1 ▶my $_read_filter = sub {
Subroutine __ANON__: called CC=22 SCAR=32.4
28 1 my ($self, $rdr) = @_; my $path = $rdr->pathname; my $raw = $rdr->all;
29  
30 1 ▶ my $size = length $raw; $size < 28 and throw 'Path [_1] corrupted', [ $path];
Branch: 1 branch
BranchTrueFalseTotal
$size < 28 and &throw("Path [_1] corrupted", [$path]) 0 1 1
31 1 my %meta = (); my $unpack = 'N';
32  
33 1 $meta{magic} = unpack $unpack, substr $raw, 0, 4;
34  
35 1 ▶ if ($meta{magic} == MAGIC_V) { $unpack = 'V' }
Branch: 2 branches
BranchTrueFalseTotal
if ($meta{'magic'} == &MAGIC_V()) { } 1 0 1
elsif ($meta{'magic'} != &MAGIC_N()) { } 0 0 0
36 0 elsif ($meta{magic} != MAGIC_N) { throw 'Path [_1] bad magic', [ $path ] }
37  
38 1 @meta{ qw( revision num_strings msgids_off msgstrs_off hash_size hash_off ) }
39 = unpack( ($unpack x 6), substr $raw, 4, 24 );
40  
41 1 ▶ $meta{revision} == 0 or throw 'Path [_1 ] invalid version', [ $path ];
Branch: 1 branch
BranchTrueFalseTotal
$meta{"revision"} == 0 or &throw("Path [_1 ] invalid version", [$path]) 1 0 1
42  
43 1 my $nstrs = $meta{num_strings};
44  
45 1 ▶ $meta{msgids_off} + 4 * $nstrs > $size and
Branch: 1 branch
BranchTrueFalseTotal
$meta{"msgids_off"} + 4 * $nstrs > $size and &throw("Path [_1] bad msgid offset", [$path]) 0 1 1
46 throw 'Path [_1] bad msgid offset', [ $path ];
47 1 ▶ $meta{msgstrs_off} + 4 * $nstrs > $size and
Branch: 1 branch
BranchTrueFalseTotal
$meta{"msgstrs_off"} + 4 * $nstrs > $size and &throw("Path [_1] bad msgstr offset", [$path]) 0 1 1
48 throw 'Path [_1] bad msgstr offset', [ $path ];
49  
50 my @orig_tab = unpack( ($unpack x (2 * $nstrs)),
51 1 substr $raw, $meta{msgids_off}, 8 * $nstrs );
52 my @trans_tab = unpack( ($unpack x (2 * $nstrs)),
53 1 substr $raw, $meta{msgstrs_off}, 8 * $nstrs );
54 1 my $sep = PLURAL_SEP;
55 1 my $messages = {};
56  
57 1 ▶ for (my $count = 0; $count < 2 * $nstrs; $count += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 2 * $nstrs) 19 1 20
58 19 my $orig_length = $orig_tab[ $count ];
59 19 my $orig_offset = $orig_tab[ $count + 1 ];
60 19 my $trans_length = $trans_tab[ $count ];
61 19 my $trans_offset = $trans_tab[ $count + 1 ];
62  
63 19 ▶ $orig_offset + $orig_length > $size
Branch: 1 branch
BranchTrueFalseTotal
$orig_offset + $orig_length > $size and &throw("Path [_1] bad key length", [$path]) 0 19 19
64 and throw 'Path [_1] bad key length', [ $path ];
65 19 ▶ $trans_offset + $trans_length > $size
Branch: 1 branch
BranchTrueFalseTotal
$trans_offset + $trans_length > $size and &throw("Path [_1] bad text length", [$path]) 0 19 19
66 and throw 'Path [_1] bad text length', [ $path ];
67  
68 19 my @origs = split m{ $sep }mx, substr $raw, $orig_offset, $orig_length;
69 19 my @trans = split m{ $sep }mx, substr $raw, $trans_offset, $trans_length;
70 19 my $msgs = { msgstr => [ @trans ] };
71  
72 # The singular is the origs 0, the plural is origs 1
73 19 ▶ $messages->{ $origs[ 0 ] || NUL } = $msgs;
Condition100%2/2
$origs[0] || ''
l!l
181
MC/DC100%1/1
$origs[0] || ''
$origs[0]
Truth table100%2/2
A
A= $origs[0]
Aresult
11
00
74 19 ▶ $origs[ 1 ] and $messages->{ $origs[ 1 ] } = $msgs;
Branch: 1 branch
BranchTrueFalseTotal
$origs[1] and $$messages{$origs[1]} = $msgs 3 16 19
75 }
76  
77 1 my $header = {}; my $null_entry;
78  
79 # Try to find po header information.
80 1 ▶ if ($null_entry = $messages->{ NUL() }->{msgstr}->[ 0 ]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($null_entry = $messages->{""}{"msgstr"}[0]) 1 0 1
81 1 for my $line (split m{ [\n] }msx, $null_entry) {
82 9 my ($k, $v) = split m{ [:] }msx, $line, 2;
83  
84 9 $k =~ s{ [-] }{_}gmsx; $v =~ s{ \A \s+ }{}msx;
85 9 $header->{ lc $k } = $v;
86 }
87 }
88  
89 1 ▶ if (exists $header->{content_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $header->{"content_type"}) 1 0 1
90 1 my $content_type = $header->{content_type};
91  
92 1 ▶ $content_type =~ s{ .* = }{}msx and $header->{charset} = $content_type;
Branch: 1 branch
BranchTrueFalseTotal
$content_type =~ s/ .* = //msx and $header->{"charset"} = $content_type 1 0 1
93 }
94  
95 my $charset = exists $header->{charset}
96 1 ▶ ? $header->{charset} : $self->schema->charset;
Branch: 1 branch
BranchTrueFalseTotal
exists $header->{'charset'} ? : 1 0 1
97 1 my $tmp = $messages; $messages = {};
98  
99 1 for my $key (grep { $_ } keys %{ $tmp }) {
100 20 my $msg = $tmp->{ $key }; my $id = $_decode->( $charset, $key );
101  
102 25 $messages->{ $id } = { msgstr => [ map { $_decode->( $charset, $_ ) }
103 20 ▶ @{ $msg->{msgstr} || [] } ] };
Condition50%1/2
$msg->{'msgstr'} || []
l!l
200
MC/DC0%0/1
$msg->{'msgstr'} || []
$msg->{'msgstr'}
Truth table50%1/2
A
A= $msg->{'msgstr'}
Aresult
11
00
104 defined $msg->{msgid_plural}
105 and $messages->{ $id }->{msgid_plural}
106 20 ▶ = $_decode->( $charset, $msg->{msgid_plural} );
Branch: 1 branch
BranchTrueFalseTotal
defined $msg->{"msgid_plural"} and $messages->{$id}{"msgid_plural"} = &$_decode($charset, $msg->{"msgid_plural"}) 0 20 20
107 }
108  
109 1 ▶ my $code = $header->{plural_forms} || NUL;
Condition50%1/2
$header->{'plural_forms'} || ''
l!l
10
MC/DC0%0/1
$header->{'plural_forms'} || ''
$header->{'plural_forms'}
Truth table50%1/2
A
A= $header->{'plural_forms'}
Aresult
11
00
110 1 my $s = '[ \t\r\n\013\014]'; # Whitespace, locale-independent.
111  
112 # Untaint the plural header. Keep line breaks as is Perl 5_005 compatibility
113 1 ▶ if ($code =~ m{ \A ($s* nplurals $s* = $s* [0-9]+ $s* ; $s*
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ m[ \A ($s* nplurals $s* = $s* [0-9]+ $s* ; $s* plural $s* = $s* (?:$s|[-\?\|\&=!<>+*/\%:;a-zA-Z0-9_\(\)])+ ) ]msx) { } 1 0 1
114 plural $s* = $s*
115 (?:$s|[-\?\|\&=!<>+*/\%:;a-zA-Z0-9_\(\)])+ ) }msx) {
116 1 $header->{plural_forms} = $1;
117 }
118 0 else { $header->{plural_forms} = NUL }
119  
120 1 return { meta => \%meta,
121 mo => $messages,
122 po_header => { msgid => NUL, msgstr => $header } };
123 1 };
124  
125 # Public methods
126 1 ▶sub read_from_file {
Subroutine read_from_file: called CC=1 SCAR=0.0
Pod documented
127 1 my ($self, $rdr) = @_; return $self->$_read_filter( $rdr );
128 }
129  
130 1 ▶sub write_to_file {
Subroutine write_to_file: called CC=1 SCAR=0.0
Pod documented
131 1 my ($self, $wtr, $data) = @_; return $data;
132 }
133  
134 1 1;
135  
136 __END__
↑ Summary Gettext.pm »