authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-02-09 19:58:45 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-02-09 19:58:45 -08:00
logf7684f8a33fd0d78882f03b3751c941262926f8c
treea82afe347bfd4ea768e5cd0078af7167e3adc5cc
parent0c8e1fee2fe74a329c6130c249e9e56c007c2864

add BufIndexer


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

src/lib.zig+20
...@@ -384,3 +384,23 @@ pub fn indexBufferT(bytes: []const u8, comptime T: type, endian: std.builtin.End...@@ -384,3 +384,23 @@ pub fn indexBufferT(bytes: []const u8, comptime T: type, endian: std.builtin.End
384 error.EndOfStream => unreachable, // assert above has been violated384 error.EndOfStream => unreachable, // assert above has been violated
385 };385 };
386}386}
387
388pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type {
389 return struct {
390 bytes: []const u8,
391 max_len: usize,
392
393 const Self = @This();
394
395 pub fn init(bytes: []const u8, max_len: usize) Self {
396 return .{
397 .bytes = bytes,
398 .max_len = max_len,
399 };
400 }
401
402 pub fn at(self: *const Self, idx: usize) T {
403 return indexBufferT(self.bytes, T, endian, idx, self.max_len);
404 }
405 };
406}