go-json/decode_int.go

151 lines
2.8 KiB
Go
Raw Permalink Normal View History

2020-04-23 19:39:20 +03:00
package json
type intDecoder struct {
op func(uintptr, int64)
}
func newIntDecoder(op func(uintptr, int64)) *intDecoder {
return &intDecoder{op: op}
}
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,
}
)
2020-04-23 19:39:20 +03:00
func (d *intDecoder) parseInt(b []byte) int64 {
isNegative := false
if b[0] == '-' {
b = b[1:]
isNegative = true
}
maxDigit := len(b)
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 {
return -1 * sum
}
return sum
}
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,
}
)
2020-08-11 13:05:20 +03:00
func (d *intDecoder) setDisallowUnknownFields(_ bool) {}
2020-07-30 16:41:53 +03:00
func (d *intDecoder) decodeStreamByte(s *stream) ([]byte, error) {
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]
2020-07-31 06:22:00 +03:00
s.reset()
2020-05-24 15:31:10 +03:00
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
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
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]
2020-07-31 06:22:00 +03:00
s.reset()
2020-05-24 15:31:10 +03:00
return num, 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-07-31 11:10:03 +03:00
goto ERROR
2020-05-24 15:31:10 +03:00
}
}
2020-07-31 11:10:03 +03:00
ERROR:
2020-05-24 15:31:10 +03:00
return nil, errUnexpectedEndOfJSON("number(integer)", s.totalOffset())
}
2020-05-23 06:51:09 +03:00
func (d *intDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
2020-05-07 08:21:29 +03:00
for {
2020-04-24 08:07:33 +03:00
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
2020-05-07 08:21:29 +03:00
cursor++
2020-04-24 08:07:33 +03:00
continue
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := cursor
cursor++
2020-05-07 08:21:29 +03:00
LOOP:
if numTable[buf[cursor]] {
cursor++
goto LOOP
2020-04-23 19:39:20 +03:00
}
2020-05-06 20:37:29 +03:00
num := buf[start:cursor]
return num, cursor, nil
2020-05-07 08:21:29 +03:00
default:
2020-05-23 06:51:09 +03:00
return nil, 0, errInvalidCharacter(buf[cursor], "number(integer)", cursor)
2020-04-23 19:39:20 +03:00
}
}
2020-05-23 06:51:09 +03:00
return nil, 0, errUnexpectedEndOfJSON("number(integer)", cursor)
2020-04-23 19:39:20 +03:00
}
2020-05-24 15:31:10 +03:00
func (d *intDecoder) decodeStream(s *stream, p uintptr) 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
}
d.op(p, d.parseInt(bytes))
return nil
}
2020-05-23 06:51:09 +03:00
func (d *intDecoder) 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
d.op(p, d.parseInt(bytes))
2020-05-06 20:37:29 +03:00
return cursor, nil
2020-04-23 19:39:20 +03:00
}