line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hashids::Util; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
69834
|
use strict; |
|
6
|
|
|
|
|
21
|
|
|
6
|
|
|
|
|
181
|
|
4
|
6
|
|
|
6
|
|
31
|
use warnings; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
302
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our ( @EXPORT_OK, %EXPORT_TAGS ); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
BEGIN { |
9
|
6
|
|
|
6
|
|
35
|
use Exporter 'import'; |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
375
|
|
10
|
6
|
|
|
6
|
|
37
|
@EXPORT_OK = qw(consistent_shuffle to_alphabet from_alphabet any bignum); |
11
|
6
|
|
|
|
|
160
|
%EXPORT_TAGS = ( all => \@EXPORT_OK ); |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
6
|
|
|
6
|
|
49
|
use List::Util 'reduce'; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
657
|
|
15
|
6
|
|
|
6
|
|
6241
|
use Math::BigInt; |
|
6
|
|
|
|
|
153860
|
|
|
6
|
|
|
|
|
35
|
|
16
|
|
|
|
|
|
|
|
17
|
6
|
|
|
6
|
|
138517
|
use namespace::clean -except => [qw(import)]; |
|
6
|
|
|
|
|
94338
|
|
|
6
|
|
|
|
|
52
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub consistent_shuffle { |
20
|
3232
|
|
|
3232
|
1
|
12509
|
my ( $alphabet, $salt ) = @_; |
21
|
|
|
|
|
|
|
|
22
|
3232
|
100
|
|
|
|
7443
|
return ('') unless $alphabet; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my @alphabet |
25
|
3231
|
100
|
|
|
|
27022
|
= ref $alphabet eq 'ARRAY' ? @$alphabet : split // => $alphabet; |
26
|
3231
|
100
|
|
|
|
6302
|
return @alphabet unless $salt; |
27
|
3216
|
100
|
|
|
|
24187
|
my @salt = ref $salt eq 'ARRAY' ? @$salt : split //, $salt; |
28
|
|
|
|
|
|
|
|
29
|
3216
|
|
|
|
|
10300
|
for ( my ( $i, $v, $p ) = ( $#alphabet, 0, 0 ); $i > 0; $i--, $v++ ) { |
30
|
136919
|
|
|
|
|
200835
|
$p += my $int = ord $salt[ $v %= @salt ]; |
31
|
136919
|
|
|
|
|
184793
|
my $j = ( $int + $v + $p ) % $i; |
32
|
|
|
|
|
|
|
|
33
|
136919
|
|
|
|
|
302545
|
@alphabet[ $j, $i ] = @alphabet[ $i, $j ]; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
3216
|
|
|
|
|
46140
|
@alphabet; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub to_alphabet { |
40
|
2113
|
|
|
2113
|
1
|
9389
|
my ( $num, $alphabet ) = @_; |
41
|
|
|
|
|
|
|
|
42
|
2113
|
|
|
|
|
3288
|
my $hash = ''; |
43
|
|
|
|
|
|
|
my @alphabet |
44
|
2113
|
100
|
|
|
|
14897
|
= ref $alphabet eq 'ARRAY' ? @$alphabet : split // => $alphabet; |
45
|
|
|
|
|
|
|
|
46
|
2113
|
|
|
|
|
4339
|
$num = bignum($num); |
47
|
2113
|
|
|
|
|
319104
|
do { |
48
|
4311
|
|
|
|
|
988872
|
$hash = $alphabet[ $num % @alphabet ] . $hash; |
49
|
4311
|
|
|
|
|
885028
|
$num = int($num / @alphabet); |
50
|
|
|
|
|
|
|
} while ( $num != 0 ); |
51
|
|
|
|
|
|
|
|
52
|
2113
|
|
|
|
|
941853
|
$hash; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub from_alphabet { |
56
|
1053
|
|
|
1053
|
1
|
2271
|
my ( $hash, $alphabet ) = @_; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my @alphabet |
59
|
1053
|
100
|
|
|
|
7342
|
= ref $alphabet eq 'ARRAY' ? @$alphabet : split // => $alphabet; |
60
|
|
|
|
|
|
|
|
61
|
1103
|
|
|
1103
|
|
50713
|
my $num = reduce { bignum($a) * @alphabet + $b } |
62
|
1053
|
|
|
|
|
6059
|
map { index join( '' => @alphabet ), $_ } split // => $hash; |
|
2156
|
|
|
|
|
11081
|
|
63
|
|
|
|
|
|
|
|
64
|
1053
|
|
|
|
|
449860
|
"$num"; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub any (&@) { ## no critic (ProhibitSubroutinePrototypes) |
68
|
357
|
|
|
357
|
1
|
10912
|
my $f = shift; |
69
|
357
|
|
|
|
|
590
|
for (@_) { |
70
|
27207
|
100
|
|
|
|
69100
|
return 1 if $f->(); |
71
|
|
|
|
|
|
|
} |
72
|
50
|
|
|
|
|
632
|
0; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub bignum { |
76
|
9508
|
|
|
9508
|
1
|
23602
|
my $n = Math::BigInt->bzero(); |
77
|
9508
|
|
|
|
|
410021
|
$n->round_mode('zero'); |
78
|
9508
|
|
|
|
|
126841
|
$n->badd("@{[shift]}"); |
|
9508
|
|
|
|
|
33164
|
|
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
1; |
82
|
|
|
|
|
|
|
__END__ |