Fix decoder

This commit is contained in:
Masaaki Goshima 2020-04-26 14:59:45 +09:00
parent 2c698a30a8
commit 3bd7507c30
3 changed files with 31 additions and 2 deletions

View File

@ -130,7 +130,6 @@ func Benchmark_Decode_MediumStruct_GoJayUnsafe(b *testing.B) {
} }
} }
/*
func Benchmark_Decode_MediumStruct_GoJson(b *testing.B) { func Benchmark_Decode_MediumStruct_GoJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -150,7 +149,6 @@ func Benchmark_Decode_MediumStruct_GoJsonNoEscape(b *testing.B) {
} }
} }
} }
*/
func Benchmark_Decode_LargeStruct_EncodingJson(b *testing.B) { func Benchmark_Decode_LargeStruct_EncodingJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()

View File

@ -46,6 +46,21 @@ func (d *stringDecoder) decodeByte(ctx *context) ([]byte, error) {
} }
} }
return nil, errors.New("unexpected error string") return nil, errors.New("unexpected error string")
case 'n':
if cursor+3 >= ctx.buflen {
return nil, errors.New("unexpected error. invalid bool character")
}
if buf[cursor+1] != 'u' {
return nil, errors.New("unexpected error. invalid bool character")
}
if buf[cursor+2] != 'l' {
return nil, errors.New("unexpected error. invalid bool character")
}
if buf[cursor+3] != 'l' {
return nil, errors.New("unexpected error. invalid bool character")
}
ctx.cursor += 5
return []byte{}, nil
} }
} }
return nil, errors.New("unexpected error key delimiter") return nil, errors.New("unexpected error key delimiter")

View File

@ -38,14 +38,29 @@ func (d *structDecoder) skipValue(ctx *context) error {
case '}': case '}':
braceCount-- braceCount--
if braceCount == -1 && bracketCount == 0 { if braceCount == -1 && bracketCount == 0 {
ctx.cursor = cursor
return nil return nil
} }
case ']': case ']':
bracketCount-- bracketCount--
case ',': case ',':
if bracketCount == 0 && braceCount == 0 { if bracketCount == 0 && braceCount == 0 {
ctx.cursor = cursor
return nil return nil
} }
case '"':
cursor++
for ; cursor < buflen; cursor++ {
tk := buf[cursor]
if tk == '\\' {
cursor++
continue
}
if tk == '"' {
cursor++
break
}
}
} }
} }
return errors.New("unexpected error value") return errors.New("unexpected error value")
@ -91,6 +106,7 @@ func (d *structDecoder) decode(ctx *context, p uintptr) error {
} }
cursor = ctx.skipWhiteSpace() cursor = ctx.skipWhiteSpace()
if buf[cursor] == '}' { if buf[cursor] == '}' {
ctx.cursor++
return nil return nil
} }
if buf[cursor] != ',' { if buf[cursor] != ',' {