| 1 | package Math::Int2Base; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008006; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | our @EXPORT_OK = qw( int2base base2int base_chars ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | my @Chars = ('0'..'9', 'A'..'Z', 'a'..'z'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | my $MaxBase = scalar @Chars; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | my $CharStr = join '', @Chars; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # int2base( $num, $base, $minlen ); # base ||= 10 minlen ||= 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 134 | ▶ | sub int2base { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
int2base: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 134 | my( $ret, $num, $base, $minlen ) = ( '', @_ ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 134 | ▶ | $num ||= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$num ||= 0
MC/DC100%1/1
$num ||= 0
$num
Truth table100%2/2
A
A= $num
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 134 | ▶ | $base ||= 10; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$base ||= 10
MC/DC0%0/1
$base ||= 10
$base
Truth table50%1/2
A
A= $base
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 134 | ▶ | $minlen ||= 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$minlen ||= 1
MC/DC100%1/1
$minlen ||= 1
$minlen
Truth table100%2/2
A
A= $minlen
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 134 | ▶ | if( $num < 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$num < 0 or $base < 2 or $MaxBase < $base or $minlen < 1
$num < 0 or $base < 2 or $MaxBase < $base
$num < 0 or $base < 2
MC/DC100%4/4
$num < 0 or $base < 2 or $MaxBase < $base or $minlen < 1
$num < 0
$base < 2
$MaxBase < $base
$minlen < 1
Truth table100%5/5
A or B or C or D
A= $num < 0B= $base < 2C= $MaxBase < $baseD= $minlen < 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | || $base < 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | || $MaxBase < $base | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | || $minlen < 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # || $num != int( $num ) # XXX[1] do we care? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 4 | croak "not supported: int2base( '$num', $base, $minlen )" } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 130 | ▶ | for (; $num; $num = int($num/$base) ) { $ret .= $Chars[$num % $base] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | no warnings 'numeric'; # negative repeat count | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 130 | return scalar reverse $ret . '0'x($minlen - length($ret)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # base2int( $num, $base ); # base ||= 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 68 | ▶ | sub base2int { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
base2int: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 68 | my( $ret, $num, $base ) = ( 0, @_ ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 68 | ▶ | $num ||= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$num ||= 0
MC/DC100%1/1
$num ||= 0
$num
Truth table100%2/2
A
A= $num
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 68 | ▶ | $base ||= 10; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$base ||= 10
MC/DC0%0/1
$base ||= 10
$base
Truth table50%1/2
A
A= $base
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 68 | my $chars = substr $CharStr, 0, $base; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 68 | ▶ | if( $num !~ /^[$chars]+$/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $num =~ /^[$chars]+$/ or $base < 2 or $MaxBase < $base
not $num =~ /^[$chars]+$/ or $base < 2
MC/DC66%2/3
not $num =~ /^[$chars]+$/ or $base < 2 or $MaxBase < $base
not $num =~ /^[$chars]+$/
$base < 2
$MaxBase < $base
Truth table75%3/4
not(A) or B or C
A= not $num =~ /^[$chars]+$/B= $base < 2C= $MaxBase < $base
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | || $base < 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | || $MaxBase < $base ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | croak "not supported: base2int( '$num', $base )" } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 64 | $num =~ s/^0+//; # trim leading zeros | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 64 | ▶ | for( my $i = length($num)-1, my $c = 0; $i >= 0; --$i ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 170 | $ret += index($CharStr, substr($num, $i, 1)) * $base**$c++ } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 64 | return $ret; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # base_chars( $base ); # base ||= 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | sub base_chars { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
base_chars: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | my( $base ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | $base ||= 10; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$base ||= 10
MC/DC0%0/1
$base ||= 10
$base
Truth table50%1/2
A
A= $base
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | return substr $CharStr, 0, $base; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||