2020-04-24 10:46:12 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type floatDecoder struct {
|
2020-11-23 11:16:31 +03:00
|
|
|
op func(unsafe.Pointer, float64)
|
|
|
|
structName string
|
|
|
|
fieldName string
|
2020-04-24 10:46:12 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func newFloatDecoder(structName, fieldName string, op func(unsafe.Pointer, float64)) *floatDecoder {
|
|
|
|
return &floatDecoder{op: op, structName: structName, fieldName: fieldName}
|
2020-04-24 10:46:12 +03:00
|
|
|
}
|
|
|
|
|
2020-08-24 18:56:01 +03:00
|
|
|
var (
|
|
|
|
floatTable = [256]bool{
|
|
|
|
'0': true,
|
|
|
|
'1': true,
|
|
|
|
'2': true,
|
|
|
|
'3': true,
|
|
|
|
'4': true,
|
|
|
|
'5': true,
|
|
|
|
'6': true,
|
|
|
|
'7': true,
|
|
|
|
'8': true,
|
|
|
|
'9': true,
|
|
|
|
'.': true,
|
|
|
|
'e': true,
|
|
|
|
'E': true,
|
|
|
|
'+': true,
|
|
|
|
'-': true,
|
|
|
|
}
|
|
|
|
|
|
|
|
validEndNumberChar = [256]bool{
|
|
|
|
nul: true,
|
|
|
|
' ': true,
|
|
|
|
'\t': true,
|
|
|
|
'\r': true,
|
|
|
|
'\n': true,
|
|
|
|
',': true,
|
|
|
|
':': true,
|
|
|
|
'}': true,
|
|
|
|
']': true,
|
|
|
|
}
|
|
|
|
)
|
2020-07-30 16:41:53 +03:00
|
|
|
|
|
|
|
func floatBytes(s *stream) []byte {
|
|
|
|
start := s.cursor
|
2020-07-31 11:10:03 +03:00
|
|
|
for {
|
|
|
|
s.cursor++
|
2020-07-30 16:41:53 +03:00
|
|
|
if floatTable[s.char()] {
|
|
|
|
continue
|
2020-07-31 11:10:03 +03:00
|
|
|
} else if s.char() == nul {
|
|
|
|
if s.read() {
|
2020-07-31 12:07:11 +03:00
|
|
|
s.cursor-- // for retry current character
|
2020-07-31 11:10:03 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-07-30 16:41:53 +03:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return s.buf[start:s.cursor]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *floatDecoder) decodeStreamByte(s *stream) ([]byte, error) {
|
|
|
|
for {
|
|
|
|
switch s.char() {
|
|
|
|
case ' ', '\n', '\t', '\r':
|
2020-07-31 11:10:03 +03:00
|
|
|
s.cursor++
|
2020-07-30 16:41:53 +03:00
|
|
|
continue
|
|
|
|
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
return floatBytes(s), nil
|
2021-02-16 19:51:42 +03:00
|
|
|
case 'n':
|
|
|
|
if err := nullBytes(s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
2020-07-31 11:10:03 +03:00
|
|
|
case nul:
|
|
|
|
if s.read() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
goto ERROR
|
2020-07-30 16:41:53 +03:00
|
|
|
default:
|
2020-07-31 11:10:03 +03:00
|
|
|
goto ERROR
|
2020-07-30 16:41:53 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-31 11:10:03 +03:00
|
|
|
ERROR:
|
|
|
|
return nil, errUnexpectedEndOfJSON("float", s.totalOffset())
|
2020-07-30 16:41:53 +03:00
|
|
|
}
|
|
|
|
|
2020-05-23 06:51:09 +03:00
|
|
|
func (d *floatDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
|
2021-05-08 21:05:36 +03:00
|
|
|
for {
|
2020-04-24 10:46:12 +03:00
|
|
|
switch buf[cursor] {
|
|
|
|
case ' ', '\n', '\t', '\r':
|
2021-05-08 21:05:36 +03:00
|
|
|
cursor++
|
2020-04-24 10:46:12 +03:00
|
|
|
continue
|
|
|
|
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
start := cursor
|
|
|
|
cursor++
|
2021-05-08 21:05:36 +03:00
|
|
|
for floatTable[buf[cursor]] {
|
|
|
|
cursor++
|
2020-04-24 10:46:12 +03:00
|
|
|
}
|
2020-05-06 20:37:29 +03:00
|
|
|
num := buf[start:cursor]
|
|
|
|
return num, cursor, nil
|
2021-02-16 19:51:42 +03:00
|
|
|
case 'n':
|
2021-05-08 21:05:36 +03:00
|
|
|
if err := validateNull(buf, cursor); err != nil {
|
|
|
|
return nil, 0, err
|
2021-02-16 19:51:42 +03:00
|
|
|
}
|
|
|
|
cursor += 4
|
|
|
|
return nil, cursor, nil
|
2020-07-30 16:41:53 +03:00
|
|
|
default:
|
|
|
|
return nil, 0, errUnexpectedEndOfJSON("float", cursor)
|
2020-04-24 10:46:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:05:06 +03:00
|
|
|
func (d *floatDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
|
2020-07-30 16:41:53 +03:00
|
|
|
bytes, err := d.decodeStreamByte(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-16 19:51:42 +03:00
|
|
|
if bytes == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-30 16:41:53 +03:00
|
|
|
str := *(*string)(unsafe.Pointer(&bytes))
|
|
|
|
f64, err := strconv.ParseFloat(str, 64)
|
|
|
|
if err != nil {
|
2021-03-13 07:31:14 +03:00
|
|
|
return errSyntax(err.Error(), s.totalOffset())
|
2020-07-30 16:41:53 +03:00
|
|
|
}
|
|
|
|
d.op(p, f64)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:05:06 +03:00
|
|
|
func (d *floatDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
|
2020-05-06 20:37:29 +03:00
|
|
|
bytes, c, err := d.decodeByte(buf, cursor)
|
2020-04-24 10:46:12 +03:00
|
|
|
if err != nil {
|
2020-05-06 20:37:29 +03:00
|
|
|
return 0, err
|
2020-04-24 10:46:12 +03:00
|
|
|
}
|
2021-02-16 19:51:42 +03:00
|
|
|
if bytes == nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
2020-05-06 20:37:29 +03:00
|
|
|
cursor = c
|
2020-08-24 18:56:01 +03:00
|
|
|
if !validEndNumberChar[buf[cursor]] {
|
|
|
|
return 0, errUnexpectedEndOfJSON("float", cursor)
|
|
|
|
}
|
2020-04-24 10:46:12 +03:00
|
|
|
s := *(*string)(unsafe.Pointer(&bytes))
|
|
|
|
f64, err := strconv.ParseFloat(s, 64)
|
|
|
|
if err != nil {
|
2021-03-13 07:31:14 +03:00
|
|
|
return 0, errSyntax(err.Error(), cursor)
|
2020-04-24 10:46:12 +03:00
|
|
|
}
|
|
|
|
d.op(p, f64)
|
2020-05-06 20:37:29 +03:00
|
|
|
return cursor, nil
|
2020-04-24 10:46:12 +03:00
|
|
|
}
|