Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › hash › Adler32 Public Struct Adler32 Doc Source Public Struct Fields Namespaces Values Functions pub fn permute (state : u32 , input : []const u8 ) u32 pub fn update (a : *Adler32 , input : []const u8 ) void pub fn hash (input : []const u8 ) u32 File hash/Adler32.zig
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 //! https://tools.ietf.org/html/rfc1950#section-9
//! https://github.com/madler/zlib/blob/master/adler32.c
const Adler32 = @This ();
const std = @import ("std " );
const testing = std .testing ;
adler : u32 = 1 ,
pub fn permute (state : u32 , input : []const u8 ) u32 {
const base = 65521 ;
const nmax = 5552 ;
var s1 = state & 0xffff ;
var s2 = (state >> 16 ) & 0xffff ;
if (input .len == 1 ) {
s1 +%= input [0 ];
if (s1 >= base ) {
s1 -= base ;
}
s2 +%= s1 ;
if (s2 >= base ) {
s2 -= base ;
}
} else if (input .len < 16 ) {
for (input ) |b | {
s1 +%= b ;
s2 +%= s1 ;
}
if (s1 >= base ) {
s1 -= base ;
}
s2 %= base ;
} else {
const n = nmax / 16 ; // note: 16 | nmax
var i : usize = 0 ;
while (i + nmax <= input .len ) {
var rounds : usize = 0 ;
while (rounds < n ) : (rounds += 1 ) {
comptime var j : usize = 0 ;
inline while (j < 16 ) : (j += 1 ) {
s1 +%= input [i + j ];
s2 +%= s1 ;
}
i += 16 ;
}
s1 %= base ;
s2 %= base ;
}
if (i < input .len ) {
while (i + 16 <= input .len ) : (i += 16 ) {
comptime var j : usize = 0 ;
inline while (j < 16 ) : (j += 1 ) {
s1 +%= input [i + j ];
s2 +%= s1 ;
}
}
while (i < input .len ) : (i += 1 ) {
s1 +%= input [i ];
s2 +%= s1 ;
}
s1 %= base ;
s2 %= base ;
}
}
return s1 | (s2 << 16 );
}
pub fn update (a : *Adler32 , input : []const u8 ) void {
a .adler = permute (a .adler , input );
}
pub fn hash (input : []const u8 ) u32 {
return permute (1 , input );
}
test "sanity" {
try testing .expectEqual (@as (u32 , 0x620062 ), hash ("a" ));
try testing .expectEqual (@as (u32 , 0xbc002ed ), hash ("example" ));
}
test "long" {
const long1 : [1024 ]u8 = @splat (1 );
try testing .expectEqual (@as (u32 , 0x06780401 ), hash (long1 [0 ..]));
const long2 : [1025 ]u8 = @splat (1 );
try testing .expectEqual (@as (u32 , 0x0a7a0402 ), hash (long2 [0 ..]));
}
test "very long" {
const long : [5553 ]u8 = @splat (1 );
try testing .expectEqual (@as (u32 , 0x707f15b2 ), hash (long [0 ..]));
}
test "very long with variation" {
const long = comptime blk : {
@setEvalBranchQuota (7000 );
var result : [6000 ]u8 = undefined ;
var i : usize = 0 ;
while (i < result .len ) : (i += 1 ) {
result [i ] = @as (u8 , @truncate (i ));
}
break :blk result ;
};
try testing .expectEqual (@as (u32 , 0x5af38d6e ), hash (long [0 ..]));
}
https://tools.ietf.org/html/rfc1950#section-9 https://github.com/madler/zlib/blob/master/adler32.c