M4/Impl/Default/BaseConversion.pm

filter: Statementstmt 95.6% 220 / 230 Branchbran 58.1% 50 / 86 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 37 / 37 Podpod n/a totaltotal 85.1% 309 / 363 CC 47 SCAR 28.5
CC
47
Cov
84%
bitvector_fr_base
32.8
bitvector_to_base
25.9
BEGIN
1.0
CRAP 17.2
« M4/Impl/Default/Eval.pm ↑ Summary M4/Impl/Regexp.pm »
1 7 ▶use Moops;
Subroutine __ANON__: called CC=1 SCAR=0.0
2  
3 # PODNAME: MarpaX::Languages::M4::Impl::Default::BaseConversion
4  
5 # ABSTRACT: Base conversion util class
6 5  
7 7 ▶class MarpaX::Languages::M4::Impl::Default::BaseConversion {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.1
8 5 ▶ use Types::Common::Numeric -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶ use Bit::Vector;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶ use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.020'; # VERSION
13  
14 3 our $AUTHORITY = 'cpan:JDDPAUSE'; # AUTHORITY
15  
16 #
17 # We handle bases [0..31].
18 #
19  
20 #
21 # Eval: constants for radix and the grammar
22 #
23 3 our @nums = ( 0 .. 9, 'a' .. 'z' );
24 3 our %nums = map { $nums[$_] => $_ } 0 .. $#nums;
25  
26 # Adaptation of http://www.perlmonks.org/?node_id=27148
27 5 ▶ method bitvector_fr_base (ClassName $class: PositiveInt $bits, PositiveInt|Undef $base, Str $input, Bool $binary?) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine bitvector_fr_base: called CC=24 SCAR=34.9
Branch: 16 branches
BranchTrueFalseTotal
@_ ? : 344 0 344
do { Types::Standard::_is_class_loaded(do { my $tmp = $class }) } or _failed_check(14, "ClassName", $class, "exception_class", "Error::TypeTiny::Assertion") 344 0 344
if @_ < 3 0 344 344
if @_ > 4 0 344 344
$#_ >= 0 ? : 344 0 344
if ($#_ >= 0) 344 0 344
Type::Tiny::XS::PositiveInt($bits) or _failed_check(47, "PositiveInt", $bits, "exception_class", "Error::TypeTiny::Assertion") 344 0 344
$#_ >= 1 ? : 344 0 344
if ($#_ >= 1) 344 0 344
Type::Tiny::XS::AUTO::TC4($base) or _failed_check(104, "PositiveInt|Undef", $base, "exception_class", "Error::TypeTiny::Assertion") 344 0 344
$#_ >= 2 ? : 344 0 344
if ($#_ >= 2) 344 0 344
Type::Tiny::XS::Str($input) or _failed_check(9, "Str", $input, "exception_class", "Error::TypeTiny::Assertion") 344 0 344
$#_ >= 3 ? : 344 0 344
if ($#_ >= 3) 344 0 344
Type::Tiny::XS::Bool($binary) or _failed_check(3, "Bool", $binary, "exception_class", "Error::TypeTiny::Assertion") 344 0 344
28 #
29 # Per def the caller is responsabible to make sure input can contain only [0..9a-zA-Z].
30 # Thus it is safe to call lc()
31 #
32 # Note that we use $bits + 1, because Bit::Vector->Multiply() treats its arguments
33 # as SIGNED.
34 # Therefore we cannot reach the case where all bits would be setted to 1.
35 # We resize at the very end.
36 #
37 #
38 # Radix 1, i.e. the unary numeral system is a special case. GNU M4 say that the
39 # '1' is used to represent it, leading zeroes being ignored, and all remaining digits
40 # must be 1.
41 # The "value" is then just a count of them (== unary system).
42 #
43  
44 344 ▶ if ($binary) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binary) 1 343 344
45 1 return Bit::Vector->new_Bin( $bits, $input );
46 }
47 343 ▶ if ( $base == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base == 1) 1 342 343
48 1 $input =~ s/^0*//;
49 1 ▶ if ( $input =~ /[^1]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /[^1]/u) 0 1 1
50 0 croak
51 "radix 1 imposes eventual leading zeroes followed by zero or more '1' character(s)";
52 }
53 1 return Bit::Vector->new_Dec( $bits, length($input) );
54 }
55  
56 342 my $b = Bit::Vector->new_Dec( $bits + 1, $base );
57 342 my $v = Bit::Vector->new( $bits + 1 );
58 342 my $i = 0;
59 342 for ( lc($input) =~ /./g ) {
60 424 ++$i;
61 {
62 424 my $s = $v->Shadow;
63 424 $s->Multiply( $v, $b );
64 424 $v = $s;
65 }
66 424 my $num = $nums{$_};
67 424 ▶ if ( $num >= $base ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num >= $base) 0 424 424
68 0 my $range = '';
69 0 ▶ if ( $base <= 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base <= 10) { } 0 0 0
70 0 $range = '[0-' . ( $base - 1 ) . ']';
71 }
72 else {
73 0 $range = '[0-9';
74 0 ▶ if ( $base == 11 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base == 11) { } 0 0 0
75 0 $range .= 'a] (case independant)';
76 }
77 else {
78 0 $range
79 .= 'a-'
80 . $nums[ $base - 1 ]
81 . '] (case independant)';
82 }
83 }
84 0 croak "character '$_' is not in the range $range";
85 }
86 {
87 424 my $s = $v->Shadow;
88 424 my $n = Bit::Vector->new_Dec( $bits + 1, $num );
89 424 $s->add( $v, $n, 0 );
90 424 $v = $s;
91 }
92 }
93 342 $v->Resize($bits);
94 342 return $v;
95 }
96  
97 5 ▶ method bitvector_to_base (ClassName $class: PositiveInt $base, ConsumerOf['Bit::Vector'] $v, Int $min --> Str) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine bitvector_to_base: called CC=24 SCAR=32.6
Branch: 12 branches
BranchTrueFalseTotal
@_ ? : 162 0 162
do { Types::Standard::_is_class_loaded(do { my $tmp = $class }) } or _failed_check(14, "ClassName", $class, "exception_class", "Error::TypeTiny::Assertion") 162 0 162
if @_ != 3 0 162 162
$#_ >= 0 ? : 162 0 162
if ($#_ >= 0) 162 0 162
Type::Tiny::XS::PositiveInt($base) or _failed_check(47, "PositiveInt", $base, "exception_class", "Error::TypeTiny::Assertion") 162 0 162
$#_ >= 1 ? : 162 0 162
if ($#_ >= 1) 162 0 162
do { builtin::blessed($v) and do { my $method = $v->can("DOES") || $v->can("isa"); $v->$method("Bit::Vector") } } or _failed_check(105, "ConsumerOf[\"Bit::Vector\"]", $v, "exception_class", "Error::TypeTiny::Assertion") 162 0 162
$#_ >= 2 ? : 162 0 162
if ($#_ >= 2) 162 0 162
Type::Tiny::XS::Int($min) or _failed_check(13, "Int", $min, "exception_class", "Error::TypeTiny::Assertion") 162 0 162
Condition33%2/6
builtin::blessed($v) && do { my $method = $v->can('DOES') || $v->can('isa'); $v->$method('Bit::Vector') }
!ll&&!rl&&r
00162
$v->can('DOES') || $v->can('isa')
l!l&&r!l&&!r
16200
MC/DC0%0/2
builtin::blessed($v) && do { my $method = $v->can('DOES') || $v->can('isa'); $v->$method('Bit::Vector') }
builtin::blessed($v) do { my $method = $v->can('DOES') || $v->can('isa'); $v->$method('Bit::Vector') }
MC/DC0%0/2
$v->can('DOES') || $v->can('isa')
$v->can('DOES') $v->can('isa')
Truth table33%1/3
A && B
A= builtin::blessed($v)B= do { my $method = $v->can('DOES') || $v->can('isa'); $v->$method('Bit::Vector') }
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= $v->can('DOES')B= $v->can('isa')
ABresult
1X1
011
000
98  
99 162 my $b = Bit::Vector->new_Dec( $v->Size(), $base );
100 162 $v = $v->Clone();
101 #
102 # Per construction $base is in the range [1..61]
103 #
104 162 my $rep = '';
105 162 my $s = '';
106 162 ▶ my $signed = ( $v->Sign() < 0 ) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$v->Sign < 0 ? : 9 153 162
107 162 my $abs;
108 162 ▶ if ($signed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signed) { } 9 153 162
109 9 $abs = $v->Shadow;
110 9 $abs->Negate($v);
111 }
112 else {
113 153 $abs = $v;
114 }
115  
116 162 ▶ if ( $base == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base == 1) 1 161 162
117 1 my $rep = '1' x $abs->to_Dec();
118 #
119 # Adapt to width
120 #
121 1 ▶ if ( length($rep) < $min ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rep < $min) 1 0 1
122 1 $rep = ( '0' x ( $min - length($rep) ) ) . $rep;
123 }
124 1 ▶ if ($signed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signed) 0 1 1
125 0 $rep = "-$rep";
126 }
127 1 return $rep;
128 }
129  
130 161 ▶ while ( $abs->to_Dec() ne "0" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($abs->to_Dec ne 0) 231 161 392
131 231 my $mod;
132 {
133 231 my $s = $abs->Shadow;
134 231 $abs->Shadow->Divide( $abs, $b, $s );
135 231 $mod = $s;
136 }
137 #
138 # Why abs() ? Because when $v is equal to 2^(n-1), number remains the same.
139 #
140 231 $s = $nums[ abs($mod->to_Dec()) ] . $s;
141 {
142 231 my $s = $abs->Shadow;
143 231 $s->Divide( $abs, $b, $abs->Shadow );
144 231 $abs = $s;
145 }
146 }
147 161 ▶ if ($signed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signed) 9 152 161
148 9 $s = "-$s";
149 }
150 161 ▶ if ( substr( $s, 0, 1 ) eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, 0, 1) eq "-") 9 152 161
151 9 $rep .= '-';
152 9 substr( $s, 0, 1, '' );
153 }
154 161 ▶ for ( $min -= length($s); --$min >= 0; ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$min >= 0) 19 161 180
155 19 $rep .= '0';
156 }
157 161 $rep .= $s;
158  
159 161 return $rep;
160 }
161 }
162  
163 5 1;
164  
165 __END__
« M4/Impl/Default/Eval.pm ↑ Summary M4/Impl/Regexp.pm »