Have std/json decode null, false and true

This commit is contained in:
Nigel Tao committed 2020-02-21 11:16:27 +11:00
1 parent 9a5b635778
commit c92c0f50e4
7 files changed
+194 -12

No files matched your search

+43 -2
View File
@@ -61,6 +61,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
var depth : base.u32[..= 1024]
var stack_byte : base.u32[..= (1024 / 32) - 1]
var stack_bit : base.u32[..= 31]
var match : base.u32[..= 2]
var c : base.u8
// expect is a bitmask of what the next token can be:
@@ -391,8 +392,48 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
continue.outer
} else {
// TODO: literals (false, true, null).
return "#bad input"
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
match = 2
if c == 0x6E { // 0x6E is 'n'.
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)
}
} else if c == 0x66 { // 0x66 is 'f'.
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)
}
} else if c == 0x74 { // 0x74 is 't'.
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)
}
}
if match == 1 {
yield? base."$short read"
continue.outer
} else if match == 2 {
return "#bad input"
}
}
// We've just parsed a value.