Localizer/Style/Gettext.pm

filter: Statementstmt 100% 67 / 67 Branchbran 96.4% 27 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 2 totaltotal 97.2% 104 / 107 CC 16 SCAR 21.1
CC
16
Cov
99%
_compile
16.0
BEGIN
1.0
BEGIN
1.0
CRAP 8.3
« Localizer/Style/Maketext.pm ↑ Summary Localizer/BuiltinFunctions.pm »
1 package Localizer::Style::Gettext;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use 5.010_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
11  
12 25 ▶sub compile {
Subroutine compile: called CC=1 SCAR=0.0
Pod undocumented
13 26 my ($self, $msgid, $fmt, $functions) = @_;
14 26 my $code = $self->_compile($msgid, $fmt, $functions);
15 25 return $code;
16 }
17  
18 25 ▶sub _compile {
Subroutine _compile: called CC=16 SCAR=27.7
19 25 my ($self, $msgid, $str, $functions) = @_;
20  
21 25 ▶ return \$str unless $str =~ /%/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /%/ 12 13 25
22  
23 13 my @code;
24 my @bind;
25 13 ▶ while ($str =~ m/
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ / (.*?) (?: ([\\%]%) | %(?: ([A-Za-z#*]\w*)\(([^\)]*)\) | ([1-9]\d*|\*) ) | $ ) /gsx) 34 12 46
26 (.*?)
27 (?:
28 ([\\%]%)
29 |
30 %(?:
31 ([A-Za-z#*]\w*)\(([^\)]*)\)
32 |
33 ([1-9]\d*|\*)
34 )
35 |
36 $
37 )
38 /gsx
39 ) {
40 34 ▶ if ($1) { # Raw string
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 15 19 34
41 15 my $text = $1;
42 15 ▶ if ($text !~ m/[^\x20-\x7E]/s) { # ASCII very safe chars
Branch: 1 branch
BranchTrueFalseTotal
if (not $text =~ /[^\x20-\x7E]/s) { } 12 3 15
43 12 $text =~ s/\\/\\\\/g;
44 12 push @code, B::perlstring($text) . ',';
45 } else {
46 # For example, `(eval "sub { qq{% usar\x{e1}n}}")->()` drops UTF-8 flag.
47 # This code is the workaround for this issue.
48 3 push @code, sprintf(q{$bind[%d],}, 0+@bind);
49 3 push @bind, $text;
50 }
51 }
52 34 ▶ if ($2) { # \% %%
Branch: 3 branches
BranchTrueFalseTotal
if ($2) { } 2 32 34
elsif ($3) { } 6 26 32
elsif ($5) { } 9 17 26
53 2 my $text = $2;
54 2 $text =~ s/\\/\\\\\\\\/g;
55 2 push @code, "'" . $text . "',";
56 }
57 elsif ($3) {
58 6 my $function_name = $3;
59 6 ▶ if ($function_name eq '*') {
Branch: 2 branches
BranchTrueFalseTotal
if ($function_name eq '*') { } 1 5 6
elsif ($function_name eq '#') { } 1 4 5
60 1 $function_name = 'quant';
61 }
62 elsif ($function_name eq '#') {
63 1 $function_name = 'numf';
64 }
65  
66 6 ▶ unless (exists $functions->{$function_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $functions->{$function_name}) 1 5 6
67 1 Carp::confess("Language resource compilation error. Unknown function: '${function_name}'");
68 }
69  
70 5 my $code = q!$functions->{'! . $function_name . q!'}->(!;
71 5 for my $arg (split(/,/, $4)) {
72 7 ▶ if (my ($num) = $arg =~ /%(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($num) = $arg =~ /%(\d+)/) { } 5 2 7
73 5 $code .= '$_[' . $num . '], ';
74 }
75 else {
76 2 $code .= "'" . $arg . "', ";
77 }
78 }
79 5 $code .= '), ';
80 5 push @code, $code;
81 }
82 elsif ($5) {
83 9 my $arg = $5;
84  
85 9 my $var = '';
86 9 ▶ if ($arg eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq '*') { } 2 7 9
87 2 $var = '@_[1 .. $#_],';
88 }
89 else {
90 7 $var = '$_[' . $arg . '],';
91 }
92 9 push @code, $var;
93 }
94 }
95  
96 12 ▶ if (@code > 1) { # most cases, presumably!
Branch: 1 branch
BranchTrueFalseTotal
if (@code > 1) 10 2 12
97 10 unshift @code, "join '',\n";
98 }
99 12 unshift @code, qq!#line 1 "${msgid}"\n!;
100 12 unshift @code, "use strict; sub {\n";
101 12 push @code, "}\n";
102  
103 4 ▶ my $sub = eval(join '', @code); ## no critic.
Subroutine BEGIN: called CC=1 SCAR=0.0
104 12 ▶ die "Language resource compilation error: $@ while evalling" . join('', @code) if $@; # Should be impossible.
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
105 12 return $sub;
106 }
107  
108 5 1;
109 __END__
« Localizer/Style/Maketext.pm ↑ Summary Localizer/BuiltinFunctions.pm »