Add std/json backslash-related quirks

This commit is contained in:
Nigel Tao committed 2020-03-19 15:56:21 +11:00
1 parent b86373aff5
commit 97f7f28218
4 files changed
+175 -36

No files matched your search

+30 -5
View File
@@ -56,6 +56,24 @@ pri const decoder_number_length_max_incl base.u64 = 99
// Look-Up Tables (LUTs).
// lut_backslashes[i] helps decode "\i", for various 'i's.
//
// If the element's 0x80 bit is set then "\i" is unconditionally a valid
// single-output-byte backslash-escape. The low 7 bits are the unescaped value.
// For example, lut_backslashes['n'] is (0x80 | 0x0A), because "\n" is U+000A.
//
// If the element is non-zero (but the 0x80 bit is not set) then "\i"'s
// validity depends on the relevant quirk. The element's value is an enum:
// - 1: "\a", U+0007, quirk_allow_backslash_a.
// - 2: "\e", U+001B, quirk_allow_backslash_e.
// - 3: "\?", U+003F, quirk_allow_backslash_question_mark.
// - 4: "\'", U+0027, quirk_allow_backslash_single_quote.
// - 5: "\v", U+000B, quirk_allow_backslash_v.
// - 6: "\0", U+0000, quirk_allow_backslash_zero.
// The U+1234 values are held in lut_quirky_backslashes, below.
//
// If the element is zero then "\i" is invalid, or it is a special case, the
// start of "\x12", "\u1234" or "\U12345678".
pri const lut_backslashes array[256] base.u8 = [
// 0 1 2 3 4 5 6 7
// 8 9 A B C D E F
@@ -63,18 +81,18 @@ pri const lut_backslashes array[256] base.u8 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F.
0x00, 0x00, 0xA2, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27. '"'.
0x00, 0x00, 0xA2, 0x00, 0x00, 0x00, 0x00, 0x04, // 0x20 ..= 0x27. '"', '\''.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAF, // 0x28 ..= 0x2F. '/'.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x30 ..= 0x37.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F.
0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x30 ..= 0x37. '0'.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // 0x38 ..= 0x3F. '?'
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 ..= 0x47.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57.
0x00, 0x00, 0x00, 0x00, 0xDC, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F. '\\'.
0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x8C, 0x00, // 0x60 ..= 0x67. 'b', 'f'.
0x00, 0x01, 0x88, 0x00, 0x00, 0x02, 0x8C, 0x00, // 0x60 ..= 0x67. 'a', 'b', 'e', 'f'.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8A, 0x00, // 0x68 ..= 0x6F. 'n'.
0x00, 0x00, 0x8D, 0x00, 0x89, 0x00, 0x00, 0x00, // 0x70 ..= 0x77. 'r', 't'.
0x00, 0x00, 0x8D, 0x00, 0x89, 0x00, 0x05, 0x00, // 0x70 ..= 0x77. 'r', 't', 'v'.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87.
@@ -98,6 +116,13 @@ pri const lut_backslashes array[256] base.u8 = [
// 8 9 A B C D E F
]
// lut_quirky_backslashes is discussed in the lut_backslashes comment. The
// first and last elements (indexes 0 and 7) are not used, but 8 is a round
// power of 2, so enforcing index-in-bounds is a simple "&7" operation.
pri const lut_quirky_backslashes array[8] base.u8 = [
0x00, 0x07, 0x1B, 0x3F, 0x27, 0x0B, 0x00, 0x00,
]
// lut_chars helps decode bytes within a string:
// - 0x00 is 1-byte UTF-8 (ASCII) but not '"', '\\' or a C0 control code.
// - 0x01 is '"'.