From db70f7e35558c0df34cce0497ec0eea8e984d30d Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 4 Jun 2026 20:12:02 -0700 Subject: [PATCH] delete more functions, we're moving these elsewhere --- src/BufIndexer.zig | 61 --------------------------- src/FixedMaxBuffer.zig | 52 ----------------------- src/indexBufferT.zig | 24 ----------- src/lib.zig | 16 ------- src/pipe.zig | 22 ---------- src/randomBytes.zig | 13 ------ src/randomSlice.zig | 22 ---------- src/readType.zig | 95 ------------------------------------------ src/skipToBoundary.zig | 61 --------------------------- 9 files changed, 366 deletions(-) delete mode 100644 src/BufIndexer.zig delete mode 100644 src/FixedMaxBuffer.zig delete mode 100644 src/indexBufferT.zig delete mode 100644 src/pipe.zig delete mode 100644 src/randomBytes.zig delete mode 100644 src/randomSlice.zig delete mode 100644 src/readType.zig delete mode 100644 src/skipToBoundary.zig diff --git a/src/BufIndexer.zig b/src/BufIndexer.zig deleted file mode 100644 index f3f3a26840d0fcdc1958dbf0128ab3bde419fa46..0000000000000000000000000000000000000000 --- a/src/BufIndexer.zig +++ /dev/null @@ -1,61 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); -const indexBufferT = extras.indexBufferT; -const rawIntBytes = extras.rawIntBytes; - -pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type { - return struct { - bytes: [*]const u8, - max_len: usize, - - const Self = @This(); - - pub fn init(bytes: [*]const u8, max_len: usize) Self { - return .{ - .bytes = bytes, - .max_len = max_len, - }; - } - - /// asserts 'idx' to be in bounds - pub fn at(self: *const Self, idx: usize) T { - return indexBufferT(self.bytes, T, endian, idx, self.max_len); - } - }; -} - -test { - const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); - const bindex = BufIndexer(u64, .big).init(&bytes, 1); - try std.testing.expect(bindex.at(0) == 0x4e5a7da9f3f1d132); -} - -test { - const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); - const bindex = BufIndexer(u32, .big).init(&bytes, 2); - try std.testing.expect(bindex.at(0) == 0x4e5a7da9); - try std.testing.expect(bindex.at(1) == 0xf3f1d132); -} - -test { - const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); - const bindex = BufIndexer(u16, .big).init(&bytes, 4); - try std.testing.expect(bindex.at(0) == 0x4e5a); - try std.testing.expect(bindex.at(1) == 0x7da9); - try std.testing.expect(bindex.at(2) == 0xf3f1); - try std.testing.expect(bindex.at(3) == 0xd132); -} - -test { - const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); - const bindex = BufIndexer(u8, .big).init(&bytes, 8); - try std.testing.expect(bindex.at(0) == 0x4e); - try std.testing.expect(bindex.at(1) == 0x5a); - try std.testing.expect(bindex.at(2) == 0x7d); - try std.testing.expect(bindex.at(3) == 0xa9); - try std.testing.expect(bindex.at(4) == 0xf3); - try std.testing.expect(bindex.at(5) == 0xf1); - try std.testing.expect(bindex.at(6) == 0xd1); - try std.testing.expect(bindex.at(7) == 0x32); -} diff --git a/src/FixedMaxBuffer.zig b/src/FixedMaxBuffer.zig deleted file mode 100644 index 210b6c7d16de3ababce2b5dacb8f7e622a2a8a12..0000000000000000000000000000000000000000 --- a/src/FixedMaxBuffer.zig +++ /dev/null @@ -1,52 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); -const assert = std.debug.assert; - -pub fn FixedMaxBuffer(comptime max_len: usize) type { - return struct { - buf: [max_len]u8, - len: usize, - pos: usize, - - const Self = @This(); - pub const Reader = std.io.GenericReader(*Self, error{}, read); - - pub fn init(r: anytype, runtime_len: usize) !Self { - var fmr = Self{ - .buf = undefined, - .len = runtime_len, - .pos = 0, - }; - _ = try r.readAll(fmr.buf[0..runtime_len]); - return fmr; - } - - pub fn reader(self: *Self) Reader { - return .{ .context = self }; - } - - fn read(self: *Self, dest: []u8) error{}!usize { - const buf = self.buf[0..self.len]; - const size = @min(dest.len, buf.len - self.pos); - const end = self.pos + size; - std.mem.copy(u8, dest[0..size], buf[self.pos..end]); - self.pos = end; - return size; - } - - pub fn readLen(self: *Self, len: usize) []const u8 { - assert(self.pos + len <= self.len); - defer self.pos += len; - return self.buf[self.pos..][0..len]; - } - - pub fn atEnd(self: *const Self) bool { - return self.pos == self.len; - } - }; -} - -test { - std.testing.refAllDecls(FixedMaxBuffer(16)); -} diff --git a/src/indexBufferT.zig b/src/indexBufferT.zig deleted file mode 100644 index 92afc431668651416c81d520402e5ea219ff9bfc..0000000000000000000000000000000000000000 --- a/src/indexBufferT.zig +++ /dev/null @@ -1,24 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); -const readType = extras.readType; -const rawIntBytes = extras.rawIntBytes; - -pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.Endian, idx: usize, max_len: usize) T { - std.debug.assert(idx < max_len); - var fbs = std.io.fixedBufferStream((bytes + (idx * @sizeOf(T)))[0..@sizeOf(T)]); - return readType(fbs.reader(), T, endian) catch |err| switch (err) { - error.EndOfStream => unreachable, // assert above has been violated - }; -} - -test { - const bytes = rawIntBytes(u32, 0x4e5a7da9); - try std.testing.expect(indexBufferT(&bytes, u16, .big, 0, 2) == 0x4e5a); - try std.testing.expect(indexBufferT(&bytes, u16, .big, 1, 2) == 0x7da9); -} -test { - const bytes = rawIntBytes(u32, 0x4e5a7da9); - try std.testing.expect(indexBufferT(&bytes, u16, .little, 0, 2) == 0x5a4e); - try std.testing.expect(indexBufferT(&bytes, u16, .little, 1, 2) == 0xa97d); -} diff --git a/src/lib.zig b/src/lib.zig index 6d045b505052045e0786f948b1507a11b3f90706..b200eeeaa3e0a87c97123fd55d0f591638d49419 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -6,7 +6,6 @@ const builtin = @import("builtin"); pub const reduceNumber = @import("./reduceNumber.zig").reduceNumber; pub const fmtByteCountIEC = @import("./fmtByteCountIEC.zig").fmtByteCountIEC; -pub const randomSlice = @import("./randomSlice.zig").randomSlice; pub const trimPrefix = @import("./trimPrefix.zig").trimPrefix; pub const trimPrefixEnsure = @import("./trimPrefixEnsure.zig").trimPrefixEnsure; pub const trimSuffix = @import("./trimSuffix.zig").trimSuffix; @@ -15,7 +14,6 @@ pub const base64EncodeAlloc = @import("./base64EncodeAlloc.zig").base64EncodeAll pub const base64DecodeAlloc = @import("./base64DecodeAlloc.zig").base64DecodeAlloc; pub const asciiUpper = @import("./asciiUpper.zig").asciiUpper; pub const sliceToInt = @import("./sliceToInt.zig").sliceToInt; -pub const pipe = @import("./pipe.zig").pipe; pub const countScalar = @import("./countScalar.zig").countScalar; pub const sortBy = @import("./sortBy.zig").sortBy; pub const sortBySlice = @import("./sortBySlice.zig").sortBySlice; @@ -23,13 +21,7 @@ pub const lessThanSlice = @import("./lessThanSlice.zig").lessThanSlice; pub const containsString = @import("./containsString.zig").containsString; pub const ensureFieldSubset = @import("./ensureFieldSubset.zig").ensureFieldSubset; pub const fmtReplacer = @import("./fmtReplacer.zig").fmtReplacer; -pub const randomBytes = @import("./randomBytes.zig").randomBytes; -pub const FixedMaxBuffer = @import("./FixedMaxBuffer.zig").FixedMaxBuffer; pub const hashBytes = @import("./hashBytes.zig").hashBytes; -pub const readType = @import("./readType.zig").readType; -pub const indexBufferT = @import("./indexBufferT.zig").indexBufferT; -pub const BufIndexer = @import("./BufIndexer.zig").BufIndexer; -pub const skipToBoundary = @import("./skipToBoundary.zig").skipToBoundary; pub const is = @import("./is.zig").is; pub const safeAdd = @import("./safeAdd.zig").safeAdd; pub const safeAddWrap = @import("./safeAddWrap.zig").safeAddWrap; @@ -98,7 +90,6 @@ pub const sumLen = @import("./sumLen.zig").sumLen; test { _ = @import("reduceNumber.zig"); _ = @import("fmtByteCountIEC.zig"); - _ = @import("randomSlice.zig"); _ = @import("trimPrefix.zig"); _ = @import("trimPrefixEnsure.zig"); _ = @import("trimSuffix.zig"); @@ -107,7 +98,6 @@ test { _ = @import("base64DecodeAlloc.zig"); _ = @import("asciiUpper.zig"); _ = @import("sliceToInt.zig"); - _ = @import("pipe.zig"); _ = @import("countScalar.zig"); _ = @import("sortBy.zig"); _ = @import("sortBySlice.zig"); @@ -115,13 +105,7 @@ test { _ = @import("containsString.zig"); _ = @import("ensureFieldSubset.zig"); _ = @import("fmtReplacer.zig"); - _ = @import("randomBytes.zig"); - _ = @import("FixedMaxBuffer.zig"); _ = @import("hashBytes.zig"); - _ = @import("readType.zig"); - _ = @import("indexBufferT.zig"); - _ = @import("BufIndexer.zig"); - _ = @import("skipToBoundary.zig"); _ = @import("is.zig"); _ = @import("safeAdd.zig"); _ = @import("safeAddWrap.zig"); diff --git a/src/pipe.zig b/src/pipe.zig deleted file mode 100644 index 8f43e87da2a2ea75c9cb2e4400355af817a88b67..0000000000000000000000000000000000000000 --- a/src/pipe.zig +++ /dev/null @@ -1,22 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); - -pub fn pipe(reader_from: anytype, writer_to: anytype) !void { - var buf: [4096]u8 = undefined; - while (true) { - const n = try reader_from.read(&buf); - if (n == 0) break; - try writer_to.writeAll(buf[0..n]); - } -} - -test { - const bytes = "abcdefghijklmnopqrstuvwxyz".*; - var fba = std.io.fixedBufferStream(&bytes); - const allocator = std.testing.allocator; - var list = std.Io.Writer.Allocating.init(allocator); - defer list.deinit(); - try pipe(fba.reader(), &list.writer); - try std.testing.expect(std.mem.eql(u8, &bytes, list.written())); -} diff --git a/src/randomBytes.zig b/src/randomBytes.zig deleted file mode 100644 index 98853132c30ec9bbe40c198c5a35ceeffa78d6b5..0000000000000000000000000000000000000000 --- a/src/randomBytes.zig +++ /dev/null @@ -1,13 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); - -pub fn randomBytes(comptime len: usize) [len]u8 { - var bytes: [len]u8 = undefined; - std.crypto.random.bytes(&bytes); - return bytes; -} - -test { - std.testing.refAllDecls(@This()); -} diff --git a/src/randomSlice.zig b/src/randomSlice.zig deleted file mode 100644 index bb38de40da3dc583f83ef5b5479250d2399edd86..0000000000000000000000000000000000000000 --- a/src/randomSlice.zig +++ /dev/null @@ -1,22 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); - -const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"; - -pub fn randomSlice(alloc: std.mem.Allocator, rand: std.Random, comptime T: type, len: usize) ![]T { - var buf = try alloc.alloc(T, len); - var i: usize = 0; - while (i < len) : (i += 1) { - buf[i] = alphabet[rand.int(u8) % alphabet.len]; - } - return buf; -} - -test { - const allocator = std.testing.allocator; - const rand = std.crypto.random; - const slice = try randomSlice(allocator, rand, u8, 10); - defer allocator.free(slice); - try std.testing.expect(slice.len == 10); -} diff --git a/src/readType.zig b/src/readType.zig deleted file mode 100644 index e1531813c260a2f3f48598c3037a3e2c3273865e..0000000000000000000000000000000000000000 --- a/src/readType.zig +++ /dev/null @@ -1,95 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); -const builtin = @import("builtin"); -const native_endian = builtin.target.cpu.arch.endian(); -const rawIntBytes = extras.rawIntBytes; - -pub fn readType(reader: anytype, comptime T: type, endian: std.builtin.Endian) !T { - if (T == u8) return reader.readByte(); // single bytes dont have an endianness - return switch (@typeInfo(T)) { - .@"struct" => |t| { - switch (t.layout) { - .auto, .@"extern" => { - var s: T = undefined; - inline for (std.meta.fields(T)) |field| { - @field(s, field.name) = try readType(reader, field.type, endian); - } - return s; - }, - .@"packed" => return @bitCast(try readType(reader, t.backing_integer.?, endian)), - } - }, - .array => |t| { - var s: T = undefined; - for (0..t.len) |i| { - s[i] = try readType(reader, t.child, endian); - } - return s; - }, - .int => try reader.readInt(T, endian), - .@"enum" => |t| @enumFromInt(try readType(reader, t.tag_type, endian)), - else => |e| @compileError(@tagName(e)), - }; -} - -test { - const bytes = std.mem.toBytes(@as(u32, 0x4e5a7da9)); - var fba = std.io.fixedBufferStream(&bytes); - const I = u32; - const i = try readType(fba.reader(), I, native_endian); - try std.testing.expect(i == 1314553257); -} - -test { - const bytes = std.mem.toBytes(@as(u32, 0x4e5a7da9)); - var fba = std.io.fixedBufferStream(&bytes); - const E = enum(u32) { - a, - b, - c, - d = 0x4e5a7da9, - e, - }; - const e = try readType(fba.reader(), E, native_endian); - try std.testing.expect(e == .d); -} - -test { - const bytes = rawIntBytes(u32, 0x4e5a7da9); - var fba = std.io.fixedBufferStream(&bytes); - const S = struct { - a: u16, - b: u8, - c: u8, - }; - const s = try readType(fba.reader(), S, .big); - try std.testing.expect(s.a == 0x4e5a); - try std.testing.expect(s.b == 0x7d); - try std.testing.expect(s.c == 0xa9); -} - -test { - const bytes = rawIntBytes(u32, 0x4e5a7da9); - var fba = std.io.fixedBufferStream(&bytes); - const A = [2]u16; - const a = try readType(fba.reader(), A, .big); - try std.testing.expect(a[0] == 0x4e5a); - try std.testing.expect(a[1] == 0x7da9); -} - -test { - const bytes = rawIntBytes(u32, 0x4e5a7da9); - var fba = std.io.fixedBufferStream(&bytes); - const S = packed struct { - a: u16, - b: u4, - c: u4, - d: u8, - }; - const s = try readType(fba.reader(), S, .big); - try std.testing.expect(s.a == 0x7da9); - try std.testing.expect(s.b == 0xa); - try std.testing.expect(s.c == 0x5); - try std.testing.expect(s.d == 0x4e); -} diff --git a/src/skipToBoundary.zig b/src/skipToBoundary.zig deleted file mode 100644 index 639957c027dc673069d38de5c0953960d06b9d58..0000000000000000000000000000000000000000 --- a/src/skipToBoundary.zig +++ /dev/null @@ -1,61 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); - -pub fn skipToBoundary(pos: u64, boundary: u64, reader: anytype) !void { - // const gdiff = counter.bytes_read % 4; - // for (range(if (gdiff > 0) 4 - gdiff else 0)) |_| { - const a = pos; - const b = boundary; - try reader.skipBytes(((a + (b - 1)) & ~(b - 1)) - a, .{}); -} - -test { - const array = [_]u8{ 5, 4, 3, 2, 1, 0 }; - var fba = std.io.fixedBufferStream(&array); - var counter = std.io.countingReader(fba.reader()); - const reader = counter.reader(); - try reader.skipBytes(0, .{}); - try skipToBoundary(counter.bytes_read, 4, reader); - try std.testing.expect(counter.bytes_read == 0); -} - -test { - const array = [_]u8{ 5, 4, 3, 2, 1, 0 }; - var fba = std.io.fixedBufferStream(&array); - var counter = std.io.countingReader(fba.reader()); - const reader = counter.reader(); - try reader.skipBytes(1, .{}); - try skipToBoundary(counter.bytes_read, 4, reader); - try std.testing.expect(counter.bytes_read == 4); -} - -test { - const array = [_]u8{ 5, 4, 3, 2, 1, 0 }; - var fba = std.io.fixedBufferStream(&array); - var counter = std.io.countingReader(fba.reader()); - const reader = counter.reader(); - try reader.skipBytes(2, .{}); - try skipToBoundary(counter.bytes_read, 4, reader); - try std.testing.expect(counter.bytes_read == 4); -} - -test { - const array = [_]u8{ 5, 4, 3, 2, 1, 0 }; - var fba = std.io.fixedBufferStream(&array); - var counter = std.io.countingReader(fba.reader()); - const reader = counter.reader(); - try reader.skipBytes(3, .{}); - try skipToBoundary(counter.bytes_read, 4, reader); - try std.testing.expect(counter.bytes_read == 4); -} - -test { - const array = [_]u8{ 5, 4, 3, 2, 1, 0 }; - var fba = std.io.fixedBufferStream(&array); - var counter = std.io.countingReader(fba.reader()); - const reader = counter.reader(); - try reader.skipBytes(4, .{}); - try skipToBoundary(counter.bytes_read, 4, reader); - try std.testing.expect(counter.bytes_read == 4); -} -- 2.54.0