authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-31 14:44:48 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-31 14:44:48 -07:00
log133dea370171f39fd98f2d772abffcdb28e1b967
treef3313c53d335338f4dd2a7674066341d2ccda092
parent1a90f8f77b3e01150399d0fca057bc8e109bf7e2
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

Readable: more methods


4 files changed, 39 insertions(+), 0 deletions(-)

AnyReadable.zig+3
......@@ -24,6 +24,9 @@ pub const readAlloc = R.readAlloc;
2424pub const readInt = R.readInt;
2525pub const readUntilDelimitersAlloc = R.readUntilDelimitersAlloc;
2626pub const readUntilDelimiterOrEof = R.readUntilDelimiterOrEof;
27pub const readExpected = R.readExpected;
28pub const skipBytes = R.skipBytes;
29pub const skipUntilDelimiterOrEof = R.skipUntilDelimiterOrEof;
2730
2831pub const Error = ReadError; // std compat
2932pub const ReadError = anyerror;
buffered_reader.zig+3
......@@ -35,6 +35,9 @@ pub fn BufferedReader(comptime buffer_size: usize, comptime ReaderType: type) ty
3535 pub const readInt = R.readInt;
3636 pub const readUntilDelimitersAlloc = R.readUntilDelimitersAlloc;
3737 pub const readUntilDelimiterOrEof = R.readUntilDelimiterOrEof;
38 pub const readExpected = R.readExpected;
39 pub const skipBytes = R.skipBytes;
40 pub const skipUntilDelimiterOrEof = R.skipUntilDelimiterOrEof;
3841
3942 pub const ReadError = ReaderType.ReadError;
4043 pub fn read(self: *Self, dest: []u8) ReadError!usize {
fixed_buffer_stream.zig+3
......@@ -44,6 +44,9 @@ pub fn FixedBufferStream(comptime Buffer: type) type {
4444 pub const readInt = R.readInt;
4545 pub const readUntilDelimitersAlloc = R.readUntilDelimitersAlloc;
4646 pub const readUntilDelimiterOrEof = R.readUntilDelimiterOrEof;
47 pub const readExpected = R.readExpected;
48 pub const skipBytes = R.skipBytes;
49 pub const skipUntilDelimiterOrEof = R.skipUntilDelimiterOrEof;
4750
4851 pub const ReadError = error{};
4952 pub fn read(self: *Self, dest: []u8) ReadError!usize {
nio.zig+30
......@@ -194,6 +194,36 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type {
194194 }
195195 return error.StreamTooLong;
196196 }
197
198 pub fn readExpected(self: Self, expected: []const u8) !bool {
199 for (expected) |item| {
200 const actual = try readByte(self);
201 if (actual != item) {
202 return false;
203 }
204 }
205 return true;
206 }
207
208 pub fn skipBytes(self: Self, num_bytes: u64, comptime options: struct { buf_size: usize = 512 }) anyerror!void {
209 var buf: [options.buf_size]u8 = undefined;
210 var remaining = num_bytes;
211 while (remaining > 0) {
212 const amt = @min(remaining, options.buf_size);
213 try readNoEof(self, buf[0..amt]);
214 remaining -= amt;
215 }
216 }
217
218 pub fn skipUntilDelimiterOrEof(self: Self, delimiter: u8) anyerror!void {
219 while (true) {
220 const byte = self.readByte() catch |err| switch (err) {
221 error.EndOfStream => return,
222 else => |e| return e,
223 };
224 if (byte == delimiter) return;
225 }
226 }
197227 };
198228}
199229