blib/lib/Locale/MO/File.pm

filter: Statementstmt 100% 165 / 165 Branchbran 84.4% 49 / 58 Conditioncond 63.6% 21 / 33 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 100% 22 / 22 Podpod 100% 2 / 2 totaltotal 88.8% 264 / 297 CC 48 SCAR 19.4
CC
48
Cov
92%
_pack_message
13.2
read_file
10.2
write_file
10.1
CRAP 7.0
↑ Summary
1 package Locale::MO::File; ## no critic (TidyCode)
2
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use charnames qw(:full);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Const::Fast qw(const);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Encode qw(find_encoding);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use English qw(-no_match_vars $INPUT_RECORD_SEPARATOR $OS_ERROR);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 require IO::File;
12 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use MooX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use MooX::Types::MooseLike::Base qw(Bool Str ArrayRef FileHandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Params::Validate qw(validate_with SCALAR ARRAYREF);
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 8 our $VERSION = '0.09';
18
19 8 const my $INTEGER_LENGTH => length pack 'N', 0;
20 8 const my $REVISION_OFFSET => $INTEGER_LENGTH;
21 8 const my $MAPS_OFFSET => $INTEGER_LENGTH * 7;
22 8 const my $MAGIC_NUMBER => 0x95_04_12_DE;
23 8 const our $CONTEXT_SEPARATOR => "\N{END OF TRANSMISSION}";
24 8 const our $PLURAL_SEPARATOR => "\N{NULL}";
25
26 8 has filename => (
27 is => 'rw',
28 isa => Str,
29 reader => 'get_filename',
30 writer => 'set_filename',
31 clearer => 'clear_filename',
32 );
33 8 has file_handle => (
34 is => 'rw',
35 isa => FileHandle,
36 reader => 'get_file_handle',
37 writer => 'set_file_handle',
38 clearer => 'clear_file_handle',
39 );
40 8 has encoding => (
41 is => 'rw',
42 isa => Str,
43 reader => 'get_encoding',
44 writer => 'set_encoding',
45 clearer => 'clear_encoding',
46 );
47 8 has newline => (
48 is => 'rw',
49 isa => Str,
50 reader => 'get_newline',
51 writer => 'set_newline',
52 clearer => 'clear_newline',
53 );
54 8 has is_big_endian => (
55 is => 'rw',
56 isa => Bool,
57 reader => 'is_big_endian',
58 writer => 'set_is_big_endian',
59 clearer => 'clear_is_big_endian',
60 );
61 has messages => (
62 is => 'rw',
63 isa => ArrayRef,
64 2 ▶ default => sub { return [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
65 8 lazy => 1,
66 reader => 'get_messages',
67 writer => 'set_messages',
68 );
69
70 142 ▶sub _encode_and_replace_newline {
Subroutine _encode_and_replace_newline: called CC=4 SCAR=13.9
71 142 my ($self, $string) = @_;
72
73 142 ▶ if ( $self->get_encoding ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_encoding) 40 102 142
74 40 ▶ my $encoder = find_encoding( $self->get_encoding )
Branch: 1 branch
BranchTrueFalseTotal
my $encoder = &find_encoding(scalar $self->get_encoding) or confess("Can not find encoding for ", $self->get_encoding) 40 0 40
75 or confess 'Can not find encoding for ', $self->get_encoding;
76 40 $string = $encoder->encode($string);
77 }
78 142 ▶ if ( $self->get_newline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_newline) 40 102 142
79 40 $string =~ s{ \r? \n }{ $self->get_newline }xmsge;
80 }
81
82 142 return $string;
83 }
84
85 88 ▶sub _decode_and_replace_newline {
Subroutine _decode_and_replace_newline: called CC=4 SCAR=13.9
86 88 my ($self, $string) = @_;
87
88 88 ▶ if ( $self->get_encoding ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_encoding) 36 52 88
89 36 ▶ my $encoder = find_encoding( $self->get_encoding )
Branch: 1 branch
BranchTrueFalseTotal
my $encoder = &find_encoding(scalar $self->get_encoding) or confess("Can not find encoding for ", $self->get_encoding) 36 0 36
90 or confess 'Can not find encoding for ', $self->get_encoding;
91 36 $string = $encoder->decode($string, Encode::FB_CROAK);
92 }
93 88 ▶ if ( $self->get_newline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_newline) 36 52 88
94 36 $string =~ s{ \r? \n }{ $self->get_newline }xmsge;
95 }
96
97 88 return $string;
98 }
99
100 71 ▶sub _pack_message {
Subroutine _pack_message: called CC=12 SCAR=25.8
101 71 my ($self, $message) = @_;
102
103 my ($msgid, $msgstr) = map {
104 71 ( exists $message->{$_} && defined $message->{$_} )
105 142 ▶ ? $message->{$_}
Branch: 1 branch
BranchTrueFalseTotal
exists $message->{$_} && defined $message->{$_} ? : 109 33 142
Condition66%2/3
exists $message->{$_} and defined $message->{$_}
!ll&&!rl&&r
330109
MC/DC50%1/2
exists $message->{$_} and defined $message->{$_}
exists $message->{$_} defined $message->{$_}
Truth table66%2/3
A and B
A= exists $message->{$_}B= defined $message->{$_}
ABresult
0X0
100
111
106 : q{};
107 } qw(msgid msgstr);
108
109 # original
110 $msgid = $self->_encode_and_replace_newline(
111 (
112 (
113 exists $message->{msgctxt}
114 && defined $message->{msgctxt}
115 && length $message->{msgctxt}
116 )
117 ? $message->{msgctxt} . $CONTEXT_SEPARATOR . $msgid
118 : $msgid
119 )
120 . (
121 (
122 exists $message->{msgid_plural}
123 && defined $message->{msgid_plural}
124 && length $message->{msgid_plural}
125 )
126 ? $PLURAL_SEPARATOR . $message->{msgid_plural}
127 71 ▶ : q{}
Branch: 2 branches
BranchTrueFalseTotal
exists $message->{'msgctxt'} && defined $message->{'msgctxt'} && length $message->{'msgctxt'} ? : 24 47 71
exists $message->{'msgid_plural'} && defined $message->{'msgid_plural'} && length $message->{'msgid_plural'} ? : 33 38 71
Condition66%8/12
exists $message->{"msgctxt"} and defined $message->{"msgctxt"} and length $message->{"msgctxt"}
!ll&&!rl&&r
47024
exists $message->{"msgctxt"} and defined $message->{"msgctxt"}
!ll&&!rl&&r
47024
exists $message->{"msgid_plural"} and defined $message->{"msgid_plural"} and length $message->{"msgid_plural"}
!ll&&!rl&&r
38033
exists $message->{"msgid_plural"} and defined $message->{"msgid_plural"}
!ll&&!rl&&r
38033
MC/DC33%1/3
exists $message->{"msgctxt"} and defined $message->{"msgctxt"} and length $message->{"msgctxt"}
exists $message->{"msgctxt"} defined $message->{"msgctxt"} length $message->{"msgctxt"}
MC/DC33%1/3
exists $message->{"msgid_plural"} and defined $message->{"msgid_plural"} and length $message->{"msgid_plural"}
exists $message->{"msgid_plural"} defined $message->{"msgid_plural"} length $message->{"msgid_plural"}
Truth table50%2/4
A and B and C
A= exists $message->{"msgctxt"}B= defined $message->{"msgctxt"}C= length $message->{"msgctxt"}
ABCresult
0XX0
10X0
1100
1111
Truth table50%2/4
A and B and C
A= exists $message->{"msgid_plural"}B= defined $message->{"msgid_plural"}C= length $message->{"msgid_plural"}
ABCresult
0XX0
10X0
1100
1111
128 ),
129 );
130
131 # translation
132 $msgstr = $self->_encode_and_replace_newline(
133 length $msgstr
134 ? $msgstr
135 : join
136 $PLURAL_SEPARATOR,
137 map {
138 69 ▶ defined $_ ? $_ : q{}
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 69 0 69
139 71 ▶ } @{ $message->{msgstr_plural} || [] }
Branch: 1 branch
BranchTrueFalseTotal
length $msgstr ? : 38 33 71
Condition50%1/2
$message->{'msgstr_plural'} || []
l!l
330
MC/DC0%0/1
$message->{'msgstr_plural'} || []
$message->{'msgstr_plural'}
Truth table50%1/2
A
A= $message->{'msgstr_plural'}
Aresult
11
00
140 );
141
142 return {
143 71 msgid => $msgid,
144 msgstr => $msgstr,
145 };
146 }
147
148 48 ▶sub _unpack_message {
Subroutine _unpack_message: called CC=6 SCAR=17.9
149 48 my ($self, $message) = @_;
150
151 my ($msgid, $msgstr) = map {
152 ( defined && length )
153 96 ▶ ? $self->_decode_and_replace_newline($_)
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && length $_ ? : 88 8 96
Condition66%2/3
defined $_ and length $_
!ll&&!rl&&r
0888
MC/DC50%1/2
defined $_ and length $_
defined $_ length $_
Truth table66%2/3
A and B
A= defined $_B= length $_
ABresult
0X0
100
111
154 : q{};
155 48 } @{$message}{qw(msgid msgstr)};
156
157 # return value
158 48 my %message;
159
160 # split original
161 48 my @strings = split m{ \Q$CONTEXT_SEPARATOR\E }xms, $msgid;
162 48 ▶ if ( @strings > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@strings > 1) 16 32 48
163 16 ( $message{msgctxt}, $msgid ) = @strings;
164 }
165 48 my @plurals = split m{ \Q$PLURAL_SEPARATOR\E }xms, $msgid;
166 48 my $is_plural = @plurals > 1;
167 48 ▶ if ( $is_plural ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_plural) { } 22 26 48
168 22 @message{qw(msgid msgid_plural)} = @plurals;
169 }
170 else {
171 26 $message{msgid} = $msgid;
172 }
173
174 # split translation
175 48 @plurals = split m{ \Q$PLURAL_SEPARATOR\E }xms, $msgstr, -1; ## no critic (MagicNumbers)
176 48 ▶ if ( $is_plural ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_plural) { } 22 26 48
177 22 $message{msgstr_plural} = \@plurals;
178 }
179 else {
180 26 $message{msgstr} = $plurals[0];
181 }
182
183 48 return \%message;
184 }
185
186 15 ▶before 'write_file' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
187 15 my $self = shift;
188
189 15 my $index = 0;
190 137 ▶ my $chars_callback = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
191 137 my $string = shift;
192 137 ▶ STRING: for ( ref $string ? @{$string} : $string ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $string ? : 23 114 137
193 defined
194 161 ▶ or next STRING;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or next STRING 161 0 161
195 161 ▶ m{ \Q$CONTEXT_SEPARATOR\E | \Q$PLURAL_SEPARATOR\E }xmso
Branch: 1 branch
BranchTrueFalseTotal
/ \Q$Locale::MO::File::CONTEXT_SEPARATOR\E | \Q$Locale::MO::File::PLURAL_SEPARATOR\E /mosx and return 2 159 161
196 and return;
197 }
198 135 return 1;
199 15 };
200 15 for my $message ( @{ $self->get_messages } ) {
201 validate_with(
202 params => (
203 ref $message eq 'HASH'
204 ? $message
205 : confess "messages[$index] is not a hash reference"
206 ),
207 spec => {
208 msgctxt => {
209 type => SCALAR,
210 optional => 1,
211 callbacks => {
212 'no control chars' => $chars_callback,
213 },
214 },
215 msgid => {
216 type => SCALAR,
217 optional => 1,
218 callbacks => {
219 'no control chars' => $chars_callback,
220 },
221 },
222 msgid_plural => {
223 type => SCALAR,
224 optional => 1,
225 callbacks => {
226 'no control chars' => $chars_callback,
227 },
228 },
229 msgstr => {
230 type => SCALAR,
231 optional => 1,
232 callbacks => {
233 'no control chars' => $chars_callback,
234 },
235 },
236 msgstr_plural => {
237 type => ARRAYREF,
238 optional => 1,
239 callbacks => {
240 24 ▶ 'msgstr not set' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
241 return ! (
242 exists $message->{msgstr_plural}
243 && exists $message->{msgstr}
244 24 ▶ );
Condition66%2/3
exists $message->{'msgstr_plural'} && exists $message->{'msgstr'}
!ll&&!rl&&r
0231
MC/DC50%1/2
exists $message->{'msgstr_plural'} && exists $message->{'msgstr'}
exists $message->{'msgstr_plural'} exists $message->{'msgstr'}
Truth table66%2/3
A && B
A= exists $message->{'msgstr_plural'}B= exists $message->{'msgstr'}
ABresult
0X0
100
111
245 },
246 52 ▶ 'no control chars' => $chars_callback,
Branch: 1 branch
BranchTrueFalseTotal
ref $message eq 'HASH' ? : 51 1 52
247 },
248 },
249 },
250 called => "messages[$index]",
251 );
252 48 ++$index;
253 }
254
255 11 return $self;
256 8 };
257
258 11 ▶sub write_file {
Subroutine write_file: called CC=10 SCAR=23.1
Pod documented
259 11 my $self = shift;
260
261 my $messages = [
262 sort {
263 77 $a->{msgid} cmp $b->{msgid};
264 }
265 map {
266 48 $self->_pack_message($_);
267 11 } @{ $self->get_messages }
268 ];
269
270 11 my $number_of_strings = @{$messages};
271
272 # Set the byte order of the MO file creator
273 11 ▶ my $template = $self->is_big_endian ? q{N} : q{V};
Branch: 1 branch
BranchTrueFalseTotal
$self->is_big_endian ? : 5 6 11
274
275 11 my $maps = q{};
276 11 my $strings = q{};
277 11 my $current_offset
278 = $MAPS_OFFSET
279 # length of map
280 + $INTEGER_LENGTH * 4 * $number_of_strings; ## no critic (MagicNumbers)
281 11 for my $key (qw(msgid msgstr)) {
282 22 for my $message ( @{$messages} ) {
283 96 my $string = $message->{$key};
284 96 my $length = length $string;
285 96 my $map = pack $template x 2, $length, $current_offset;
286 96 $maps .= $map;
287 96 $string .= $PLURAL_SEPARATOR;
288 96 $strings .= $string;
289 96 $current_offset += length $string;
290 }
291 }
292
293 11 my $offset_original
294 = $MAPS_OFFSET;
295 11 my $offset_translated
296 = $MAPS_OFFSET
297 + $INTEGER_LENGTH * 2 * $number_of_strings;
298 11 my $content
299 = (
300 pack $template x 7, ## no critic (MagicNumbers)
301 $MAGIC_NUMBER,
302 0, # revision
303 $number_of_strings,
304 $offset_original,
305 $offset_translated,
306 0, # hash size
307 0, # hash offset
308 )
309 . $maps
310 . $strings;
311
312 11 my $filename = $self->get_filename;
313 11 ▶ defined $filename
Branch: 1 branch
BranchTrueFalseTotal
defined $filename or confess("Filename not set") 9 2 11
314 or confess 'Filename not set';
315 9 ▶ my $file_handle
Condition60%3/5
$self->get_file_handle || 'IO::File'->new($filename, '> :raw') || confess("Can not open mo file $filename $Locale::MO::File::OS_ERROR")
l!l
90
$self->get_file_handle or "IO::File"->new($filename, "> :raw")
l!l&&r!l&&!r
450
MC/DC0%0/2
$self->get_file_handle || 'IO::File'->new($filename, '> :raw') || confess("Can not open mo file $filename $Locale::MO::File::OS_ERROR")
$self->get_file_handle "IO::File"->new($filename, "> :raw")
Truth table66%2/3
A or B
A= $self->get_file_handleB= "IO::File"->new($filename, "> :raw")
ABresult
1X1
011
000
316 = $self->get_file_handle
317 || IO::File->new($filename, '> :raw')
318 || confess "Can not open mo file $filename $OS_ERROR";
319 9 ▶ $file_handle->print($content)
Branch: 1 branch
BranchTrueFalseTotal
$file_handle->print($content) or confess("Can not write mo file $filename $Locale::MO::File::OS_ERROR") 9 0 9
320 or confess "Can not write mo file $filename $OS_ERROR";
321 9 ▶ if ( ! $self->get_file_handle ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_file_handle) 5 4 9
322 5 ▶ $file_handle->close
Branch: 1 branch
BranchTrueFalseTotal
$file_handle->close or confess("Can not close mo file $filename $Locale::MO::File::OS_ERROR") 5 0 5
323 or confess "Can not close mo file $filename $OS_ERROR";
324 }
325
326 9 return $self;
327 }
328
329 9 ▶sub read_file {
Subroutine read_file: called CC=10 SCAR=23.3
Pod documented
330 9 my $self = shift;
331
332 9 my $filename = $self->get_filename;
333 9 ▶ defined $filename
Branch: 1 branch
BranchTrueFalseTotal
defined $filename or confess("filename not set") 9 0 9
334 or confess 'filename not set';
335 9 ▶ my $file_handle
Condition60%3/5
$self->get_file_handle || 'IO::File'->new($filename, '< :raw') || confess("Can not open mo file $filename $Locale::MO::File::OS_ERROR")
l!l
90
$self->get_file_handle or "IO::File"->new($filename, "< :raw")
l!l&&r!l&&!r
450
MC/DC0%0/2
$self->get_file_handle || 'IO::File'->new($filename, '< :raw') || confess("Can not open mo file $filename $Locale::MO::File::OS_ERROR")
$self->get_file_handle "IO::File"->new($filename, "< :raw")
Truth table66%2/3
A or B
A= $self->get_file_handleB= "IO::File"->new($filename, "< :raw")
ABresult
1X1
011
000
336 = $self->get_file_handle
337 || IO::File->new($filename, '< :raw')
338 || confess "Can not open mo file $filename $OS_ERROR";
339 9 my $content = do {
340 9 local $INPUT_RECORD_SEPARATOR = ();
341 9 <$file_handle>;
342 };
343 9 ▶ if ( ! $self->get_file_handle ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_file_handle) 5 4 9
344 5 $file_handle->close;
345 }
346
347 # Find the byte order of the MO file creator
348 9 my $magic_number = substr $content, 0, $INTEGER_LENGTH;
349 9 ▶ my $template =
Branch: 2 branches
BranchTrueFalseTotal
$magic_number eq pack('V', $MAGIC_NUMBER) ? : 4 5 9
$magic_number eq pack('N', $MAGIC_NUMBER) ? : 5 0 5
350 ( $magic_number eq pack 'V', $MAGIC_NUMBER )
351 # Little endian
352 ? q{V}
353 : ( $magic_number eq pack 'N', $MAGIC_NUMBER )
354 # Big endian
355 ? q{N}
356 # Wrong magic number. Not a valid MO file.
357 : confess "MO file expected: $filename";
358
359 9 my ($revision, $number_of_strings, $offset_original, $offset_translated)
360 = unpack
361 $template x 4, ## no critic (MagicNumbers)
362 substr
363 $content,
364 $REVISION_OFFSET,
365 $INTEGER_LENGTH * 4; ## no critic (MagicNumbers)
366 9 ▶ $revision > 0
Branch: 1 branch
BranchTrueFalseTotal
$revision > 0 and confess("Revision > 0 is unknown: $revision") 0 9 9
367 and confess "Revision > 0 is unknown: $revision";
368
369 9 $self->set_messages(\my @messages);
370 9 for my $index (0 .. $number_of_strings - 1) {
371 48 my $key = 'msgid';
372 48 my $message;
373 48 for my $offset ($offset_original, $offset_translated) {
374 96 my ($string_length, $string_offset)
375 = unpack
376 $template x 2,
377 substr
378 $content,
379 $offset + $index * $INTEGER_LENGTH * 2,
380 $INTEGER_LENGTH * 2;
381 96 $message->{$key}
382 = substr $content, $string_offset, $string_length;
383 96 $key = 'msgstr';
384 }
385 48 $messages[$index] = $self->_unpack_message($message);
386 }
387
388 9 return $self;
389 }
390
391 8 __PACKAGE__->meta->make_immutable;
392
393 8 1;
394
395 __END__
↑ Summary