| 1 | package Text::KDL::XS::Value; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 24 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 24 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 24 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 24 | ▶ | use overload (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 24 | ▶ | use Text::KDL::XS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 24 | our @CARP_NOT = qw( | |||||||||||||||||||||||||||
| 12 | Text::KDL::XS Text::KDL::XS::Document Text::KDL::XS::Emitter | ||||||||||||||||||||||||||||
| 13 | Text::KDL::XS::Node Text::KDL::XS::Parser | ||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 24 | my %IS_FIELD = map { $_ => 1 } qw(type kind value type_annotation); | |||||||||||||||||||||||||||
| 17 | 24 | my %IS_TYPE = map { $_ => 1 } qw(null bool number string); | |||||||||||||||||||||||||||
| 18 | 24 | my %IS_KIND = map { $_ => 1 } qw(integer float string); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 24 | my $INFINITY = 9**9**9; | |||||||||||||||||||||||||||
| 21 | 24 | my %KEYWORD_NUMBER = ('#inf' => $INFINITY, '#-inf' => -$INFINITY, '#nan' => $INFINITY - $INFINITY); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 126 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 126 | my ($class, @fields) = @_; | |||||||||||||||||||||||||||
| 25 | 126 | my $fields = Text::KDL::XS::_parse_named_arguments(__PACKAGE__ . '->new', 'field', \%IS_FIELD, @fields); | |||||||||||||||||||||||||||
| 26 | 126 | my $type = $fields->{type}; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 126 | ▶ | Carp::croak(__PACKAGE__ . "->new: 'type' is required") unless defined $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | Carp::croak(__PACKAGE__ . "->new: unknown type '$type' (expected null, bool, number or string)") | ||||||||||||||||||||||||||||
| 30 | 126 | ▶ | unless $IS_TYPE{$type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | Carp::croak(__PACKAGE__ . "->new: 'kind' is only allowed for numbers") | ||||||||||||||||||||||||||||
| 32 | 124 | ▶ | if defined $fields->{kind} && $type ne 'number'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $fields->{"kind"} and $type ne "number"
MC/DC100%2/2
defined $fields->{"kind"} and $type ne "number"
defined $fields->{"kind"}
$type ne "number"
Truth table100%3/3
A and B
A= defined $fields->{"kind"}B= $type ne "number"
| |||||||||||||||||||||||||||||
| 33 | Carp::croak(__PACKAGE__ . "->new: 'type_annotation' must be a string") | ||||||||||||||||||||||||||||
| 34 | 123 | ▶ | if ref $fields->{type_annotation}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 123 | my %value = (type => $type, kind => undef, value => undef, type_annotation => $fields->{type_annotation}); | |||||||||||||||||||||||||||
| 37 | 123 | ▶ | if ($type eq 'bool') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 38 | 5 | ▶ | $value{value} = $fields->{value} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | elsif ($type eq 'string') { | ||||||||||||||||||||||||||||
| 41 | 72 | $value{value} = _text_field('a string', $fields->{value}); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | elsif ($type eq 'number') { | ||||||||||||||||||||||||||||
| 44 | 42 | @value{qw(kind value)} = _number_fields($fields->{kind}, $fields->{value}); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 104 | return bless \%value, $class; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub type { $_[0]->{type} } | ||||||||||||||||||||||||||
|
Subroutine
type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 14 | ▶ | sub kind { $_[0]->{kind} } | ||||||||||||||||||||||||||
|
Subroutine
kind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 35 | ▶ | sub type_annotation { $_[0]->{type_annotation} } | ||||||||||||||||||||||||||
|
Subroutine
type_annotation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 69 | ▶ | sub value { $_[0]->{value} } | ||||||||||||||||||||||||||
|
Subroutine
value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 3 | ▶ | sub is_null { $_[0]->{type} eq 'null' } | ||||||||||||||||||||||||||
|
Subroutine
is_null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | sub is_bool { $_[0]->{type} eq 'bool' } | ||||||||||||||||||||||||||
|
Subroutine
is_bool: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 4 | ▶ | sub is_number { $_[0]->{type} eq 'number' } | ||||||||||||||||||||||||||
|
Subroutine
is_number: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 33 | ▶ | sub is_string { $_[0]->{type} eq 'string' } | ||||||||||||||||||||||||||
|
Subroutine
is_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 47 | ▶ | sub as_perl { | ||||||||||||||||||||||||||
|
Subroutine
as_perl: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 47 | my ($self) = @_; | |||||||||||||||||||||||||||
| 61 | 47 | ▶ | return undef if $self->{type} eq 'null'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 47 | ▶ | return $self->{value} ? 1 : 0 if $self->{type} eq 'bool'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 63 | 42 | return $self->{value}; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 34 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 34 | my ($self) = @_; | |||||||||||||||||||||||||||
| 68 | 34 | my $type = $self->{type}; | |||||||||||||||||||||||||||
| 69 | 34 | ▶ | return undef if $type eq 'null'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 34 | ▶ | return $self->{value} ? 'true' : 'false' if $type eq 'bool'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 71 | 34 | ▶ | return Text::KDL::XS::_float_text($self->{value}) if $type eq 'number' && $self->_kind_is('float'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "number" and $self->_kind_is("float")
MC/DC100%2/2
$type eq "number" and $self->_kind_is("float")
$type eq "number"
$self->_kind_is("float")
Truth table100%3/3
A and B
A= $type eq "number"B= $self->_kind_is("float")
| |||||||||||||||||||||||||||||
| 72 | 26 | return "$self->{value}"; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 10017 | ▶ | sub as_number { | ||||||||||||||||||||||||||
|
Subroutine
as_number: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 10017 | my ($self) = @_; | |||||||||||||||||||||||||||
| 77 | 10017 | my $type = $self->{type}; | |||||||||||||||||||||||||||
| 78 | 10017 | ▶ | return undef if $type eq 'null'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 10016 | ▶ | return $self->{value} ? 1 : 0 if $type eq 'bool'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 80 | 10015 | ▶ | return _number_from_text($self->{value}) if $type eq 'number' && $self->_kind_is('string'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type eq "number" and $self->_kind_is("string")
MC/DC50%1/2
$type eq "number" and $self->_kind_is("string")
$type eq "number"
$self->_kind_is("string")
Truth table66%2/3
A and B
A= $type eq "number"B= $self->_kind_is("string")
| |||||||||||||||||||||||||||||
| 81 | 558 | return _numified($self->{value}); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 9 | ▶ | sub as_bignum { | ||||||||||||||||||||||||||
|
Subroutine
as_bignum: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||
| 86 | 9 | my $type = $self->{type}; | |||||||||||||||||||||||||||
| 87 | 9 | ▶ | return undef if $type eq 'null'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 8 | ▶ | return $self->{value} ? 1 : 0 if $type eq 'bool'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 89 | 7 | ▶ | Carp::croak(__PACKAGE__ . "::as_bignum: the value is a $type, not a number") unless $type eq 'number'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 6 | return _bignum_from_text($self->as_string); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 10035 | ▶ | sub _kind_is { | ||||||||||||||||||||||||||
|
Subroutine
_kind_is: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 94 | 10035 | my ($self, $kind) = @_; | |||||||||||||||||||||||||||
| 95 | 10035 | ▶ | return defined $self->{kind} && $self->{kind} eq $kind; | ||||||||||||||||||||||||||
|
Condition66%2/3
defined $self->{'kind'} && $self->{'kind'} eq $kind
MC/DC50%1/2
defined $self->{'kind'} && $self->{'kind'} eq $kind
defined $self->{'kind'}
$self->{'kind'} eq $kind
Truth table66%2/3
A && B
A= defined $self->{'kind'}B= $self->{'kind'} eq $kind
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # The value of a string: a plain scalar, or an object with a string | ||||||||||||||||||||||||||||
| 99 | # conversion, which is stringified once here. | ||||||||||||||||||||||||||||
| 100 | 114 | ▶ | sub _text_field { | ||||||||||||||||||||||||||
|
Subroutine
_text_field: called CC=5 SCAR=22.7
| |||||||||||||||||||||||||||||
| 101 | 114 | my ($what, $text) = @_; | |||||||||||||||||||||||||||
| 102 | 114 | ▶ | Carp::croak(__PACKAGE__ . "->new: $what needs a defined value") unless defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 112 | ▶ | return $text unless ref $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | Carp::croak(__PACKAGE__ . "->new: cannot use a " . ref($text) . " reference as $what") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($text) and overload::Method($text, "\"\"")
MC/DC0%0/2
builtin::blessed($text) and overload::Method($text, "\"\"")
builtin::blessed($text)
overload::Method($text, "\"\"")
Truth table0%0/3
A and B
A= builtin::blessed($text)B= overload::Method($text, "\"\"")
| |||||||||||||||||||||||||||||
| 105 | unless Scalar::Util::blessed($text) && overload::Method($text, '""'); | ||||||||||||||||||||||||||||
| 106 | 0 | return "$text"; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # The kind and value of a number, the kind inferred when it is not given. | ||||||||||||||||||||||||||||
| 110 | 42 | ▶ | sub _number_fields { | ||||||||||||||||||||||||||
|
Subroutine
_number_fields: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 111 | 42 | my ($kind, $number) = @_; | |||||||||||||||||||||||||||
| 112 | 42 | $number = _text_field('a number', $number); | |||||||||||||||||||||||||||
| 113 | 41 | ▶ | $kind //= _inferred_kind($number); | ||||||||||||||||||||||||||
|
Condition66%2/3
$kind //= _inferred_kind($number)
MC/DC0%0/2
$kind //= _inferred_kind($number)
$kind
_inferred_kind($number)
Truth table66%2/3
A //= B
A= $kindB= _inferred_kind($number)
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Carp::croak(__PACKAGE__ . "->new: unknown kind '$kind' (expected integer, float or string)") | ||||||||||||||||||||||||||||
| 116 | 40 | ▶ | unless $IS_KIND{$kind}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 39 | ▶ | Carp::croak(__PACKAGE__ . "->new: '$number' is not an integer from -2**63 to 2**64-1") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$kind eq "integer" and not Text::KDL::XS::_is_native_integer($number)
MC/DC100%2/2
$kind eq "integer" and not Text::KDL::XS::_is_native_integer($number)
$kind eq "integer"
not Text::KDL::XS::_is_native_integer($number)
Truth table100%3/3
A and not(B)
A= $kind eq "integer"B= not Text::KDL::XS::_is_native_integer($number)
| |||||||||||||||||||||||||||||
| 118 | if $kind eq 'integer' && !Text::KDL::XS::_is_native_integer($number); | ||||||||||||||||||||||||||||
| 119 | 33 | ▶ | Carp::croak(__PACKAGE__ . "->new: '$number' is not a number") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$kind eq "float" and not &Scalar::Util::looks_like_number($number)
MC/DC100%2/2
$kind eq "float" and not &Scalar::Util::looks_like_number($number)
$kind eq "float"
not &Scalar::Util::looks_like_number($number)
Truth table100%3/3
A and not(B)
A= $kind eq "float"B= not &Scalar::Util::looks_like_number($number)
| |||||||||||||||||||||||||||||
| 120 | if $kind eq 'float' && !Scalar::Util::looks_like_number($number); | ||||||||||||||||||||||||||||
| 121 | 32 | ▶ | Carp::croak(__PACKAGE__ . "->new: '$number' is not a KDL number") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$kind eq "string" and not Text::KDL::XS::_is_number_literal($number)
MC/DC100%2/2
$kind eq "string" and not Text::KDL::XS::_is_number_literal($number)
$kind eq "string"
not Text::KDL::XS::_is_number_literal($number)
Truth table100%3/3
A and not(B)
A= $kind eq "string"B= not Text::KDL::XS::_is_number_literal($number)
| |||||||||||||||||||||||||||||
| 122 | if $kind eq 'string' && !Text::KDL::XS::_is_number_literal($number); | ||||||||||||||||||||||||||||
| 123 | 24 | return ($kind, $number); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # A Perl number is an integer or a float; any other text must be a KDL number | ||||||||||||||||||||||||||||
| 127 | # literal, which is kept verbatim as kind 'string'. | ||||||||||||||||||||||||||||
| 128 | 7 | ▶ | sub _inferred_kind { | ||||||||||||||||||||||||||
|
Subroutine
_inferred_kind: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 129 | 7 | my ($number) = @_; | |||||||||||||||||||||||||||
| 130 | 7 | my $kind = Text::KDL::XS::_number_kind_of($number); | |||||||||||||||||||||||||||
| 131 | 7 | ▶ | return $kind if defined $kind; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 2 | ▶ | return 'string' if Text::KDL::XS::_is_number_literal($number); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 1 | Carp::croak(__PACKAGE__ . "->new: '$number' is not a number"); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # A native number from a Perl number or numeric text. A number is returned as | ||||||||||||||||||||||||||||
| 137 | # it is: arithmetic would turn -0.0 into 0. | ||||||||||||||||||||||||||||
| 138 | 10011 | ▶ | sub _numified { | ||||||||||||||||||||||||||
|
Subroutine
_numified: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 139 | 10011 | my ($number) = @_; | |||||||||||||||||||||||||||
| 140 | 10011 | ▶ | return $number if defined Text::KDL::XS::_number_kind_of($number); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 9453 | return 0 + $number; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # A native number from KDL number text; lossy beyond the range and precision | ||||||||||||||||||||||||||||
| 145 | # of Perl's numbers. | ||||||||||||||||||||||||||||
| 146 | 9457 | ▶ | sub _number_from_text { | ||||||||||||||||||||||||||
|
Subroutine
_number_from_text: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 147 | 9457 | my ($text) = @_; | |||||||||||||||||||||||||||
| 148 | 9457 | ▶ | return $KEYWORD_NUMBER{$text} if exists $KEYWORD_NUMBER{$text}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 9455 | (my $digits = $text) =~ tr/_//d; | |||||||||||||||||||||||||||
| 150 | 9455 | ▶ | my ($sign, $radix, $magnitude) = $digits =~ /\A([+-]?)0([xob])(.+)\z/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | or return _numified($digits); | ||||||||||||||||||||||||||||
| 152 | 24 | ▶ | no warnings qw(overflow portable); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 153 | 2 | ▶ | my $number = oct($radix eq 'o' ? "0$magnitude" : "0$radix$magnitude"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 2 | ▶ | return $sign eq '-' ? -$number : $number; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # A Math::BigInt for integral KDL number text, a Math::BigFloat otherwise. | ||||||||||||||||||||||||||||
| 158 | 6 | ▶ | sub _bignum_from_text { | ||||||||||||||||||||||||||
|
Subroutine
_bignum_from_text: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 159 | 6 | my ($text) = @_; | |||||||||||||||||||||||||||
| 160 | 6 | require Math::BigInt; | |||||||||||||||||||||||||||
| 161 | 6 | require Math::BigFloat; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 6 | ▶ | return Math::BigFloat->binf('+') if $text eq '#inf'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 5 | ▶ | return Math::BigFloat->binf('-') if $text eq '#-inf'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 5 | ▶ | return Math::BigFloat->bnan if $text eq '#nan'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 5 | (my $digits = $text) =~ tr/_//d; | |||||||||||||||||||||||||||
| 168 | 5 | ▶ | if (my ($sign, $radix, $magnitude) = $digits =~ /\A([+-]?)0([xob])(.+)\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 2 | my $number = _bigint_from_radix($radix, $magnitude); | |||||||||||||||||||||||||||
| 170 | 2 | ▶ | return $sign eq '-' ? $number->bneg : $number; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 3 | ▶ | return $digits =~ /[.eE]/ ? Math::BigFloat->new($digits) : Math::BigInt->new($digits); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2 | ▶ | sub _bigint_from_radix { | ||||||||||||||||||||||||||
|
Subroutine
_bigint_from_radix: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 176 | 2 | my ($radix, $magnitude) = @_; | |||||||||||||||||||||||||||
| 177 | 2 | ▶ | return Math::BigInt->new("0x$magnitude") if $radix eq 'x'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 1 | ▶ | return Math::BigInt->new("0b$magnitude") if $radix eq 'b'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | # Octal goes through the binary form (three bits per digit), which every | ||||||||||||||||||||||||||||
| 180 | # Math::BigInt version reads. | ||||||||||||||||||||||||||||
| 181 | 1 | return Math::BigInt->new('0b' . join '', map { sprintf '%03b', $_ } split //, $magnitude); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 24 | 1; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||||||||||