| 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
| |||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 13 | my @code; | |||||||||||||||||
| 24 | my @bind; | ||||||||||||||||||
| 25 | 13 | ▶ | while ($str =~ m/ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 41 | 15 | my $text = $1; | |||||||||||||||||
| 42 | 15 | ▶ | if ($text !~ m/[^\x20-\x7E]/s) { # ASCII very safe chars | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 105 | 12 | return $sub; | |||||||||||||||||
| 106 | } | ||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | 5 | 1; | |||||||||||||||||
| 109 | __END__ | ||||||||||||||||||