diff --git a/src/AnyReader.zig b/src/AnyReader.zig deleted file mode 100644 index adfcde91f088fbd5f4c65212f60cf856436dd906..0000000000000000000000000000000000000000 --- a/src/AnyReader.zig +++ /dev/null @@ -1,127 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); -const assert = std.debug.assert; - -pub const AnyReader = struct { - readFn: *const fn (*anyopaque, []u8) anyerror!usize, - state: *anyopaque, - - pub fn from(reader: anytype) AnyReader { - const R = @TypeOf(reader); - const ctx = reader.context; - const Ctx = @TypeOf(ctx); - switch (@typeInfo(Ctx)) { - .pointer => { - const S = struct { - fn foo(s: *anyopaque, buffer: []u8) anyerror!usize { - const r = R{ .context = @ptrCast(@alignCast(s)) }; - return r.read(buffer); - } - }; - return .{ - .readFn = S.foo, - .state = ctx, - }; - }, - .@"struct" => switch (R) { - std.fs.File.Reader => { - const S = struct { - fn foo(s: *anyopaque, buffer: []u8) anyerror!usize { - const r = R{ .context = .{ .handle = @intCast(@intFromPtr(s)) } }; - return r.read(buffer); - } - }; - return .{ - .readFn = S.foo, - .state = @ptrFromInt(@as(usize, @intCast(ctx.handle))), - }; - }, - else => @compileError(@typeName(R)), - }, - else => |v| @compileError(@typeName(R) ++ " , " ++ @tagName(v)), - } - } - - pub fn read(r: AnyReader, buffer: []u8) anyerror!usize { - return r.readFn(r.state, buffer); - } - - pub fn readByte(self: AnyReader) !u8 { - var result: [1]u8 = undefined; - const amt_read = try self.read(result[0..]); - if (amt_read < 1) return error.EndOfStream; - return result[0]; - } - - pub fn readInt(self: AnyReader, comptime T: type, endian: std.builtin.Endian) !T { - const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))); - return std.mem.readInt(T, &bytes, endian); - } - - pub fn readBytesNoEof(self: AnyReader, comptime num_bytes: usize) ![num_bytes]u8 { - var bytes: [num_bytes]u8 = undefined; - try self.readNoEof(&bytes); - return bytes; - } - - pub fn readNoEof(self: AnyReader, buf: []u8) !void { - const amt_read = try self.readAll(buf); - if (amt_read < buf.len) return error.EndOfStream; - } - - pub fn readAll(self: AnyReader, buffer: []u8) !usize { - return self.readAtLeast(buffer, buffer.len); - } - - pub fn readAtLeast(self: AnyReader, buffer: []u8, len: usize) !usize { - assert(len <= buffer.len); - var index: usize = 0; - while (index < len) { - const amt = try self.read(buffer[index..]); - if (amt == 0) break; - index += amt; - } - return index; - } - - pub fn readAllAlloc(self: AnyReader, allocator: std.mem.Allocator, max_size: usize) ![]u8 { - var array_list = std.ArrayList(u8).init(allocator); - defer array_list.deinit(); - try self.readAllArrayList(&array_list, max_size); - return try array_list.toOwnedSlice(); - } - - pub fn readAllArrayList(self: AnyReader, array_list: *std.ArrayList(u8), max_append_size: usize) !void { - return self.readAllArrayListAligned(null, array_list, max_append_size); - } - - pub fn readAllArrayListAligned(self: AnyReader, comptime alignment: ?u29, array_list: *std.ArrayListAligned(u8, alignment), max_append_size: usize) !void { - try array_list.ensureTotalCapacity(@min(max_append_size, 4096)); - const original_len = array_list.items.len; - var start_index: usize = original_len; - while (true) { - array_list.expandToCapacity(); - const dest_slice = array_list.items[start_index..]; - const bytes_read = try self.readAll(dest_slice); - start_index += bytes_read; - - if (start_index - original_len > max_append_size) { - array_list.shrinkAndFree(original_len + max_append_size); - return error.StreamTooLong; - } - - if (bytes_read != dest_slice.len) { - array_list.shrinkAndFree(start_index); - return; - } - - // This will trigger ArrayList to expand superlinearly at whatever its growth rate is. - try array_list.ensureTotalCapacity(start_index + 1); - } - } -}; - -test { - std.testing.refAllDecls(AnyReader); -} diff --git a/src/dirSize.zig b/src/dirSize.zig deleted file mode 100644 index 7dcdffe2fc881c4c83cb85786cb29f80b8771577..0000000000000000000000000000000000000000 --- a/src/dirSize.zig +++ /dev/null @@ -1,20 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); -const fileSize = extras.fileSize; - -pub fn dirSize(alloc: std.mem.Allocator, dir: std.fs.Dir) !u64 { - var res: u64 = 0; - - var walk = try dir.walk(alloc); - defer walk.deinit(); - while (try walk.next()) |entry| { - if (entry.kind != .file) continue; - res += try fileSize(dir, entry.path); - } - return res; -} - -test { - std.testing.refAllDecls(@This()); -} diff --git a/src/doesFileExist.zig b/src/doesFileExist.zig deleted file mode 100644 index 63b28019269dd082ab47d9e1aa7afb2f6cde7364..0000000000000000000000000000000000000000 --- a/src/doesFileExist.zig +++ /dev/null @@ -1,17 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); - -pub fn doesFileExist(dir: ?std.fs.Dir, fpath: []const u8) !bool { - const file = (dir orelse std.fs.cwd()).openFile(fpath, .{}) catch |e| switch (e) { - error.FileNotFound => return false, - error.IsDir => return true, - else => return e, - }; - defer file.close(); - return true; -} - -test { - std.testing.refAllDecls(@This()); -} diff --git a/src/doesFolderExist.zig b/src/doesFolderExist.zig deleted file mode 100644 index 2fdb739a8760a142e1da1e6b57609288a04f7975..0000000000000000000000000000000000000000 --- a/src/doesFolderExist.zig +++ /dev/null @@ -1,21 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); - -pub fn doesFolderExist(dir: ?std.fs.Dir, fpath: []const u8) !bool { - const file = (dir orelse std.fs.cwd()).openFile(fpath, .{}) catch |e| switch (e) { - error.FileNotFound => return false, - error.IsDir => return true, - else => return e, - }; - defer file.close(); - const s = try file.stat(); - if (s.kind != .directory) { - return false; - } - return true; -} - -test { - std.testing.refAllDecls(@This()); -} diff --git a/src/fileList.zig b/src/fileList.zig deleted file mode 100644 index 357b861f49c0adedffaa9a7a47be28f85153b9e0..0000000000000000000000000000000000000000 --- a/src/fileList.zig +++ /dev/null @@ -1,20 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); - -pub fn fileList(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]string { - var list = std.ArrayList(string).init(alloc); - defer list.deinit(); - - var walk = try dir.walk(alloc); - defer walk.deinit(); - while (try walk.next()) |entry| { - if (entry.kind != .file) continue; - try list.append(try alloc.dupe(u8, entry.path)); - } - return list.toOwnedSlice(); -} - -test { - std.testing.refAllDecls(@This()); -} diff --git a/src/fileSize.zig b/src/fileSize.zig deleted file mode 100644 index 1bb8f7c368bd32a11aba6a73c15c56ddfa1d7ed7..0000000000000000000000000000000000000000 --- a/src/fileSize.zig +++ /dev/null @@ -1,14 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); - -pub fn fileSize(dir: std.fs.Dir, sub_path: string) !u64 { - const f = try dir.openFile(sub_path, .{}); - defer f.close(); - const s = try f.stat(); - return s.size; -} - -test { - std.testing.refAllDecls(@This()); -} diff --git a/src/hashFile.zig b/src/hashFile.zig deleted file mode 100644 index 71c2900dd27b756aef2e31812ca904726a308319..0000000000000000000000000000000000000000 --- a/src/hashFile.zig +++ /dev/null @@ -1,25 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); -const pipe = extras.pipe; - -pub fn hashFile(dir: std.fs.Dir, sub_path: string, comptime Algo: type) ![Algo.digest_length * 2]u8 { - const file = try dir.openFile(sub_path, .{}); - defer file.close(); - var h = Algo.init(.{}); - var out: [Algo.digest_length]u8 = undefined; - try pipe(file.reader(), h.writer()); - h.final(&out); - var res: [Algo.digest_length * 2]u8 = undefined; - var fbs = std.io.fixedBufferStream(&res); - try std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&out)}); - return res; -} - -test { - const tdir = std.testing.tmpDir(.{}).dir; - try tdir.writeFile(.{ .sub_path = "yo.txt", .data = "hello" }); - const A = std.crypto.hash.sha2.Sha256; - const expected = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824".*; - try std.testing.expect(std.mem.eql(u8, &try hashFile(tdir, "yo.txt", A), &expected)); -} diff --git a/src/lib.zig b/src/lib.zig index 2fccf4ebe0c39beb646d90b9b100230b8e25a482..6d045b505052045e0786f948b1507a11b3f90706 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -14,13 +14,7 @@ pub const trimSuffixEnsure = @import("./trimSuffixEnsure.zig").trimSuffixEnsure; pub const base64EncodeAlloc = @import("./base64EncodeAlloc.zig").base64EncodeAlloc; pub const base64DecodeAlloc = @import("./base64DecodeAlloc.zig").base64DecodeAlloc; pub const asciiUpper = @import("./asciiUpper.zig").asciiUpper; -pub const doesFileExist = @import("./doesFileExist.zig").doesFileExist; -pub const doesFolderExist = @import("./doesFolderExist.zig").doesFolderExist; pub const sliceToInt = @import("./sliceToInt.zig").sliceToInt; -pub const fileList = @import("./fileList.zig").fileList; -pub const fileSize = @import("./fileSize.zig").fileSize; -pub const dirSize = @import("./dirSize.zig").dirSize; -pub const hashFile = @import("./hashFile.zig").hashFile; pub const pipe = @import("./pipe.zig").pipe; pub const countScalar = @import("./countScalar.zig").countScalar; pub const sortBy = @import("./sortBy.zig").sortBy; @@ -30,8 +24,6 @@ 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 readExpected = @import("./readExpected.zig").readExpected; -pub const readBytes = @import("./readBytes.zig").readBytes; pub const FixedMaxBuffer = @import("./FixedMaxBuffer.zig").FixedMaxBuffer; pub const hashBytes = @import("./hashBytes.zig").hashBytes; pub const readType = @import("./readType.zig").readType; @@ -41,7 +33,6 @@ 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; -pub const readBytesAlloc = @import("./readBytesAlloc.zig").readBytesAlloc; pub const nullifyS = @import("./nullifyS.zig").nullifyS; pub const sliceTo = @import("./sliceTo.zig").sliceTo; pub const matchesAll = @import("./matchesAll.zig").matchesAll; @@ -53,7 +44,6 @@ pub fn assertLog(ok: bool, comptime message: string, args: anytype) void { if (!ok) unreachable; // assertion failure } -pub const parse_json = @import("./parse_json.zig").parse_json; pub const isArrayOf = @import("./isArrayOf.zig").isArrayOf; pub const parse_int = @import("./parse_int.zig").parse_int; pub const parse_bool = @import("./parse_bool.zig").parse_bool; @@ -68,7 +58,6 @@ pub const OneBiggerInt = @import("./OneBiggerInt.zig").OneBiggerInt; pub const ReverseFields = @import("./ReverseFields.zig").ReverseFields; pub const stringToEnum = @import("./stringToEnum.zig").stringToEnum; pub const containsAggregate = @import("./containsAggregate.zig").containsAggregate; -pub const AnyReader = @import("./AnyReader.zig").AnyReader; pub const sum = @import("./sum.zig").sum; pub const RingBuffer = @import("./RingBuffer.zig").RingBuffer; pub const rawInt = @import("./rawInt.zig").rawInt; @@ -117,13 +106,7 @@ test { _ = @import("base64EncodeAlloc.zig"); _ = @import("base64DecodeAlloc.zig"); _ = @import("asciiUpper.zig"); - _ = @import("doesFileExist.zig"); - _ = @import("doesFolderExist.zig"); _ = @import("sliceToInt.zig"); - _ = @import("fileList.zig"); - _ = @import("fileSize.zig"); - _ = @import("dirSize.zig"); - _ = @import("hashFile.zig"); _ = @import("pipe.zig"); _ = @import("countScalar.zig"); _ = @import("sortBy.zig"); @@ -133,8 +116,6 @@ test { _ = @import("ensureFieldSubset.zig"); _ = @import("fmtReplacer.zig"); _ = @import("randomBytes.zig"); - _ = @import("readExpected.zig"); - _ = @import("readBytes.zig"); _ = @import("FixedMaxBuffer.zig"); _ = @import("hashBytes.zig"); _ = @import("readType.zig"); @@ -144,13 +125,11 @@ test { _ = @import("is.zig"); _ = @import("safeAdd.zig"); _ = @import("safeAddWrap.zig"); - _ = @import("readBytesAlloc.zig"); _ = @import("nullifyS.zig"); _ = @import("sliceTo.zig"); _ = @import("matchesAll.zig"); _ = @import("matchesAny.zig"); _ = @import("opslice.zig"); - _ = @import("parse_json.zig"); _ = @import("isArrayOf.zig"); _ = @import("parse_int.zig"); _ = @import("parse_bool.zig"); @@ -165,7 +144,6 @@ test { _ = @import("ReverseFields.zig"); _ = @import("stringToEnum.zig"); _ = @import("containsAggregate.zig"); - _ = @import("AnyReader.zig"); _ = @import("sum.zig"); _ = @import("RingBuffer.zig"); _ = @import("rawInt.zig"); diff --git a/src/parse_json.zig b/src/parse_json.zig deleted file mode 100644 index 5ece213b5278c781923c166a645a97529776906c..0000000000000000000000000000000000000000 --- a/src/parse_json.zig +++ /dev/null @@ -1,7 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); - -pub fn parse_json(alloc: std.mem.Allocator, input: string) !std.json.Parsed(std.json.Value) { - return std.json.parseFromSlice(std.json.Value, alloc, input, .{}); -} diff --git a/src/readBytes.zig b/src/readBytes.zig deleted file mode 100644 index 39268aa07f43b03cd2b0f7177ac8d98667333049..0000000000000000000000000000000000000000 --- a/src/readBytes.zig +++ /dev/null @@ -1,26 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); -const assert = std.debug.assert; - -pub fn readBytes(reader: anytype, comptime len: usize) ![len]u8 { - var bytes: [len]u8 = undefined; - if (try reader.readAll(&bytes) != len) return error.EndOfStream; - return bytes; -} - -test { - const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; - var fba = std.io.fixedBufferStream(&array); - const reader = fba.reader(); - const res = try readBytes(reader, 2); - try std.testing.expect(res.len == 2); - try std.testing.expect(std.mem.eql(u8, &res, &.{ 9, 8 })); -} - -test { - const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; - var fba = std.io.fixedBufferStream(&array); - const reader = fba.reader(); - try std.testing.expect(readBytes(reader, 13) == error.EndOfStream); -} diff --git a/src/readBytesAlloc.zig b/src/readBytesAlloc.zig deleted file mode 100644 index bafd005b85fb6c93691545cbecaaff94565e563b..0000000000000000000000000000000000000000 --- a/src/readBytesAlloc.zig +++ /dev/null @@ -1,30 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); - -pub fn readBytesAlloc(reader: anytype, alloc: std.mem.Allocator, len: usize) ![]u8 { - var list = try std.ArrayListUnmanaged(u8).initCapacity(alloc, len); - errdefer list.deinit(alloc); - list.appendNTimesAssumeCapacity(0, len); - try reader.readNoEof(list.items[0..len]); - return list.items; -} - -test { - const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; - var fba = std.io.fixedBufferStream(&array); - const reader = fba.reader(); - const alloc = std.testing.allocator; - const res = try readBytesAlloc(reader, alloc, 2); - defer alloc.free(res); - try std.testing.expect(res.len == 2); - try std.testing.expect(std.mem.eql(u8, res, &.{ 9, 8 })); -} - -test { - const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; - var fba = std.io.fixedBufferStream(&array); - const reader = fba.reader(); - const alloc = std.testing.allocator; - try std.testing.expect(readBytesAlloc(reader, alloc, 11) == error.EndOfStream); -} diff --git a/src/readExpected.zig b/src/readExpected.zig deleted file mode 100644 index 3d221c9558f047a7bf006c56a6034f3e5966da55..0000000000000000000000000000000000000000 --- a/src/readExpected.zig +++ /dev/null @@ -1,31 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); - -pub fn readExpected(reader: anytype, expected: []const u8) !bool { - for (expected) |item| { - const actual = try reader.readByte(); - if (actual != item) { - return false; - } - } - return true; -} - -test { - const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; - var fba = std.io.fixedBufferStream(&array); - try std.testing.expect(try readExpected(fba.reader(), &.{ 9, 8, 7, 6 })); -} - -test { - const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; - var fba = std.io.fixedBufferStream(&array); - try std.testing.expect(!try readExpected(fba.reader(), &.{ 1, 2, 3 })); -} - -test { - const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; - var fba = std.io.fixedBufferStream(&array); - try std.testing.expect(try readExpected(fba.reader(), &.{})); -}