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
signature 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(
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
......@@ -231,6 +234,18 @@ pub fn InStream(
231234 }
232235 }
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
234249 pub fn readStruct(self: Self, comptime T: type) !T {
235250 // Only extern and packed structs have defined in-memory layout.
236251 comptime assert(@typeInfo(T).Struct.layout != builtin.TypeInfo.ContainerLayout.Auto);
......@@ -273,3 +288,9 @@ test "InStream" {
273288 }, undefined)) == .c);
274289 testing.expectError(error.EndOfStream, in_stream.readByte());
275290}
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}