authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-05-28 11:07:55 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-05-28 11:07:55 -07:00
logf090898e13b96d1e5f115e3c274be64d6374dcd3
tree9a04b740c41c377befa09d14da459da17943ee7e
parent42bf4b50b14381d0526f3346d099536100f257af

replace some eatAnyScalar with eatRange


1 files changed, 15 insertions(+), 3 deletions(-)

json.zig+15-3
......@@ -184,7 +184,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
184184 if (try p.eatByte('0')) |_| {
185185 return try p.addNumber(alloc, "0");
186186 }
187 while (try p.eatAnyScalar("0123456789")) |d| {
187 while (try p.eatRange('0', '9')) |d| {
188188 try characters.append(d);
189189 }
190190 if (characters.items.len == 0) {
......@@ -193,7 +193,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
193193 if (try p.eatByte('.')) |c| {
194194 try characters.append(c);
195195 const l = characters.items.len;
196 while (try p.eatAnyScalar("0123456789")) |d| {
196 while (try p.eatRange('0', '9')) |d| {
197197 try characters.append(d);
198198 }
199199 if (characters.items.len == l) return error.JsonExpectedTODO;
......@@ -202,7 +202,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
202202 try characters.append('e');
203203 try characters.append(try p.eatAnyScalar("+-") orelse return error.JsonExpectedTODO);
204204 const l = characters.items.len;
205 while (try p.eatAnyScalar("0123456789")) |d| {
205 while (try p.eatRange('0', '9')) |d| {
206206 try characters.append(d);
207207 }
208208 if (characters.items.len == l) return error.JsonExpectedTODO;
......@@ -289,6 +289,18 @@ const Parser = struct {
289289 return null;
290290 }
291291
292 pub fn eatRange(p: *Parser, comptime from: u8, comptime to: u8) !?u8 {
293 const t = tracer.trace(@src(), " ({d},{d})", .{ from, to });
294 defer t.end();
295
296 try p.peekAmt(1);
297 if (p.slice()[0] >= from and p.slice()[0] <= to) {
298 defer p.idx += 1;
299 return p.slice()[0];
300 }
301 return null;
302 }
303
292304 pub fn eatAnyScalar(p: *Parser, test_s: string) !?u8 {
293305 const t = tracer.trace(@src(), " ({s})", .{test_s});
294306 defer t.end();