Fix decoder for int and string type

This commit is contained in:
Masaaki Goshima 2020-05-07 14:21:29 +09:00
parent 936ff186dc
commit 304c9ab4e2
2 changed files with 27 additions and 10 deletions

View File

@ -38,26 +38,42 @@ func (d *intDecoder) parseInt(b []byte) int64 {
return sum return sum
} }
var (
numTable = [256]bool{
'0': true,
'1': true,
'2': true,
'3': true,
'4': true,
'5': true,
'6': true,
'7': true,
'8': true,
'9': true,
}
)
func (d *intDecoder) decodeByte(buf []byte, cursor int) ([]byte, int, error) { func (d *intDecoder) decodeByte(buf []byte, cursor int) ([]byte, int, error) {
buflen := len(buf) for {
for ; cursor < buflen; cursor++ {
switch buf[cursor] { switch buf[cursor] {
case ' ', '\n', '\t', '\r': case ' ', '\n', '\t', '\r':
cursor++
continue continue
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := cursor start := cursor
cursor++ cursor++
for ; cursor < buflen; cursor++ { LOOP:
tk := int(buf[cursor]) if numTable[buf[cursor]] {
if int('0') <= tk && tk <= int('9') { cursor++
continue goto LOOP
}
break
} }
num := buf[start:cursor] num := buf[start:cursor]
return num, cursor, nil return num, cursor, nil
default:
goto ERROR
} }
} }
ERROR:
return nil, 0, errors.New("unexpected error number") return nil, 0, errors.New("unexpected error number")
} }

View File

@ -25,8 +25,6 @@ func (d *stringDecoder) decode(buf []byte, cursor int, p uintptr) (int, error) {
func (d *stringDecoder) decodeByte(buf []byte, cursor int) ([]byte, int, error) { func (d *stringDecoder) decodeByte(buf []byte, cursor int) ([]byte, int, error) {
for { for {
switch buf[cursor] { switch buf[cursor] {
case '\000':
return nil, 0, errors.New("unexpected error key delimiter")
case ' ', '\n', '\t', '\r': case ' ', '\n', '\t', '\r':
cursor++ cursor++
case '"': case '"':
@ -62,7 +60,10 @@ func (d *stringDecoder) decodeByte(buf []byte, cursor int) ([]byte, int, error)
} }
cursor += 5 cursor += 5
return []byte{'n', 'u', 'l', 'l'}, cursor, nil return []byte{'n', 'u', 'l', 'l'}, cursor, nil
default:
goto ERROR
} }
} }
ERROR:
return nil, 0, errors.New("unexpected error key delimiter") return nil, 0, errors.New("unexpected error key delimiter")
} }