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

filter: Statementstmt 98.1% 212 / 216 Branchbran 84.4% 76 / 90 Conditioncond 60.0% 33 / 55 MC/DCmcdc 21.2% 7 / 33 Subroutinesub 100% 31 / 31 Podpod 0.0% 0 / 2 totaltotal 84.0% 359 / 427 CC 68 SCAR 21.3
CC
68
Cov
89%
_format_number
18.0
_trigger_pattern
14.7
_process_num_pattern
12.0
CRAP 8.4
↑ Summary blib/lib/CLDR/Number/Format/Currency.pm »
1 package CLDR::Number::Role::Format;
2  
3 16 ▶use v5.8.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Math::BigFloat;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Math::Round;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use CLDR::Number::Constant qw( $N $M $P $C $Q );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use CLDR::Number::Data::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use CLDR::Number::Data::System;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 16 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # This role does not have a publicly supported interface and may change in
16 # backward incompatible ways in the future. Please use one of the documented
17 # classes instead.
18  
19 16 our $VERSION = '0.17';
20  
21 16 requires qw( BUILD format );
22  
23 16 with qw( CLDR::Number::Role::Base );
24  
25 has pattern => (
26 is => 'rw',
27 isa => sub {
28 246 ▶ 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 246 246
29 },
30 16 trigger => 1,
31 );
32  
33 has minimum_integer_digits => (
34 is => 'rw',
35 isa => sub {
36 242 ▶ 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 242 242
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
02420
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
37 if defined $_[0] && !looks_like_number $_[0];
38 },
39 16 );
40  
41 has maximum_integer_digits => (
42 is => 'rw',
43 isa => sub {
44 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
45 if defined $_[0] && !looks_like_number $_[0];
46 },
47 16 );
48  
49 has minimum_fraction_digits => (
50 is => 'rw',
51 isa => sub {
52 333 ▶ 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 333 333
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
03330
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
53 if defined $_[0] && !looks_like_number $_[0];
54 },
55 trigger => sub {
56 333 ▶ my ($self, $min) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
57 333 ▶ return unless defined $self->maximum_fraction_digits;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->maximum_fraction_digits 106 227 333
58 227 ▶ return if $min <= $self->maximum_fraction_digits;
Branch: 1 branch
BranchTrueFalseTotal
if $min <= $self->maximum_fraction_digits 216 11 227
59 11 $self->{maximum_fraction_digits} = $min;
60 },
61 16 );
62  
63 has maximum_fraction_digits => (
64 is => 'rw',
65 isa => sub {
66 332 ▶ 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 332 332
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
03320
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
67 if defined $_[0] && !looks_like_number $_[0];
68 },
69 trigger => sub {
70 332 ▶ my ($self, $max) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
71 332 ▶ return unless defined $self->minimum_fraction_digits;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->minimum_fraction_digits 14 318 332
72 318 ▶ return if $max >= $self->minimum_fraction_digits;
Branch: 1 branch
BranchTrueFalseTotal
if $max >= $self->minimum_fraction_digits 316 2 318
73 2 $self->{minimum_fraction_digits} = $max;
74 },
75 16 );
76  
77 has primary_grouping_size => (
78 is => 'rw',
79 isa => sub {
80 243 ▶ 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 243 243
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
02430
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
81 if defined $_[0] && !looks_like_number $_[0];
82 },
83 16 );
84  
85 has secondary_grouping_size => (
86 is => 'rw',
87 isa => sub {
88 242 ▶ 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 242 242
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
02420
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
89 if defined $_[0] && !looks_like_number $_[0];
90 },
91 16 );
92  
93 has rounding_increment => (
94 is => 'rw',
95 isa => sub {
96 331 ▶ 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 331 331
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
03310
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
97 if defined $_[0] && !looks_like_number $_[0];
98 },
99 16 );
100  
101 16 has _positive_pattern => (
102 is => 'rw',
103 );
104  
105 16 has _negative_pattern => (
106 is => 'rw',
107 );
108  
109 before BUILD => sub {
110 120 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=2 SCAR=8.1
111  
112 120 ▶ return if $self->_has_init_arg('locale');
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_init_arg("locale") 120 0 120
113  
114 0 $self->_build_pattern;
115 16 };
116  
117 after _trigger_locale => sub {
118 138 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
119  
120 138 $self->_build_pattern;
121 16 };
122  
123 sub _build_pattern {
124 138 ▶ my ($self) = @_;
Subroutine _build_pattern: called CC=1 SCAR=0.0
125  
126 138 $self->_set_unless_init_arg(
127 pattern => $self->_get_data(pattern => $self->_pattern_type)
128 );
129 }
130  
131 sub _trigger_pattern {
132 246 ▶ my ($self, $input_pattern) = @_;
Subroutine _trigger_pattern: called CC=14 SCAR=26.9
133  
134 246 my $cache = $CLDR::Number::Data::Base::CACHE;
135 246 ▶ if (my $attributes
Branch: 1 branch
BranchTrueFalseTotal
if (my $attributes = $cache->{"attribute"}{$input_pattern} || $cache->{"pattern"}{$input_pattern} && $cache->{"attribute"}{$cache->{"pattern"}{$input_pattern}[0]}) 140 106 246
Condition83%5/6
$cache->{'attribute'}{$input_pattern} || $cache->{'pattern'}{$input_pattern} && $cache->{'attribute'}{$cache->{'pattern'}{$input_pattern}[0]}
l!l&&r!l&&!r
4199106
$cache->{"pattern"}{$input_pattern} and $cache->{"attribute"}{$cache->{"pattern"}{$input_pattern}[0]}
!ll&&!rl&&r
106099
MC/DC66%2/3
$cache->{'attribute'}{$input_pattern} || $cache->{'pattern'}{$input_pattern} && $cache->{'attribute'}{$cache->{'pattern'}{$input_pattern}[0]}
$cache->{'attribute'}{$input_pattern} $cache->{"pattern"}{$input_pattern} $cache->{"attribute"}{$cache->{"pattern"}{$input_pattern}[0]}
Truth table75%3/4
A || B and C
A= $cache->{'attribute'}{$input_pattern}B= $cache->{"pattern"}{$input_pattern}C= $cache->{"attribute"}{$cache->{"pattern"}{$input_pattern}[0]}
ABCresult
1XX1
0111
00X0
0100
136 = $cache->{attribute}{$input_pattern}
137 || $cache->{pattern}{$input_pattern}
138 && $cache->{attribute}{ $cache->{pattern}{$input_pattern}[0] }) {
139  
140 140 ▶ while (my ($attribute, $value) = each %$attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attributes) 840 140 980
141 840 $self->_set_unless_init_arg($attribute => $value);
142 }
143  
144 140 my $pattern = $cache->{pattern}{$input_pattern};
145  
146 140 ▶ $self->_positive_pattern(
Condition66%4/6
$pattern && $pattern->[1] || $CLDR::Number::Role::Format::N
l!l&&r!l&&!r
99410
$pattern and $pattern->[1]
!ll&&!rl&&r
41099
MC/DC0%0/3
$pattern && $pattern->[1] || $CLDR::Number::Role::Format::N
$pattern $pattern->[1] $CLDR::Number::Role::Format::N
Truth table40%2/5
A and B || C
A= $patternB= $pattern->[1]C= $CLDR::Number::Role::Format::N
ABCresult
11X1
0X11
1011
0X00
1000
147 $pattern && $pattern->[1] || $N
148 );
149  
150 140 ▶ $self->_negative_pattern(
Condition83%5/6
$pattern && $pattern->[2] || $CLDR::Number::Role::Format::M . $self->_positive_pattern
l!l&&r!l&&!r
71330
$pattern and $pattern->[2]
!ll&&!rl&&r
41927
MC/DC0%0/3
$pattern && $pattern->[2] || $CLDR::Number::Role::Format::M . $self->_positive_pattern
$pattern $pattern->[2] $CLDR::Number::Role::Format::M . $self->_positive_pattern
Truth table60%3/5
A and B || C
A= $patternB= $pattern->[2]C= $CLDR::Number::Role::Format::M . $self->_positive_pattern
ABCresult
11X1
0X11
1011
0X00
1000
151 $pattern && $pattern->[2] || $M . $self->_positive_pattern
152 );
153  
154 140 return;
155 }
156  
157 # temporarily replace escaped quotes
158 106 $input_pattern =~ s{''}{$Q}g;
159  
160 106 my $internal_pattern = '';
161 106 my $canonical_pattern = '';
162 106 my $num_subpattern;
163  
164 106 ▶ while ($input_pattern =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($input_pattern =~ / \G (?: ( [^']+ ) # non-quoted text | ' ( [^']+ ) (?: ' | $ ) # quoted text (trailing quote optional) ) /gx) 111 106 217
165 \G (?:
166 ( [^']+ ) # non-quoted text
167 |
168 ' ( [^']+ ) (?: ' | $ ) # quoted text (trailing quote optional)
169 )
170 }xg) {
171 111 my $nonquoted = $1;
172 111 my $quoted = $2;
173  
174 111 ▶ if (defined $nonquoted) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $nonquoted) { } 102 9 111
elsif (defined $quoted) { } 9 0 9
175 102 ▶ if (!defined $num_subpattern && $nonquoted =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $num_subpattern and $nonquoted =~ / ^ ( .*? ) # pre\342\200\223number pattern ( (?: \* \X )? [\@#0-9,.]+ ) # number pattern ( .* ) $ # post\342\200\223number pattern /x) { } 102 0 102
Condition33%1/3
not defined $num_subpattern and $nonquoted =~ / ^ ( .*? ) # pre\342\200\223number pattern ( (?: \* \X )? [\@#0-9,.]+ ) # number pattern ( .* ) $ # post\342\200\223number pattern /x
!ll&&!rl&&r
00102
MC/DC0%0/2
not defined $num_subpattern and $nonquoted =~ / ^ ( .*? ) # pre\342\200\223number pattern ( (?: \* \X )? [\@#0-9,.]+ ) # number pattern ( .* ) $ # post\342\200\223number pattern /x
not defined $num_subpattern $nonquoted =~ / ^ ( .*? ) # pre\342\200\223number pattern ( (?: \* \X )? [\@#0-9,.]+ ) # number pattern ( .* ) $ # post\342\200\223number pattern /x
Truth table33%1/3
not(A) and B
A= not defined $num_subpatternB= $nonquoted =~ / ^ ( .*? ) # pre\342\200\223number pattern ( (?: \* \X )? [\@#0-9,.]+ ) # number pattern ( .* ) $ # post\342\200\223number pattern /x
ABresult
0X0
100
111
176 ^ ( .*? ) # preโ€“number pattern
177 ( (?: \* \X )? [@#0-9,.]+ ) # number pattern
178 ( .* ) $ # postโ€“number pattern
179 }x) {
180 102 my $prenum = $1;
181 102 $num_subpattern = $2;
182 102 my $postnum = $3;
183  
184 102 $num_subpattern = $self->_process_num_pattern($num_subpattern);
185  
186 102 $internal_pattern .= _escape_symbols($prenum . $N . $postnum);
187 102 $canonical_pattern .= $prenum . $num_subpattern . $postnum;
188 }
189 else {
190 0 $internal_pattern .= _escape_symbols($nonquoted);
191 0 $canonical_pattern .= $nonquoted;
192 }
193 }
194 elsif (defined $quoted) {
195 9 $internal_pattern .= $quoted;
196 9 $canonical_pattern .= "'$quoted'";
197 }
198 }
199  
200 106 $internal_pattern =~ s{$Q}{'}g;
201 106 $canonical_pattern =~ s{$Q}{''}g;
202  
203 106 $self->_positive_pattern($internal_pattern);
204 106 $self->_negative_pattern($M . $internal_pattern);
205  
206 # hashref instead of attribute method so wo donโ€™t retrigger this trigger
207 106 $self->{pattern} = $canonical_pattern;
208 }
209  
210 sub _validate_number {
211 320 ▶ my ($self, $method, $num) = @_;
Subroutine _validate_number: called CC=3 SCAR=11.0
212  
213 320 ▶ if (!defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num) 6 314 320
214 6 carp qq[Use of uninitialized value in ${\ref $self}::$method];
215 6 return undef;
216 }
217  
218 314 ▶ if (!looks_like_number $num) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($num)) 14 300 314
219 14 carp qq[Argument "$num" isn't numeric in ${\ref $self}::$method];
220 16 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
221 14 $num += 0;
222 }
223  
224 314 return $num;
225 }
226  
227 16 my $INF = 9**9**9;
228  
229 sub _format_number {
230 281 ▶ my ($self, $num) = @_;
Subroutine _format_number: called CC=18 SCAR=28.9
231 281 my ($format, $num_format);
232  
233 281 ▶ if ($num < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num < 0) { } 30 251 281
234 30 my $pattern = $self->_negative_pattern;
235 30 $pattern =~ s{$M}{$self->minus_sign}e;
236 30 $format = $pattern;
237 }
238 else {
239 251 $format = $self->_positive_pattern;
240 }
241  
242 281 ▶ if ($num == $INF || $num == -$INF) {
Branch: 2 branches
BranchTrueFalseTotal
if ($num == $INF or $num == -$INF) { } 17 264 281
elsif (not defined($num <=> $INF)) { } 9 255 264
Condition100%3/3
$num == $INF or $num == -$INF
l!l&&r!l&&!r
107264
MC/DC100%2/2
$num == $INF or $num == -$INF
$num == $INF $num == -$INF
Truth table100%3/3
A or B
A= $num == $INFB= $num == -$INF
ABresult
1X1
011
000
243 17 $num_format = $self->infinity;
244 }
245 elsif (!defined($num <=> $INF)) {
246 9 $num_format = $self->nan;
247 }
248 else {
249 255 my $rounded;
250  
251 255 ▶ if ($self->rounding_increment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rounding_increment) { } 23 232 255
252 # TODO: round half to even
253 23 $rounded = Math::Round::nearest(
254 $self->rounding_increment,
255 abs $num
256 );
257 }
258 else {
259 # round half to even
260 232 $rounded = Math::BigFloat->new($num)->ffround(
261 -$self->maximum_fraction_digits,
262 'even'
263 )->babs->bstr;
264 }
265  
266 255 my ($int, $frac) = split /\./, $rounded;
267 255 ▶ if (!defined $frac) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $frac) 50 205 255
268 50 $frac = '';
269 }
270  
271 255 my $primary_group = $self->primary_grouping_size;
272 255 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($primary_group and $primary_group + $self->minimum_grouping_digits <= CORE::length $int) 96 159 255
Condition100%3/3
$primary_group and $primary_group + $self->minimum_grouping_digits <= CORE::length $int
!ll&&!rl&&r
3212796
MC/DC100%2/2
$primary_group and $primary_group + $self->minimum_grouping_digits <= CORE::length $int
$primary_group $primary_group + $self->minimum_grouping_digits <= CORE::length $int
Truth table100%3/3
A and B
A= $primary_groupB= $primary_group + $self->minimum_grouping_digits <= CORE::length $int
ABresult
0X0
100
111
273 $primary_group &&
274 $primary_group + $self->minimum_grouping_digits <= length $int
275 ) {
276 96 my $group_sign = $self->group_sign;
277 96 ▶ my $other_groups = $self->secondary_grouping_size || $primary_group;
Condition66%2/3
$self->secondary_grouping_size || $primary_group
l!l&&r!l&&!r
7890
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
278  
279 96 $int =~ s{ (?<! ^ ) (?= .{$primary_group} $ ) }{$group_sign}x;
280  
281 96 while (1) {
282 112 ▶ last if $int !~ s{
Branch: 1 branch
BranchTrueFalseTotal
unless $int =~ s/ (?<! ^ ) (?<! \Q$group_sign\E ) (?= .{$other_groups} \Q$group_sign\E ) /$group_sign/x 96 16 112
283 (?<! ^ )
284 (?<! \Q$group_sign\E )
285 (?= .{$other_groups} \Q$group_sign\E )
286 }{$group_sign}x;
287 }
288 }
289  
290 255 ▶ my $int_pad = $self->minimum_integer_digits - (length $int || 0);
Condition50%1/2
CORE::length $int || 0
l!l
2550
MC/DC0%0/1
CORE::length $int || 0
CORE::length $int
Truth table50%1/2
A
A= CORE::length $int
Aresult
11
00
291 255 ▶ if ($int_pad > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int_pad > 0) 5 250 255
292 5 $int = 0 x $int_pad . $int;
293 }
294  
295 255 ▶ my $frac_pad = $self->minimum_fraction_digits - (length $frac || 0);
Condition100%2/2
CORE::length $frac || 0
l!l
20550
MC/DC100%1/1
CORE::length $frac || 0
CORE::length $frac
Truth table100%2/2
A
A= CORE::length $frac
Aresult
11
00
296 255 ▶ if ($frac_pad > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($frac_pad > 0) { } 2 253 255
elsif ($frac_pad < 0) { } 108 145 253
297 2 $frac .= 0 x $frac_pad;
298 }
299 elsif ($frac_pad < 0) {
300 108 my $truncate_size = abs $frac_pad;
301 108 $frac =~ s{ 0{1,$truncate_size} $ }{}x;
302 }
303  
304 255 $num_format = $int;
305  
306 255 ▶ if (length $frac) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $frac) 162 93 255
307 162 $num_format .= $self->decimal_sign . $frac;
308 }
309  
310 255 ▶ if ($self->numbering_system ne 'latn') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->numbering_system ne "latn") 9 246 255
311 my $digits = $CLDR::Number::Data::System::DATA->{
312 9 $self->numbering_system
313 };
314  
315 9 $num_format =~ s{ ( [0-9] ) }{$digits->[$1]}xg;
316 }
317 }
318  
319 281 $format =~ s{$N}{$num_format};
320  
321 281 return $format;
322 }
323  
324 sub _process_num_pattern {
325 102 ▶ my ($self, $num_pattern) = @_;
Subroutine _process_num_pattern: called CC=12 SCAR=24.9
326  
327 102 for ($num_pattern) {
328 102 s{ \. $ }{}x; # no trailing decimal sign
329 102 s{ (?: ^ | \# ) (?= \. ) }{0}x; # at least one minimum integer digit
330  
331 # calculate grouping sizes
332 102 my ($secondary, $primary) = map { length } m{
333 , ( [^,]* ) # primary
334 , ( [^,.]* ) # secondary
335 (?: \. | $ )
336 }x;
337  
338 102 ▶ if (!defined $primary) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $primary) { } 71 31 102
elsif ($primary == 0) { } 6 25 31
elsif ($primary == $secondary) { } 4 21 25
339 71 ($primary) = map { length } m{
340 , ( [^,.]* ) # primary only
341 (?: \. | $ )
342 }x;
343 }
344 elsif ($primary == 0) {
345 6 $primary = $secondary;
346 6 $secondary = undef;
347 }
348 elsif ($primary == $secondary) {
349 4 $secondary = undef;
350 }
351  
352 102 tr{,}{}d; # temporarily remove groups
353  
354 102 ▶ if (!m{ \. }x) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/ \. /x) 80 22 102
355 80 s{ (?: ^ | \# ) $ }{0}x; # at least one minimum integer digit
356 }
357  
358 102 ▶ if (!$self->_has_init_arg('minimum_integer_digits')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_init_arg("minimum_integer_digits")) 102 0 102
359 102 my ($min_int) = m{ ( [0-9,]+ ) (?= \. | $ ) }x;
360 102 $self->minimum_integer_digits(length $min_int);
361 }
362  
363 102 ▶ if ($primary) {
Branch: 1 branch
BranchTrueFalseTotal
if ($primary) { } 42 60 102
364 42 s{ (?= .{$primary} (?: \. | $ ) ) }{,}x; # add primary group
365 42 $self->_set_unless_init_arg(primary_grouping_size => $primary);
366  
367 42 ▶ if ($secondary) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secondary) { } 19 23 42
368 19 s{ (?= .{$secondary} , ) }{,}x; # add secondary group
369 19 $self->_set_unless_init_arg(
370 secondary_grouping_size => $secondary
371 );
372 }
373 else {
374 23 $self->_set_unless_init_arg(secondary_grouping_size => 0);
375 }
376 }
377 else {
378 60 $self->_set_unless_init_arg(primary_grouping_size => 0);
379 60 $self->_set_unless_init_arg(secondary_grouping_size => 0);
380 }
381  
382 102 s{ ^ \#+ (?= [#0-9] ) }{}x; # no leading multiple #s
383 102 s{ ^ (?= , ) }{#}x; # leading # before group
384  
385 102 ▶ if (my ($max, $min) = m{ \. ( ( [0-9]* ) \#* ) }x) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = / \. ( ( [0-9]* ) \#* ) /x) { } 22 80 102
386 22 $self->_set_unless_init_arg(minimum_fraction_digits => length $min);
387 22 $self->_set_unless_init_arg(maximum_fraction_digits => length $max);
388 }
389 else {
390 80 $self->_set_unless_init_arg(minimum_fraction_digits => 0);
391 80 $self->_set_unless_init_arg(maximum_fraction_digits => 0);
392 }
393  
394 102 ▶ if (!$self->_has_init_arg('rounding_increment')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_init_arg("rounding_increment")) 102 0 102
395 102 ▶ if (my ($round_inc) = m{ (
Branch: 1 branch
BranchTrueFalseTotal
if (my($round_inc) = / ( (?: [1-9] [0-9,]* | 0 ) # integer (?= \. | $ ) (?: \. [0-9]* [1-9] )? # fraction ) /x) { } 102 0 102
396 (?: [1-9] [0-9,]* | 0 ) # integer
397 (?= \. | $ )
398 (?: \. [0-9]* [1-9] )? # fraction
399 ) }x) {
400 102 $self->rounding_increment($round_inc);
401 }
402 else {
403 0 $self->rounding_increment(0);
404 }
405 }
406 }
407  
408 102 return $num_pattern;
409 }
410  
411 sub _escape_symbols {
412 102 ▶ my ($pattern) = @_;
Subroutine _escape_symbols: called CC=2 SCAR=6.9
413  
414 102 for ($pattern) {
415 102 s{%}{$P};
416 102 s{ยค}{$C};
417 102 s{-}{$M};
418 }
419  
420 102 return $pattern;
421 }
422  
423 sub at_least {
424 6 ▶ my ($self, $num) = @_;
Subroutine at_least: called CC=2 SCAR=6.9
Pod undocumented
425 6 my $pattern = $self->_get_data(pattern => 'at_least');
426  
427 6 $num = $self->_validate_number(at_least => $num);
428 6 ▶ return undef unless defined $num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 1 5 6
429  
430 5 $num = $self->format($num);
431 5 $pattern =~ s{ \{ 0 \} }{$num}x;
432  
433 5 return $pattern;
434 }
435  
436 sub range {
437 15 ▶ my ($self, @nums) = @_;
Subroutine range: called CC=3 SCAR=11.0
Pod undocumented
438 15 my $pattern = $self->_get_data(pattern => 'range');
439  
440 15 for my $i (0, 1) {
441 29 my $num = $self->_validate_number(range => $nums[$i]);
442 29 ▶ return undef unless defined $num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 2 27 29
443  
444 27 $num = $self->format($num);
445 27 $pattern =~ s{ \{ $i \} }{$num}x;
446 }
447  
448 13 return $pattern;
449 }
450  
451 16 1;
↑ Summary blib/lib/CLDR/Number/Format/Currency.pm »