Refactor skipValue

This commit is contained in:
Masaaki Goshima 2021-02-15 18:45:22 +09:00
parent 1f75cdd023
commit fa87dde0c3
1 changed files with 89 additions and 58 deletions

View File

@ -1,6 +1,8 @@
package json package json
import "unsafe" import (
"unsafe"
)
var ( var (
isWhiteSpace = [256]bool{} isWhiteSpace = [256]bool{}
@ -26,69 +28,104 @@ LOOP:
return cursor return cursor
} }
func skipValue(buf []byte, cursor int64) (int64, error) { func skipObject(buf []byte, cursor int64) (int64, error) {
cursor = skipWhiteSpace(buf, cursor) braceCount := 1
braceCount := 0
bracketCount := 0
buflen := int64(len(buf))
start := cursor
for { for {
switch buf[cursor] { switch buf[cursor] {
case nul:
if start == cursor {
return cursor, errUnexpectedEndOfJSON("value of object", cursor)
}
if braceCount == 0 && bracketCount == 0 {
return cursor, nil
}
return cursor, errUnexpectedEndOfJSON("value of object", cursor)
case '{': case '{':
braceCount++ braceCount++
case '[':
bracketCount++
case '}': case '}':
braceCount-- braceCount--
if braceCount == -1 && bracketCount == 0 { if braceCount == 0 {
return cursor, nil return cursor + 1, nil
}
case ']':
bracketCount--
if braceCount == 0 && bracketCount == -1 {
return cursor, nil
}
case ',':
if bracketCount == 0 && braceCount == 0 {
return cursor, nil
} }
case '"': case '"':
for {
cursor++ cursor++
switch buf[cursor] {
for ; cursor < buflen; cursor++ { case '"':
if buf[cursor] != '"' {
continue
}
if buf[cursor-1] == '\\' { if buf[cursor-1] == '\\' {
continue continue
} }
if bracketCount == 0 && braceCount == 0 { goto SWITCH_OUT
case nul:
return 0, errUnexpectedEndOfJSON("string of object", cursor)
}
}
case nul:
return 0, errUnexpectedEndOfJSON("object of object", cursor)
}
SWITCH_OUT:
cursor++
}
}
func skipArray(buf []byte, cursor int64) (int64, error) {
bracketCount := 1
for {
switch buf[cursor] {
case '[':
bracketCount++
case ']':
bracketCount--
if bracketCount == 0 {
return cursor + 1, nil return cursor + 1, nil
} }
break case '"':
for {
cursor++
switch buf[cursor] {
case '"':
if buf[cursor-1] == '\\' {
continue
}
goto SWITCH_OUT
case nul:
return 0, errUnexpectedEndOfJSON("string of object", cursor)
}
}
case nul:
return 0, errUnexpectedEndOfJSON("array of object", cursor)
}
SWITCH_OUT:
cursor++
}
}
func skipValue(buf []byte, cursor int64) (int64, error) {
for {
switch buf[cursor] {
case ' ', '\t', '\n', '\r':
cursor++
continue
case '{':
return skipObject(buf, cursor+1)
case '[':
return skipArray(buf, cursor+1)
case '"':
for {
cursor++
switch buf[cursor] {
case '"':
if buf[cursor-1] == '\\' {
continue
}
return cursor + 1, nil
case nul:
return 0, errUnexpectedEndOfJSON("string of object", cursor)
}
} }
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
for {
cursor++ cursor++
for ; cursor < buflen; cursor++ { if floatTable[buf[cursor]] {
tk := int(buf[cursor])
if (int('0') <= tk && tk <= int('9')) || tk == '.' || tk == 'e' || tk == 'E' {
continue continue
} }
break break
} }
if bracketCount == 0 && braceCount == 0 {
return cursor, nil return cursor, nil
}
continue
case 't': case 't':
buflen := int64(len(buf))
if cursor+3 >= buflen { if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("bool of object", cursor) return 0, errUnexpectedEndOfJSON("bool of object", cursor)
} }
@ -102,11 +139,9 @@ func skipValue(buf []byte, cursor int64) (int64, error) {
return 0, errUnexpectedEndOfJSON("bool of object", cursor) return 0, errUnexpectedEndOfJSON("bool of object", cursor)
} }
cursor += 4 cursor += 4
if bracketCount == 0 && braceCount == 0 {
return cursor, nil return cursor, nil
}
continue
case 'f': case 'f':
buflen := int64(len(buf))
if cursor+4 >= buflen { if cursor+4 >= buflen {
return 0, errUnexpectedEndOfJSON("bool of object", cursor) return 0, errUnexpectedEndOfJSON("bool of object", cursor)
} }
@ -123,11 +158,9 @@ func skipValue(buf []byte, cursor int64) (int64, error) {
return 0, errUnexpectedEndOfJSON("bool of object", cursor) return 0, errUnexpectedEndOfJSON("bool of object", cursor)
} }
cursor += 5 cursor += 5
if bracketCount == 0 && braceCount == 0 {
return cursor, nil return cursor, nil
}
continue
case 'n': case 'n':
buflen := int64(len(buf))
if cursor+3 >= buflen { if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("null", cursor) return 0, errUnexpectedEndOfJSON("null", cursor)
} }
@ -141,11 +174,9 @@ func skipValue(buf []byte, cursor int64) (int64, error) {
return 0, errUnexpectedEndOfJSON("null", cursor) return 0, errUnexpectedEndOfJSON("null", cursor)
} }
cursor += 4 cursor += 4
if bracketCount == 0 && braceCount == 0 {
return cursor, nil return cursor, nil
} default:
continue return cursor, errUnexpectedEndOfJSON("null", cursor)
} }
cursor++
} }
} }