go-json/decode_context.go

146 lines
3.4 KiB
Go
Raw Permalink Normal View History

2020-04-23 19:39:20 +03:00
package json
var (
isWhiteSpace = [256]bool{}
)
func init() {
isWhiteSpace[' '] = true
isWhiteSpace['\n'] = true
isWhiteSpace['\t'] = true
isWhiteSpace['\r'] = true
}
2021-02-05 18:54:10 +03:00
func skipWhiteSpace(buf *sliceHeader, cursor int64) int64 {
2020-05-07 07:44:41 +03:00
LOOP:
2021-02-05 18:54:10 +03:00
if isWhiteSpace[char(buf.data, cursor)] {
2020-05-07 07:44:41 +03:00
cursor++
goto LOOP
2020-04-23 19:39:20 +03:00
}
2020-05-07 07:44:41 +03:00
return cursor
2020-04-23 19:39:20 +03:00
}
2020-05-08 14:22:57 +03:00
2021-02-05 18:54:10 +03:00
func skipValue(buf *sliceHeader, cursor int64) (int64, error) {
2020-05-08 14:22:57 +03:00
cursor = skipWhiteSpace(buf, cursor)
braceCount := 0
bracketCount := 0
2021-02-05 18:54:10 +03:00
buflen := int64(buf.len)
2020-11-27 11:41:47 +03:00
start := cursor
2020-05-08 14:22:57 +03:00
for {
2021-02-05 18:54:10 +03:00
switch char(buf.data, cursor) {
2020-11-27 11:41:47 +03:00
case nul:
if start == cursor {
return cursor, errUnexpectedEndOfJSON("value of object", cursor)
}
if braceCount == 0 && bracketCount == 0 {
return cursor, nil
}
2020-05-23 06:51:09 +03:00
return cursor, errUnexpectedEndOfJSON("value of object", cursor)
2020-05-08 14:22:57 +03:00
case '{':
braceCount++
case '[':
bracketCount++
case '}':
braceCount--
if braceCount == -1 && bracketCount == 0 {
return cursor, nil
}
case ']':
bracketCount--
2020-11-27 11:41:47 +03:00
if braceCount == 0 && bracketCount == -1 {
return cursor, nil
}
2020-05-08 14:22:57 +03:00
case ',':
if bracketCount == 0 && braceCount == 0 {
return cursor, nil
}
case '"':
cursor++
for ; cursor < buflen; cursor++ {
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor) != '"' {
2020-05-08 14:22:57 +03:00
continue
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor-1) == '\\' {
2020-05-08 14:22:57 +03:00
continue
}
if bracketCount == 0 && braceCount == 0 {
return cursor + 1, nil
}
break
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
cursor++
for ; cursor < buflen; cursor++ {
2021-02-05 18:54:10 +03:00
tk := int(char(buf.data, cursor))
2020-05-08 14:22:57 +03:00
if (int('0') <= tk && tk <= int('9')) || tk == '.' || tk == 'e' || tk == 'E' {
continue
}
break
}
if bracketCount == 0 && braceCount == 0 {
return cursor, nil
}
continue
2020-07-31 14:24:39 +03:00
case 't':
if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+1) != 'r' {
2020-07-31 14:24:39 +03:00
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+2) != 'u' {
2020-07-31 14:24:39 +03:00
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+3) != 'e' {
2020-07-31 14:24:39 +03:00
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
cursor += 4
if bracketCount == 0 && braceCount == 0 {
return cursor, nil
}
continue
case 'f':
if cursor+4 >= buflen {
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+1) != 'a' {
2020-07-31 14:24:39 +03:00
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+2) != 'l' {
2020-07-31 14:24:39 +03:00
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+3) != 's' {
2020-07-31 14:24:39 +03:00
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+4) != 'e' {
2020-07-31 14:24:39 +03:00
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
cursor += 5
if bracketCount == 0 && braceCount == 0 {
return cursor, nil
}
continue
case 'n':
if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("null", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+1) != 'u' {
2020-07-31 14:24:39 +03:00
return 0, errUnexpectedEndOfJSON("null", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+2) != 'l' {
2020-07-31 14:24:39 +03:00
return 0, errUnexpectedEndOfJSON("null", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+3) != 'l' {
2020-07-31 14:24:39 +03:00
return 0, errUnexpectedEndOfJSON("null", cursor)
}
cursor += 4
if bracketCount == 0 && braceCount == 0 {
return cursor, nil
}
continue
2020-05-08 14:22:57 +03:00
}
cursor++
}
}