line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hashids::Util; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
67349
|
use strict; |
|
6
|
|
|
|
|
23
|
|
|
6
|
|
|
|
|
172
|
|
4
|
6
|
|
|
6
|
|
28
|
use warnings; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
275
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our ( @EXPORT_OK, %EXPORT_TAGS ); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
BEGIN { |
9
|
6
|
|
|
6
|
|
31
|
use Exporter 'import'; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
420
|
|
10
|
6
|
|
|
6
|
|
37
|
@EXPORT_OK = qw(consistent_shuffle to_alphabet from_alphabet any bignum); |
11
|
6
|
|
|
|
|
198
|
%EXPORT_TAGS = ( all => \@EXPORT_OK ); |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
6
|
|
|
6
|
|
37
|
use List::Util 'reduce'; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
601
|
|
15
|
6
|
|
|
6
|
|
5976
|
use Math::BigInt; |
|
6
|
|
|
|
|
143944
|
|
|
6
|
|
|
|
|
28
|
|
16
|
|
|
|
|
|
|
|
17
|
6
|
|
|
6
|
|
132792
|
use namespace::clean -except => [qw(import)]; |
|
6
|
|
|
|
|
89598
|
|
|
6
|
|
|
|
|
46
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub consistent_shuffle { |
20
|
3232
|
|
|
3232
|
1
|
12385
|
my ( $alphabet, $salt ) = @_; |
21
|
|
|
|
|
|
|
|
22
|
3232
|
100
|
|
|
|
7684
|
return ('') unless $alphabet; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my @alphabet |
25
|
3231
|
100
|
|
|
|
25916
|
= ref $alphabet eq 'ARRAY' ? @$alphabet : split // => $alphabet; |
26
|
3231
|
100
|
|
|
|
6506
|
return @alphabet unless $salt; |
27
|
3216
|
100
|
|
|
|
23439
|
my @salt = ref $salt eq 'ARRAY' ? @$salt : split //, $salt; |
28
|
|
|
|
|
|
|
|
29
|
3216
|
|
|
|
|
10013
|
for ( my ( $i, $v, $p ) = ( $#alphabet, 0, 0 ); $i > 0; $i--, $v++ ) { |
30
|
136919
|
|
|
|
|
193307
|
$p += my $int = ord $salt[ $v %= @salt ]; |
31
|
136919
|
|
|
|
|
182780
|
my $j = ( $int + $v + $p ) % $i; |
32
|
|
|
|
|
|
|
|
33
|
136919
|
|
|
|
|
301306
|
@alphabet[ $j, $i ] = @alphabet[ $i, $j ]; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
3216
|
|
|
|
|
46496
|
@alphabet; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub to_alphabet { |
40
|
2113
|
|
|
2113
|
1
|
8759
|
my ( $num, $alphabet ) = @_; |
41
|
|
|
|
|
|
|
|
42
|
2113
|
|
|
|
|
3242
|
my $hash = ''; |
43
|
|
|
|
|
|
|
my @alphabet |
44
|
2113
|
100
|
|
|
|
14668
|
= ref $alphabet eq 'ARRAY' ? @$alphabet : split // => $alphabet; |
45
|
|
|
|
|
|
|
|
46
|
2113
|
|
|
|
|
3770
|
$num = bignum($num); |
47
|
2113
|
|
|
|
|
314894
|
do { |
48
|
4311
|
|
|
|
|
978608
|
$hash = $alphabet[ $num % @alphabet ] . $hash; |
49
|
4311
|
|
|
|
|
874648
|
$num = int($num / @alphabet); |
50
|
|
|
|
|
|
|
} while ( $num != 0 ); |
51
|
|
|
|
|
|
|
|
52
|
2113
|
|
|
|
|
928522
|
$hash; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub from_alphabet { |
56
|
1053
|
|
|
1053
|
1
|
2276
|
my ( $hash, $alphabet ) = @_; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my @alphabet |
59
|
1053
|
100
|
|
|
|
7332
|
= ref $alphabet eq 'ARRAY' ? @$alphabet : split // => $alphabet; |
60
|
|
|
|
|
|
|
|
61
|
1103
|
|
|
1103
|
|
50522
|
my $num = reduce { bignum($a) * @alphabet + $b } |
62
|
1053
|
|
|
|
|
5788
|
map { index join( '' => @alphabet ), $_ } split // => $hash; |
|
2156
|
|
|
|
|
10344
|
|
63
|
|
|
|
|
|
|
|
64
|
1053
|
|
|
|
|
442891
|
"$num"; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub any (&@) { ## no critic (ProhibitSubroutinePrototypes) |
68
|
357
|
|
|
357
|
1
|
9955
|
my $f = shift; |
69
|
357
|
|
|
|
|
585
|
for (@_) { |
70
|
27207
|
100
|
|
|
|
70696
|
return 1 if $f->(); |
71
|
|
|
|
|
|
|
} |
72
|
50
|
|
|
|
|
612
|
0; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub bignum { |
76
|
9508
|
|
|
9508
|
1
|
22970
|
my $n = Math::BigInt->bzero(); |
77
|
9508
|
|
|
|
|
407046
|
$n->round_mode('zero'); |
78
|
9508
|
|
|
|
|
128866
|
$n->badd("@{[shift]}"); |
|
9508
|
|
|
|
|
31865
|
|
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
1; |
82
|
|
|
|
|
|
|
__END__ |