line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Base36; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
16173
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
140
|
|
4
|
3
|
|
|
3
|
|
10
|
use warnings; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
79
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
14
|
use base qw( Exporter ); |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
184
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
9
|
use Carp 'croak'; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
119
|
|
9
|
3
|
|
|
3
|
|
4598
|
use Math::BigInt (); |
|
3
|
|
|
|
|
28583
|
|
|
3
|
|
|
|
|
920
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw(encode_base36 decode_base36) ] ); |
12
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{ 'all' } } ); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.14'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub decode_base36 { |
17
|
17
|
|
|
17
|
1
|
3679
|
my $base36 = uc( shift ); |
18
|
17
|
100
|
|
|
|
193
|
croak "Invalid base36 number ($base36)" if $base36 =~ m{[^0-9A-Z]}; |
19
|
|
|
|
|
|
|
|
20
|
15
|
|
|
|
|
16
|
my ( $result, $digit ) = ( 0, 0 ); |
21
|
15
|
|
|
|
|
42
|
for my $char ( split( //, reverse $base36 ) ) { |
22
|
73
|
100
|
|
|
|
12881
|
my $value = $char =~ m{\d} ? $char : ord( $char ) - 55; |
23
|
73
|
|
|
|
|
122
|
$result += $value * Math::BigInt->new( 36 )->bpow( $digit++ ); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
15
|
|
|
|
|
3855
|
return $result; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub encode_base36 { |
30
|
21
|
|
|
21
|
1
|
1667
|
my ( $number, $padlength ) = @_; |
31
|
21
|
|
100
|
|
|
68
|
$padlength ||= 1; |
32
|
|
|
|
|
|
|
|
33
|
21
|
100
|
|
|
|
133
|
croak "Invalid base10 number ($number)" if $number =~ m{\D}; |
34
|
19
|
100
|
|
|
|
137
|
croak "Invalid padding length ($padlength)" if $padlength =~ m{\D}; |
35
|
|
|
|
|
|
|
|
36
|
17
|
|
|
|
|
39
|
$number = Math::BigInt->new( $number ); |
37
|
17
|
|
|
|
|
485
|
my $result = ''; |
38
|
17
|
|
|
|
|
34
|
while ( $number ) { |
39
|
68
|
|
|
|
|
6096
|
my $remainder = $number % 36; |
40
|
68
|
100
|
|
|
|
5650
|
$result .= $remainder <= 9 ? $remainder : chr( 55 + $remainder ); |
41
|
68
|
|
|
|
|
8232
|
$number = int $number / 36; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
17
|
|
|
|
|
1611
|
my $padding = $padlength - length $result; |
45
|
17
|
|
|
|
|
21
|
my $return = reverse( $result ); |
46
|
17
|
100
|
|
|
|
31
|
$return = '0' x $padding . $return if $padding > 0; |
47
|
17
|
|
|
|
|
59
|
return $return; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
1; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
__END__ |