BSON/Decimal128.pm

filter: Statementstmt 97.5% 120 / 123 Branchbran 96.3% 79 / 82 Conditioncond 85.1% 23 / 27 MC/DCmcdc 77.7% 14 / 18 Subroutinesub 100% 20 / 20 Podpod 50.0% 1 / 2 totaltotal 94.4% 257 / 272 CC 51 SCAR 29.1
CC
51
Cov
96%
_string_to_bid
29.7
_bid_to_string
20.0
BUILD
3.0
CRAP 18.4
« BSON/PP.pm ↑ Summary BSON/Time.pm »
1 71 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 71 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package BSON::Decimal128;
6 # ABSTRACT: BSON type wrapper for Decimal128
7  
8 71 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 71 our $VERSION = 'v1.12.2';
10  
11 71 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 71 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 71 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #pod =attr value
17 #pod
18 #pod The Decimal128 value represented as string. If not provided, it will be
19 #pod generated from the C<bytes> attribute on demand.
20 #pod
21 #pod =cut
22  
23 71 has 'value' => (
24 is => 'lazy',
25 );
26  
27 #pod =attr bytes
28 #pod
29 #pod The Decimal128 value represented in L<Binary Integer
30 #pod Decimal|https://en.wikipedia.org/wiki/Binary_Integer_Decimal> (BID) format.
31 #pod If not provided, it will be generated from the C<value> attribute on
32 #pod demand.
33 #pod
34 #pod =cut
35  
36 71 has 'bytes' => (
37 is => 'lazy',
38 );
39  
40 71 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 71 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
43 71 PLIM => 34, # precision limit, i.e. max coefficient chars
44 EMAX => 6144, # for 9.999999999999999999999999999999999E+6144
45 EMIN => -6143, # for 1.000000000000000000000000000000000E-6143
46 AEMAX => 6111, # EMAX - (PLIM - 1); largest encodable exponent
47 AEMIN => -6176, # EMIN - (PLIM - 1); smallest encodable exponent
48 BIAS => 6176, # offset for encoding exponents
49 71 };
50  
51 71 my $digits = qr/[0-9]+/;
52 71 my $decimal_re = qr{
53 ( [-+]? ) # maybe a sign
54 ( (?:$digits \. $digits? ) | (?: \.? $digits ) ) # decimal-part
55 ( (?:e [-+]? $digits)? ) # maybe exponent
56 }ix;
57  
58 605 ▶sub _build_value {
Subroutine _build_value: called CC=1 SCAR=0.0
59 605 return _bid_to_string( $_[0]->{bytes} );
60 }
61  
62 1965 ▶sub _build_bytes {
Subroutine _build_bytes: called CC=1 SCAR=0.0
63 1965 return _string_to_bid( $_[0]->{value} );
64 }
65  
66 3172 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
67 3172 my $self = shift;
68  
69 croak "One and only one of 'value' or 'bytes' must be provided"
70 3172 ▶ unless 1 == grep { exists $self->{$_} } qw/value bytes/;
Branch: 1 branch
BranchTrueFalseTotal
unless 1 == grep({exists $self->{$_};} "value", "bytes") 1 3171 3172
71  
72 # must check for errors and canonicalize value if provided
73 3171 ▶ if (exists $self->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"value"}) 1965 1206 3171
74 1965 $self->{value} = _bid_to_string( $self->bytes );
75 }
76  
77 3040 return;
78 }
79  
80 2439 ▶sub _bid_to_string {
Subroutine _bid_to_string: called CC=20 SCAR=30.0
81 2439 my $bid = shift;
82 2439 my $binary = unpack( "B*", scalar reverse($bid) );
83 2439 my ( $coef, $e );
84  
85 # sign bit
86 2439 my $pos = !substr( $binary, 0, 1 );
87  
88 # detect special values from first 5 bits after sign bit
89 2439 my $special = substr( $binary, 1, 5 );
90 2439 ▶ if ( $special eq "11111" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($special eq 11111) 29 2410 2439
91 29 return "NaN";
92 }
93 2410 ▶ if ( $special eq "11110" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($special eq 11110) 66 2344 2410
94 66 ▶ return $pos ? "Infinity" : "-Infinity";
Branch: 1 branch
BranchTrueFalseTotal
$pos ? : 33 33 66
95 }
96  
97 2344 ▶ if ( substr( $binary, 1, 2 ) eq '11' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($binary, 1, 2) eq '11') { } 3 2341 2344
98 # Bits: 1*sign 2*ignored 14*exponent 111*significand.
99 # Implicit 0b100 prefix in significand.
100 3 $coef = "" . Math::BigInt->new( "0b100" . substr( $binary, 17 ) );
101 3 $e = unpack( "n", pack( "B*", "00" . substr( $binary, 3, 14 ) ) ) - BIAS;
102 }
103 else {
104 # Bits: 1*sign 14*exponent 113*significand
105 2341 $coef = "" . Math::BigInt->new( "0b" . substr( $binary, 15 ) );
106 2341 $e = unpack( "n", pack( "B*", "00" . substr( $binary, 1, 14 ) ) ) - BIAS;
107 }
108  
109 # Out of range is treated as zero
110 2344 ▶ if ( length($coef) > PLIM ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $coef > 34) 3 2341 2344
111 3 $coef = "0";
112 }
113  
114 # Shortcut on zero
115 2344 ▶ if ( $coef == 0 && $e == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coef == 0 and $e == 0) 101 2243 2344
Condition100%3/3
$coef == 0 and $e == 0
!ll&&!rl&&r
1717526101
MC/DC100%2/2
$coef == 0 and $e == 0
$coef == 0 $e == 0
Truth table100%3/3
A and B
A= $coef == 0B= $e == 0
ABresult
0X0
100
111
116 101 ▶ return $pos ? "0" : "-0";
Branch: 1 branch
BranchTrueFalseTotal
$pos ? : 75 26 101
117 }
118  
119 # convert to scientific form ( e.g. 123E+4 -> 1.23E6 )
120 2243 my $adj_exp = $e + length($coef) - 1;
121 # warn "# XXX COEF: $coef; EXP: $e; AEXP: $adj_exp\n";
122  
123 # exponential notation
124 2243 ▶ if ( $e > 0 || $adj_exp < -6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e > 0 or $adj_exp < -6) { } 1244 999 2243
Condition100%3/3
$e > 0 or $adj_exp < -6
l!l&&r!l&&!r
855389999
MC/DC100%2/2
$e > 0 or $adj_exp < -6
$e > 0 $adj_exp < -6
Truth table100%3/3
A or B
A= $e > 0B= $adj_exp < -6
ABresult
1X1
011
000
125 # insert decimal if more than one digit
126 1244 ▶ if ( length($coef) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $coef > 1) 696 548 1244
127 696 substr( $coef, 1, 0, "." );
128 }
129  
130 return (
131 1244 ▶ ( $pos ? "" : "-" ) . $coef . "E" . ( $adj_exp >= 0 ? "+" : "" ) . $adj_exp );
Branch: 2 branches
BranchTrueFalseTotal
$pos ? : 1094 150 1244
$adj_exp >= 0 ? : 855 389 1244
132 }
133  
134 # not exponential notation (integers or small negative exponents)
135 else {
136 # e == 0 means integer
137 999 ▶ return $pos ? $coef : "-$coef"
Branch: 2 branches
BranchTrueFalseTotal
if $e == 0 305 694 999
$pos ? : 266 39 305
138 if $e == 0;
139  
140 # pad with leading zeroes if coefficient is too short
141 694 ▶ if ( length($coef) < abs($e) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $coef < abs $e) 348 346 694
142 348 substr( $coef, 0, 0, "0" x ( abs($e) - length($coef) ) );
143 }
144  
145 # maybe coefficient is exact length?
146 694 ▶ return $pos ? "0.$coef" : "-0.$coef"
Branch: 2 branches
BranchTrueFalseTotal
if length $coef == abs $e 457 237 694
$pos ? : 357 100 457
147 if length($coef) == abs($e);
148  
149 # otherwise length(coef) > abs($e), so insert dot after first digit
150 237 substr( $coef, $e, 0, "." );
151 237 ▶ return $pos ? $coef : "-$coef";
Branch: 1 branch
BranchTrueFalseTotal
$pos ? : 192 45 237
152 }
153 }
154  
155 my ( $bidNaN, $bidPosInf, $bidNegInf ) =
156 71 map { scalar reverse pack( "B*", $_ . ( "0" x 118 ) ) } qw/ 011111 011110 111110 /;
157  
158 119 ▶sub _croak { croak("Couldn't parse '$_[0]' as valid Decimal128") }
Subroutine _croak: called CC=1 SCAR=0.0
159  
160 3 ▶sub _erange { croak("Value '$_[0]' is out of range for Decimal128") }
Subroutine _erange: called CC=1 SCAR=0.0
161  
162 9 ▶sub _erounding { croak("Value '$_[0]' can't be rounded to Decimal128") }
Subroutine _erounding: called CC=1 SCAR=0.0
163  
164 1965 ▶sub _string_to_bid {
Subroutine _string_to_bid: called CC=29 SCAR=33.9
165 1965 my $s = shift;
166  
167 # Check special values
168 1965 ▶ return $bidNaN if $s =~ /\A -? NaN \z/ix;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\A -? NaN \z/xi 19 1946 1965
169 1946 ▶ return $bidPosInf if $s =~ /\A \+?Inf(?:inity)? \z/ix;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\A \+?Inf(?:inity)? \z/xi 25 1921 1946
170 1921 ▶ return $bidNegInf if $s =~ /\A -Inf(?:inity)? \z/ix;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\A -Inf(?:inity)? \z/xi 25 1896 1921
171  
172 # Parse string
173 1896 my ( $sign, $mant, $exp ) = $s =~ /\A $decimal_re \z/x;
174 1896 ▶ $sign = "" unless defined $sign;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sign 119 1777 1896
175 1896 ▶ $exp = 0 unless defined $exp && length($exp);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exp and length $exp 793 1103 1896
Condition100%3/3
defined $exp and length $exp
!ll&&!rl&&r
1196741103
MC/DC100%2/2
defined $exp and length $exp
defined $exp length $exp
Truth table100%3/3
A and B
A= defined $expB= length $exp
ABresult
0X0
100
111
176 1896 $exp =~ s{^e}{}i;
177  
178 # Throw error if unparseable
179 1896 ▶ _croak($s) unless length $exp && defined $mant;
Branch: 1 branch
BranchTrueFalseTotal
unless length $exp and defined $mant 119 1777 1896
Condition66%2/3
length $exp and defined $mant
!ll&&!rl&&r
01191777
MC/DC50%1/2
length $exp and defined $mant
length $exp defined $mant
Truth table66%2/3
A and B
A= length $expB= defined $mant
ABresult
0X0
100
111
180  
181 # Extract sign bit
182 1777 ▶ my $neg = defined($sign) && $sign eq '-' ? "1" : "0";
Branch: 1 branch
BranchTrueFalseTotal
defined $sign && $sign eq '-' ? : 268 1509 1777
Condition66%2/3
defined $sign and $sign eq "-"
!ll&&!rl&&r
01509268
MC/DC50%1/2
defined $sign and $sign eq "-"
defined $sign $sign eq "-"
Truth table66%2/3
A and B
A= defined $signB= $sign eq "-"
ABresult
0X0
100
111
183  
184 # Remove leading zeroes unless "0."
185 1777 $mant =~ s{^(?:0(?!\.))+}{};
186  
187 # Locate decimal, remove it and adjust the exponent
188 1777 my $dot = index( $mant, "." );
189 1777 $mant =~ s/\.//;
190 1777 ▶ $exp += $dot - length($mant) if $dot >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dot >= 0 989 788 1777
191  
192 # Remove leading zeros from mantissa (after decimal point removed)
193 1777 $mant =~ s/^0+//;
194 1777 ▶ $mant = "0" unless length $mant;
Branch: 1 branch
BranchTrueFalseTotal
unless length $mant 482 1295 1777
195  
196 # Apply exact rounding if necessary
197 1777 ▶ if ( length($mant) > PLIM ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $mant > 34) { } 17 1760 1777
elsif ($exp < -6176) { } 11 1749 1760
198 17 my $plim = PLIM;
199 17 $mant =~ s{(.{$plim})(0+)$}{$1};
200 17 ▶ $exp += length($2) if defined $2 && length $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 and length $2 13 4 17
Condition66%2/3
defined $2 and length $2
!ll&&!rl&&r
4013
MC/DC50%1/2
defined $2 and length $2
defined $2 length $2
Truth table66%2/3
A and B
A= defined $2B= length $2
ABresult
0X0
100
111
201 }
202 elsif ( $exp < AEMIN ) {
203 11 $mant =~ s{(.*[1-9])(0+)$}{$1};
204 11 ▶ $exp += length($2) if defined $2 && length $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 and length $2 2 9 11
Condition66%2/3
defined $2 and length $2
!ll&&!rl&&r
902
MC/DC50%1/2
defined $2 and length $2
defined $2 length $2
Truth table66%2/3
A and B
A= defined $2B= length $2
ABresult
0X0
100
111
205 }
206  
207 # Apply clamping if possible
208 1777 ▶ if ( $mant == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mant == 0) { } 482 1295 1777
elsif ($exp > 6111 and $exp - 6111 <= 34 - length($mant)) { } 76 2436 2512
209 482 ▶ if ( $exp > AEMAX ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exp > 6111) { } 12 470 482
elsif ($exp < -6176) { } 8 462 470
210 12 $mant = "0";
211 12 $exp = AEMAX;
212 }
213 elsif ( $exp < AEMIN ) {
214 8 $mant = "0";
215 8 $exp = AEMIN;
216 }
217 }
218 ▶ elsif ( $exp > AEMAX && $exp - AEMAX <= PLIM - length($mant) ) {
Condition100%3/3
$exp > 6111 and $exp - 6111 <= 34 - length($mant)
!ll&&!rl&&r
1217276
MC/DC100%2/2
$exp > 6111 and $exp - 6111 <= 34 - length($mant)
$exp > 6111 $exp - 6111 <= 34 - length($mant)
Truth table100%3/3
A and B
A= $exp > 6111B= $exp - 6111 <= 34 - length($mant)
ABresult
0X0
100
111
219 76 $mant .= "0" x ( $exp - AEMAX );
220 76 $exp = AEMAX;
221 }
222  
223 # Throw errors if result won't fit in Decimal128
224 1777 ▶ _erounding($s) if length($mant) > PLIM;
Branch: 1 branch
BranchTrueFalseTotal
if length $mant > 34 9 1768 1777
225 1768 ▶ _erange($s) if $exp > AEMAX || $exp < AEMIN;
Branch: 1 branch
BranchTrueFalseTotal
if $exp > 6111 or $exp < -6176 3 1765 1768
Condition100%3/3
$exp > 6111 or $exp < -6176
l!l&&r!l&&!r
211765
MC/DC100%2/2
$exp > 6111 or $exp < -6176
$exp > 6111 $exp < -6176
Truth table100%3/3
A or B
A= $exp > 6111B= $exp < -6176
ABresult
1X1
011
000
226  
227 # Get binary representation of coefficient
228 1765 my $coef = Math::BigInt->new($mant)->as_bin;
229 1765 $coef =~ s/^0b//;
230  
231 # Get 14-bit binary representation of biased exponent
232 1765 my $biased_exp = unpack( "B*", pack( "n", $exp + BIAS ) );
233 1765 substr( $biased_exp, 0, 2, "" );
234  
235 # Choose representation based on coefficient length
236 1765 my $coef_len = length($coef);
237 1765 ▶ if ( $coef_len <= 113 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($coef_len <= 113) { } 1765 0 1765
elsif ($coef_len <= 114) { } 0 0 0
238 1765 substr( $coef, 0, 0, "0" x ( 113 - $coef_len ) );
239 1765 return scalar reverse pack( "B*", $neg . $biased_exp . $coef );
240 }
241 elsif ( $coef_len <= 114 ) {
242 0 substr( $coef, 0, 3, "" );
243 0 return scalar reverse pack( "B*", $neg . "11" . $biased_exp . $coef );
244 }
245 else {
246 0 _erange($s);
247 }
248 }
249  
250 #pod =method TO_JSON
251 #pod
252 #pod Returns the value as a string.
253 #pod
254 #pod If the C<BSON_EXTJSON> option is true, it will instead
255 #pod be compatible with MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst>
256 #pod format, which represents it as a document as follows:
257 #pod
258 #pod {"$numberDecimal" : "2.23372036854775807E+57"}
259 #pod
260 #pod =cut
261  
262 1522 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=2 SCAR=6.9
Pod documented
263 1522 ▶ return "" . $_[0]->value unless $ENV{BSON_EXTJSON};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"BSON_EXTJSON"} 3 1519 1522
264 1519 return { '$numberDecimal' => "" . ($_[0]->value) };
265 }
266  
267 71 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
268 2 ▶ q{""} => sub { $_[0]->value },
Subroutine __ANON__: called CC=1 SCAR=0.0
269 71 fallback => 1,
270 71 );
271  
272 71 1;
273  
274 =pod
275  
276 =encoding UTF-8
277  
278 =head1 NAME
279  
280 BSON::Decimal128 - BSON type wrapper for Decimal128
281  
282 =head1 VERSION
283  
284 version v1.12.2
285  
286 =head1 SYNOPSIS
287  
288 use BSON::Types ':all';
289  
290 # string representation
291 $decimal = bson_decimal128( "1.23456789E+1000" );
292  
293 # binary representation in BID format
294 $decimal = BSON::Decimal128->new( bytes => $bid )
295  
296 =head1 DESCRIPTION
297  
298 This module provides a BSON type wrapper for Decimal128 values.
299  
300 It may be initialized with either a numeric value in string form, or
301 with a binary Decimal128 representation (16 bytes), but not both.
302  
303 Initialization from a string will throw an error if the string cannot be
304 parsed as a Decimal128 or if the resulting number would not fit into 128
305 bits. If required, clamping or exact rounding will be applied to try to
306 fit the value into 128 bits.
307  
308 =head1 ATTRIBUTES
309  
310 =head2 value
311  
312 The Decimal128 value represented as string. If not provided, it will be
313 generated from the C<bytes> attribute on demand.
314  
315 =head2 bytes
316  
317 The Decimal128 value represented in L<Binary Integer
318 Decimal|https://en.wikipedia.org/wiki/Binary_Integer_Decimal> (BID) format.
319 If not provided, it will be generated from the C<value> attribute on
320 demand.
321  
322 =head1 METHODS
323  
324 =head2 TO_JSON
325  
326 Returns the value as a string.
327  
328 If the C<BSON_EXTJSON> option is true, it will instead
329 be compatible with MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst>
330 format, which represents it as a document as follows:
331  
332 {"$numberDecimal" : "2.23372036854775807E+57"}
333  
334 =for Pod::Coverage BUILD
335  
336 =head1 OVERLOADING
337  
338 The stringification operator (C<"">) is overloaded to return a (normalized)
339 string representation. Fallback overloading is enabled.
340  
341 =head1 AUTHORS
342  
343 =over 4
344  
345 =item *
346  
347 David Golden <david@mongodb.com>
348  
349 =item *
350  
351 Stefan G. <minimalist@lavabit.com>
352  
353 =back
354  
355 =head1 COPYRIGHT AND LICENSE
356  
357 This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.
358  
359 This is free software, licensed under:
360  
361 The Apache License, Version 2.0, January 2004
362  
363 =cut
364  
365 __END__
« BSON/PP.pm ↑ Summary BSON/Time.pm »