Compare commits

...

6 Commits

Author SHA1 Message Date
Daniel Berg
dbddeec49b
Merge 90df6f62a1 into acba2645f7 2024-11-19 23:27:50 -06:00
D-Berg
90df6f62a1 ran zig fmt 2024-11-14 10:43:42 +01:00
Daniel Berg
84b7364b59
Merge branch 'master' into json_stricter_number_parsing 2024-11-14 10:39:22 +01:00
D-Berg
4e17b6b4e5 Fixed tests in static_test.zig when parsing strings to numbers 2024-11-11 12:15:34 +01:00
D-Berg
8cfb9239e7 removed .string and .allocated_string from inner_parse when parsing numbers
Now returns an error UnexpectedToken
2024-11-11 12:15:34 +01:00
D-Berg
a7792b4563 return UnexpectedToken when trying to parse a string as a int 2024-11-11 12:15:34 +01:00
2 changed files with 7 additions and 4 deletions

View File

@ -230,7 +230,7 @@ pub fn innerParse(
const token = try source.nextAllocMax(allocator, .alloc_if_needed, options.max_value_len.?);
defer freeAllocated(allocator, token);
const slice = switch (token) {
inline .number, .allocated_number, .string, .allocated_string => |slice| slice,
inline .number, .allocated_number => |slice| slice,
else => return error.UnexpectedToken,
};
return try std.fmt.parseFloat(T, slice);
@ -239,7 +239,7 @@ pub fn innerParse(
const token = try source.nextAllocMax(allocator, .alloc_if_needed, options.max_value_len.?);
defer freeAllocated(allocator, token);
const slice = switch (token) {
inline .number, .allocated_number, .string, .allocated_string => |slice| slice,
inline .number, .allocated_number => |slice| slice,
else => return error.UnexpectedToken,
};
return sliceToInt(T, slice);

View File

@ -384,8 +384,11 @@ test "parse" {
try testing.expectEqual("foo".*, try parseFromSliceLeaky([3]u8, testing.allocator, "[102, 111, 111]", .{}));
try testing.expectEqual(undefined, try parseFromSliceLeaky([0]u8, testing.allocator, "[]", .{}));
try testing.expectEqual(12345678901234567890, try parseFromSliceLeaky(u64, testing.allocator, "\"12345678901234567890\"", .{}));
try testing.expectEqual(123.456, try parseFromSliceLeaky(f64, testing.allocator, "\"123.456\"", .{}));
try testing.expectEqual("12345678901234567890".*, try parseFromSliceLeaky([20]u8, testing.allocator, "\"12345678901234567890\"", .{}));
try testing.expectEqual(12345678901234567890, try parseFromSliceLeaky(u64, testing.allocator, "12345678901234567890", .{}));
try testing.expectEqual("123.456".*, try parseFromSliceLeaky([7]u8, testing.allocator, "\"123.456\"", .{}));
try testing.expectEqual(123.456, try parseFromSliceLeaky(f64, testing.allocator, "123.456", .{}));
}
test "parse into enum" {