authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-04-25 20:42:13+10:00
committergravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-04-25 20:42:13+10:00
log1d6e53756b13e3ddb28b6d221b8396f06586bb55
tree634a5b15245f934071dc2c51abc53e177ac6c040
parentb531c0e6769fc89e4028600dae1cbfef1dc89a7f
signaturelock-open Commit is signed but in an unrecognized format.

std: add in_stream.isBytes


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

lib/std/io/in_stream.zig+18
......@@ -234,6 +234,18 @@ pub fn InStream(
234234 }
235235 }
236236
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
237249 pub fn readStruct(self: Self, comptime T: type) !T {
238250 // Only extern and packed structs have defined in-memory layout.
239251 comptime assert(@typeInfo(T).Struct.layout != builtin.TypeInfo.ContainerLayout.Auto);
......@@ -276,3 +288,9 @@ test "InStream" {
276288 }, undefined)) == .c);
277289 testing.expectError(error.EndOfStream, in_stream.readByte());
278290}
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}