Skip to content

Commit 56f2fb0

Browse files
committed
update the code to run in v13
1 parent bbe455e commit 56f2fb0

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ jobs:
1313
- name: Zig test
1414
uses: goto-bus-stop/setup-zig@v2
1515
with:
16-
version: 0.11.0
16+
version: 0.13.0
1717
- run: make

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
main*
22
!main.zig
33
zig-cache/
4+
.zig-cache/

main.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn readFile(allocator: Allocator, path: []const u8) ![]u8 {
99
var f = try fs.cwd().openFile(path, file.OpenFlags{ .mode = file.OpenMode.read_only });
1010
const fMetadata = try f.stat();
1111

12-
var output = try allocator.alloc(u8, fMetadata.size);
12+
const output = try allocator.alloc(u8, fMetadata.size);
1313
errdefer allocator.free(output);
1414

1515
_ = try f.readAll(output);
@@ -30,7 +30,7 @@ pub fn main() !void {
3030

3131
// try stdout.print("{}", .{decoded});
3232

33-
var encInput = try readFile(allocator, "encode");
33+
const encInput = try readFile(allocator, "encode");
3434
defer allocator.free(encInput);
3535

3636
const encoded = try snappy.encode(allocator, encInput);

snappy.zig

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const std = @import("std");
22
const Allocator = std.mem.Allocator;
3-
const crc32 = std.hash.crc;
3+
const crc32 = std.hash.crc.Crc32Iscsi;
44
const mem = std.mem;
55
const testing = std.testing;
66

@@ -38,8 +38,9 @@ const SnappyError = error{
3838
// Perform the CRC hash per the snappy documentation. We must use wrapping addition since this is
3939
// the default behavior in other languages.
4040
fn crc(b: []const u8) u32 {
41-
const c = crc32.Crc32SmallWithPoly(.Castagnoli);
42-
const hash = c.hash(b);
41+
var c = crc32.init();
42+
c.update(b);
43+
const hash = c.final();
4344
return @as(u32, hash >> 15 | hash << 17) +% 0xa282ead8;
4445
}
4546

@@ -172,7 +173,7 @@ fn runDecode(dst: []u8, src: []const u8) u8 {
172173
return 1;
173174
}
174175

175-
mem.copy(u8, dst[d..], src[s .. s + @as(usize, @intCast(length))]);
176+
std.mem.copyForwards(u8, dst[d..], src[s .. s + @as(usize, @intCast(length))]);
176177
const l = @as(usize, @intCast(length));
177178
d += l;
178179
s += l;
@@ -217,14 +218,14 @@ fn runDecode(dst: []u8, src: []const u8) u8 {
217218

218219
if (offset >= length) {
219220
const upper_bound = d - @as(usize, @intCast(offset)) + @as(usize, @intCast(length));
220-
mem.copy(u8, dst[d .. d + @as(usize, @intCast(length))], dst[d - @as(usize, @intCast(offset)) .. upper_bound]);
221+
std.mem.copyForwards(u8, dst[d .. d + @as(usize, @intCast(length))], dst[d - @as(usize, @intCast(offset)) .. upper_bound]);
221222
d += @as(usize, @intCast(length));
222223
continue;
223224
}
224225

225226
var a = dst[d .. d + @as(usize, @intCast(length))];
226227
var b = dst[d - @as(usize, @intCast(offset)) ..];
227-
var aLen = a.len;
228+
const aLen = a.len;
228229
b = b[0..aLen];
229230
for (a, 0..) |_, i| {
230231
a[i] = b[i];
@@ -244,11 +245,11 @@ fn runDecode(dst: []u8, src: []const u8) u8 {
244245
pub fn decode(allocator: Allocator, src: []const u8) ![]u8 {
245246
const block = try decodedLen(src);
246247

247-
var dst = try allocator.alloc(u8, block.blockLen);
248+
const dst = try allocator.alloc(u8, block.blockLen);
248249
errdefer allocator.free(dst);
249250

250251
// Skip past how many bytes we read to get the length.
251-
var s = src[block.headerLen..];
252+
const s = src[block.headerLen..];
252253

253254
if (runDecode(dst, s) != 0) {
254255
return SnappyError.Corrupt;
@@ -278,7 +279,7 @@ fn emitLiteral(dst: []u8, lit: []const u8) usize {
278279
i = 3;
279280
},
280281
}
281-
mem.copy(u8, dst[i..], lit);
282+
std.mem.copyForwards(u8, dst[i..], lit);
282283

283284
return i + @min(dst.len, lit.len);
284285
}
@@ -346,7 +347,7 @@ fn encodeBlock(dst: []u8, src: []u8) usize {
346347
}
347348

348349
var table = mem.zeroes([maxTableSize]u16);
349-
var sLimit = src.len - inputMargin;
350+
const sLimit = src.len - inputMargin;
350351
var nextEmit: usize = 0;
351352
var s: usize = 1;
352353
var nextHash = snappyHash(load32(src, @as(isize, @intCast(s))), shift);
@@ -358,7 +359,7 @@ fn encodeBlock(dst: []u8, src: []u8) usize {
358359

359360
inner: while (true) {
360361
s = nextS;
361-
var bytesBetweenHashLookups = skip >> 5;
362+
const bytesBetweenHashLookups = skip >> 5;
362363
nextS = s + @as(usize, @intCast(bytesBetweenHashLookups));
363364
skip += bytesBetweenHashLookups;
364365
if (nextS > sLimit) {
@@ -375,7 +376,7 @@ fn encodeBlock(dst: []u8, src: []u8) usize {
375376
d += emitLiteral(dst[d..], src[nextEmit..s]);
376377

377378
while (true) {
378-
var base = s;
379+
const base = s;
379380
s += 4;
380381
var i = @as(usize, @intCast(candidate + 4));
381382
while (s < src.len and src[i] == src[s]) {
@@ -389,10 +390,10 @@ fn encodeBlock(dst: []u8, src: []u8) usize {
389390
break :outer;
390391
}
391392

392-
var x = load64(src, @as(isize, @intCast(s - 1)));
393-
var prevHash = snappyHash(@as(u32, @truncate(x >> 0)), shift);
393+
const x = load64(src, @as(isize, @intCast(s - 1)));
394+
const prevHash = snappyHash(@as(u32, @truncate(x >> 0)), shift);
394395
table[prevHash & tableMask] = @as(u16, @intCast(s - 1));
395-
var currHash = snappyHash(@as(u32, @truncate(x >> 8)), shift);
396+
const currHash = snappyHash(@as(u32, @truncate(x >> 8)), shift);
396397
candidate = @as(isize, @intCast(table[currHash & tableMask]));
397398
table[currHash & tableMask] = @as(u16, @intCast(s));
398399
if (@as(u32, @truncate(x >> 8)) != load32(src, candidate)) {
@@ -425,7 +426,7 @@ pub fn encode(allocator: Allocator, src: []u8) ![]u8 {
425426

426427
while (mutSrc.len > 0) {
427428
var p = try allocator.alloc(u8, mutSrc.len);
428-
mem.copy(u8, p, mutSrc);
429+
std.mem.copyForwards(u8, p, mutSrc);
429430
var empty = [_]u8{};
430431
mutSrc = empty[0..];
431432
if (p.len > maxBlockSize) {
@@ -440,8 +441,8 @@ pub fn encode(allocator: Allocator, src: []u8) ![]u8 {
440441
allocator.free(p);
441442
}
442443

443-
var output = try allocator.alloc(u8, d);
444-
mem.copy(u8, output, dst[0..d]);
444+
const output = try allocator.alloc(u8, d);
445+
std.mem.copyForwards(u8, output, dst[0..d]);
445446
allocator.free(dst);
446447

447448
return output;
@@ -463,7 +464,8 @@ pub fn maxEncodedLen(srcLen: usize) isize {
463464
}
464465

465466
test "snappy crc" {
466-
try testing.expect(crc("snappy") == 0x293d0c23);
467+
const snappycrc = crc("snappy");
468+
try testing.expect(snappycrc == 0x293d0c23);
467469
}
468470

469471
test "decoding variable integers" {
@@ -481,17 +483,18 @@ test "simple encode" {
481483
const allocator = testing.allocator;
482484

483485
var input: [4]u8 = [_]u8{ 't', 'h', 'i', 's' };
484-
var i: []u8 = &input;
485-
var output = try encode(allocator, i);
486+
const i: []u8 = &input;
487+
const output = try encode(allocator, i);
488+
486489
defer allocator.free(output);
487490

488491
try testing.expectEqualSlices(u8, output, "\x04\x0cthis");
489492
}
490493

491494
test "simple decode" {
492495
const allocator = testing.allocator;
493-
494-
const decoded = try decode(allocator, "\x19\x1coh snap,\x05\x06,py is cool!\x0a");
496+
const encodedbytes = "\x19\x1coh snap,\x05\x06,py is cool!\x0a";
497+
const decoded = try decode(allocator, encodedbytes);
495498
defer allocator.free(decoded);
496499

497500
try testing.expectEqualSlices(u8, decoded, "oh snap, snappy is cool!\n");

0 commit comments

Comments
 (0)