go-json/internal/decoder/int.go

243 lines
4.8 KiB
Go
Raw Normal View History

2021-06-03 12:49:01 +03:00
package decoder
2020-04-23 19:39:20 +03:00
2020-12-05 16:27:33 +03:00
import (
2020-12-15 06:29:19 +03:00
"fmt"
"reflect"
2020-12-05 16:27:33 +03:00
"unsafe"
2021-06-03 12:49:01 +03:00
"github.com/goccy/go-json/internal/errors"
"github.com/goccy/go-json/internal/runtime"
2020-12-05 16:27:33 +03:00
)
2020-11-19 06:47:42 +03:00
2020-04-23 19:39:20 +03:00
type intDecoder struct {
2021-06-03 12:49:01 +03:00
typ *runtime.Type
kind reflect.Kind
op func(unsafe.Pointer, int64)
structName string
fieldName string
2020-04-23 19:39:20 +03:00
}
2021-06-03 12:49:01 +03:00
func newIntDecoder(typ *runtime.Type, structName, fieldName string, op func(unsafe.Pointer, int64)) *intDecoder {
2020-12-15 06:29:19 +03:00
return &intDecoder{
typ: typ,
kind: typ.Kind(),
2020-12-15 06:29:19 +03:00
op: op,
structName: structName,
fieldName: fieldName,
}
}
2021-06-03 12:49:01 +03:00
func (d *intDecoder) typeError(buf []byte, offset int64) *errors.UnmarshalTypeError {
return &errors.UnmarshalTypeError{
2020-12-15 06:29:19 +03:00
Value: fmt.Sprintf("number %s", string(buf)),
2021-06-03 12:49:01 +03:00
Type: runtime.RType2Type(d.typ),
2020-12-24 11:47:53 +03:00
Struct: d.structName,
Field: d.fieldName,
2020-12-15 06:29:19 +03:00
Offset: offset,
}
}
2020-04-24 08:07:33 +03:00
var (
pow10i64 = [...]int64{
1e00, 1e01, 1e02, 1e03, 1e04, 1e05, 1e06, 1e07, 1e08, 1e09,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18,
}
2021-04-11 09:18:08 +03:00
pow10i64Len = len(pow10i64)
2020-04-24 08:07:33 +03:00
)
2021-04-11 09:18:08 +03:00
func (d *intDecoder) parseInt(b []byte) (int64, error) {
2020-04-23 19:39:20 +03:00
isNegative := false
if b[0] == '-' {
b = b[1:]
isNegative = true
}
maxDigit := len(b)
2021-04-11 09:18:08 +03:00
if maxDigit > pow10i64Len {
return 0, fmt.Errorf("invalid length of number")
}
2020-04-23 19:39:20 +03:00
sum := int64(0)
for i := 0; i < maxDigit; i++ {
c := int64(b[i]) - 48
2020-04-24 08:07:33 +03:00
digitValue := pow10i64[maxDigit-i-1]
2020-04-23 19:39:20 +03:00
sum += c * digitValue
}
if isNegative {
2021-04-11 09:18:08 +03:00
return -1 * sum, nil
2020-04-23 19:39:20 +03:00
}
2021-04-11 09:18:08 +03:00
return sum, nil
2020-04-23 19:39:20 +03:00
}
2020-05-07 08:21:29 +03:00
var (
numTable = [256]bool{
'0': true,
'1': true,
'2': true,
'3': true,
'4': true,
'5': true,
'6': true,
'7': true,
'8': true,
'9': true,
}
)
var (
numZeroBuf = []byte{'0'}
)
2021-06-03 12:49:01 +03:00
func (d *intDecoder) decodeStreamByte(s *Stream) ([]byte, error) {
2020-07-30 16:41:53 +03:00
for {
2020-05-24 15:31:10 +03:00
switch s.char() {
case ' ', '\n', '\t', '\r':
2020-07-31 11:10:03 +03:00
s.cursor++
2020-05-24 15:31:10 +03:00
continue
case '-':
start := s.cursor
2020-07-31 11:10:03 +03:00
for {
s.cursor++
2020-05-24 15:31:10 +03:00
if numTable[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-05-24 15:31:10 +03:00
}
break
}
num := s.buf[start:s.cursor]
if len(num) < 2 {
2020-07-31 11:10:03 +03:00
goto ERROR
2020-05-24 15:31:10 +03:00
}
return num, nil
2021-04-11 09:18:08 +03:00
case '0':
s.cursor++
return numZeroBuf, nil
2021-04-11 09:18:08 +03:00
case '1', '2', '3', '4', '5', '6', '7', '8', '9':
2020-05-24 15:31:10 +03:00
start := s.cursor
2020-07-31 11:10:03 +03:00
for {
s.cursor++
2020-05-24 15:31:10 +03:00
if numTable[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-05-24 15:31:10 +03:00
}
break
}
num := s.buf[start:s.cursor]
return num, 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-05-24 15:31:10 +03:00
default:
2020-12-15 06:29:19 +03:00
return nil, d.typeError([]byte{s.char()}, s.totalOffset())
2020-05-24 15:31:10 +03:00
}
}
2020-07-31 11:10:03 +03:00
ERROR:
2021-06-03 12:49:01 +03:00
return nil, errors.ErrUnexpectedEndOfJSON("number(integer)", s.totalOffset())
2020-05-24 15:31:10 +03:00
}
2020-05-23 06:51:09 +03:00
func (d *intDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
b := (*sliceHeader)(unsafe.Pointer(&buf)).data
2020-05-07 08:21:29 +03:00
for {
switch char(b, cursor) {
2020-04-24 08:07:33 +03:00
case ' ', '\n', '\t', '\r':
2020-05-07 08:21:29 +03:00
cursor++
2020-04-24 08:07:33 +03:00
continue
2021-04-11 09:18:08 +03:00
case '0':
cursor++
return numZeroBuf, cursor, nil
2021-04-11 09:18:08 +03:00
case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9':
2020-04-24 08:07:33 +03:00
start := cursor
cursor++
2021-05-08 21:05:36 +03:00
for numTable[char(b, cursor)] {
2020-05-07 08:21:29 +03:00
cursor++
2020-04-23 19:39:20 +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-05-07 08:21:29 +03:00
default:
return nil, 0, d.typeError([]byte{char(b, cursor)}, cursor)
2020-04-23 19:39:20 +03:00
}
}
}
2021-06-03 12:49:01 +03:00
func (d *intDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
2020-07-30 16:41:53 +03:00
bytes, err := d.decodeStreamByte(s)
2020-05-24 15:31:10 +03:00
if err != nil {
return err
}
2021-02-16 19:51:42 +03:00
if bytes == nil {
return nil
}
2021-04-11 09:18:08 +03:00
i64, err := d.parseInt(bytes)
if err != nil {
return d.typeError(bytes, s.totalOffset())
}
switch d.kind {
case reflect.Int8:
if i64 <= -1*(1<<7) || (1<<7) <= i64 {
return d.typeError(bytes, s.totalOffset())
}
case reflect.Int16:
if i64 <= -1*(1<<15) || (1<<15) <= i64 {
return d.typeError(bytes, s.totalOffset())
}
case reflect.Int32:
if i64 <= -1*(1<<31) || (1<<31) <= i64 {
return d.typeError(bytes, s.totalOffset())
}
}
d.op(p, i64)
2020-12-05 16:27:33 +03:00
s.reset()
2020-05-24 15:31:10 +03:00
return nil
}
2021-06-04 19:08:27 +03:00
func (d *intDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.decodeByte(ctx.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
}
2021-02-16 19:51:42 +03:00
if bytes == nil {
return c, nil
}
2020-05-06 20:37:29 +03:00
cursor = c
2021-04-11 09:18:08 +03:00
i64, err := d.parseInt(bytes)
if err != nil {
return 0, d.typeError(bytes, cursor)
}
switch d.kind {
case reflect.Int8:
if i64 <= -1*(1<<7) || (1<<7) <= i64 {
return 0, d.typeError(bytes, cursor)
}
case reflect.Int16:
if i64 <= -1*(1<<15) || (1<<15) <= i64 {
return 0, d.typeError(bytes, cursor)
}
case reflect.Int32:
if i64 <= -1*(1<<31) || (1<<31) <= i64 {
return 0, d.typeError(bytes, cursor)
}
}
d.op(p, i64)
2020-05-06 20:37:29 +03:00
return cursor, nil
2020-04-23 19:39:20 +03:00
}