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(...@@ -184,35 +184,38 @@ pub fn InStream(
184 return @bitCast(i8, try self.readByte());184 return @bitCast(i8, try self.readByte());
185 }185 }
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
187 /// Reads a native-endian integer195 /// Reads a native-endian integer
188 pub fn readIntNative(self: Self, comptime T: type) !T {196 pub fn readIntNative(self: Self, comptime T: type) !T {
189 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;197 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
190 try self.readNoEof(bytes[0..]);
191 return mem.readIntNative(T, &bytes);198 return mem.readIntNative(T, &bytes);
192 }199 }
193200
194 /// Reads a foreign-endian integer201 /// Reads a foreign-endian integer
195 pub fn readIntForeign(self: Self, comptime T: type) !T {202 pub fn readIntForeign(self: Self, comptime T: type) !T {
196 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;203 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
197 try self.readNoEof(bytes[0..]);
198 return mem.readIntForeign(T, &bytes);204 return mem.readIntForeign(T, &bytes);
199 }205 }
200206
201 pub fn readIntLittle(self: Self, comptime T: type) !T {207 pub fn readIntLittle(self: Self, comptime T: type) !T {
202 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;208 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
203 try self.readNoEof(bytes[0..]);
204 return mem.readIntLittle(T, &bytes);209 return mem.readIntLittle(T, &bytes);
205 }210 }
206211
207 pub fn readIntBig(self: Self, comptime T: type) !T {212 pub fn readIntBig(self: Self, comptime T: type) !T {
208 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;213 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
209 try self.readNoEof(bytes[0..]);
210 return mem.readIntBig(T, &bytes);214 return mem.readIntBig(T, &bytes);
211 }215 }
212216
213 pub fn readInt(self: Self, comptime T: type, endian: builtin.Endian) !T {217 pub fn readInt(self: Self, comptime T: type, endian: builtin.Endian) !T {
214 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;218 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
215 try self.readNoEof(bytes[0..]);
216 return mem.readInt(T, &bytes, endian);219 return mem.readInt(T, &bytes, endian);
217 }220 }
218221