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 {...@@ -184,7 +184,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
184 if (try p.eatByte('0')) |_| {184 if (try p.eatByte('0')) |_| {
185 return try p.addNumber(alloc, "0");185 return try p.addNumber(alloc, "0");
186 }186 }
187 while (try p.eatAnyScalar("0123456789")) |d| {187 while (try p.eatRange('0', '9')) |d| {
188 try characters.append(d);188 try characters.append(d);
189 }189 }
190 if (characters.items.len == 0) {190 if (characters.items.len == 0) {
...@@ -193,7 +193,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -193,7 +193,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
193 if (try p.eatByte('.')) |c| {193 if (try p.eatByte('.')) |c| {
194 try characters.append(c);194 try characters.append(c);
195 const l = characters.items.len;195 const l = characters.items.len;
196 while (try p.eatAnyScalar("0123456789")) |d| {196 while (try p.eatRange('0', '9')) |d| {
197 try characters.append(d);197 try characters.append(d);
198 }198 }
199 if (characters.items.len == l) return error.JsonExpectedTODO;199 if (characters.items.len == l) return error.JsonExpectedTODO;
...@@ -202,7 +202,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -202,7 +202,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
202 try characters.append('e');202 try characters.append('e');
203 try characters.append(try p.eatAnyScalar("+-") orelse return error.JsonExpectedTODO);203 try characters.append(try p.eatAnyScalar("+-") orelse return error.JsonExpectedTODO);
204 const l = characters.items.len;204 const l = characters.items.len;
205 while (try p.eatAnyScalar("0123456789")) |d| {205 while (try p.eatRange('0', '9')) |d| {
206 try characters.append(d);206 try characters.append(d);
207 }207 }
208 if (characters.items.len == l) return error.JsonExpectedTODO;208 if (characters.items.len == l) return error.JsonExpectedTODO;
...@@ -289,6 +289,18 @@ const Parser = struct {...@@ -289,6 +289,18 @@ const Parser = struct {
289 return null;289 return null;
290 }290 }
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
292 pub fn eatAnyScalar(p: *Parser, test_s: string) !?u8 {304 pub fn eatAnyScalar(p: *Parser, test_s: string) !?u8 {
293 const t = tracer.trace(@src(), " ({s})", .{test_s});305 const t = tracer.trace(@src(), " ({s})", .{test_s});
294 defer t.end();306 defer t.end();