authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2019-11-11 19:06:35+01:00
committergravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2019-11-11 19:06:35+01:00
log739f71610836868fdc64be4f81586a10a280dccb
tree85ca4daca1a665dd4c63309e13bc8d22775e3ae5
parentf6d124418fdcd0387f079cee8e5e39c33ef91911

minor fixes


1 files changed, 19 insertions(+), 16 deletions(-)

lib/std/json.zig+19-16
......@@ -1002,6 +1002,8 @@ pub const ValueTree = struct {
10021002pub const ObjectMap = StringHashMap(Value);
10031003pub const Array = ArrayList(Value);
10041004
1005/// Represents a JSON value
1006/// Currently only supports numbers that fit into i64 or f64.
10051007pub const Value = union(enum) {
10061008 Null,
10071009 Bool: bool,
......@@ -1270,10 +1272,11 @@ pub const Parser = struct {
12701272 }
12711273};
12721274
1273/// Unescape a JSON string
1274/// Optimized for arena allocators, uses Allocator.shrink
1275pub fn unescapeStringAlloc(alloc: *Allocator, input: []const u8) ![]u8 {
1276 var output = try alloc.alloc(u8, input.len);
1275// Unescape a JSON string
1276// Only to be used on strings already validated by the parser
1277// Optimized for arena allocators, uses Allocator.shrink
1278fn unescapeStringAlloc(alloc: *Allocator, input: []const u8) ![]u8 {
1279 const output = try alloc.alloc(u8, input.len);
12771280 errdefer alloc.free(output);
12781281
12791282 var inIndex: usize = 0;
......@@ -1281,8 +1284,8 @@ pub fn unescapeStringAlloc(alloc: *Allocator, input: []const u8) ![]u8 {
12811284 while(inIndex < input.len) {
12821285 if(input[inIndex] == '\\'){
12831286 if(input[inIndex + 1] == 'u'){
1284 const codepoint = std.fmt.parseInt(u32, input[inIndex+2 .. inIndex+6], 16) catch return error.InvalidUnicodeHexSymbol;
1285 outIndex += std.unicode.utf8Encode(codepoint, output[outIndex..]) catch return error.InvalidUnicodeHexSymbol;
1287 const codepoint = std.fmt.parseInt(u32, input[inIndex+2 .. inIndex+6], 16) catch unreachable;
1288 outIndex += std.unicode.utf8Encode(codepoint, output[outIndex..]) catch unreachable;
12861289 inIndex += 6;
12871290 } else {
12881291 output[outIndex] = @as(u8,
......@@ -1295,7 +1298,7 @@ pub fn unescapeStringAlloc(alloc: *Allocator, input: []const u8) ![]u8 {
12951298 'f' => 12,
12961299 'b' => 8,
12971300 '"' => '"',
1298 else => return error.InvalidEscapeCharacter
1301 else => unreachable
12991302 }
13001303 );
13011304 inIndex += 2;
......@@ -1441,13 +1444,13 @@ test "escaped characters" {
14411444
14421445 const obj = tree.root.Object;
14431446
1444 testing.expect(mem.eql(u8, obj.get("backslash").?.value.String, "\\"));
1445 testing.expect(mem.eql(u8, obj.get("forwardslash").?.value.String, "/"));
1446 testing.expect(mem.eql(u8, obj.get("newline").?.value.String, "\n"));
1447 testing.expect(mem.eql(u8, obj.get("carriagereturn").?.value.String, "\r"));
1448 testing.expect(mem.eql(u8, obj.get("tab").?.value.String, "\t"));
1449 testing.expect(mem.eql(u8, obj.get("formfeed").?.value.String, "\x0C"));
1450 testing.expect(mem.eql(u8, obj.get("backspace").?.value.String, "\x08"));
1451 testing.expect(mem.eql(u8, obj.get("doublequote").?.value.String, "\""));
1452 testing.expect(mem.eql(u8, obj.get("unicode").?.value.String, "ą"));
1447 testing.expectEqualSlices(u8, obj.get("backslash").?.value.String, "\\");
1448 testing.expectEqualSlices(u8, obj.get("forwardslash").?.value.String, "/");
1449 testing.expectEqualSlices(u8, obj.get("newline").?.value.String, "\n");
1450 testing.expectEqualSlices(u8, obj.get("carriagereturn").?.value.String, "\r");
1451 testing.expectEqualSlices(u8, obj.get("tab").?.value.String, "\t");
1452 testing.expectEqualSlices(u8, obj.get("formfeed").?.value.String, "\x0C");
1453 testing.expectEqualSlices(u8, obj.get("backspace").?.value.String, "\x08");
1454 testing.expectEqualSlices(u8, obj.get("doublequote").?.value.String, "\"");
1455 testing.expectEqualSlices(u8, obj.get("unicode").?.value.String, "ą");
14531456}