From a0b32e3f7a6571c509c4b2c48c8d56a24d082f5e Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 24 Jun 2024 04:12:07 -0700 Subject: [PATCH] add eatUntilStr for multi-byte eatUntil --- intrusive_parser.zig | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/intrusive_parser.zig b/intrusive_parser.zig index 76ead6bcb92efc26a28dc5d184dcff7e3c97ad29..1d05d3c076af92c52a2fc4ec3682767948d33b60 100644 --- a/intrusive_parser.zig +++ b/intrusive_parser.zig @@ -132,6 +132,22 @@ pub const Parser = struct { } } + pub fn eatUntilStr(p: *Parser, test_s: []const u8) !?[2]usize { + const start = p.idx; + while (true) { + try p.peekAmt(1) orelse return null; + const amt = std.mem.indexOf(u8, p.slice(), test_s) orelse { + const left = p.avail(); + p.idx += left; + continue; + }; + p.idx += amt; + p.idx += test_s.len; + const end = p.idx; + return .{ start, end }; + } + } + // tag(u8) + len(u32) + bytes(N) pub fn addStr(p: *Parser, alloc: std.mem.Allocator, str: string) !usize { const adapter: AdapterStr = .{ .p = p }; @@ -213,5 +229,9 @@ pub fn Mixin(comptime T: type) type { pub fn eatUntil(pp: *T, test_c: u8) !?[2]usize { return pp.parser.eatUntil(test_c); } + + pub fn eatUntilStr(pp: *T, test_s: []const u8) !?[2]usize { + return pp.parser.eatUntilStr(test_s); + } }; } -- 2.54.0