line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Base36; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
115577
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
131
|
|
4
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
117
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
16
|
use base qw( Exporter ); |
|
3
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
288
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
16
|
use Carp 'croak'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
187
|
|
9
|
3
|
|
|
3
|
|
7261
|
use Math::BigInt (); |
|
3
|
|
|
|
|
92502
|
|
|
3
|
|
|
|
|
1481
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw(encode_base36 decode_base36) ] ); |
12
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{ 'all' } } ); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.12'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub decode_base36 { |
17
|
16
|
|
|
16
|
1
|
7005
|
my $base36 = uc( shift ); |
18
|
16
|
100
|
|
|
|
312
|
croak "Invalid base36 number ($base36)" if $base36 =~ m{[^0-9A-Z]}; |
19
|
|
|
|
|
|
|
|
20
|
14
|
|
|
|
|
25
|
my ( $result, $digit ) = ( 0, 0 ); |
21
|
14
|
|
|
|
|
66
|
for my $char ( split( //, reverse $base36 ) ) { |
22
|
48
|
100
|
|
|
|
15823
|
my $value = $char =~ m{\d} ? $char : ord( $char ) - 55; |
23
|
48
|
|
|
|
|
188
|
$result += $value * Math::BigInt->new( 36 )->bpow( $digit++ ); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
14
|
|
|
|
|
11187
|
return $result; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub encode_base36 { |
30
|
20
|
|
|
20
|
1
|
2419
|
my ( $number, $padlength ) = @_; |
31
|
20
|
|
100
|
|
|
123
|
$padlength ||= 1; |
32
|
|
|
|
|
|
|
|
33
|
20
|
100
|
|
|
|
215
|
croak "Invalid base10 number ($number)" if $number =~ m{\D}; |
34
|
18
|
100
|
|
|
|
217
|
croak "Invalid padding length ($padlength)" if $padlength =~ m{\D}; |
35
|
|
|
|
|
|
|
|
36
|
16
|
|
|
|
|
33
|
my $result = ''; |
37
|
16
|
|
|
|
|
52
|
while ( $number ) { |
38
|
44
|
|
|
|
|
5763
|
my $remainder = $number % 36; |
39
|
44
|
100
|
|
|
|
3728
|
$result .= $remainder <= 9 ? $remainder : chr( 55 + $remainder ); |
40
|
44
|
|
|
|
|
7368
|
$number = int $number / 36; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
16
|
|
|
|
|
300
|
return '0' x ( $padlength - length $result ) . reverse( $result ); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
1; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
__END__ |