AnyBase/Converter.pm

filter: Statementstmt 100% 47 / 47 Branchbran 70.0% 7 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 92.3% 60 / 65 CC 7 SCAR 11.0
CC
7
Cov
95%
decode
4.0
encode
4.0
BEGIN
1.0
CRAP 3.0
↑ Summary AnyBase.pm »
1 package Convert::AnyBase::Converter;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Convert::AnyBase::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 has set => qw/is ro required 1 isa Str/;
9 1 has base => qw/is ro lazy_build 1/, init_arg => undef;
10 20 ▶sub _build_base {
Subroutine _build_base: called CC=1 SCAR=0.0
11 20 return length shift->set;
12 }
13 1 has normalize => qw/is ro isa CodeRef/;
14  
15 37 ▶sub encode {
Subroutine encode: called CC=4 SCAR=13.9
Pod undocumented
16 37 my $self = shift;
17 37 my $number = shift;
18  
19 37 ▶ croak "Can't encode \"$number\"" unless looks_like_number $number;
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($number) 0 37 37
20  
21 37 my $set = $self->set;
22 37 my $base = $self->base;
23  
24 37 my ( $done, @string );
25  
26 37 ▶ while ( ! $done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 237 37 274
27 237 my $quotient = int( $number / $base );
28 237 my $remainder;
29 237 ▶ if ( $quotient != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quotient != 0) { } 200 37 237
30 200 $remainder = $number % $base;
31 200 $number = $quotient;
32 }
33 else {
34 37 $remainder = $number;
35 37 $done = 1;
36 }
37  
38 237 push @string, substr $set, $remainder, 1;
39 }
40  
41 37 return join '', reverse @string;
42 }
43  
44 34 ▶sub decode {
Subroutine decode: called CC=4 SCAR=13.9
Pod undocumented
45 34 my $self = shift;
46 34 my $string = shift;
47  
48 34 my $set = $self->set;
49 34 my $base = $self->base;
50 34 my $normalize = $self->normalize;
51  
52 34 ▶ if ( $normalize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($normalize) 34 0 34
53 34 local $_ = $string;
54 34 $string = $normalize->();
55 }
56  
57 34 my $number = 0;
58 34 my $offset = 1;
59 34 my @string = reverse split m//, $string;
60  
61 34 for ( @string ) {
62 229 my $value = index $set, $_;
63 229 ▶ croak "Unknown character $_ in input \"$string\"\n" if -1 == $value;
Branch: 1 branch
BranchTrueFalseTotal
if -1 == $value 0 229 229
64 229 $number += ( $value * $offset );
65 229 $offset *= $base;
66 }
67  
68 34 return $number;
69 }
70  
71 1 1;
↑ Summary AnyBase.pm »