authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-21 04:09:01 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-21 04:09:01 -07:00
log784588b650abfc429124538b910bd0975ce09fe3
treeb8e28e680ab852c3a42fc48f0a98e1ae2a92608f
parent4fdaa8e521fce2b66f6db19b976960f751c2cbbd
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add readInt and readUntilDelimitersAlloc to Readable


1 files changed, 17 insertions(+), 1 deletions(-)

nio.zig+17-1
...@@ -125,6 +125,7 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type {...@@ -125,6 +125,7 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type {
125 return list.toOwnedSlice();125 return list.toOwnedSlice();
126 }126 }
127127
128 /// Returned slice is not suffixed by needle but buffer will contain it.
128 pub fn readUntilDelimitersBuf(self: Self, buffer: []u8, needle: []const u8) ![]u8 {129 pub fn readUntilDelimitersBuf(self: Self, buffer: []u8, needle: []const u8) ![]u8 {
129 var real_len: usize = 0;130 var real_len: usize = 0;
130 for (0..buffer.len) |_| {131 for (0..buffer.len) |_| {
...@@ -136,6 +137,7 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type {...@@ -136,6 +137,7 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type {
136 return error.StreamTooLong;137 return error.StreamTooLong;
137 }138 }
138139
140 /// Returned slice is not suffixed by needle but array_list will contain it.
139 pub fn readUntilDelimitersArrayList(self: Self, array_list: *std.ArrayList(u8), needle: []const u8, max_size: usize) ![]u8 {141 pub fn readUntilDelimitersArrayList(self: Self, array_list: *std.ArrayList(u8), needle: []const u8, max_size: usize) ![]u8 {
140 const initial_len = array_list.items.len;142 const initial_len = array_list.items.len;
141 for (0..max_size) |i| {143 for (0..max_size) |i| {
...@@ -154,6 +156,20 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type {...@@ -154,6 +156,20 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type {
154 if (len != size) return error.EndOfStream;156 if (len != size) return error.EndOfStream;
155 return array_list.toOwnedSlice();157 return array_list.toOwnedSlice();
156 }158 }
159
160 pub fn readInt(self: Self, I: type, endian: std.builtin.Endian) !I {
161 comptime std.debug.assert(@bitSizeOf(I) % 8 == 0);
162 const array = try readArray(self, @sizeOf(I));
163 return std.mem.readInt(I, &array, endian);
164 }
165
166 /// Returned slice is suffixed by needle.
167 pub fn readUntilDelimitersAlloc(self: Self, allocator: std.mem.Allocator, needle: []const u8, max_size: usize) ![]u8 {
168 var list: std.ArrayList(u8) = .init(allocator);
169 errdefer list.deinit();
170 _ = try readUntilDelimitersArrayList(self, &list, needle, max_size);
171 return list.toOwnedSlice();
172 }
157 };173 };
158}174}
159175
...@@ -234,7 +250,7 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type {...@@ -234,7 +250,7 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type {
234 }250 }
235251
236 pub fn writeInt(self: Self, comptime I: type, value: I, endian: std.builtin.Endian) Error!void {252 pub fn writeInt(self: Self, comptime I: type, value: I, endian: std.builtin.Endian) Error!void {
237 var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(I).Int.bits) + 7) / 8))]u8 = undefined;253 var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(I).int.bits) + 7) / 8))]u8 = undefined;
238 std.mem.writeInt(std.math.ByteAlignedInt(I), &bytes, value, endian);254 std.mem.writeInt(std.math.ByteAlignedInt(I), &bytes, value, endian);
239 return writeAll(self, &bytes);255 return writeAll(self, &bytes);
240 }256 }