line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Base36; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
23364
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
131
|
|
4
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
98
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
25
|
use base qw( Exporter ); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
321
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
19
|
use Carp 'croak'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
228
|
|
9
|
3
|
|
|
3
|
|
2361
|
use Math::BigInt (); |
|
3
|
|
|
|
|
48641
|
|
|
3
|
|
|
|
|
1274
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw(encode_base36 decode_base36) ] ); |
12
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{ 'all' } } ); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.13'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub decode_base36 { |
17
|
17
|
|
|
17
|
1
|
7802
|
my $base36 = uc( shift ); |
18
|
17
|
100
|
|
|
|
364
|
croak "Invalid base36 number ($base36)" if $base36 =~ m{[^0-9A-Z]}; |
19
|
|
|
|
|
|
|
|
20
|
15
|
|
|
|
|
33
|
my ( $result, $digit ) = ( 0, 0 ); |
21
|
15
|
|
|
|
|
69
|
for my $char ( split( //, reverse $base36 ) ) { |
22
|
73
|
100
|
|
|
|
25162
|
my $value = $char =~ m{\d} ? $char : ord( $char ) - 55; |
23
|
73
|
|
|
|
|
495
|
$result += $value * Math::BigInt->new( 36 )->bpow( $digit++ ); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
15
|
|
|
|
|
7346
|
return $result; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub encode_base36 { |
30
|
21
|
|
|
21
|
1
|
3053
|
my ( $number, $padlength ) = @_; |
31
|
21
|
|
100
|
|
|
131
|
$padlength ||= 1; |
32
|
|
|
|
|
|
|
|
33
|
21
|
100
|
|
|
|
263
|
croak "Invalid base10 number ($number)" if $number =~ m{\D}; |
34
|
19
|
100
|
|
|
|
280
|
croak "Invalid padding length ($padlength)" if $padlength =~ m{\D}; |
35
|
|
|
|
|
|
|
|
36
|
17
|
|
|
|
|
79
|
$number = Math::BigInt->new( $number ); |
37
|
17
|
|
|
|
|
896
|
my $result = ''; |
38
|
17
|
|
|
|
|
73
|
while ( $number ) { |
39
|
68
|
|
|
|
|
12483
|
my $remainder = $number % 36; |
40
|
68
|
100
|
|
|
|
27632
|
$result .= $remainder <= 9 ? $remainder : chr( 55 + $remainder ); |
41
|
68
|
|
|
|
|
23178
|
$number = int $number / 36; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
17
|
|
|
|
|
2730
|
return '0' x ( $padlength - length $result ) . reverse( $result ); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
1; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
__END__ |