Localizer/Style/Maketext.pm

filter: Statementstmt 78.6% 103 / 131 Branchbran 57.8% 44 / 76 Conditioncond 52.3% 11 / 21 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 67.8% 171 / 252 CC 48 SCAR 41.3
CC
48
Cov
69%
_compile
89.8
_die_pointing
14.0
BEGIN
1.0
CRAP 62.4
↑ Summary Localizer/Style/Gettext.pm »
1 package Localizer::Style::Maketext;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use 5.010_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub DEBUG () { 0 }
8  
9 4 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
10  
11 17 ▶sub compile {
Subroutine compile: called CC=1 SCAR=0.0
Pod undocumented
12 17 my ($self, $msgid, $fmt, $functions) = @_;
13 17 my $code = $self->_compile($msgid, $fmt, $functions);
14 16 return $code;
15 }
16  
17 # Based on Locale::Maketext::_compile
18 17 ▶sub _compile {
Subroutine _compile: called CC=41 SCAR=45.0
19 # This big scary routine compiles an entry.
20 # It returns either a coderef if there's brackety bits in this, or
21 # otherwise a ref to a scalar.
22  
23 17 my $msgid = $_[1];
24 17 my $string_to_compile = $_[2]; # There are taint issues using regex on @_ - perlbug 60378,27344
25 17 my $functions = $_[3];
26  
27 # The while() regex is more expensive than this check on strings that don't need a compile.
28 # this op causes a ~2% speed hit for strings that need compile and a 250% speed improvement
29 # on strings that don't need compiling.
30 17 ▶ return \"$string_to_compile" if($string_to_compile !~ m/[\[~\]]/ms); # return a string ref if chars [~] are not in the string
Branch: 1 branch
BranchTrueFalseTotal
unless $string_to_compile =~ /[\[~\]]/ms 7 10 17
31  
32 10 ▶ my $target = ref($_[0]) || $_[0];
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
1000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
33  
34 10 my(@code);
35 10 my(@c) = (''); # "chunks" -- scratch.
36 10 my $call_count = 0;
37 10 my $big_pile = '';
38 {
39 10 my $in_group = 0; # start out outside a group
40 10 my($m, @params); # scratch
41  
42 10 ▶ while($string_to_compile =~ # Iterate over chunks.
Branch: 1 branch
BranchTrueFalseTotal
if ($string_to_compile =~ /( [^\~\[\]]+ # non-~[] stuff (Capture everything else here) | ~. # ~[, ~], ~~, ~other | \[ # [ presumably opening a group | \] # ] presumably closing a group | ~ # terminal ~ ? | $ )/gsx) 60 9 69
43 m/(
44 [^\~\[\]]+ # non-~[] stuff (Capture everything else here)
45 |
46 ~. # ~[, ~], ~~, ~other
47 |
48 \[ # [ presumably opening a group
49 |
50 \] # ] presumably closing a group
51 |
52 ~ # terminal ~ ?
53 |
54 $
55 )/xgs
56 ) {
57 60 DEBUG>2 and warn qq{ "$1"\n};
58  
59 60 ▶ if($1 eq '[' or $1 eq '') { # "[" or end
Branch: 8 branches
BranchTrueFalseTotal
if ($1 eq '[' or $1 eq '') { } 22 38 60
elsif ($1 eq ']') { } 14 24 38
elsif (substr($1, 0, 1) ne '~') { } 24 0 24
elsif ($1 eq '~~') { } 0 0 0
elsif ($1 eq '~[') { } 0 0 0
elsif ($1 eq '~]') { } 0 0 0
elsif ($1 eq '~,') { } 0 0 0
elsif ($1 eq '~') { } 0 0 0
Condition100%3/3
$1 eq "[" or $1 eq ""
l!l&&r!l&&!r
13938
MC/DC100%2/2
$1 eq "[" or $1 eq ""
$1 eq "[" $1 eq ""
Truth table100%3/3
A or B
A= $1 eq "["B= $1 eq ""
ABresult
1X1
011
000
60 # Whether this is "[" or end, force processing of any
61 # preceding literal.
62 22 ▶ if($in_group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_group) { } 0 22 22
63 0 ▶ if($1 eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '') { } 0 0 0
64 0 $target->_die_pointing($string_to_compile, 'Unterminated bracket group');
65 }
66 else {
67 0 $target->_die_pointing($string_to_compile, 'You can\'t nest bracket groups');
68 }
69 }
70 else {
71 22 ▶ if ($1 eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '') { } 9 13 22
72 9 DEBUG>2 and warn " [end-string]\n";
73 }
74 else {
75 13 $in_group = 1;
76 }
77 22 ▶ die "How come \@c is empty?? in <$string_to_compile>" unless @c; # sanity
Branch: 1 branch
BranchTrueFalseTotal
unless @c 0 22 22
78 22 ▶ if(length $c[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $c[-1]) 10 12 22
79 # Now actually processing the preceding literal
80 10 $big_pile .= $c[-1];
81 10 ▶ if ($c[-1] !~ m/[^\x20-\x7E]/s) { # ASCII very safe chars
Branch: 1 branch
BranchTrueFalseTotal
if (not $c[-1] =~ /[^\x20-\x7E]/s) { } 7 3 10
82 # normal case -- all very safe chars
83 7 $c[-1] =~ s/'/\\'/g;
84 7 push @code, q{ '} . $c[-1] . "',\n";
85 7 $c[-1] = ''; # reuse this slot
86 }
87 else {
88 3 $c[-1] =~ s/\\\\/\\/g;
89 3 push @code, ' $c[' . $#c . "],\n";
90 3 push @c, ''; # new chunk
91 }
92 }
93 # else just ignore the empty string.
94 }
95  
96 }
97 elsif($1 eq ']') { # "]"
98 # close group -- go back in-band
99 14 ▶ if($in_group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_group) { } 13 1 14
100 13 $in_group = 0;
101  
102 13 DEBUG>2 and warn " --Closing group [$c[-1]]\n";
103  
104 # And now process the group...
105  
106 13 ▶ if(!length($c[-1]) or $c[-1] =~ m/^\s+$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $c[-1] or $c[-1] =~ /^\s+$/s) 0 13 13
Condition33%1/3
not length $c[-1] or $c[-1] =~ /^\s+$/s
l!l&&r!l&&!r
0013
MC/DC0%0/2
not length $c[-1] or $c[-1] =~ /^\s+$/s
not length $c[-1] $c[-1] =~ /^\s+$/s
Truth table33%1/3
not(A) or B
A= not length $c[-1]B= $c[-1] =~ /^\s+$/s
ABresult
1X1
011
000
107 0 DEBUG>2 and warn " -- (Ignoring)\n";
108 0 $c[-1] = ''; # reset out chink
109 0 next;
110 }
111  
112 #$c[-1] =~ s/^\s+//s;
113 #$c[-1] =~ s/\s+$//s;
114 13 ($m,@params) = split(/,/, $c[-1], -1); # was /\s*,\s*/
115  
116 # A bit of a hack -- we've turned "~,"'s into DELs, so turn
117 # 'em into real commas here.
118 13 foreach($m, @params) { tr/\x7F/,/ }
119  
120 # Special-case handling of some method names:
121 13 ▶ if($m eq '_*' or $m =~ m/^_(-?\d+)$/s) {
Branch: 3 branches
BranchTrueFalseTotal
if ($m eq '_*' or $m =~ /^_(-?\d+)$/s) { } 9 4 13
elsif ($m eq '*') { } 1 3 4
elsif ($m eq '#') { } 1 2 3
Condition100%3/3
$m eq "_*" or $m =~ /^_(-?\d+)$/s
l!l&&r!l&&!r
274
MC/DC100%2/2
$m eq "_*" or $m =~ /^_(-?\d+)$/s
$m eq "_*" $m =~ /^_(-?\d+)$/s
Truth table100%3/3
A or B
A= $m eq "_*"B= $m =~ /^_(-?\d+)$/s
ABresult
1X1
011
000
122 # Treat [_1,...] as [,_1,...], etc.
123 9 unshift @params, $m;
124 9 $m = '';
125 }
126 elsif($m eq '*') {
127 1 $m = 'quant'; # "*" for "times": "4 cars" is 4 times "cars"
128 }
129 elsif($m eq '#') {
130 1 $m = 'numf'; # "#" for "number": [#,_1] for "the number _1"
131 }
132  
133 # Most common case: a simple, legal-looking method name
134 13 ▶ if($m eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($m eq '') { } 9 4 13
elsif ($m =~ /^\w+$/s) { } 4 0 4
135 # 0-length method name means to just interpolate:
136 9 push @code, ' (';
137 }
138 elsif($m =~ /^\w+$/s
139 # exclude anything fancy, especially fully-qualified module names
140 ) {
141 4 ▶ unless (exists $functions->{$m}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $functions->{$m}) 0 4 4
142 0 Carp::confess("Language resource compilation error. Unknown function: '${m}'");
143 }
144 4 push @code, ' $functions->{"' . $m . '"}->(';
145 }
146 else {
147 # TODO: implement something? or just too icky to consider?
148 0 $target->_die_pointing(
149 $string_to_compile,
150 "Can't use \"$m\" as a method name in bracket group",
151 2 + length($c[-1])
152 );
153 }
154  
155 13 pop @c; # we don't need that chunk anymore
156 13 ++$call_count;
157  
158 13 foreach my $p (@params) {
159 16 ▶ if($p eq '_*') {
Branch: 3 branches
BranchTrueFalseTotal
if ($p eq '_*') { } 2 14 16
elsif ($p =~ /^_(-?\d+)$/s) { } 10 4 14
elsif (not $p =~ /[^\x20-\x7E]/s) { } 4 0 4
160 # Meaning: all parameters except $_[0]
161 2 $code[-1] .= ' @_[1 .. $#_], ';
162 # and yes, that does the right thing for all @_ < 3
163 }
164 elsif($p =~ m/^_(-?\d+)$/s) {
165 # _3 meaning $_[3]
166 10 $code[-1] .= '$_[' . (0 + $1) . '], ';
167 }
168 elsif($p !~ m/[^\x20-\x7E]/s) { # ASCII very safe chars
169 # Normal case: a literal containing only safe characters
170 4 $p =~ s/'/\\'/g;
171 4 $code[-1] .= q{'} . $p . q{', };
172 }
173 else {
174 # Stow it on the chunk-stack, and just refer to that.
175 0 push @c, $p;
176 0 push @code, ' $c[' . $#c . '], ';
177 }
178 }
179 13 $code[-1] .= "),\n";
180  
181 13 push @c, '';
182 }
183 else {
184 1 $target->_die_pointing($string_to_compile, q{Unbalanced ']'});
185 }
186  
187 }
188 elsif(substr($1,0,1) ne '~') {
189 # it's stuff not containing "~" or "[" or "]"
190 # i.e., a literal blob
191 24 my $text = $1;
192 24 $text =~ s/\\/\\\\/g;
193 24 $c[-1] .= $text;
194  
195 }
196 elsif($1 eq '~~') { # "~~"
197 0 $c[-1] .= '~';
198  
199 }
200 elsif($1 eq '~[') { # "~["
201 0 $c[-1] .= '[';
202  
203 }
204 elsif($1 eq '~]') { # "~]"
205 0 $c[-1] .= ']';
206  
207 }
208 elsif($1 eq '~,') { # "~,"
209 0 ▶ if($in_group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_group) { } 0 0 0
210 # This is a hack, based on the assumption that no-one will actually
211 # want a DEL inside a bracket group. Let's hope that's it's true.
212 0 $c[-1] .= "\x7F";
213 }
214 else {
215 0 $c[-1] .= '~,';
216 }
217  
218 }
219 elsif($1 eq '~') { # possible only at string-end, it seems.
220 0 $c[-1] .= '~';
221  
222 }
223 else {
224 # It's a "~X" where X is not a special character.
225 # Consider it a literal ~ and X.
226 0 my $text = $1;
227 0 $text =~ s/\\/\\\\/g;
228 0 $c[-1] .= $text;
229 }
230 }
231 }
232  
233 9 ▶ if($call_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call_count) { } 9 0 9
234 9 undef $big_pile; # Well, nevermind that.
235 }
236 else {
237 # It's all literals! Ahwell, that can happen.
238 # So don't bother with the eval. Return a SCALAR reference.
239 0 return \$big_pile;
240 }
241  
242 9 ▶ die q{Last chunk isn't null??} if @c and length $c[-1]; # sanity
Branch: 1 branch
BranchTrueFalseTotal
if @c and length $c[-1] 0 9 9
Condition33%1/3
@c and length $c[-1]
!ll&&!rl&&r
090
MC/DC0%0/2
@c and length $c[-1]
@c length $c[-1]
Truth table33%1/3
A and B
A= @cB= length $c[-1]
ABresult
0X0
100
111
243 9 DEBUG and warn scalar(@c), " chunks under closure\n";
244 9 ▶ if(@code == 0) { # not possible?
Branch: 2 branches
BranchTrueFalseTotal
if (@code == 0) { } 0 9 9
elsif (@code > 1) { } 9 0 9
245 0 DEBUG and warn "Empty code\n";
246 0 return \'';
247 }
248 elsif(@code > 1) { # most cases, presumably!
249 9 unshift @code, "join '',\n";
250 }
251 9 unshift @code, qq!#line 1 "${msgid}"\n!;
252 9 unshift @code, "use strict; sub {\n";
253 9 push @code, "}\n";
254  
255 9 DEBUG and warn @code;
256 4 ▶ my $sub = eval(join '', @code); ## no critic.
Subroutine BEGIN: called CC=1 SCAR=0.0
257 9 ▶ die "Language resource compilation error: $@ while evalling" . join('', @code) if $@; # Should be impossible.
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
258 9 return $sub;
259 }
260  
261 #--------------------------------------------------------------------------
262  
263 1 ▶sub _die_pointing {
Subroutine _die_pointing: called CC=8 SCAR=26.4
264 # This is used by _compile to throw a fatal error
265 1 my $target = shift; # class name
266 # ...leaving $_[0] the error-causing text, and $_[1] the error message
267  
268 1 my $i = index($_[0], "\n");
269  
270 1 my $pointy;
271 1 ▶ my $pos = pos($_[0]) - (defined($_[2]) ? $_[2] : 0) - 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 1 1
272 1 ▶ if($pos < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 1) { } 0 1 1
273 0 $pointy = "^=== near there\n";
274 }
275 else { # we need to space over
276 1 my $first_tab = index($_[0], "\t");
277 1 ▶ if($pos > 2 and ( -1 == $first_tab or $first_tab > pos($_[0]))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > 2 and -1 == $first_tab || $first_tab > pos $_[0]) { } 1 0 1
Condition33%2/6
$pos > 2 and -1 == $first_tab || $first_tab > pos $_[0]
!ll&&!rl&&r
001
-1 == $first_tab or $first_tab > pos $_[0]
l!l&&r!l&&!r
100
MC/DC0%0/3
$pos > 2 and -1 == $first_tab || $first_tab > pos $_[0]
$pos > 2 -1 == $first_tab $first_tab > pos $_[0]
Truth table25%1/4
A and B or C
A= $pos > 2B= -1 == $first_tabC= $first_tab > pos $_[0]
ABCresult
0XX0
1000
11X1
1011
278 # No tabs, or the first tab is harmlessly after where we will point to,
279 # AND we're far enough from the margin that we can draw a proper arrow.
280 1 $pointy = ('=' x $pos) . "^ near there\n";
281 }
282 else {
283 # tabs screw everything up!
284 0 $pointy = substr($_[0],0,$pos);
285 0 $pointy =~ tr/\t //cd;
286 # make everything into whitespace, but preserving tabs
287 0 $pointy .= "^=== near there\n";
288 }
289 }
290  
291 1 my $errmsg = "$_[1], in\:\n$_[0]";
292  
293 1 ▶ if($i == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i == -1) { } 1 0 1
elsif ($i == length($_[0]) - 1) { } 0 0 0
294 # No newline.
295 1 $errmsg .= "\n" . $pointy;
296 }
297 elsif($i == (length($_[0]) - 1) ) {
298 # Already has a newline at end.
299 0 $errmsg .= $pointy;
300 }
301 else {
302 # don't bother with the pointy bit, I guess.
303 }
304 1 Carp::croak( "$errmsg via $target, as used" );
305 }
306  
307 3 1;
308 __END__
↑ Summary Localizer/Style/Gettext.pm »