2020-08-11 12:04:32 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2021-03-11 13:29:35 +03:00
|
|
|
"strconv"
|
2020-08-11 12:04:32 +03:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type numberDecoder struct {
|
2021-03-11 13:29:35 +03:00
|
|
|
stringDecoder *stringDecoder
|
|
|
|
op func(unsafe.Pointer, Number)
|
|
|
|
structName string
|
|
|
|
fieldName string
|
2020-08-11 12:04:32 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func newNumberDecoder(structName, fieldName string, op func(unsafe.Pointer, Number)) *numberDecoder {
|
2020-08-11 12:04:32 +03:00
|
|
|
return &numberDecoder{
|
2021-03-11 13:29:35 +03:00
|
|
|
stringDecoder: newStringDecoder(structName, fieldName),
|
|
|
|
op: op,
|
|
|
|
structName: structName,
|
|
|
|
fieldName: fieldName,
|
2020-08-11 12:04:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:05:06 +03:00
|
|
|
func (d *numberDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
|
2021-03-11 13:29:35 +03:00
|
|
|
bytes, err := d.decodeStreamByte(s)
|
2020-08-11 12:04:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-11 13:29:35 +03:00
|
|
|
if _, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&bytes)), 64); err != nil {
|
2021-03-13 07:31:14 +03:00
|
|
|
return errSyntax(err.Error(), s.totalOffset())
|
2021-03-11 13:29:35 +03:00
|
|
|
}
|
2020-12-05 16:27:33 +03:00
|
|
|
d.op(p, Number(string(bytes)))
|
|
|
|
s.reset()
|
2020-08-11 12:04:32 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:05:06 +03:00
|
|
|
func (d *numberDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
|
2021-03-11 13:29:35 +03:00
|
|
|
bytes, c, err := d.decodeByte(buf, cursor)
|
2020-08-11 12:04:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-03-11 13:29:35 +03:00
|
|
|
if _, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&bytes)), 64); err != nil {
|
2021-03-13 07:31:14 +03:00
|
|
|
return 0, errSyntax(err.Error(), c)
|
2021-03-11 13:29:35 +03:00
|
|
|
}
|
2020-08-11 12:04:32 +03:00
|
|
|
cursor = c
|
|
|
|
s := *(*string)(unsafe.Pointer(&bytes))
|
|
|
|
d.op(p, Number(s))
|
|
|
|
return cursor, nil
|
|
|
|
}
|
2021-03-11 13:29:35 +03:00
|
|
|
|
|
|
|
func (d *numberDecoder) decodeStreamByte(s *stream) ([]byte, error) {
|
|
|
|
for {
|
|
|
|
switch s.char() {
|
|
|
|
case ' ', '\n', '\t', '\r':
|
|
|
|
s.cursor++
|
|
|
|
continue
|
|
|
|
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
return floatBytes(s), nil
|
|
|
|
case 'n':
|
|
|
|
if err := nullBytes(s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
case '"':
|
|
|
|
return d.stringDecoder.decodeStreamByte(s)
|
|
|
|
case nul:
|
|
|
|
if s.read() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
goto ERROR
|
|
|
|
default:
|
|
|
|
goto ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ERROR:
|
|
|
|
return nil, errUnexpectedEndOfJSON("json.Number", s.totalOffset())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *numberDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
|
2021-05-08 21:05:36 +03:00
|
|
|
for {
|
2021-03-11 13:29:35 +03:00
|
|
|
switch buf[cursor] {
|
|
|
|
case ' ', '\n', '\t', '\r':
|
2021-05-08 21:05:36 +03:00
|
|
|
cursor++
|
2021-03-11 13:29:35 +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++
|
2021-03-11 13:29:35 +03:00
|
|
|
}
|
|
|
|
num := buf[start:cursor]
|
|
|
|
return num, cursor, nil
|
|
|
|
case 'n':
|
2021-05-08 21:05:36 +03:00
|
|
|
if err := validateNull(buf, cursor); err != nil {
|
|
|
|
return nil, 0, err
|
2021-03-11 13:29:35 +03:00
|
|
|
}
|
|
|
|
cursor += 4
|
|
|
|
return nil, cursor, nil
|
|
|
|
case '"':
|
|
|
|
return d.stringDecoder.decodeByte(buf, cursor)
|
|
|
|
default:
|
|
|
|
return nil, 0, errUnexpectedEndOfJSON("json.Number", cursor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|