forked from mirror/go-json
Merge pull request #220 from goccy/feature/fix-217
Fix calculating of length for stream decoder
This commit is contained in:
commit
72cf940731
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
@ -73,10 +82,14 @@ func (s *stream) readBuf() []byte {
|
|||
copy(s.buf, remainBuf)
|
||||
}
|
||||
remainLen := s.length - s.cursor
|
||||
if remainLen > 0 {
|
||||
remainLen-- // last char is nul
|
||||
remainNotNulCharNum := int64(0)
|
||||
for i := int64(0); i < remainLen; i++ {
|
||||
if s.buf[s.cursor+i] == nul {
|
||||
break
|
||||
}
|
||||
remainNotNulCharNum++
|
||||
}
|
||||
return s.buf[s.cursor+remainLen:]
|
||||
return s.buf[s.cursor+remainNotNulCharNum:]
|
||||
}
|
||||
|
||||
func (s *stream) read() bool {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
@ -211,6 +213,7 @@ func stringBytes(s *stream) ([]byte, error) {
|
|||
s.buf = append(append(append([]byte{}, s.buf[:cursor]...), runeErrBytes...), s.buf[cursor+1:]...)
|
||||
_, _, p = s.stat()
|
||||
cursor += runeErrBytesLen
|
||||
s.length += runeErrBytesLen
|
||||
continue
|
||||
case nul:
|
||||
s.cursor = cursor
|
||||
|
@ -236,6 +239,7 @@ func stringBytes(s *stream) ([]byte, error) {
|
|||
_, _, p = s.stat()
|
||||
}
|
||||
cursor += int64(len(b))
|
||||
s.length += int64(len(b))
|
||||
continue
|
||||
}
|
||||
cursor++
|
||||
|
|
Loading…
Reference in New Issue