forked from Rotwang/digest-xxhash
/
xxHash.pm6
156 lines (130 loc) · 3 KB
/
xxHash.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use v6;
use NativeCall;
unit module Digest::xxHash;
constant XXHASH = %?RESOURCES<libraries/xxhash>.Str;
# xxHash C wrapper functions (/usr/lib/libxxhash.so) {{{
# unsigned int XXH32 (const void* input, size_t length, unsigned seed);
sub XXH32(
CArray[int8],
size_t,
uint32
--> uint32
) is native(XXHASH, v0.6.4)
{*}
# unsigned long long XXH64 (const void* input, size_t length, unsigned long long seed);
sub XXH64(
CArray[int8],
size_t,
ulonglong
--> ulonglong
) is native(XXHASH, v0.6.4)
{*}
# end xxHash C wrapper functions (/usr/lib/libxxhash.so) }}}
# 32 or 64 bit {{{
multi sub xxHash(
Str $string,
Str :$enc = 'UTF-8',
Int :$seed = 0
--> Int
) is export
{
my Int @data = $string.encode($enc).list;
build-xxhash(@data, $seed);
}
multi sub xxHash(Str :$file!, Int :$seed = 0 --> Int) is export
{
xxHash($file.IO.slurp(:bin), :$seed);
}
multi sub xxHash(IO $fh, Int :$seed = 0 --> Int) is export
{
xxHash($fh.slurp(:bin), :$seed);
}
multi sub xxHash(Buf[uint8] $buf-u8, Int :$seed = 0 --> Int) is export
{
my Int @data = $buf-u8.list;
build-xxhash(@data, $seed);
}
multi sub build-xxhash(
Int @data,
Int $seed = 0,
$? where { $*KERNEL.bits == 64 }
--> Int
)
{
build-xxhash64(@data, $seed);
}
multi sub build-xxhash(
Int @data,
Int $seed = 0,
$?
--> Int
)
{
build-xxhash32(@data, $seed);
}
# end 32 or 64 bit }}}
# 32 bit only {{{
multi sub xxHash32(
Str $string,
Str :$enc = 'UTF-8',
Int :$seed = 0
--> Int
) is export
{
my Int @data = $string.encode($enc).list;
build-xxhash32(@data, $seed);
}
multi sub xxHash32(Str :$file!, Int :$seed = 0 --> Int) is export
{
xxHash32($file.IO.slurp(:bin), :$seed);
}
multi sub xxHash32(IO $fh, Int :$seed = 0 --> Int) is export
{
xxHash32($fh.slurp(:bin), :$seed);
}
multi sub xxHash32(Buf[uint8] $buf-u8, Int :$seed = 0 --> Int) is export
{
my Int @data = $buf-u8.list;
build-xxhash32(@data, $seed);
}
sub build-xxhash32(Int @data, uint $seed = 0 --> uint)
{
my @input := CArray[int8].new;
my Int $len = 0;
@data.map({ @input[$len++] = $_ });
XXH32(@input, $len, $seed);
}
# end 32 bit only }}}
# 64 bit only {{{
multi sub xxHash64(
Str $string,
Str :$enc = 'UTF-8',
Int :$seed = 0
--> Int
) is export
{
my Int @data = $string.encode($enc).list;
build-xxhash64(@data, $seed);
}
multi sub xxHash64(Str :$file!, Int :$seed = 0 --> Int) is export
{
xxHash64($file.IO.slurp(:bin), :$seed);
}
multi sub xxHash64(IO $fh, Int :$seed = 0 --> Int) is export
{
xxHash64($fh.slurp(:bin), :$seed);
}
multi sub xxHash64(Buf[uint8] $buf-u8, Int :$seed = 0 --> Int) is export
{
my Int @data = $buf-u8.list;
build-xxhash64(@data, $seed);
}
sub build-xxhash64(Int @data, ulonglong $seed = 0 --> ulonglong)
{
my @input := CArray[int8].new;
my Int $len = 0;
@data.map({ @input[$len++] = $_ });
XXH64(@input, $len, $seed);
}
# end 64 bit only }}}
# vim: set filetype=perl6 foldmethod=marker foldlevel=0: