From 4a592d3fac424b7d95dbfdc11c813276033f2627 Mon Sep 17 00:00:00 2001 From: guha-rahul <69rahul16@gmail.com> Date: Thu, 16 Jan 2025 20:52:43 +0530 Subject: [PATCH 1/2] simplify tests and --- build.zig.zon | 4 ++-- src/frames.zig | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index 497c6b7..4945a6c 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,8 +3,8 @@ .version = "0.0.1", .dependencies = .{ .snappyz = .{ - .url = "https://github.com/blockblaz/zig-snappy/archive/13fe068.tar.gz", - .hash = "1220613ed9cbd8ec648d59db9b798644b3578f8ca36c8ec313426ac19dc9f95da825", + .url = "https://github.com/guha-rahul/zig-snappy/archive/d1fd3d7.tar.gz", + .hash = "1220c856c5d5b87a07060fd0f74568a36ac9d619cf711d949d342c2340df936d20e7", }, }, .paths = .{""}, diff --git a/src/frames.zig b/src/frames.zig index 6a2810a..8c32fe1 100644 --- a/src/frames.zig +++ b/src/frames.zig @@ -40,7 +40,7 @@ pub fn decode(allocator: Allocator, writer: anytype, data: []const u8) !void { } } -pub fn encode(allocator: Allocator, writer: anytype, data: []u8) !void { +pub fn encode(allocator: Allocator, writer: anytype, data: []const u8) !void { // push identifier frame try writer.writeAll(&IDENTIFIER_FRAME); var i: usize = 0; @@ -58,7 +58,8 @@ pub fn encode(allocator: Allocator, writer: anytype, data: []u8) !void { defer allocator.free(crc_bytes); std.mem.writePackedInt(u32, crc_bytes, 0, crc_hash, .little); - try writer.writeAll(frame_header ++ crc_bytes[0..4]); + try writer.writeAll(&frame_header); + try writer.writeAll(crc_bytes[0..4]); try writer.writeAll(compressed); } else { const size = chunk.len + 4; @@ -69,7 +70,8 @@ pub fn encode(allocator: Allocator, writer: anytype, data: []u8) !void { defer allocator.free(crc_bytes); std.mem.writePackedInt(u32, crc_bytes, 0, crc_hash, .little); - try writer.writeAll(frame_header ++ crc_bytes[0..4]); + try writer.writeAll(&frame_header); + try writer.writeAll(crc_bytes[0..4]); try writer.writeAll(chunk); } i = i + UNCOMPRESSED_CHUNK_SIZE; @@ -147,34 +149,27 @@ test "decode" { test "encode" { // this should lead to an uncompressed chunk const data = "thissNaPpY"; - const data_slice = try std.testing.allocator.alloc(u8, data.len); - defer std.testing.allocator.free(data_slice); - - std.mem.copyForwards(u8, data_slice, data); var arraylistdata = std.ArrayList(u8).init(std.testing.allocator); defer arraylistdata.deinit(); const fbswriter = arraylistdata.writer(); - try encode(std.testing.allocator, fbswriter, data_slice); + try encode(std.testing.allocator, fbswriter, data); const dumped = arraylistdata.items; const expected = IDENTIFIER_FRAME ++ [_]u8{ 0x01, 0x0e, 0x00, 0x00, 0x58, 0x09, 0xd7, 0x88 } ++ "thissNaPpY"; - try std.testing.expectEqualSlices(std.meta.Child([]const u8), dumped, expected); + try std.testing.expectEqualSlices(u8, dumped, expected); } test "encode<>decode" { // this should lead to a compressed chunk const data = "thissNaPpYYYYYYYYYYYYYYYYYYYY"; - const data_slice = try std.testing.allocator.alloc(u8, data.len); - defer std.testing.allocator.free(data_slice); - std.mem.copyForwards(u8, data_slice, data); var arraylistdata = std.ArrayList(u8).init(std.testing.allocator); defer arraylistdata.deinit(); const fbswriter = arraylistdata.writer(); - try encode(std.testing.allocator, fbswriter, data_slice); + try encode(std.testing.allocator, fbswriter, data); const encoded = arraylistdata.items; var arraylistdata1 = std.ArrayList(u8).init(std.testing.allocator); @@ -183,5 +178,5 @@ test "encode<>decode" { try decode(std.testing.allocator, fbswriter1, encoded); const decoded = arraylistdata1.items; - try std.testing.expectEqualSlices(std.meta.Child([]const u8), data, decoded); + try std.testing.expectEqualSlices(u8, data, decoded); } From 45f545adba079b6c2e697a3b5cde94fc5074863a Mon Sep 17 00:00:00 2001 From: guha-rahul <69rahul16@gmail.com> Date: Fri, 17 Jan 2025 01:50:52 +0530 Subject: [PATCH 2/2] chore: bump version --- build.zig.zon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.zig.zon b/build.zig.zon index 4945a6c..b3042ce 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,7 +3,7 @@ .version = "0.0.1", .dependencies = .{ .snappyz = .{ - .url = "https://github.com/guha-rahul/zig-snappy/archive/d1fd3d7.tar.gz", + .url = "https://github.com/blockblaz/zig-snappy/archive/beb2a56.tar.gz", .hash = "1220c856c5d5b87a07060fd0f74568a36ac9d619cf711d949d342c2340df936d20e7", }, },