From 03849b83b473f8fbc9c3aac34aa3da5c2e6b9ea3 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 23 Jun 2024 00:58:46 -0700 Subject: [PATCH] add eatUntil --- intrusive_parser.zig | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/intrusive_parser.zig b/intrusive_parser.zig index 81c72c65eabd614ee9ec070bb66fc9119a59d228..7d69a3acad8de491eee555c804398273622a7aeb 100644 --- a/intrusive_parser.zig +++ b/intrusive_parser.zig @@ -116,6 +116,23 @@ pub const Parser = struct { return amt; } + pub fn eatUntil(p: *Parser, test_c: u8) !?struct { usize, usize } { + const start = p.idx; + var len: usize = 0; + while (true) { + try p.peekAmt(1) orelse return null; + const amt = std.mem.indexOfScalar(u8, p.slice(), test_c) orelse { + const left = p.avail(); + len += left; + p.idx += left; + continue; + }; + len += amt; + p.idx += amt; + return .{ start, len }; + } + } + // tag(u8) + len(u32) + bytes(N) pub fn addStr(p: *Parser, alloc: std.mem.Allocator, str: string) !usize { const adapter: AdapterStr = .{ .p = p }; @@ -193,5 +210,9 @@ pub fn Mixin(comptime T: type) type { pub fn trimByte(pp: *T, test_c: u8) !usize { return pp.parser.trimByte(test_c); } + + pub fn eatUntil(pp: *T, test_c: u8) !usize { + return pp.parser.eatUntil(test_c); + } }; } -- 2.54.0