go-json/decode_uint.go

202 lines
4.4 KiB
Go
Raw Permalink Normal View History

2020-04-23 19:39:20 +03:00
package json
import (
"fmt"
"reflect"
"unsafe"
)
2020-11-19 06:47:42 +03:00
2020-04-23 19:39:20 +03:00
type uintDecoder struct {
typ *rtype
kind reflect.Kind
op func(unsafe.Pointer, uint64)
structName string
fieldName string
2020-04-23 19:39:20 +03:00
}
func newUintDecoder(typ *rtype, structName, fieldName string, op func(unsafe.Pointer, uint64)) *uintDecoder {
return &uintDecoder{
typ: typ,
kind: typ.Kind(),
op: op,
structName: structName,
fieldName: fieldName,
}
}
func (d *uintDecoder) typeError(buf []byte, offset int64) *UnmarshalTypeError {
return &UnmarshalTypeError{
Value: fmt.Sprintf("number %s", string(buf)),
Type: rtype2type(d.typ),
Offset: offset,
}
2020-04-23 19:39:20 +03:00
}
2021-04-11 09:18:08 +03:00
var (
pow10u64 = [...]uint64{
1e00, 1e01, 1e02, 1e03, 1e04, 1e05, 1e06, 1e07, 1e08, 1e09,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
}
pow10u64Len = len(pow10u64)
)
2020-04-24 08:07:33 +03:00
2021-04-11 09:18:08 +03:00
func (d *uintDecoder) parseUint(b []byte) (uint64, error) {
2020-04-23 19:39:20 +03:00
maxDigit := len(b)
2021-04-11 09:18:08 +03:00
if maxDigit > pow10u64Len {
return 0, fmt.Errorf("invalid length of number")
}
2020-04-23 19:39:20 +03:00
sum := uint64(0)
for i := 0; i < maxDigit; i++ {
c := uint64(b[i]) - 48
2020-04-24 08:07:33 +03:00
digitValue := pow10u64[maxDigit-i-1]
2020-04-23 19:39:20 +03:00
sum += c * digitValue
}
2021-04-11 09:18:08 +03:00
return sum, nil
2020-04-23 19:39:20 +03:00
}
2020-07-30 16:41:53 +03:00
func (d *uintDecoder) 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
2021-04-11 09:18:08 +03:00
case '0':
s.cursor++
return []byte{'0'}, nil
case '1', '2', '3', '4', '5', '6', '7', '8', '9':
2020-07-30 16:41:53 +03:00
start := s.cursor
2020-07-31 11:10:03 +03:00
for {
s.cursor++
if numTable[s.char()] {
2020-07-30 16:41:53 +03:00
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
}
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
}
2021-02-01 16:31:39 +03:00
default:
return nil, d.typeError([]byte{s.char()}, s.totalOffset())
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, errUnexpectedEndOfJSON("number(unsigned integer)", s.totalOffset())
}
2020-05-23 06:51:09 +03:00
func (d *uintDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
buflen := int64(len(buf))
2020-04-24 08:07:33 +03:00
for ; cursor < buflen; cursor++ {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
2021-04-11 09:18:08 +03:00
case '0':
cursor++
return []byte{'0'}, cursor, nil
case '1', '2', '3', '4', '5', '6', '7', '8', '9':
2020-04-24 08:07:33 +03:00
start := cursor
cursor++
for ; cursor < buflen; cursor++ {
tk := int(buf[cursor])
if int('0') <= tk && tk <= int('9') {
continue
}
break
2020-04-23 19:39:20 +03:00
}
2020-04-24 08:07:33 +03:00
num := buf[start:cursor]
2020-05-06 20:37:29 +03:00
return num, cursor, nil
2021-02-16 19:51:42 +03:00
case 'n':
if cursor+3 >= buflen {
return nil, 0, errUnexpectedEndOfJSON("null", cursor)
}
if buf[cursor+1] != 'u' {
return nil, 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
}
if buf[cursor+2] != 'l' {
return nil, 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
}
if buf[cursor+3] != 'l' {
return nil, 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
}
cursor += 4
return nil, cursor, nil
2020-05-23 06:51:09 +03:00
default:
return nil, 0, d.typeError([]byte{buf[cursor]}, cursor)
2020-04-23 19:39:20 +03:00
}
}
2020-05-23 06:51:09 +03:00
return nil, 0, errUnexpectedEndOfJSON("number(unsigned integer)", cursor)
2020-04-23 19:39:20 +03:00
}
func (d *uintDecoder) 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
}
2021-04-11 09:18:08 +03:00
u64, err := d.parseUint(bytes)
if err != nil {
return d.typeError(bytes, s.totalOffset())
}
switch d.kind {
case reflect.Uint8:
if (1 << 8) <= u64 {
return d.typeError(bytes, s.totalOffset())
}
case reflect.Uint16:
if (1 << 16) <= u64 {
return d.typeError(bytes, s.totalOffset())
}
case reflect.Uint32:
if (1 << 32) <= u64 {
return d.typeError(bytes, s.totalOffset())
}
}
d.op(p, u64)
2020-07-30 16:41:53 +03:00
return nil
}
func (d *uintDecoder) 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-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
u64, err := d.parseUint(bytes)
if err != nil {
return 0, d.typeError(bytes, cursor)
}
switch d.kind {
case reflect.Uint8:
if (1 << 8) <= u64 {
return 0, d.typeError(bytes, cursor)
}
case reflect.Uint16:
if (1 << 16) <= u64 {
return 0, d.typeError(bytes, cursor)
}
case reflect.Uint32:
if (1 << 32) <= u64 {
return 0, d.typeError(bytes, cursor)
}
}
d.op(p, u64)
2020-05-06 20:37:29 +03:00
return cursor, nil
2020-04-23 19:39:20 +03:00
}