go-json/decode_string.go

263 lines
5.5 KiB
Go
Raw Normal View History

2020-04-23 19:39:20 +03:00
package json
import (
"unsafe"
)
type stringDecoder struct {
}
func newStringDecoder() *stringDecoder {
return &stringDecoder{}
}
2020-07-30 16:41:53 +03:00
func (d *stringDecoder) decodeStream(s *stream, p uintptr) error {
bytes, err := d.decodeStreamByte(s)
if err != nil {
return err
}
*(*string)(unsafe.Pointer(p)) = *(*string)(unsafe.Pointer(&bytes))
return nil
}
2020-05-23 06:51:09 +03:00
func (d *stringDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) {
2020-05-06 20:37:29 +03:00
bytes, c, err := d.decodeByte(buf, cursor)
2020-04-23 19:39:20 +03:00
if err != nil {
2020-05-06 20:37:29 +03:00
return 0, err
2020-04-23 19:39:20 +03:00
}
2020-05-06 20:37:29 +03:00
cursor = c
2020-04-23 19:39:20 +03:00
*(*string)(unsafe.Pointer(p)) = *(*string)(unsafe.Pointer(&bytes))
2020-05-06 20:37:29 +03:00
return cursor, nil
2020-04-23 19:39:20 +03:00
}
2020-08-20 06:38:50 +03:00
var (
hexToInt = [256]int{
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'A': 10,
'B': 11,
'C': 12,
'D': 13,
'E': 14,
'F': 15,
'a': 10,
'b': 11,
'c': 12,
'd': 13,
'e': 14,
'f': 15,
}
)
func unicodeToRune(code []byte) rune {
sum := 0
for i := 0; i < len(code); i++ {
sum += hexToInt[code[i]] << (uint(len(code)-i-1) * 4)
}
return rune(sum)
}
func decodeEscapeString(s *stream) error {
s.cursor++
RETRY:
switch s.buf[s.cursor] {
case '"':
s.buf[s.cursor] = '"'
case '\\':
s.buf[s.cursor] = '\\'
case '/':
s.buf[s.cursor] = '/'
case 'b':
s.buf[s.cursor] = '\b'
case 'f':
s.buf[s.cursor] = '\f'
case 'n':
s.buf[s.cursor] = '\n'
case 'r':
s.buf[s.cursor] = '\r'
case 't':
s.buf[s.cursor] = '\t'
case 'u':
if s.cursor+5 >= s.length {
if !s.read() {
return errInvalidCharacter(s.char(), "escaped string", s.totalOffset())
}
}
code := unicodeToRune(s.buf[s.cursor+1 : s.cursor+5])
unicode := []byte(string(code))
s.buf = append(append(s.buf[:s.cursor-1], unicode...), s.buf[s.cursor+5:]...)
s.cursor--
return nil
case nul:
if !s.read() {
return errInvalidCharacter(s.char(), "escaped string", s.totalOffset())
}
goto RETRY
default:
return errUnexpectedEndOfJSON("string", s.totalOffset())
}
s.buf = append(s.buf[:s.cursor-1], s.buf[s.cursor:]...)
s.cursor--
return nil
}
2020-07-30 16:41:53 +03:00
func stringBytes(s *stream) ([]byte, error) {
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
start := s.cursor
for {
switch s.char() {
case '\\':
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
case '"':
literal := s.buf[start:s.cursor]
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
s.reset()
return literal, nil
2020-07-31 11:10:03 +03:00
case nul:
if s.read() {
continue
}
2020-07-30 16:41:53 +03:00
goto ERROR
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
}
ERROR:
return nil, errUnexpectedEndOfJSON("string", s.totalOffset())
}
func nullBytes(s *stream) error {
2020-07-31 11:10:03 +03:00
if s.cursor+3 >= s.length {
if !s.read() {
return errInvalidCharacter(s.char(), "null", s.totalOffset())
}
}
s.cursor++
2020-07-30 16:41:53 +03:00
if s.char() != 'u' {
return errInvalidCharacter(s.char(), "null", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
if s.char() != 'l' {
return errInvalidCharacter(s.char(), "null", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
if s.char() != 'l' {
return errInvalidCharacter(s.char(), "null", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
return nil
}
func (d *stringDecoder) decodeStreamByte(s *stream) ([]byte, error) {
for {
switch s.char() {
case ' ', '\n', '\t', '\r':
2020-07-31 11:10:03 +03:00
s.cursor++
continue
2020-07-30 16:41:53 +03:00
case '"':
return stringBytes(s)
case 'n':
if err := nullBytes(s); err != nil {
return nil, err
}
2020-07-31 14:24:39 +03:00
return []byte{}, nil
2020-07-31 11:10:03 +03:00
case nul:
if s.read() {
continue
}
2020-07-30 16:41:53 +03:00
}
2020-07-31 11:10:03 +03:00
break
2020-07-30 16:41:53 +03:00
}
return nil, errNotAtBeginningOfValue(s.totalOffset())
}
2020-05-23 06:51:09 +03:00
func (d *stringDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
2020-05-07 07:51:17 +03:00
for {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
cursor++
case '"':
cursor++
start := cursor
for {
switch buf[cursor] {
case '\\':
cursor++
2020-08-20 06:38:50 +03:00
switch buf[cursor] {
case '"':
buf[cursor] = '"'
buf = append(buf[:cursor-1], buf[cursor:]...)
case '\\':
buf[cursor] = '\\'
buf = append(buf[:cursor-1], buf[cursor:]...)
case '/':
buf[cursor] = '/'
buf = append(buf[:cursor-1], buf[cursor:]...)
case 'b':
buf[cursor] = '\b'
buf = append(buf[:cursor-1], buf[cursor:]...)
case 'f':
buf[cursor] = '\f'
buf = append(buf[:cursor-1], buf[cursor:]...)
case 'n':
buf[cursor] = '\n'
buf = append(buf[:cursor-1], buf[cursor:]...)
case 'r':
buf[cursor] = '\r'
buf = append(buf[:cursor-1], buf[cursor:]...)
case 't':
buf[cursor] = '\t'
buf = append(buf[:cursor-1], buf[cursor:]...)
case 'u':
buflen := int64(len(buf))
if cursor+5 >= buflen {
return nil, 0, errUnexpectedEndOfJSON("escaped string", cursor)
}
code := unicodeToRune(buf[cursor+1 : cursor+5])
unicode := []byte(string(code))
buf = append(append(buf[:cursor-1], unicode...), buf[cursor+5:]...)
default:
return nil, 0, errUnexpectedEndOfJSON("escaped string", cursor)
}
continue
2020-05-07 07:51:17 +03:00
case '"':
literal := buf[start:cursor]
cursor++
return literal, cursor, nil
2020-07-31 12:07:23 +03:00
case nul:
2020-05-23 06:51:09 +03:00
return nil, 0, errUnexpectedEndOfJSON("string", cursor)
2020-05-07 07:51:17 +03:00
}
2020-05-07 07:46:32 +03:00
cursor++
2020-04-26 08:59:45 +03:00
}
2020-05-23 06:51:09 +03:00
return nil, 0, errUnexpectedEndOfJSON("string", cursor)
2020-05-07 07:51:17 +03:00
case 'n':
2020-05-23 06:51:09 +03:00
buflen := int64(len(buf))
2020-05-07 07:51:17 +03:00
if cursor+3 >= buflen {
2020-05-23 06:51:09 +03:00
return nil, 0, errUnexpectedEndOfJSON("null", cursor)
2020-05-07 07:51:17 +03:00
}
if buf[cursor+1] != 'u' {
2020-05-23 06:51:09 +03:00
return nil, 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
2020-05-07 07:51:17 +03:00
}
if buf[cursor+2] != 'l' {
2020-05-23 06:51:09 +03:00
return nil, 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
2020-05-07 07:51:17 +03:00
}
if buf[cursor+3] != 'l' {
2020-05-23 06:51:09 +03:00
return nil, 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
2020-05-07 07:51:17 +03:00
}
2020-08-08 06:21:25 +03:00
cursor += 4
2020-07-31 14:24:39 +03:00
return []byte{}, cursor, nil
2020-05-07 08:21:29 +03:00
default:
goto ERROR
2020-04-23 19:39:20 +03:00
}
}
2020-05-07 08:21:29 +03:00
ERROR:
2020-05-23 06:51:09 +03:00
return nil, 0, errNotAtBeginningOfValue(cursor)
2020-04-23 19:39:20 +03:00
}