blib/lib/Proquint.pm

filter: Statementstmt 98.7% 78 / 79 Branchbran 60.0% 12 / 20 Conditioncond 40.7% 11 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 4 totaltotal 71.1% 111 / 156 CC 24 SCAR 16.2
CC
24
Cov
80%
uint32proquint
7.0
_chunk_to_uint16
6.3
proquint2hex
5.6
CRAP 5.0
↑ Summary
1 package Proquint;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Exporter::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.003';
8 1 our @ISA = 'Exporter::Tiny';
9 1 our @EXPORT_OK = (qw/uint32proquint proquint32uint hex2proquint proquint2hex/);
10 1 our @EXPORT_TAGS = ( all => \@EXPORT_OK );
11  
12 1 my @UINT_TO_CONSONANT = (qw/ b d f g h j k l m n p r s t v z /);
13 1 my @UINT_TO_VOWEL = (qw/ a i o u /);
14 1 my $CHARS_PER_CHUNK = 5;
15 1 my $MASK_LAST2 = 0x3;
16 1 my $MASK_LAST4 = 0xF;
17 1 my $SEPARATOR = '-';
18  
19 1 my %CONSONANT_TO_UINT = do {
20 1 my $i = 0;
21 1 map { $_ => $i++ } @UINT_TO_CONSONANT;
22 };
23  
24 1 my %VOWEL_TO_UINT = do {
25 1 my $i = 0;
26 1 map { $_ => $i++ } @UINT_TO_VOWEL;
27 };
28  
29 7 ▶sub _uint16_to_chunk {
Subroutine _uint16_to_chunk: called CC=4 SCAR=13.9
30 7 ▶ my $in = shift // Carp::croak 'usage: _uint16_to_chunk($INTEGER)';
Condition50%1/2
shift() // Carp::croak("usage: _uint16_to_chunk(\$INTEGER)")
l!l
70
MC/DC0%0/1
shift() // Carp::croak("usage: _uint16_to_chunk(\$INTEGER)")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
31 7 my $out = '';
32  
33 7 foreach my $i ( 1 .. $CHARS_PER_CHUNK ) {
34 35 ▶ if ( $i & 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i & 1) { } 21 14 35
35 21 $out .= $UINT_TO_CONSONANT[ $in & $MASK_LAST4 ];
36 21 $in >>= 4;
37 }
38 else {
39 14 $out .= $UINT_TO_VOWEL[ $in & $MASK_LAST2 ];
40 14 $in >>= 2;
41 }
42 }
43 7 scalar reverse $out;
44 }
45  
46 # uint32proquint(0x7f000001) eq 'lusab-babad';
47 1 ▶sub uint32proquint {
Subroutine uint32proquint: called CC=5 SCAR=19.4
Pod undocumented
48 1 ▶ my $in = shift // Carp::croak 'usage: uint32proquint($INTEGER)';
Condition50%1/2
shift() // Carp::croak("usage: uint32proquint(\$INTEGER)")
l!l
10
MC/DC0%0/1
shift() // Carp::croak("usage: uint32proquint(\$INTEGER)")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
49 1 ▶ my $sep = shift // $SEPARATOR;
Condition33%1/3
shift() // $SEPARATOR
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() // $SEPARATOR
shift() $SEPARATOR
Truth table33%1/3
A // B
A= shift()B= $SEPARATOR
ABresult
1X1
011
000
50  
51 1 ▶ Carp::croak('input out of range 0-0xFFFFFFFF')
Branch: 1 branch
BranchTrueFalseTotal
if $in < 0 or $in > 4294967295 0 1 1
Condition33%1/3
$in < 0 or $in > 4294967295
l!l&&r!l&&!r
001
MC/DC0%0/2
$in < 0 or $in > 4294967295
$in < 0 $in > 4294967295
Truth table33%1/3
A or B
A= $in < 0B= $in > 4294967295
ABresult
1X1
011
000
52 if $in < 0 or $in > 0xffffffff;
53  
54 1 _uint16_to_chunk( $in >> 16 ) . $sep . _uint16_to_chunk($in);
55 }
56  
57 7 ▶sub _chunk_to_uint16 {
Subroutine _chunk_to_uint16: called CC=6 SCAR=18.5
58 7 ▶ my $in = shift // Carp::croak 'usage: _chunk_to_uint16($INTEGER)';
Condition50%1/2
shift() // Carp::croak("usage: _chunk_to_uint16(\$INTEGER)")
l!l
70
MC/DC0%0/1
shift() // Carp::croak("usage: _chunk_to_uint16(\$INTEGER)")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
59  
60 7 ▶ Carp::croak 'invalid chunk: ' . $in unless length($in) == $CHARS_PER_CHUNK;
Branch: 1 branch
BranchTrueFalseTotal
unless length $in == $CHARS_PER_CHUNK 0 7 7
61  
62 7 my $res = 0;
63 7 foreach my $c ( split //, $in ) {
64 35 ▶ if ( exists $CONSONANT_TO_UINT{$c} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $CONSONANT_TO_UINT{$c}) { } 21 14 35
elsif (exists $VOWEL_TO_UINT{$c}) { } 14 0 14
65 21 $res <<= 4;
66 21 $res += $CONSONANT_TO_UINT{$c};
67 }
68 elsif ( exists $VOWEL_TO_UINT{$c} ) {
69 14 $res <<= 2;
70 14 $res += $VOWEL_TO_UINT{$c};
71 }
72 else {
73 0 Carp::croak 'invalid quint char: ' . $c;
74 }
75 }
76  
77 7 $res;
78 }
79  
80 # proquint32uint('lusab-babad') == 0x7f000001;
81 1 ▶sub proquint32uint {
Subroutine proquint32uint: called CC=5 SCAR=17.0
Pod undocumented
82 1 ▶ my $in = shift // Carp::croak 'usage: proquint32uint($QUINT)';
Condition50%1/2
shift() // Carp::croak("usage: proquint32uint(\$QUINT)")
l!l
10
MC/DC0%0/1
shift() // Carp::croak("usage: proquint32uint(\$QUINT)")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
83 1 ▶ my $sep = shift // $SEPARATOR;
Condition33%1/3
shift() // $SEPARATOR
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() // $SEPARATOR
shift() $SEPARATOR
Truth table33%1/3
A // B
A= shift()B= $SEPARATOR
ABresult
1X1
011
000
84  
85 1 $in =~ s/$sep//g;
86 1 ▶ Carp::croak 'invalid quint: ' . $in
Branch: 1 branch
BranchTrueFalseTotal
if length($in) % $CHARS_PER_CHUNK 0 1 1
87 unless not length($in) % $CHARS_PER_CHUNK;
88  
89 1 my @chunks = $in =~ m/(.{$CHARS_PER_CHUNK})/gx;
90 1 ▶ Carp::croak 'invalid quint: ' . $in unless @chunks == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @chunks == 2 0 1 1
91  
92 1 my $out = _chunk_to_uint16( $chunks[0] );
93 1 $out <<= 16;
94 1 $out += _chunk_to_uint16( $chunks[1] );
95 1 $out;
96 }
97  
98 # hex2proquint('7f00001') eq 'lusab-babad'
99 2 ▶sub hex2proquint {
Subroutine hex2proquint: called CC=4 SCAR=15.0
Pod undocumented
100 2 ▶ my $in = shift // Carp::croak 'usage: hex2proquint($HEXIDECIMAL)';
Condition50%1/2
shift() // Carp::croak("usage: hex2proquint(\$HEXIDECIMAL)")
l!l
20
MC/DC0%0/1
shift() // Carp::croak("usage: hex2proquint(\$HEXIDECIMAL)")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
101 2 ▶ my $sep = shift // $SEPARATOR;
Condition33%1/3
shift() // $SEPARATOR
l!l&&r!l&&!r
020
MC/DC0%0/2
shift() // $SEPARATOR
shift() $SEPARATOR
Truth table33%1/3
A // B
A= shift()B= $SEPARATOR
ABresult
1X1
011
000
102  
103 2 $in =~ s/^0[xX]//;
104  
105 2 ▶ Carp::croak 'input must be multiple of 4-characters'
Branch: 1 branch
BranchTrueFalseTotal
if length($in) % 4 0 2 2
106 unless not length($in) % 4;
107  
108 join( $sep,
109 2 map { _uint16_to_chunk( hex( '0x' . $_ ) ) } $in =~ m/(.{4})/g );
110 }
111  
112 # proquint2hex('lusab-babad') eq '7f000001';
113 2 ▶sub proquint2hex {
Subroutine proquint2hex: called CC=5 SCAR=17.3
Pod undocumented
114 2 ▶ my $in = shift // Carp::croak 'usage: proquint2hex($QUINT)';
Condition50%1/2
shift() // Carp::croak("usage: proquint2hex(\$QUINT)")
l!l
20
MC/DC0%0/1
shift() // Carp::croak("usage: proquint2hex(\$QUINT)")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
115 2 ▶ my $sep = shift // $SEPARATOR;
Condition33%1/3
shift() // $SEPARATOR
l!l&&r!l&&!r
020
MC/DC0%0/2
shift() // $SEPARATOR
shift() $SEPARATOR
Truth table33%1/3
A // B
A= shift()B= $SEPARATOR
ABresult
1X1
011
000
116  
117 2 $in =~ s/$sep//g;
118 2 ▶ Carp::croak 'invalid quint: ' . $in
Branch: 1 branch
BranchTrueFalseTotal
if length($in) % $CHARS_PER_CHUNK 0 2 2
119 unless not length($in) % $CHARS_PER_CHUNK;
120  
121 2 my @chunks = $in =~ m/(.{$CHARS_PER_CHUNK})/g;
122 2 ▶ Carp::croak 'invalid quint: ' . $in unless @chunks;
Branch: 1 branch
BranchTrueFalseTotal
unless @chunks 0 2 2
123  
124 2 join( '', map { sprintf( '%04x', _chunk_to_uint16($_) ) } @chunks );
125 }
126  
127 1 1;
128  
129 __END__
↑ Summary