From 23081eadad30d6704f8ff278359501b6dcc4347c Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 7 May 2021 23:15:16 +0900 Subject: [PATCH] Fix calculating of length for stream decoder --- decode_map.go | 12 +++--------- decode_stream.go | 9 +++++++++ decode_string.go | 4 +++- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/decode_map.go b/decode_map.go index c09e2a2..a3fa291 100644 --- a/decode_map.go +++ b/decode_map.go @@ -68,10 +68,7 @@ func (d *mapDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) erro return err } s.skipWhiteSpace() - if s.char() == nul { - s.read() - } - if s.char() != ':' { + if !s.equalChar(':') { return errExpected("colon after object key", s.totalOffset()) } s.cursor++ @@ -81,15 +78,12 @@ func (d *mapDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) erro } mapassign(d.mapType, mapValue, k, v) s.skipWhiteSpace() - if s.char() == nul { - s.read() - } - if s.char() == '}' { + if s.equalChar('}') { **(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue s.cursor++ return nil } - if s.char() != ',' { + if !s.equalChar(',') { return errExpected("comma after object value", s.totalOffset()) } } diff --git a/decode_stream.go b/decode_stream.go index 0d3e263..ba874e5 100644 --- a/decode_stream.go +++ b/decode_stream.go @@ -49,6 +49,15 @@ func (s *stream) char() byte { return s.buf[s.cursor] } +func (s *stream) equalChar(c byte) bool { + cur := s.buf[s.cursor] + if cur == nul { + s.read() + cur = s.buf[s.cursor] + } + return cur == c +} + func (s *stream) stat() ([]byte, int64, unsafe.Pointer) { return s.buf, s.cursor, (*sliceHeader)(unsafe.Pointer(&s.buf)).data } diff --git a/decode_string.go b/decode_string.go index 6b97c52..fc9aae2 100644 --- a/decode_string.go +++ b/decode_string.go @@ -127,7 +127,8 @@ func decodeUnicode(s *stream) error { unicode := []byte(string(r)) unicodeLen := int64(len(unicode)) s.buf = append(append(s.buf[:s.cursor-1], unicode...), s.buf[s.cursor+offset:]...) - s.length = int64(len(s.buf)) + unicodeOrgLen := offset - 1 + s.length = s.length - (backSlashAndULen + (unicodeOrgLen - unicodeLen)) s.cursor = s.cursor - backSlashAndULen + unicodeLen return nil } @@ -163,6 +164,7 @@ RETRY: return errUnexpectedEndOfJSON("string", s.totalOffset()) } s.buf = append(s.buf[:s.cursor-1], s.buf[s.cursor:]...) + s.length-- s.cursor-- return nil }