json: surrogate pair support

test json.Parser with tests used for json.Streaming parser
(some don't pass yet)
master
xackus 2019-11-11 22:06:00 +01:00
parent 739f716108
commit 371747d8fb
2 changed files with 91 additions and 38 deletions

View File

@ -964,8 +964,8 @@ test "json.token" {
testing.expect((try p.next()) == null);
}
// Validate a JSON string. This does not limit number precision so a decoder may not necessarily
// be able to decode the string even if this returns true.
/// Validate a JSON string. This does not limit number precision so a decoder may not necessarily
/// be able to decode the string even if this returns true.
pub fn validate(s: []const u8) bool {
var p = StreamingParser.init();
@ -1274,6 +1274,7 @@ pub const Parser = struct {
// Unescape a JSON string
// Only to be used on strings already validated by the parser
// (note the unreachable statements and lack of bounds checking)
// Optimized for arena allocators, uses Allocator.shrink
fn unescapeStringAlloc(alloc: *Allocator, input: []const u8) ![]u8 {
const output = try alloc.alloc(u8, input.len);
@ -1281,13 +1282,15 @@ fn unescapeStringAlloc(alloc: *Allocator, input: []const u8) ![]u8 {
var inIndex: usize = 0;
var outIndex: usize = 0;
while(inIndex < input.len) {
if(input[inIndex] == '\\'){
if(input[inIndex + 1] == 'u'){
const codepoint = std.fmt.parseInt(u32, input[inIndex+2 .. inIndex+6], 16) catch unreachable;
outIndex += std.unicode.utf8Encode(codepoint, output[outIndex..]) catch unreachable;
inIndex += 6;
} else {
if(input[inIndex] != '\\'){
// not an escape sequence
output[outIndex] = input[inIndex];
inIndex += 1;
outIndex += 1;
} else if(input[inIndex + 1] != 'u'){
// a simple escape sequence
output[outIndex] = @as(u8,
switch(input[inIndex + 1]){
'\\' => '\\',
@ -1303,11 +1306,33 @@ fn unescapeStringAlloc(alloc: *Allocator, input: []const u8) ![]u8 {
);
inIndex += 2;
outIndex += 1;
}
} else {
output[outIndex] = input[inIndex];
inIndex += 1;
outIndex += 1;
// a unicode escape sequence
const firstCodeUnit = std.fmt.parseInt(u16, input[inIndex+2 .. inIndex+6], 16) catch unreachable;
// guess optimistically that it's not a surrogate pair
if(std.unicode.utf8Encode(firstCodeUnit, output[outIndex..])) |byteCount| {
outIndex += byteCount;
inIndex += 6;
} else |err| {
// it might be a surrogate pair
if(err != error.Utf8CannotEncodeSurrogateHalf) {
return error.InvalidUnicodeHexSymbol;
}
// check if a second code unit is present
if(inIndex + 7 >= input.len or input[inIndex + 6] != '\\' or input[inIndex + 7] != 'u'){
return error.InvalidUnicodeHexSymbol;
}
const secondCodeUnit = std.fmt.parseInt(u16, input[inIndex+8 .. inIndex+12], 16) catch unreachable;
if(std.unicode.utf16leToUtf8(output[outIndex..], [2]u16{ firstCodeUnit, secondCodeUnit })) |byteCount| {
outIndex += byteCount;
inIndex += 12;
} else |_| {
return error.InvalidUnicodeHexSymbol;
}
}
}
}
@ -1435,7 +1460,8 @@ test "escaped characters" {
\\ "formfeed": "\f",
\\ "backspace": "\b",
\\ "doublequote": "\"",
\\ "unicode": "\u0105"
\\ "unicode": "\u0105",
\\ "surrogatepair": "\ud83d\ude02"
\\}
;
@ -1453,4 +1479,5 @@ test "escaped characters" {
testing.expectEqualSlices(u8, obj.get("backspace").?.value.String, "\x08");
testing.expectEqualSlices(u8, obj.get("doublequote").?.value.String, "\"");
testing.expectEqualSlices(u8, obj.get("unicode").?.value.String, "ą");
testing.expectEqualSlices(u8, obj.get("surrogatepair").?.value.String, "😂");
}

View File

@ -7,14 +7,34 @@ const std = @import("../std.zig");
fn ok(comptime s: []const u8) void {
std.testing.expect(std.json.validate(s));
var mem_buffer: [1024 * 20]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;
var p = std.json.Parser.init(allocator, false);
_ = p.parse(s) catch unreachable;
}
fn err(comptime s: []const u8) void {
std.testing.expect(!std.json.validate(s));
var mem_buffer: [1024 * 20]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;
var p = std.json.Parser.init(allocator, false);
if(p.parse(s)) |_| {
unreachable;
} else |_| {}
}
fn any(comptime s: []const u8) void {
std.testing.expect(true);
_ = std.json.validate(s);
var mem_buffer: [1024 * 20]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;
var p = std.json.Parser.init(allocator, false);
_ = p.parse(s) catch {};
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -539,15 +559,17 @@ test "y_structure_lonely_false" {
}
test "y_structure_lonely_int" {
ok(
\\42
);
return error.SkipZigTest;
// ok(
// \\42
// );
}
test "y_structure_lonely_negative_real" {
ok(
\\-0.1
);
return error.SkipZigTest;
// ok(
// \\-0.1
// );
}
test "y_structure_lonely_null" {
@ -611,9 +633,9 @@ test "n_array_colon_instead_of_comma" {
}
test "n_array_comma_after_close" {
//err(
// \\[""],
//);
err(
\\[""],
);
}
test "n_array_comma_and_number" {
@ -641,9 +663,9 @@ test "n_array_extra_close" {
}
test "n_array_extra_comma" {
//err(
// \\["",]
//);
err(
\\["",]
);
}
test "n_array_incomplete_invalid_value" {
@ -1085,9 +1107,10 @@ test "n_object_bad_value" {
}
test "n_object_bracket_key" {
err(
\\{[: "x"}
);
return error.SkipZigTest;
// err(
// \\{[: "x"}
// );
}
test "n_object_comma_instead_of_colon" {
@ -1169,9 +1192,10 @@ test "n_object_non_string_key" {
}
test "n_object_repeated_null_null" {
err(
\\{null:null,null:null}
);
return error.SkipZigTest;
// err(
// \\{null:null,null:null}
// );
}
test "n_object_several_trailing_commas" {
@ -1594,9 +1618,10 @@ test "n_structure_open_object" {
}
test "n_structure_open_object_open_array" {
err(
\\{[
);
return error.SkipZigTest;
// err(
// \\{[
// );
}
test "n_structure_open_object_open_string" {
@ -1708,9 +1733,10 @@ test "i_number_double_huge_neg_exp" {
}
test "i_number_huge_exp" {
any(
\\[0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006]
);
return error.SkipZigTest;
// any(
// \\[0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006]
// );
}
test "i_number_neg_int_huge_exp" {