authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-02 11:46:50+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-02 11:46:50+01:00
log02efc2236a8223872f57a1a7a5aec756b5a23a76
tree30c48ad99e9cdedcb89858dd2f918662e86b670b
parent4fd1ec78e93d496811534c6e3aa9af126650f5de

std: Fix json utf{8,16} decoding on BE targets

Byteswap some values when LE ordering is required.

2 files changed, 11 insertions(+), 82 deletions(-)

lib/std/json.zig+11-13
...@@ -2125,7 +2125,11 @@ fn unescapeString(output: []u8, input: []const u8) !void {...@@ -2125,7 +2125,11 @@ fn unescapeString(output: []u8, input: []const u8) !void {
21252125
2126 const secondCodeUnit = std.fmt.parseInt(u16, input[inIndex + 8 .. inIndex + 12], 16) catch unreachable;2126 const secondCodeUnit = std.fmt.parseInt(u16, input[inIndex + 8 .. inIndex + 12], 16) catch unreachable;
21272127
2128 if (std.unicode.utf16leToUtf8(output[outIndex..], &[2]u16{ firstCodeUnit, secondCodeUnit })) |byteCount| {2128 const utf16le_seq = [2]u16{
2129 mem.littleToNative(u16, firstCodeUnit),
2130 mem.littleToNative(u16, secondCodeUnit),
2131 };
2132 if (std.unicode.utf16leToUtf8(output[outIndex..], &utf16le_seq)) |byteCount| {
2129 outIndex += byteCount;2133 outIndex += byteCount;
2130 inIndex += 12;2134 inIndex += 12;
2131 } else |_| {2135 } else |_| {
...@@ -2265,9 +2269,6 @@ test "integer after float has proper type" {...@@ -2265,9 +2269,6 @@ test "integer after float has proper type" {
2265}2269}
22662270
2267test "escaped characters" {2271test "escaped characters" {
2268 // https://github.com/ziglang/zig/issues/5127
2269 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
2270
2271 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);2272 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
2272 defer arena_allocator.deinit();2273 defer arena_allocator.deinit();
2273 const input =2274 const input =
...@@ -2300,9 +2301,6 @@ test "escaped characters" {...@@ -2300,9 +2301,6 @@ test "escaped characters" {
2300}2301}
23012302
2302test "string copy option" {2303test "string copy option" {
2303 // https://github.com/ziglang/zig/issues/5127
2304 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
2305
2306 const input =2304 const input =
2307 \\{2305 \\{
2308 \\ "noescape": "aą😂",2306 \\ "noescape": "aą😂",
...@@ -2721,9 +2719,9 @@ test "stringify struct with indentation" {...@@ -2721,9 +2719,9 @@ test "stringify struct with indentation" {
2721 \\}2719 \\}
2722 ,2720 ,
2723 struct {2721 struct {
2724 foo: u32,2722 foo: u32,
2725 bar: [3]u32,2723 bar: [3]u32,
2726 }{2724 }{
2727 .foo = 42,2725 .foo = 42,
2728 .bar = .{ 1, 2, 3 },2726 .bar = .{ 1, 2, 3 },
2729 },2727 },
...@@ -2734,9 +2732,9 @@ test "stringify struct with indentation" {...@@ -2734,9 +2732,9 @@ test "stringify struct with indentation" {
2734 try teststringify(2732 try teststringify(
2735 "{\n\t\"foo\":42,\n\t\"bar\":[\n\t\t1,\n\t\t2,\n\t\t3\n\t]\n}",2733 "{\n\t\"foo\":42,\n\t\"bar\":[\n\t\t1,\n\t\t2,\n\t\t3\n\t]\n}",
2736 struct {2734 struct {
2737 foo: u32,2735 foo: u32,
2738 bar: [3]u32,2736 bar: [3]u32,
2739 }{2737 }{
2740 .foo = 42,2738 .foo = 42,
2741 .bar = .{ 1, 2, 3 },2739 .bar = .{ 1, 2, 3 },
2742 },2740 },
lib/std/json/test.zig-69
...@@ -337,18 +337,12 @@ test "y_string_1_2_3_bytes_UTF-8_sequences" {...@@ -337,18 +337,12 @@ test "y_string_1_2_3_bytes_UTF-8_sequences" {
337}337}
338338
339test "y_string_accepted_surrogate_pair" {339test "y_string_accepted_surrogate_pair" {
340 // https://github.com/ziglang/zig/issues/5127
341 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
342
343 ok(340 ok(
344 \\["\uD801\udc37"]341 \\["\uD801\udc37"]
345 );342 );
346}343}
347344
348test "y_string_accepted_surrogate_pairs" {345test "y_string_accepted_surrogate_pairs" {
349 // https://github.com/ziglang/zig/issues/5127
350 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
351
352 ok(346 ok(
353 \\["\ud83d\ude39\ud83d\udc8d"]347 \\["\ud83d\ude39\ud83d\udc8d"]
354 );348 );
...@@ -415,9 +409,6 @@ test "y_string_in_array_with_leading_space" {...@@ -415,9 +409,6 @@ test "y_string_in_array_with_leading_space" {
415}409}
416410
417test "y_string_last_surrogates_1_and_2" {411test "y_string_last_surrogates_1_and_2" {
418 // https://github.com/ziglang/zig/issues/5127
419 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
420
421 ok(412 ok(
422 \\["\uDBFF\uDFFF"]413 \\["\uDBFF\uDFFF"]
423 );414 );
...@@ -478,9 +469,6 @@ test "y_string_space" {...@@ -478,9 +469,6 @@ test "y_string_space" {
478}469}
479470
480test "y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF" {471test "y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF" {
481 // https://github.com/ziglang/zig/issues/5127
482 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
483
484 ok(472 ok(
485 \\["\uD834\uDd1e"]473 \\["\uD834\uDd1e"]
486 );474 );
...@@ -523,90 +511,60 @@ test "y_string_unescaped_char_delete" {...@@ -523,90 +511,60 @@ test "y_string_unescaped_char_delete" {
523}511}
524512
525test "y_string_unicode_2" {513test "y_string_unicode_2" {
526 // https://github.com/ziglang/zig/issues/5127
527 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
528
529 ok(514 ok(
530 \\["⍂㈴⍂"]515 \\["⍂㈴⍂"]
531 );516 );
532}517}
533518
534test "y_string_unicodeEscapedBackslash" {519test "y_string_unicodeEscapedBackslash" {
535 // https://github.com/ziglang/zig/issues/5127
536 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
537
538 ok(520 ok(
539 \\["\u005C"]521 \\["\u005C"]
540 );522 );
541}523}
542524
543test "y_string_unicode_escaped_double_quote" {525test "y_string_unicode_escaped_double_quote" {
544 // https://github.com/ziglang/zig/issues/5127
545 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
546
547 ok(526 ok(
548 \\["\u0022"]527 \\["\u0022"]
549 );528 );
550}529}
551530
552test "y_string_unicode" {531test "y_string_unicode" {
553 // https://github.com/ziglang/zig/issues/5127
554 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
555
556 ok(532 ok(
557 \\["\uA66D"]533 \\["\uA66D"]
558 );534 );
559}535}
560536
561test "y_string_unicode_U+10FFFE_nonchar" {537test "y_string_unicode_U+10FFFE_nonchar" {
562 // https://github.com/ziglang/zig/issues/5127
563 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
564
565 ok(538 ok(
566 \\["\uDBFF\uDFFE"]539 \\["\uDBFF\uDFFE"]
567 );540 );
568}541}
569542
570test "y_string_unicode_U+1FFFE_nonchar" {543test "y_string_unicode_U+1FFFE_nonchar" {
571 // https://github.com/ziglang/zig/issues/5127
572 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
573
574 ok(544 ok(
575 \\["\uD83F\uDFFE"]545 \\["\uD83F\uDFFE"]
576 );546 );
577}547}
578548
579test "y_string_unicode_U+200B_ZERO_WIDTH_SPACE" {549test "y_string_unicode_U+200B_ZERO_WIDTH_SPACE" {
580 // https://github.com/ziglang/zig/issues/5127
581 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
582
583 ok(550 ok(
584 \\["\u200B"]551 \\["\u200B"]
585 );552 );
586}553}
587554
588test "y_string_unicode_U+2064_invisible_plus" {555test "y_string_unicode_U+2064_invisible_plus" {
589 // https://github.com/ziglang/zig/issues/5127
590 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
591
592 ok(556 ok(
593 \\["\u2064"]557 \\["\u2064"]
594 );558 );
595}559}
596560
597test "y_string_unicode_U+FDD0_nonchar" {561test "y_string_unicode_U+FDD0_nonchar" {
598 // https://github.com/ziglang/zig/issues/5127
599 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
600
601 ok(562 ok(
602 \\["\uFDD0"]563 \\["\uFDD0"]
603 );564 );
604}565}
605566
606test "y_string_unicode_U+FFFE_nonchar" {567test "y_string_unicode_U+FFFE_nonchar" {
607 // https://github.com/ziglang/zig/issues/5127
608 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
609
610 ok(568 ok(
611 \\["\uFFFE"]569 \\["\uFFFE"]
612 );570 );
...@@ -1858,63 +1816,42 @@ test "i_object_key_lone_2nd_surrogate" {...@@ -1858,63 +1816,42 @@ test "i_object_key_lone_2nd_surrogate" {
1858}1816}
18591817
1860test "i_string_1st_surrogate_but_2nd_missing" {1818test "i_string_1st_surrogate_but_2nd_missing" {
1861 // https://github.com/ziglang/zig/issues/5127
1862 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
1863
1864 anyStreamingErrNonStreaming(1819 anyStreamingErrNonStreaming(
1865 \\["\uDADA"]1820 \\["\uDADA"]
1866 );1821 );
1867}1822}
18681823
1869test "i_string_1st_valid_surrogate_2nd_invalid" {1824test "i_string_1st_valid_surrogate_2nd_invalid" {
1870 // https://github.com/ziglang/zig/issues/5127
1871 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
1872
1873 anyStreamingErrNonStreaming(1825 anyStreamingErrNonStreaming(
1874 \\["\uD888\u1234"]1826 \\["\uD888\u1234"]
1875 );1827 );
1876}1828}
18771829
1878test "i_string_incomplete_surrogate_and_escape_valid" {1830test "i_string_incomplete_surrogate_and_escape_valid" {
1879 // https://github.com/ziglang/zig/issues/5127
1880 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
1881
1882 anyStreamingErrNonStreaming(1831 anyStreamingErrNonStreaming(
1883 \\["\uD800\n"]1832 \\["\uD800\n"]
1884 );1833 );
1885}1834}
18861835
1887test "i_string_incomplete_surrogate_pair" {1836test "i_string_incomplete_surrogate_pair" {
1888 // https://github.com/ziglang/zig/issues/5127
1889 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
1890
1891 anyStreamingErrNonStreaming(1837 anyStreamingErrNonStreaming(
1892 \\["\uDd1ea"]1838 \\["\uDd1ea"]
1893 );1839 );
1894}1840}
18951841
1896test "i_string_incomplete_surrogates_escape_valid" {1842test "i_string_incomplete_surrogates_escape_valid" {
1897 // https://github.com/ziglang/zig/issues/5127
1898 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
1899
1900 anyStreamingErrNonStreaming(1843 anyStreamingErrNonStreaming(
1901 \\["\uD800\uD800\n"]1844 \\["\uD800\uD800\n"]
1902 );1845 );
1903}1846}
19041847
1905test "i_string_invalid_lonely_surrogate" {1848test "i_string_invalid_lonely_surrogate" {
1906 // https://github.com/ziglang/zig/issues/5127
1907 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
1908
1909 anyStreamingErrNonStreaming(1849 anyStreamingErrNonStreaming(
1910 \\["\ud800"]1850 \\["\ud800"]
1911 );1851 );
1912}1852}
19131853
1914test "i_string_invalid_surrogate" {1854test "i_string_invalid_surrogate" {
1915 // https://github.com/ziglang/zig/issues/5127
1916 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
1917
1918 anyStreamingErrNonStreaming(1855 anyStreamingErrNonStreaming(
1919 \\["\ud800abc"]1856 \\["\ud800abc"]
1920 );1857 );
...@@ -1927,9 +1864,6 @@ test "i_string_invalid_utf-8" {...@@ -1927,9 +1864,6 @@ test "i_string_invalid_utf-8" {
1927}1864}
19281865
1929test "i_string_inverted_surrogates_U+1D11E" {1866test "i_string_inverted_surrogates_U+1D11E" {
1930 // https://github.com/ziglang/zig/issues/5127
1931 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
1932
1933 anyStreamingErrNonStreaming(1867 anyStreamingErrNonStreaming(
1934 \\["\uDd1e\uD834"]1868 \\["\uDd1e\uD834"]
1935 );1869 );
...@@ -1942,9 +1876,6 @@ test "i_string_iso_latin_1" {...@@ -1942,9 +1876,6 @@ test "i_string_iso_latin_1" {
1942}1876}
19431877
1944test "i_string_lone_second_surrogate" {1878test "i_string_lone_second_surrogate" {
1945 // https://github.com/ziglang/zig/issues/5127
1946 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
1947
1948 anyStreamingErrNonStreaming(1879 anyStreamingErrNonStreaming(
1949 \\["\uDFAA"]1880 \\["\uDFAA"]
1950 );1881 );