authorgravatar for inkryption07@gmail.comInKryption <inkryption07@gmail.com> 2022-08-11 18:28:35+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-22 14:07:22+03:00
logf1999712b0a8560bd84726c8a5e8fd37dbdf5375
tree6ef85c9332056a6341fca5339357235fb99a5985
parentcd5a9ba1f4e38292d8e8c122c2e8ce4d3879367f

std.io.Reader: bounded array functions

* Add readIntoBoundedBytes * Add readBoundedBytes

1 files changed, 21 insertions(+), 0 deletions(-)

lib/std/io/reader.zig+21
...@@ -247,6 +247,27 @@ pub fn Reader(...@@ -247,6 +247,27 @@ pub fn Reader(
247 return bytes;247 return bytes;
248 }248 }
249249
250 /// Reads bytes into the bounded array, until
251 /// the bounded array is full, or the stream ends.
252 pub fn readIntoBoundedBytes(
253 self: Self,
254 comptime num_bytes: usize,
255 bounded: *std.BoundedArray(u8, num_bytes),
256 ) !void {
257 while (bounded.len < num_bytes) {
258 const bytes_read = try self.read(bounded.unusedCapacitySlice());
259 if (bytes_read == 0) return;
260 bounded.len += bytes_read;
261 }
262 }
263
264 /// Reads at most `num_bytes` and returns as a bounded array.
265 pub fn readBoundedBytes(self: Self, comptime num_bytes: usize) !std.BoundedArray(u8, num_bytes) {
266 var result = std.BoundedArray(u8, num_bytes){};
267 try self.readIntoBoundedBytes(num_bytes, &result);
268 return result;
269 }
270
250 /// Reads a native-endian integer271 /// Reads a native-endian integer
251 pub fn readIntNative(self: Self, comptime T: type) !T {272 pub fn readIntNative(self: Self, comptime T: type) !T {
252 const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);273 const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);