authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-30 03:36:05 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-30 03:36:05 -07:00
log0cf756c80a4bc1b0459bc81bf8071891f12c3738
tree552c0301b03a41a0294ffe30ad8f666381fdb125
parentb3457b31a1d1ea2c19c5d0b007c903d3e80add40
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

depreate a bunch of functions, they can use nfs or nio now


12 files changed, 0 insertions(+), 360 deletions(-)

src/AnyReader.zig deleted-127
...@@ -1,127 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const assert = std.debug.assert;
5
6pub const AnyReader = struct {
7 readFn: *const fn (*anyopaque, []u8) anyerror!usize,
8 state: *anyopaque,
9
10 pub fn from(reader: anytype) AnyReader {
11 const R = @TypeOf(reader);
12 const ctx = reader.context;
13 const Ctx = @TypeOf(ctx);
14 switch (@typeInfo(Ctx)) {
15 .pointer => {
16 const S = struct {
17 fn foo(s: *anyopaque, buffer: []u8) anyerror!usize {
18 const r = R{ .context = @ptrCast(@alignCast(s)) };
19 return r.read(buffer);
20 }
21 };
22 return .{
23 .readFn = S.foo,
24 .state = ctx,
25 };
26 },
27 .@"struct" => switch (R) {
28 std.fs.File.Reader => {
29 const S = struct {
30 fn foo(s: *anyopaque, buffer: []u8) anyerror!usize {
31 const r = R{ .context = .{ .handle = @intCast(@intFromPtr(s)) } };
32 return r.read(buffer);
33 }
34 };
35 return .{
36 .readFn = S.foo,
37 .state = @ptrFromInt(@as(usize, @intCast(ctx.handle))),
38 };
39 },
40 else => @compileError(@typeName(R)),
41 },
42 else => |v| @compileError(@typeName(R) ++ " , " ++ @tagName(v)),
43 }
44 }
45
46 pub fn read(r: AnyReader, buffer: []u8) anyerror!usize {
47 return r.readFn(r.state, buffer);
48 }
49
50 pub fn readByte(self: AnyReader) !u8 {
51 var result: [1]u8 = undefined;
52 const amt_read = try self.read(result[0..]);
53 if (amt_read < 1) return error.EndOfStream;
54 return result[0];
55 }
56
57 pub fn readInt(self: AnyReader, comptime T: type, endian: std.builtin.Endian) !T {
58 const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8)));
59 return std.mem.readInt(T, &bytes, endian);
60 }
61
62 pub fn readBytesNoEof(self: AnyReader, comptime num_bytes: usize) ![num_bytes]u8 {
63 var bytes: [num_bytes]u8 = undefined;
64 try self.readNoEof(&bytes);
65 return bytes;
66 }
67
68 pub fn readNoEof(self: AnyReader, buf: []u8) !void {
69 const amt_read = try self.readAll(buf);
70 if (amt_read < buf.len) return error.EndOfStream;
71 }
72
73 pub fn readAll(self: AnyReader, buffer: []u8) !usize {
74 return self.readAtLeast(buffer, buffer.len);
75 }
76
77 pub fn readAtLeast(self: AnyReader, buffer: []u8, len: usize) !usize {
78 assert(len <= buffer.len);
79 var index: usize = 0;
80 while (index < len) {
81 const amt = try self.read(buffer[index..]);
82 if (amt == 0) break;
83 index += amt;
84 }
85 return index;
86 }
87
88 pub fn readAllAlloc(self: AnyReader, allocator: std.mem.Allocator, max_size: usize) ![]u8 {
89 var array_list = std.ArrayList(u8).init(allocator);
90 defer array_list.deinit();
91 try self.readAllArrayList(&array_list, max_size);
92 return try array_list.toOwnedSlice();
93 }
94
95 pub fn readAllArrayList(self: AnyReader, array_list: *std.ArrayList(u8), max_append_size: usize) !void {
96 return self.readAllArrayListAligned(null, array_list, max_append_size);
97 }
98
99 pub fn readAllArrayListAligned(self: AnyReader, comptime alignment: ?u29, array_list: *std.ArrayListAligned(u8, alignment), max_append_size: usize) !void {
100 try array_list.ensureTotalCapacity(@min(max_append_size, 4096));
101 const original_len = array_list.items.len;
102 var start_index: usize = original_len;
103 while (true) {
104 array_list.expandToCapacity();
105 const dest_slice = array_list.items[start_index..];
106 const bytes_read = try self.readAll(dest_slice);
107 start_index += bytes_read;
108
109 if (start_index - original_len > max_append_size) {
110 array_list.shrinkAndFree(original_len + max_append_size);
111 return error.StreamTooLong;
112 }
113
114 if (bytes_read != dest_slice.len) {
115 array_list.shrinkAndFree(start_index);
116 return;
117 }
118
119 // This will trigger ArrayList to expand superlinearly at whatever its growth rate is.
120 try array_list.ensureTotalCapacity(start_index + 1);
121 }
122 }
123};
124
125test {
126 std.testing.refAllDecls(AnyReader);
127}
src/dirSize.zig deleted-20
...@@ -1,20 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const fileSize = extras.fileSize;
5
6pub fn dirSize(alloc: std.mem.Allocator, dir: std.fs.Dir) !u64 {
7 var res: u64 = 0;
8
9 var walk = try dir.walk(alloc);
10 defer walk.deinit();
11 while (try walk.next()) |entry| {
12 if (entry.kind != .file) continue;
13 res += try fileSize(dir, entry.path);
14 }
15 return res;
16}
17
18test {
19 std.testing.refAllDecls(@This());
20}
src/doesFileExist.zig deleted-17
...@@ -1,17 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4
5pub fn doesFileExist(dir: ?std.fs.Dir, fpath: []const u8) !bool {
6 const file = (dir orelse std.fs.cwd()).openFile(fpath, .{}) catch |e| switch (e) {
7 error.FileNotFound => return false,
8 error.IsDir => return true,
9 else => return e,
10 };
11 defer file.close();
12 return true;
13}
14
15test {
16 std.testing.refAllDecls(@This());
17}
src/doesFolderExist.zig deleted-21
...@@ -1,21 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4
5pub fn doesFolderExist(dir: ?std.fs.Dir, fpath: []const u8) !bool {
6 const file = (dir orelse std.fs.cwd()).openFile(fpath, .{}) catch |e| switch (e) {
7 error.FileNotFound => return false,
8 error.IsDir => return true,
9 else => return e,
10 };
11 defer file.close();
12 const s = try file.stat();
13 if (s.kind != .directory) {
14 return false;
15 }
16 return true;
17}
18
19test {
20 std.testing.refAllDecls(@This());
21}
src/fileList.zig deleted-20
...@@ -1,20 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4
5pub fn fileList(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]string {
6 var list = std.ArrayList(string).init(alloc);
7 defer list.deinit();
8
9 var walk = try dir.walk(alloc);
10 defer walk.deinit();
11 while (try walk.next()) |entry| {
12 if (entry.kind != .file) continue;
13 try list.append(try alloc.dupe(u8, entry.path));
14 }
15 return list.toOwnedSlice();
16}
17
18test {
19 std.testing.refAllDecls(@This());
20}
src/fileSize.zig deleted-14
...@@ -1,14 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4
5pub fn fileSize(dir: std.fs.Dir, sub_path: string) !u64 {
6 const f = try dir.openFile(sub_path, .{});
7 defer f.close();
8 const s = try f.stat();
9 return s.size;
10}
11
12test {
13 std.testing.refAllDecls(@This());
14}
src/hashFile.zig deleted-25
...@@ -1,25 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const pipe = extras.pipe;
5
6pub fn hashFile(dir: std.fs.Dir, sub_path: string, comptime Algo: type) ![Algo.digest_length * 2]u8 {
7 const file = try dir.openFile(sub_path, .{});
8 defer file.close();
9 var h = Algo.init(.{});
10 var out: [Algo.digest_length]u8 = undefined;
11 try pipe(file.reader(), h.writer());
12 h.final(&out);
13 var res: [Algo.digest_length * 2]u8 = undefined;
14 var fbs = std.io.fixedBufferStream(&res);
15 try std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&out)});
16 return res;
17}
18
19test {
20 const tdir = std.testing.tmpDir(.{}).dir;
21 try tdir.writeFile(.{ .sub_path = "yo.txt", .data = "hello" });
22 const A = std.crypto.hash.sha2.Sha256;
23 const expected = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824".*;
24 try std.testing.expect(std.mem.eql(u8, &try hashFile(tdir, "yo.txt", A), &expected));
25}
src/lib.zig-22
...@@ -14,13 +14,7 @@ pub const trimSuffixEnsure = @import("./trimSuffixEnsure.zig").trimSuffixEnsure;...@@ -14,13 +14,7 @@ pub const trimSuffixEnsure = @import("./trimSuffixEnsure.zig").trimSuffixEnsure;
14pub const base64EncodeAlloc = @import("./base64EncodeAlloc.zig").base64EncodeAlloc;14pub const base64EncodeAlloc = @import("./base64EncodeAlloc.zig").base64EncodeAlloc;
15pub const base64DecodeAlloc = @import("./base64DecodeAlloc.zig").base64DecodeAlloc;15pub const base64DecodeAlloc = @import("./base64DecodeAlloc.zig").base64DecodeAlloc;
16pub const asciiUpper = @import("./asciiUpper.zig").asciiUpper;16pub const asciiUpper = @import("./asciiUpper.zig").asciiUpper;
17pub const doesFileExist = @import("./doesFileExist.zig").doesFileExist;
18pub const doesFolderExist = @import("./doesFolderExist.zig").doesFolderExist;
19pub const sliceToInt = @import("./sliceToInt.zig").sliceToInt;17pub const sliceToInt = @import("./sliceToInt.zig").sliceToInt;
20pub const fileList = @import("./fileList.zig").fileList;
21pub const fileSize = @import("./fileSize.zig").fileSize;
22pub const dirSize = @import("./dirSize.zig").dirSize;
23pub const hashFile = @import("./hashFile.zig").hashFile;
24pub const pipe = @import("./pipe.zig").pipe;18pub const pipe = @import("./pipe.zig").pipe;
25pub const countScalar = @import("./countScalar.zig").countScalar;19pub const countScalar = @import("./countScalar.zig").countScalar;
26pub const sortBy = @import("./sortBy.zig").sortBy;20pub const sortBy = @import("./sortBy.zig").sortBy;
...@@ -30,8 +24,6 @@ pub const containsString = @import("./containsString.zig").containsString;...@@ -30,8 +24,6 @@ pub const containsString = @import("./containsString.zig").containsString;
30pub const ensureFieldSubset = @import("./ensureFieldSubset.zig").ensureFieldSubset;24pub const ensureFieldSubset = @import("./ensureFieldSubset.zig").ensureFieldSubset;
31pub const fmtReplacer = @import("./fmtReplacer.zig").fmtReplacer;25pub const fmtReplacer = @import("./fmtReplacer.zig").fmtReplacer;
32pub const randomBytes = @import("./randomBytes.zig").randomBytes;26pub const randomBytes = @import("./randomBytes.zig").randomBytes;
33pub const readExpected = @import("./readExpected.zig").readExpected;
34pub const readBytes = @import("./readBytes.zig").readBytes;
35pub const FixedMaxBuffer = @import("./FixedMaxBuffer.zig").FixedMaxBuffer;27pub const FixedMaxBuffer = @import("./FixedMaxBuffer.zig").FixedMaxBuffer;
36pub const hashBytes = @import("./hashBytes.zig").hashBytes;28pub const hashBytes = @import("./hashBytes.zig").hashBytes;
37pub const readType = @import("./readType.zig").readType;29pub const readType = @import("./readType.zig").readType;
...@@ -41,7 +33,6 @@ pub const skipToBoundary = @import("./skipToBoundary.zig").skipToBoundary;...@@ -41,7 +33,6 @@ pub const skipToBoundary = @import("./skipToBoundary.zig").skipToBoundary;
41pub const is = @import("./is.zig").is;33pub const is = @import("./is.zig").is;
42pub const safeAdd = @import("./safeAdd.zig").safeAdd;34pub const safeAdd = @import("./safeAdd.zig").safeAdd;
43pub const safeAddWrap = @import("./safeAddWrap.zig").safeAddWrap;35pub const safeAddWrap = @import("./safeAddWrap.zig").safeAddWrap;
44pub const readBytesAlloc = @import("./readBytesAlloc.zig").readBytesAlloc;
45pub const nullifyS = @import("./nullifyS.zig").nullifyS;36pub const nullifyS = @import("./nullifyS.zig").nullifyS;
46pub const sliceTo = @import("./sliceTo.zig").sliceTo;37pub const sliceTo = @import("./sliceTo.zig").sliceTo;
47pub const matchesAll = @import("./matchesAll.zig").matchesAll;38pub const matchesAll = @import("./matchesAll.zig").matchesAll;
...@@ -53,7 +44,6 @@ pub fn assertLog(ok: bool, comptime message: string, args: anytype) void {...@@ -53,7 +44,6 @@ pub fn assertLog(ok: bool, comptime message: string, args: anytype) void {
53 if (!ok) unreachable; // assertion failure44 if (!ok) unreachable; // assertion failure
54}45}
5546
56pub const parse_json = @import("./parse_json.zig").parse_json;
57pub const isArrayOf = @import("./isArrayOf.zig").isArrayOf;47pub const isArrayOf = @import("./isArrayOf.zig").isArrayOf;
58pub const parse_int = @import("./parse_int.zig").parse_int;48pub const parse_int = @import("./parse_int.zig").parse_int;
59pub const parse_bool = @import("./parse_bool.zig").parse_bool;49pub const parse_bool = @import("./parse_bool.zig").parse_bool;
...@@ -68,7 +58,6 @@ pub const OneBiggerInt = @import("./OneBiggerInt.zig").OneBiggerInt;...@@ -68,7 +58,6 @@ pub const OneBiggerInt = @import("./OneBiggerInt.zig").OneBiggerInt;
68pub const ReverseFields = @import("./ReverseFields.zig").ReverseFields;58pub const ReverseFields = @import("./ReverseFields.zig").ReverseFields;
69pub const stringToEnum = @import("./stringToEnum.zig").stringToEnum;59pub const stringToEnum = @import("./stringToEnum.zig").stringToEnum;
70pub const containsAggregate = @import("./containsAggregate.zig").containsAggregate;60pub const containsAggregate = @import("./containsAggregate.zig").containsAggregate;
71pub const AnyReader = @import("./AnyReader.zig").AnyReader;
72pub const sum = @import("./sum.zig").sum;61pub const sum = @import("./sum.zig").sum;
73pub const RingBuffer = @import("./RingBuffer.zig").RingBuffer;62pub const RingBuffer = @import("./RingBuffer.zig").RingBuffer;
74pub const rawInt = @import("./rawInt.zig").rawInt;63pub const rawInt = @import("./rawInt.zig").rawInt;
...@@ -117,13 +106,7 @@ test {...@@ -117,13 +106,7 @@ test {
117 _ = @import("base64EncodeAlloc.zig");106 _ = @import("base64EncodeAlloc.zig");
118 _ = @import("base64DecodeAlloc.zig");107 _ = @import("base64DecodeAlloc.zig");
119 _ = @import("asciiUpper.zig");108 _ = @import("asciiUpper.zig");
120 _ = @import("doesFileExist.zig");
121 _ = @import("doesFolderExist.zig");
122 _ = @import("sliceToInt.zig");109 _ = @import("sliceToInt.zig");
123 _ = @import("fileList.zig");
124 _ = @import("fileSize.zig");
125 _ = @import("dirSize.zig");
126 _ = @import("hashFile.zig");
127 _ = @import("pipe.zig");110 _ = @import("pipe.zig");
128 _ = @import("countScalar.zig");111 _ = @import("countScalar.zig");
129 _ = @import("sortBy.zig");112 _ = @import("sortBy.zig");
...@@ -133,8 +116,6 @@ test {...@@ -133,8 +116,6 @@ test {
133 _ = @import("ensureFieldSubset.zig");116 _ = @import("ensureFieldSubset.zig");
134 _ = @import("fmtReplacer.zig");117 _ = @import("fmtReplacer.zig");
135 _ = @import("randomBytes.zig");118 _ = @import("randomBytes.zig");
136 _ = @import("readExpected.zig");
137 _ = @import("readBytes.zig");
138 _ = @import("FixedMaxBuffer.zig");119 _ = @import("FixedMaxBuffer.zig");
139 _ = @import("hashBytes.zig");120 _ = @import("hashBytes.zig");
140 _ = @import("readType.zig");121 _ = @import("readType.zig");
...@@ -144,13 +125,11 @@ test {...@@ -144,13 +125,11 @@ test {
144 _ = @import("is.zig");125 _ = @import("is.zig");
145 _ = @import("safeAdd.zig");126 _ = @import("safeAdd.zig");
146 _ = @import("safeAddWrap.zig");127 _ = @import("safeAddWrap.zig");
147 _ = @import("readBytesAlloc.zig");
148 _ = @import("nullifyS.zig");128 _ = @import("nullifyS.zig");
149 _ = @import("sliceTo.zig");129 _ = @import("sliceTo.zig");
150 _ = @import("matchesAll.zig");130 _ = @import("matchesAll.zig");
151 _ = @import("matchesAny.zig");131 _ = @import("matchesAny.zig");
152 _ = @import("opslice.zig");132 _ = @import("opslice.zig");
153 _ = @import("parse_json.zig");
154 _ = @import("isArrayOf.zig");133 _ = @import("isArrayOf.zig");
155 _ = @import("parse_int.zig");134 _ = @import("parse_int.zig");
156 _ = @import("parse_bool.zig");135 _ = @import("parse_bool.zig");
...@@ -165,7 +144,6 @@ test {...@@ -165,7 +144,6 @@ test {
165 _ = @import("ReverseFields.zig");144 _ = @import("ReverseFields.zig");
166 _ = @import("stringToEnum.zig");145 _ = @import("stringToEnum.zig");
167 _ = @import("containsAggregate.zig");146 _ = @import("containsAggregate.zig");
168 _ = @import("AnyReader.zig");
169 _ = @import("sum.zig");147 _ = @import("sum.zig");
170 _ = @import("RingBuffer.zig");148 _ = @import("RingBuffer.zig");
171 _ = @import("rawInt.zig");149 _ = @import("rawInt.zig");
src/parse_json.zig deleted-7
...@@ -1,7 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4
5pub fn parse_json(alloc: std.mem.Allocator, input: string) !std.json.Parsed(std.json.Value) {
6 return std.json.parseFromSlice(std.json.Value, alloc, input, .{});
7}
src/readBytes.zig deleted-26
...@@ -1,26 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const assert = std.debug.assert;
5
6pub fn readBytes(reader: anytype, comptime len: usize) ![len]u8 {
7 var bytes: [len]u8 = undefined;
8 if (try reader.readAll(&bytes) != len) return error.EndOfStream;
9 return bytes;
10}
11
12test {
13 const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
14 var fba = std.io.fixedBufferStream(&array);
15 const reader = fba.reader();
16 const res = try readBytes(reader, 2);
17 try std.testing.expect(res.len == 2);
18 try std.testing.expect(std.mem.eql(u8, &res, &.{ 9, 8 }));
19}
20
21test {
22 const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
23 var fba = std.io.fixedBufferStream(&array);
24 const reader = fba.reader();
25 try std.testing.expect(readBytes(reader, 13) == error.EndOfStream);
26}
src/readBytesAlloc.zig deleted-30
...@@ -1,30 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4
5pub fn readBytesAlloc(reader: anytype, alloc: std.mem.Allocator, len: usize) ![]u8 {
6 var list = try std.ArrayListUnmanaged(u8).initCapacity(alloc, len);
7 errdefer list.deinit(alloc);
8 list.appendNTimesAssumeCapacity(0, len);
9 try reader.readNoEof(list.items[0..len]);
10 return list.items;
11}
12
13test {
14 const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
15 var fba = std.io.fixedBufferStream(&array);
16 const reader = fba.reader();
17 const alloc = std.testing.allocator;
18 const res = try readBytesAlloc(reader, alloc, 2);
19 defer alloc.free(res);
20 try std.testing.expect(res.len == 2);
21 try std.testing.expect(std.mem.eql(u8, res, &.{ 9, 8 }));
22}
23
24test {
25 const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
26 var fba = std.io.fixedBufferStream(&array);
27 const reader = fba.reader();
28 const alloc = std.testing.allocator;
29 try std.testing.expect(readBytesAlloc(reader, alloc, 11) == error.EndOfStream);
30}
src/readExpected.zig deleted-31
...@@ -1,31 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4
5pub fn readExpected(reader: anytype, expected: []const u8) !bool {
6 for (expected) |item| {
7 const actual = try reader.readByte();
8 if (actual != item) {
9 return false;
10 }
11 }
12 return true;
13}
14
15test {
16 const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
17 var fba = std.io.fixedBufferStream(&array);
18 try std.testing.expect(try readExpected(fba.reader(), &.{ 9, 8, 7, 6 }));
19}
20
21test {
22 const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
23 var fba = std.io.fixedBufferStream(&array);
24 try std.testing.expect(!try readExpected(fba.reader(), &.{ 1, 2, 3 }));
25}
26
27test {
28 const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
29 var fba = std.io.fixedBufferStream(&array);
30 try std.testing.expect(try readExpected(fba.reader(), &.{}));
31}