authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-05-17 22:02:10+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-17 22:02:10+03:00
log2a5c0ef7f0127a699cce024d7bdcca4095a9c67e
tree1b3e2df66b7f34a894616d9d66a6dd7401e62af2
parent16f100b82e4075a047f008c0de6c44fc418eb58e
parent1d6e53756b13e3ddb28b6d221b8396f06586bb55
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5164 from daurnimator/in_stream-helpers

A couple of helpers for streams that I've found helpful

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

lib/std/io/in_stream.zig+31-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
...@@ -231,6 +234,18 @@ pub fn InStream(...@@ -231,6 +234,18 @@ pub fn InStream(
231 }234 }
232 }235 }
233236
237 /// Reads `slice.len` bytes from the stream and returns if they are the same as the passed slice
238 pub fn isBytes(self: Self, slice: []const u8) !bool {
239 var i: usize = 0;
240 var matches = true;
241 while (i < slice.len) : (i += 1) {
242 if (slice[i] != try self.readByte()) {
243 matches = false;
244 }
245 }
246 return matches;
247 }
248
234 pub fn readStruct(self: Self, comptime T: type) !T {249 pub fn readStruct(self: Self, comptime T: type) !T {
235 // Only extern and packed structs have defined in-memory layout.250 // Only extern and packed structs have defined in-memory layout.
236 comptime assert(@typeInfo(T).Struct.layout != builtin.TypeInfo.ContainerLayout.Auto);251 comptime assert(@typeInfo(T).Struct.layout != builtin.TypeInfo.ContainerLayout.Auto);
...@@ -273,3 +288,9 @@ test "InStream" {...@@ -273,3 +288,9 @@ test "InStream" {
273 }, undefined)) == .c);288 }, undefined)) == .c);
274 testing.expectError(error.EndOfStream, in_stream.readByte());289 testing.expectError(error.EndOfStream, in_stream.readByte());
275}290}
291
292test "InStream.isBytes" {
293 const in_stream = std.io.fixedBufferStream("foobar").inStream();
294 testing.expectEqual(true, try in_stream.isBytes("foo"));
295 testing.expectEqual(false, try in_stream.isBytes("qux"));
296}