blib/lib/CLDR/Number/Role/Format.pm

filter: Statementstmt 98.7% 161 / 163 Branchbran 85.9% 67 / 78 Conditioncond 50.0% 17 / 34 MC/DCmcdc 14.2% 3 / 21 Subroutinesub 100% 24 / 24 Podpod 0.0% 0 / 2 totaltotal 84.4% 272 / 322 CC 54 SCAR 22.3
CC
54
Cov
89%
_format_number
19.0
_trigger_pattern
15.0
__ANON__
4.1
CRAP 9.3
↑ Summary Build »
1 package CLDR::Number::Role::Format;
2  
3 12 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Math::BigFloat;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Math::Round;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # This role does not have a publicly supported interface and may change in
12 # backward incompatible ways in the future. Please use one of the documented
13 # classes instead.
14  
15 12 our $VERSION = '0.00_03';
16  
17 12 requires qw( BUILD format );
18  
19 12 with qw( CLDR::Number::Role::Base );
20  
21 12 my $number_pattern_re = qr{ (?: \* \X )? [@#0-9,.]+ }x;
22  
23 has pattern => (
24 is => 'rw',
25 isa => sub {
26 140 ▶ croak "pattern is not defined" if !defined $_[0];
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] 0 140 140
27 },
28 12 trigger => 1,
29 );
30  
31 has minimum_integer_digits => (
32 is => 'rw',
33 isa => sub {
34 132 ▶ croak "minimum_integer_digits '$_[0]' is invalid"
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not &looks_like_number($_[0]) 0 132 132
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
01320
MC/DC0%0/2
defined $_[0] and not &looks_like_number($_[0])
defined $_[0] not &looks_like_number($_[0])
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not &looks_like_number($_[0])
ABresult
0X0
100
111
35 if defined $_[0] && !looks_like_number $_[0];
36 },
37 12 );
38  
39 has maximum_integer_digits => (
40 is => 'rw',
41 isa => sub {
42 1 ▶ croak "maximum_integer_digits '$_[0]' is invalid"
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not &looks_like_number($_[0]) 0 1 1
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
010
MC/DC0%0/2
defined $_[0] and not &looks_like_number($_[0])
defined $_[0] not &looks_like_number($_[0])
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not &looks_like_number($_[0])
ABresult
0X0
100
111
43 if defined $_[0] && !looks_like_number $_[0];
44 },
45 12 );
46  
47 has minimum_fraction_digits => (
48 is => 'rw',
49 isa => sub {
50 154 ▶ croak "minimum_fraction_digits '$_[0]' is invalid"
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not &looks_like_number($_[0]) 0 154 154
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
01540
MC/DC0%0/2
defined $_[0] and not &looks_like_number($_[0])
defined $_[0] not &looks_like_number($_[0])
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not &looks_like_number($_[0])
ABresult
0X0
100
111
51 if defined $_[0] && !looks_like_number $_[0];
52 },
53 trigger => sub {
54 154 ▶ my ($self, $min) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
55 154 ▶ return unless defined $self->maximum_fraction_digits;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->maximum_fraction_digits 34 120 154
56 120 ▶ return if $min <= $self->maximum_fraction_digits;
Branch: 1 branch
BranchTrueFalseTotal
if $min <= $self->maximum_fraction_digits 114 6 120
57 6 $self->{maximum_fraction_digits} = $min;
58 },
59 12 );
60  
61 has maximum_fraction_digits => (
62 is => 'rw',
63 isa => sub {
64 153 ▶ croak "maximum_fraction_digits '$_[0]' is invalid"
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not &looks_like_number($_[0]) 0 153 153
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
01530
MC/DC0%0/2
defined $_[0] and not &looks_like_number($_[0])
defined $_[0] not &looks_like_number($_[0])
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not &looks_like_number($_[0])
ABresult
0X0
100
111
65 if defined $_[0] && !looks_like_number $_[0];
66 },
67 trigger => sub {
68 153 ▶ my ($self, $max) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
69 153 ▶ return unless defined $self->minimum_fraction_digits;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->minimum_fraction_digits 2 151 153
70 151 ▶ return if $max >= $self->minimum_fraction_digits;
Branch: 1 branch
BranchTrueFalseTotal
if $max >= $self->minimum_fraction_digits 149 2 151
71 2 $self->{minimum_fraction_digits} = $max;
72 },
73 12 );
74  
75 has primary_grouping_size => (
76 is => 'rw',
77 isa => sub {
78 132 ▶ croak "primary_grouping_size '$_[0]' is invalid"
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not &looks_like_number($_[0]) 0 132 132
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
01320
MC/DC0%0/2
defined $_[0] and not &looks_like_number($_[0])
defined $_[0] not &looks_like_number($_[0])
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not &looks_like_number($_[0])
ABresult
0X0
100
111
79 if defined $_[0] && !looks_like_number $_[0];
80 },
81 12 );
82  
83 has secondary_grouping_size => (
84 is => 'rw',
85 isa => sub {
86 132 ▶ croak "secondary_grouping_size '$_[0]' is invalid"
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not &looks_like_number($_[0]) 0 132 132
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
01320
MC/DC0%0/2
defined $_[0] and not &looks_like_number($_[0])
defined $_[0] not &looks_like_number($_[0])
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not &looks_like_number($_[0])
ABresult
0X0
100
111
87 if defined $_[0] && !looks_like_number $_[0];
88 },
89 12 );
90  
91 has rounding_increment => (
92 is => 'rw',
93 isa => sub {
94 152 ▶ croak "rounding_increment '$_[0]' is invalid"
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not &looks_like_number($_[0]) 0 152 152
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
01520
MC/DC0%0/2
defined $_[0] and not &looks_like_number($_[0])
defined $_[0] not &looks_like_number($_[0])
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not &looks_like_number($_[0])
ABresult
0X0
100
111
95 if defined $_[0] && !looks_like_number $_[0];
96 },
97 12 );
98  
99 before BUILD => sub {
100 36 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=2 SCAR=8.1
101  
102 36 ▶ return if $self->_has_init_arg('locale');
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_init_arg("locale") 36 0 36
103  
104 0 $self->_build_pattern;
105 12 };
106  
107 after _trigger_locale => sub {
108 44 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
109  
110 44 $self->_build_pattern;
111 12 };
112  
113 sub _build_pattern {
114 44 ▶ my ($self) = @_;
Subroutine _build_pattern: called CC=1 SCAR=0.0
115  
116 44 $self->_set_unless_init_arg(
117 pattern => $self->_get_data(patterns => $self->_pattern_type)
118 );
119 }
120  
121 sub _trigger_pattern {
122 140 ▶ my ($self, $pattern) = @_;
Subroutine _trigger_pattern: called CC=15 SCAR=27.1
123  
124 PATTERN:
125 140 ▶ while ($pattern =~ m{ \G (?:
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ / \G (?: ( (?: [^'] | '' )+ ) # non-quoted text (incl. escaped quotes) | (?: ^ | (?<! ' ) ) ' # start quote (?: [^'] | '' )+ # quoted text (incl. escaped quotes) (?: ' (?: (?! ' ) | $ ) | $ ) # end quote (optional at end of pattern) ) /gx) 142 8 150
126 ( (?: [^'] | '' )+ ) # non-quoted text (incl. escaped quotes)
127 |
128 (?: ^ | (?<! ' ) ) ' # start quote
129 (?: [^'] | '' )+ # quoted text (incl. escaped quotes)
130 (?: ' (?: (?! ' ) | $ ) | $ ) # end quote (optional at end of pattern)
131 ) }xg) {
132 142 my $subpattern = $1;
133 142 ▶ next PATTERN unless $subpattern;
Branch: 1 branch
BranchTrueFalseTotal
unless $subpattern 10 132 142
134  
135 132 my $subpattern_offset = $LAST_MATCH_START[0];
136 132 my $subpattern_length = $LAST_MATCH_END[0] - $subpattern_offset;
137  
138 132 ▶ if (my ($number_pattern) = $subpattern =~ m{ ( $number_pattern_re ) }x) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($number_pattern) = $subpattern =~ / ( $number_pattern_re ) /x) 132 0 132
139 SUBPATTERN:
140 132 for ($number_pattern) {
141 132 s{ \. $ }{}x; # no trailing decimal sign
142 132 s{ (?: ^ | \# ) (?= \. ) }{0}x; # integer requires at least one minimum digit
143  
144 # calculate grouping sizes
145 132 my ($secondary, $primary) = map { length } m{ , ( [^,]* ) , ( [^,.]* ) (?: \. | $ ) }x;
146 132 ▶ if (!defined $primary) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $primary) { } 100 32 132
elsif ($primary == 0) { } 6 26 32
elsif ($primary == $secondary) { } 4 22 26
147 100 ($primary) = map { length } m{ , ( [^,.]* ) (?: \. | $ ) }x;
148 }
149 elsif ($primary == 0) {
150 6 $primary = $secondary;
151 6 $secondary = undef;
152 }
153 elsif ($primary == $secondary) {
154 4 $secondary = undef;
155 }
156  
157 132 tr{,}{}d; # temporarily remove groups
158  
159 132 ▶ if (!m{ \. }x) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/ \. /x) 69 63 132
160 69 s{ (?: ^ | \# ) $ }{0}x; # integer requires at least one minimum digit
161 }
162  
163 132 ▶ if (!$self->_has_init_arg('minimum_integer_digits')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_init_arg("minimum_integer_digits")) 130 2 132
164 130 my ($min_int) = m{ ( [0-9,]+ ) (?= \. | $ ) }x;
165 130 $self->minimum_integer_digits(length $min_int);
166 }
167  
168 132 ▶ if ($primary) {
Branch: 1 branch
BranchTrueFalseTotal
if ($primary) { } 87 45 132
169 87 s{ (?= .{$primary} (?: \. | $ ) ) }{,}x; # add primary group
170 87 $self->_set_unless_init_arg(primary_grouping_size => $primary);
171  
172 87 ▶ if ($secondary) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secondary) { } 20 67 87
173 20 s{ (?= .{$secondary} , ) }{,}x; # add secondary group
174 20 $self->_set_unless_init_arg(secondary_grouping_size => $secondary);
175 }
176 else {
177 67 $self->_set_unless_init_arg(secondary_grouping_size => 0);
178 }
179 }
180 else {
181 45 $self->_set_unless_init_arg(primary_grouping_size => 0);
182 45 $self->_set_unless_init_arg(secondary_grouping_size => 0);
183 }
184  
185 132 s{ ^ \#+ (?= [#0-9] ) }{}x; # no leading multiple #s
186 132 s{ ^ (?= , ) }{#}x; # leading # before group
187  
188 132 ▶ if (my ($max, $min) = m{ \. ( ( [0-9]* ) \#* ) }x) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = / \. ( ( [0-9]* ) \#* ) /x) { } 63 69 132
189 63 $self->_set_unless_init_arg(minimum_fraction_digits => length $min);
190 63 $self->_set_unless_init_arg(maximum_fraction_digits => length $max);
191 }
192 else {
193 69 $self->_set_unless_init_arg(minimum_fraction_digits => 0);
194 69 $self->_set_unless_init_arg(maximum_fraction_digits => 0);
195 }
196  
197 132 ▶ if (!$self->_has_init_arg('rounding_increment')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_init_arg("rounding_increment")) 130 2 132
198 130 ▶ if (my ($round_inc) = $number_pattern =~ m{ (
Branch: 1 branch
BranchTrueFalseTotal
if (my($round_inc) = $number_pattern =~ / ( (?: [1-9] [0-9,]* | 0 ) # integer (?= \. | $ ) (?: \. [0-9]* [1-9] )? # fraction ) /x) { } 130 0 130
199 (?: [1-9] [0-9,]* | 0 ) # integer
200 (?= \. | $ )
201 (?: \. [0-9]* [1-9] )? # fraction
202 ) }x) {
203 130 $self->rounding_increment($round_inc);
204 }
205 else {
206 0 $self->rounding_increment(0);
207 }
208 }
209 }
210  
211 132 $subpattern =~ s{$number_pattern_re}{$number_pattern};
212 132 substr $pattern, $subpattern_offset, $subpattern_length, $subpattern;
213  
214 132 last PATTERN;
215 }
216 }
217  
218 # hashref instead of attribute method so wo don't retrigger this trigger
219 140 $self->{pattern} = $pattern;
220 }
221  
222 sub _format_number {
223 133 ▶ my ($self, $num) = @_;
Subroutine _format_number: called CC=19 SCAR=29.5
224 133 my $negative = $num < 0;
225  
226 133 ▶ if ($self->rounding_increment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rounding_increment) { } 21 112 133
227 # TODO: round half to even
228 21 $num = Math::Round::nearest($self->rounding_increment, $num);
229 }
230 else {
231 # round half to even
232 112 my $bf = Math::BigFloat->new(abs $num);
233 112 $bf->round_mode('even');
234 112 $bf->ffround(-$self->maximum_fraction_digits);
235 112 $num = $bf->bstr;
236 }
237  
238 133 my ($int, $frac) = split /\./, $num;
239 133 ▶ if (!defined $frac) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $frac) 29 104 133
240 29 $frac = '';
241 }
242  
243 133 ▶ if (my $primary_group = $self->primary_grouping_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $primary_group = $self->primary_grouping_size) 114 19 133
244 114 my $group_sign = $self->group_sign;
245 114 ▶ my $other_groups = $self->secondary_grouping_size || $primary_group;
Condition66%2/3
$self->secondary_grouping_size || $primary_group
l!l&&r!l&&!r
61080
MC/DC0%0/2
$self->secondary_grouping_size || $primary_group
$self->secondary_grouping_size $primary_group
Truth table66%2/3
A || B
A= $self->secondary_grouping_sizeB= $primary_group
ABresult
1X1
011
000
246  
247 114 $int =~ s{ (?<! ^ ) (?= .{$primary_group} $ ) }{$group_sign}x;
248  
249 114 while (1) {
250 130 ▶ last if $int !~ s{
Branch: 1 branch
BranchTrueFalseTotal
unless $int =~ s/ (?<! ^ ) (?<! \Q$group_sign\E ) (?= .{$other_groups} \Q$group_sign\E ) /$group_sign/x 114 16 130
251 (?<! ^ )
252 (?<! \Q$group_sign\E )
253 (?= .{$other_groups} \Q$group_sign\E )
254 }{$group_sign}x;
255 }
256 }
257  
258 133 ▶ my $int_pad = $self->minimum_integer_digits - (length $int || 0);
Condition50%1/2
CORE::length $int || 0
l!l
1330
MC/DC0%0/1
CORE::length $int || 0
CORE::length $int
Truth table50%1/2
A
A= CORE::length $int
Aresult
11
00
259 133 ▶ if ($int_pad > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int_pad > 0) 5 128 133
260 5 $int = 0 x $int_pad . $int;
261 }
262  
263 133 ▶ my $frac_pad = $self->minimum_fraction_digits - (length $frac || 0);
Condition100%2/2
CORE::length $frac || 0
l!l
10429
MC/DC100%1/1
CORE::length $frac || 0
CORE::length $frac
Truth table100%2/2
A
A= CORE::length $frac
Aresult
11
00
264 133 ▶ if ($frac_pad > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($frac_pad > 0) { } 2 131 133
elsif ($frac_pad < 0) { } 77 54 131
265 2 $frac .= 0 x $frac_pad;
266 }
267 elsif ($frac_pad < 0) {
268 77 my $truncate_size = abs $frac_pad;
269 77 $frac =~ s{ 0{1,$truncate_size} $ }{}x;
270 }
271  
272 133 my $num_format = $int;
273  
274 133 ▶ if (length $frac) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $frac) 84 49 133
275 84 $num_format .= $self->decimal_sign . $frac;
276 }
277  
278 133 my $format = $self->pattern;
279  
280 133 ▶ if ($negative) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negative) { } 21 112 133
281 21 my $minus_sign = $self->minus_sign;
282  
283 21 ▶ if ($format =~ s{ ^ .* ; }{}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ s/ ^ .* ; //x) { } 2 19 21
284 2 $format =~ s{-}{$minus_sign};
285 }
286 else {
287 19 $format = $minus_sign . $format;
288 }
289 }
290 else {
291 112 $format =~ s{ ; .* $ }{}x;
292 }
293  
294 133 my $final_format = '';
295 133 my $formatted;
296 # TODO: remove duplicate regex code shared with _trigger_pattern
297 133 ▶ while ($format =~ m{ \G (?:
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ / \G (?: ( (?: [^'] | '' )+ ) # non-quoted text (incl. escaped quotes) | (?: ^ | (?<! ' ) ) ' # start quote ( (?: [^'] | '' )+ ) # quoted text (incl. escaped quotes) (?: ' (?: (?! ' ) | $ ) | $ ) # end quote (optional at end of pattern) ) /gx) 138 133 271
298 ( (?: [^'] | '' )+ ) # non-quoted text (incl. escaped quotes)
299 |
300 (?: ^ | (?<! ' ) ) ' # start quote
301 ( (?: [^'] | '' )+ ) # quoted text (incl. escaped quotes)
302 (?: ' (?: (?! ' ) | $ ) | $ ) # end quote (optional at end of pattern)
303 ) }xg) {
304 138 my $nonquote = $1;
305 138 my $quote = $2;
306  
307 138 ▶ if (!$formatted && defined $nonquote && $nonquote =~ s{$number_pattern_re}{$num_format}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $formatted and defined $nonquote and $nonquote =~ s/$number_pattern_re/$num_format/x) 129 9 138
Condition83%5/6
not $formatted and defined $nonquote and $nonquote =~ s/$number_pattern_re/$num_format/x
!ll&&!rl&&r
90129
not $formatted and defined $nonquote
!ll&&!rl&&r
36129
MC/DC66%2/3
not $formatted and defined $nonquote and $nonquote =~ s/$number_pattern_re/$num_format/x
not $formatted defined $nonquote $nonquote =~ s/$number_pattern_re/$num_format/x
Truth table75%3/4
not(A) and B and C
A= not $formattedB= defined $nonquoteC= $nonquote =~ s/$number_pattern_re/$num_format/x
ABCresult
0XX0
10X0
1100
1111
308 129 $formatted = 1;
309 }
310  
311 138 ▶ $final_format .= defined $nonquote ? $nonquote : $quote;
Branch: 1 branch
BranchTrueFalseTotal
defined $nonquote ? : 129 9 138
312 }
313  
314 133 $final_format =~ s{''}{'}g;
315  
316 133 return $final_format;
317 }
318  
319 sub at_least {
320 1 ▶ my ($self, $num) = @_;
Subroutine at_least: called CC=1 SCAR=0.0
Pod undocumented
321 1 my $pattern = $self->_get_data(patterns => 'atleast');
322  
323 1 $num = $self->format($num);
324 1 $pattern =~ s{ \{ 0 \} }{$num}x;
325  
326 1 return $pattern;
327 }
328  
329 sub range {
330 1 ▶ my ($self, @nums) = @_;
Subroutine range: called CC=2 SCAR=6.9
Pod undocumented
331 1 my $pattern = $self->_get_data(patterns => 'range');
332  
333 1 for my $i (0, 1) {
334 2 $nums[$i] = $self->format($nums[$i]);
335 2 $pattern =~ s{ \{ $i \} }{$nums[$i]}x;
336 }
337  
338 1 return $pattern;
339 }
340  
341 12 1;
↑ Summary Build »