| ... | @@ -234,6 +234,18 @@ pub fn InStream( | ... | @@ -234,6 +234,18 @@ pub fn InStream( |
| 234 | } | 234 | } |
| 235 | } | 235 | } |
| 236 | | 236 | |
| | 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 | |
| 237 | pub fn readStruct(self: Self, comptime T: type) !T { | 249 | pub fn readStruct(self: Self, comptime T: type) !T { |
| 238 | // Only extern and packed structs have defined in-memory layout. | 250 | // Only extern and packed structs have defined in-memory layout. |
| 239 | comptime assert(@typeInfo(T).Struct.layout != builtin.TypeInfo.ContainerLayout.Auto); | 251 | comptime assert(@typeInfo(T).Struct.layout != builtin.TypeInfo.ContainerLayout.Auto); |
| ... | @@ -276,3 +288,9 @@ test "InStream" { | ... | @@ -276,3 +288,9 @@ test "InStream" { |
| 276 | }, undefined)) == .c); | 288 | }, undefined)) == .c); |
| 277 | testing.expectError(error.EndOfStream, in_stream.readByte()); | 289 | testing.expectError(error.EndOfStream, in_stream.readByte()); |
| 278 | } | 290 | } |
| | 291 | |
| | 292 | test "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 | } |