authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-04-25 20:34:47+10:00
committergravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-04-25 20:38:46+10:00
logb531c0e6769fc89e4028600dae1cbfef1dc89a7f
treef1fe2788a9777c630ab7b3c21fb067b88316d8f5
parenta7a8c433d0153bf8d71d17f2cf54027ba8eff805
signature Commit is signed but in an unrecognized format.

std: add instream.readBytesNoEof function


1 files changed, 13 insertions(+), 10 deletions(-)

lib/std/io/in_stream.zig+13-10
......@@ -184,35 +184,38 @@ pub fn InStream(
184184 return @bitCast(i8, try self.readByte());
185185 }
186186
187 /// Reads exactly `num_bytes` bytes and returns as an array.
188 /// `num_bytes` must be comptime-known
189 pub fn readBytesNoEof(self: Self, comptime num_bytes: usize) ![num_bytes]u8 {
190 var bytes: [num_bytes]u8 = undefined;
191 try self.readNoEof(&bytes);
192 return bytes;
193 }
194
187195 /// Reads a native-endian integer
188196 pub fn readIntNative(self: Self, comptime T: type) !T {
189 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
190 try self.readNoEof(bytes[0..]);
197 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
191198 return mem.readIntNative(T, &bytes);
192199 }
193200
194201 /// Reads a foreign-endian integer
195202 pub fn readIntForeign(self: Self, comptime T: type) !T {
196 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
197 try self.readNoEof(bytes[0..]);
203 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
198204 return mem.readIntForeign(T, &bytes);
199205 }
200206
201207 pub fn readIntLittle(self: Self, comptime T: type) !T {
202 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
203 try self.readNoEof(bytes[0..]);
208 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
204209 return mem.readIntLittle(T, &bytes);
205210 }
206211
207212 pub fn readIntBig(self: Self, comptime T: type) !T {
208 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
209 try self.readNoEof(bytes[0..]);
213 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
210214 return mem.readIntBig(T, &bytes);
211215 }
212216
213217 pub fn readInt(self: Self, comptime T: type, endian: builtin.Endian) !T {
214 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
215 try self.readNoEof(bytes[0..]);
218 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
216219 return mem.readInt(T, &bytes, endian);
217220 }
218221