Re-order std/json if-else chain

This commit is contained in:
Nigel Tao committed 2020-02-22 08:17:52 +11:00
1 parent 48e7004636
commit f6843719b5
2 files changed
+637 -627

No files matched your search

+296 -293
View File
@@ -152,249 +152,199 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
assert args.dst.available() > 0
assert args.src.available() > 0
if c <= 0x39 { // 0x39 is '9'.
if class == 0x01 { // 0x01 is CLASS_STRING.
// -------- BEGIN parse strings.
if 0 == (expect & 0x02) { // 0x02 is EXPECT_STRING_VALUE.
return "#bad input"
if class == 0x01 { // 0x01 is CLASS_STRING.
// -------- BEGIN parse strings.
if 0 == (expect & 0x02) { // 0x02 is EXPECT_STRING_VALUE.
return "#bad input"
}
// The leading '"' is filler.
args.dst.write_fast_token!(
value: 0,
length: 1)
args.src.skip32_fast!(actual: 1, worst_case: 1)
// If the string contents starts with an escape code, issue a
// zero-length string token (one with value_base_category 3)
// before issuing a token with value_base_category 4.
while true {
if args.src.available() <= 0 {
yield? base."$short read"
continue
}
// The leading '"' is filler.
args.dst.write_fast_token!(
value: 0,
length: 1)
args.src.skip32_fast!(actual: 1, worst_case: 1)
// If the string contents starts with an escape code, issue
// a zero-length string token (one with value_base_category
// 3) before issuing a token with value_base_category 4.
while true {
if args.src.available() <= 0 {
yield? base."$short read"
continue
}
if args.src.peek_u8() <> 0x5C { // 0x5C is '\\'.
break
}
if args.dst.available() <= 0 {
yield? base."$short write"
continue
}
args.dst.write_fast_token!(
value: 0x60_0001,
length: 0)
if args.src.peek_u8() <> 0x5C { // 0x5C is '\\'.
break
}
if args.dst.available() <= 0 {
yield? base."$short write"
continue
}
args.dst.write_fast_token!(
value: 0x60_0001,
length: 0)
break
}
while.string_loop true {
if args.dst.available() <= 0 {
yield? base."$short write"
while.string_loop true {
if args.dst.available() <= 0 {
yield? base."$short write"
continue.string_loop
}
string_length = 0
while true,
pre args.dst.available() > 0,
{
if args.src.available() <= 0 {
if string_length > 0 {
args.dst.write_fast_token!(
value: 0x60_0001,
length: string_length as base.u64)
string_length = 0
}
if args.src.is_closed() {
return "#bad input"
}
yield? base."$short read"
string_length = 0
continue.string_loop
}
c = args.src.peek_u8()
string_length = 0
while true,
pre args.dst.available() > 0,
{
if args.src.available() <= 0 {
if string_length > 0 {
args.dst.write_fast_token!(
value: 0x60_0001,
length: string_length as base.u64)
string_length = 0
}
if args.src.is_closed() {
return "#bad input"
}
yield? base."$short read"
if c <= 0x22 { // 0x22 is '"'.
if c == 0x22 { // 0x22 is '"'.
args.dst.write_fast_token!(
value: 0x60_0000,
length: string_length as base.u64)
string_length = 0
continue.string_loop
}
c = args.src.peek_u8()
break.string_loop
if c <= 0x22 { // 0x22 is '"'.
if c == 0x22 { // 0x22 is '"'.
args.dst.write_fast_token!(
value: 0x60_0000,
length: string_length as base.u64)
string_length = 0
break.string_loop
} else if c < 0x20 { // 0x20 is ' '.
if string_length > 0 {
args.dst.write_fast_token!(
value: 0x60_0001,
length: string_length as base.u64)
string_length = 0
}
return "#bad input"
}
} else if c == 0x5C { // 0x5C is '\\'.
} else if c < 0x20 { // 0x20 is ' '.
if string_length > 0 {
args.dst.write_fast_token!(
value: 0x60_0001,
length: string_length as base.u64)
string_length = 0
if args.dst.available() <= 0 {
continue.string_loop
}
}
assert args.dst.available() > 0
if args.src.available() < 2 {
yield? base."$short read"
string_length = 0
continue.string_loop
}
backslash = (args.src.peek_u16le() >> 8) as base.u8
c = backslashes[backslash]
if c > 0 {
args.src.skip32_fast!(actual: 2, worst_case: 2)
args.dst.write_fast_token!(
value: 0x80_0000 | ((c & 0x7F) as base.u64),
length: 2)
continue.string_loop
} else if backslash == 0x75 { // 0x75 is 'u'.
if args.src.available() < 6 {
yield? base."$short read"
string_length = 0
continue.string_loop
}
uni4_string = (args.src.peek_u48le_as_u64() >> 16) as base.u32
uni4_value = 0
uni4_ok = 0x80
c = hexadecimal_digits[0xFF & (uni4_string >> 0)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 12
c = hexadecimal_digits[0xFF & (uni4_string >> 8)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 8
c = hexadecimal_digits[0xFF & (uni4_string >> 16)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 4
c = hexadecimal_digits[0xFF & (uni4_string >> 24)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 0
if uni4_ok <> 0 {
args.src.skip32_fast!(actual: 6, worst_case: 6)
args.dst.write_fast_token!(
value: 0x80_0000 | (uni4_value as base.u64),
length: 6)
continue.string_loop
}
}
return "#bad input"
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
if string_length >= 0xFFFE {
} else if c == 0x5C { // 0x5C is '\\'.
if string_length > 0 {
args.dst.write_fast_token!(
value: 0x60_0001,
length: 0xFFFF)
length: string_length as base.u64)
string_length = 0
if args.dst.available() <= 0 {
continue.string_loop
}
}
assert args.dst.available() > 0
if args.src.available() < 2 {
yield? base."$short read"
string_length = 0
continue.string_loop
}
string_length += 1
}
} // while.string_loop
backslash = (args.src.peek_u16le() >> 8) as base.u8
c = backslashes[backslash]
if c > 0 {
args.src.skip32_fast!(actual: 2, worst_case: 2)
args.dst.write_fast_token!(
value: 0x80_0000 | ((c & 0x7F) as base.u64),
length: 2)
continue.string_loop
// The trailing '"' is filler.
while true {
if args.src.available() <= 0 {
yield? base."$short read"
continue
}
if args.dst.available() <= 0 {
yield? base."$short write"
continue
} else if backslash == 0x75 { // 0x75 is 'u'.
if args.src.available() < 6 {
yield? base."$short read"
string_length = 0
continue.string_loop
}
uni4_string = (args.src.peek_u48le_as_u64() >> 16) as base.u32
uni4_value = 0
uni4_ok = 0x80
c = hexadecimal_digits[0xFF & (uni4_string >> 0)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 12
c = hexadecimal_digits[0xFF & (uni4_string >> 8)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 8
c = hexadecimal_digits[0xFF & (uni4_string >> 16)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 4
c = hexadecimal_digits[0xFF & (uni4_string >> 24)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 0
if uni4_ok <> 0 {
args.src.skip32_fast!(actual: 6, worst_case: 6)
args.dst.write_fast_token!(
value: 0x80_0000 | (uni4_value as base.u64),
length: 6)
continue.string_loop
}
}
return "#bad input"
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
args.dst.write_fast_token!(
value: 0,
length: 1)
break
if string_length >= 0xFFFE {
args.dst.write_fast_token!(
value: 0x60_0001,
length: 0xFFFF)
string_length = 0
continue.string_loop
}
string_length += 1
}
} // while.string_loop
// As above, expect must have contained
// EXPECT_STRING_VALUE. If it didn't also contain
// EXPECT_NON_STRING_VALUE then we were parsing an object
// key and the next token should be ':'.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
expect = 0x08 // 0x08 is EXPECT_COLON.
continue.outer
// The trailing '"' is filler.
while true {
if args.src.available() <= 0 {
yield? base."$short read"
continue
}
break.goto_parsed_a_leaf_value
// -------- END parse strings.
} else if class == 0x02 { // 0x02 is CLASS_COMMA.
if 0 == (expect & 0x04) { // 0x01 is EXPECT_COMMA.
return "#bad input"
if args.dst.available() <= 0 {
yield? base."$short write"
continue
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
// The ',' is filler.
args.dst.write_fast_token!(
value: 0,
length: 1)
// What's valid after a comma depends on whether or not
// we're in an array or an object.
if 0 == (expect & 0x10) { // 0x10 is EXPECT_CLOSE_BRACKET
expect = 0x02 // 0x02 is EXPECT_STRING_VALUE.
} else {
expect = 0x03 // 0x03 is EXPECT_VALUE.
}
continue.outer
} else if class == 0x04 { // 0x04 is CLASS_NUMBER.
// -------- BEGIN parse numbers.
if 0 == (expect & 0x01) { // 0x02 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
while true,
pre args.dst.available() > 0,
{
number_length = this.decode_number!(src: args.src)
number_status = number_length >> 8
token_value = 0x40_0002
if (number_length & 0x80) <> 0 {
token_value = 0x40_0004
}
number_length = number_length & 0x7F
if number_status == 0 {
args.dst.write_fast_token!(
value: token_value,
length: number_length as base.u64)
break
}
while number_length > 0 {
number_length -= 1
if args.src.can_undo_byte() {
args.src.undo_byte!()
} else {
return "#internal error: inconsistent I/O"
}
}
if number_status == 1 {
return "#bad input"
} else if number_status == 2 {
return "#unsupported number length"
} else {
yield? base."$short read"
while args.dst.available() <= 0,
post args.dst.available() > 0,
{
yield? base."$short write"
}
}
}
break.goto_parsed_a_leaf_value
// -------- END parse numbers.
break
}
// As above, expect must have contained EXPECT_STRING_VALUE. If
// it didn't also contain EXPECT_NON_STRING_VALUE then we were
// parsing an object key and the next token should be ':'.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
expect = 0x08 // 0x08 is EXPECT_COLON.
continue.outer
}
break.goto_parsed_a_leaf_value
// -------- END parse strings.
} else if class == 0x02 { // 0x02 is CLASS_COMMA.
if 0 == (expect & 0x04) { // 0x01 is EXPECT_COMMA.
return "#bad input"
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
// The ',' is filler.
args.dst.write_fast_token!(
value: 0,
length: 1)
// What's valid after a comma depends on whether or not we're
// in an array or an object.
if 0 == (expect & 0x10) { // 0x10 is EXPECT_CLOSE_BRACKET
expect = 0x02 // 0x02 is EXPECT_STRING_VALUE.
} else {
expect = 0x03 // 0x03 is EXPECT_VALUE.
}
continue.outer
} else if class == 0x03 { // 0x03 is CLASS_COLON.
if 0 == (expect & 0x08) { // 0x08 is EXPECT_COLON.
return "#bad input"
@@ -407,6 +357,104 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
expect = 0x03 // 0x03 is EXPECT_VALUE.
continue.outer
} else if class == 0x04 { // 0x04 is CLASS_NUMBER.
// -------- BEGIN parse numbers.
if 0 == (expect & 0x01) { // 0x02 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
while true,
pre args.dst.available() > 0,
{
number_length = this.decode_number!(src: args.src)
number_status = number_length >> 8
token_value = 0x40_0002
if (number_length & 0x80) <> 0 {
token_value = 0x40_0004
}
number_length = number_length & 0x7F
if number_status == 0 {
args.dst.write_fast_token!(
value: token_value,
length: number_length as base.u64)
break
}
while number_length > 0 {
number_length -= 1
if args.src.can_undo_byte() {
args.src.undo_byte!()
} else {
return "#internal error: inconsistent I/O"
}
}
if number_status == 1 {
return "#bad input"
} else if number_status == 2 {
return "#unsupported number length"
} else {
yield? base."$short read"
while args.dst.available() <= 0,
post args.dst.available() > 0,
{
yield? base."$short write"
}
}
}
break.goto_parsed_a_leaf_value
// -------- END parse numbers.
} else if class == 0x05 { // 0x05 is CLASS_OPEN_CURLY_BRACE.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
if depth >= 1024 {
return "#unsupported recursion depth"
}
stack_byte = depth / 32
stack_bit = depth & 31
this.stack[stack_byte] |= (1 as base.u32) << stack_bit
depth += 1
args.src.skip32_fast!(actual: 1, worst_case: 1)
args.dst.write_fast_token!(
value: 0x20_0021,
length: 1)
expect = 0x22 // 0x22 is (EXPECT_CLOSE_BRACE | EXPECT_STRING_VALUE).
expect_after_value = 0x24 // 0x24 is (EXPECT_CLOSE_BRACE | EXPECT_COMMA).
continue.outer
} else if class == 0x06 { // 0x06 is CLASS_CLOSE_CURLY_BRACE.
if 0 == (expect & 0x20) { // 0x20 is EXPECT_CLOSE_BRACE.
return "#bad input"
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
if depth <= 1 {
args.dst.write_fast_token!(
value: 0x20_0022,
length: 1)
break.outer
}
depth -= 1
stack_byte = (depth - 1) / 32
stack_bit = (depth - 1) & 31
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
args.dst.write_fast_token!(
value: 0x20_1022,
length: 1)
// 0x14 is (EXPECT_CLOSE_BRACKET | EXPECT_COMMA).
expect = 0x14
expect_after_value = 0x14
} else {
args.dst.write_fast_token!(
value: 0x20_2022,
length: 1)
// 0x24 is (EXPECT_CLOSE_BRACE | EXPECT_COMMA).
expect = 0x24
expect_after_value = 0x24
}
continue.outer
} else if class == 0x07 { // 0x07 is CLASS_OPEN_SQUARE_BRACKET.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
@@ -458,104 +506,59 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
}
continue.outer
} else if class == 0x05 { // 0x05 is CLASS_OPEN_CURLY_BRACE.
} else if class == 0x09 { // 0x09 is CLASS_FALSE.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
if depth >= 1024 {
return "#unsupported recursion depth"
}
stack_byte = depth / 32
stack_bit = depth & 31
this.stack[stack_byte] |= (1 as base.u32) << stack_bit
depth += 1
args.src.skip32_fast!(actual: 1, worst_case: 1)
args.dst.write_fast_token!(
value: 0x20_0021,
length: 1)
expect = 0x22 // 0x22 is (EXPECT_CLOSE_BRACE | EXPECT_STRING_VALUE).
expect_after_value = 0x24 // 0x24 is (EXPECT_CLOSE_BRACE | EXPECT_COMMA).
continue.outer
} else if class == 0x06 { // 0x06 is CLASS_CLOSE_CURLY_BRACE.
if 0 == (expect & 0x20) { // 0x20 is EXPECT_CLOSE_BRACE.
return "#bad input"
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
if depth <= 1 {
match = args.src.match7(a: 0x6573_6C61_6605) // 5 bytes "false".
if match == 0 {
args.dst.write_fast_token!(
value: 0x20_0022,
length: 1)
break.outer
value: 0x40_0041,
length: 5)
if args.src.available() < 5 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 5, worst_case: 5)
break.goto_parsed_a_leaf_value
} else if match == 1 {
yield? base."$short read"
continue.outer
}
depth -= 1
stack_byte = (depth - 1) / 32
stack_bit = (depth - 1) & 31
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
args.dst.write_fast_token!(
value: 0x20_1022,
length: 1)
// 0x14 is (EXPECT_CLOSE_BRACKET | EXPECT_COMMA).
expect = 0x14
expect_after_value = 0x14
} else {
args.dst.write_fast_token!(
value: 0x20_2022,
length: 1)
// 0x24 is (EXPECT_CLOSE_BRACE | EXPECT_COMMA).
expect = 0x24
expect_after_value = 0x24
}
continue.outer
} else {
} else if class == 0x0A { // 0x0A is CLASS_TRUE.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
match = 2
if class == 0x0B { // 0x0B is CLASS_NULL.
match = args.src.match7(a: 0x6C_6C75_6E04) // 4 bytes "null".
if match == 0 {
args.dst.write_fast_token!(
value: 0x40_0021,
length: 4)
if args.src.available() < 4 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 4, worst_case: 4)
break.goto_parsed_a_leaf_value
match = args.src.match7(a: 0x65_7572_7404) // 4 bytes "true".
if match == 0 {
args.dst.write_fast_token!(
value: 0x40_0081,
length: 4)
if args.src.available() < 4 {
return "#internal error: inconsistent I/O"
}
} else if class == 0x09 { // 0x09 is CLASS_FALSE.
match = args.src.match7(a: 0x6573_6C61_6605) // 5 bytes "false".
if match == 0 {
args.dst.write_fast_token!(
value: 0x40_0041,
length: 5)
if args.src.available() < 5 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 5, worst_case: 5)
break.goto_parsed_a_leaf_value
}
} else if class == 0x0A { // 0x0A is CLASS_TRUE.
match = args.src.match7(a: 0x65_7572_7404) // 4 bytes "true".
if match == 0 {
args.dst.write_fast_token!(
value: 0x40_0081,
length: 4)
if args.src.available() < 4 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 4, worst_case: 4)
break.goto_parsed_a_leaf_value
}
args.src.skip32_fast!(actual: 4, worst_case: 4)
break.goto_parsed_a_leaf_value
} else if match == 1 {
yield? base."$short read"
continue.outer
}
if match == 1 {
} else if class == 0x0B { // 0x0B is CLASS_NULL.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
match = args.src.match7(a: 0x6C_6C75_6E04) // 4 bytes "null".
if match == 0 {
args.dst.write_fast_token!(
value: 0x40_0021,
length: 4)
if args.src.available() < 4 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 4, worst_case: 4)
break.goto_parsed_a_leaf_value
} else if match == 1 {
yield? base."$short read"
continue.outer
}