From 3bd7507c309fe8139c77cea0f90054e11fcd53bc Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sun, 26 Apr 2020 14:59:45 +0900 Subject: [PATCH] Fix decoder --- benchmarks/decode_test.go | 2 -- decode_string.go | 15 +++++++++++++++ decode_struct.go | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/benchmarks/decode_test.go b/benchmarks/decode_test.go index f3d2ee7..f35594e 100644 --- a/benchmarks/decode_test.go +++ b/benchmarks/decode_test.go @@ -130,7 +130,6 @@ func Benchmark_Decode_MediumStruct_GoJayUnsafe(b *testing.B) { } } -/* func Benchmark_Decode_MediumStruct_GoJson(b *testing.B) { b.ReportAllocs() 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) { b.ReportAllocs() diff --git a/decode_string.go b/decode_string.go index 44312d4..80b6b8d 100644 --- a/decode_string.go +++ b/decode_string.go @@ -46,6 +46,21 @@ func (d *stringDecoder) decodeByte(ctx *context) ([]byte, error) { } } 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") diff --git a/decode_struct.go b/decode_struct.go index c5b0535..6932b84 100644 --- a/decode_struct.go +++ b/decode_struct.go @@ -38,14 +38,29 @@ func (d *structDecoder) skipValue(ctx *context) error { case '}': braceCount-- if braceCount == -1 && bracketCount == 0 { + ctx.cursor = cursor return nil } case ']': bracketCount-- case ',': if bracketCount == 0 && braceCount == 0 { + ctx.cursor = cursor 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") @@ -91,6 +106,7 @@ func (d *structDecoder) decode(ctx *context, p uintptr) error { } cursor = ctx.skipWhiteSpace() if buf[cursor] == '}' { + ctx.cursor++ return nil } if buf[cursor] != ',' {