mirror of
https://github.com/json-iterator/go.git
synced 2025-04-26 11:42:56 +02:00
#2 fix reuse buffer
This commit is contained in:
parent
a460807cb4
commit
76bf0defae
55
jsoniter.go
55
jsoniter.go
@ -89,6 +89,19 @@ func (iter *Iterator) skipWhitespaces() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (iter *Iterator) skipWhitespacesWithoutLoadMore() bool {
|
||||||
|
for i := iter.head; i < iter.tail; i++ {
|
||||||
|
c := iter.buf[i]
|
||||||
|
switch c {
|
||||||
|
case ' ', '\n', '\t', '\r':
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
iter.head = i
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (iter *Iterator) nextToken() byte {
|
func (iter *Iterator) nextToken() byte {
|
||||||
// a variation of skip whitespaces, returning the next non-whitespace token
|
// a variation of skip whitespaces, returning the next non-whitespace token
|
||||||
for {
|
for {
|
||||||
@ -124,7 +137,7 @@ func (iter *Iterator) CurrentBuffer() string {
|
|||||||
if peekStart < 0 {
|
if peekStart < 0 {
|
||||||
peekStart = 0
|
peekStart = 0
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("parsing %v ...%s... at %s", iter.head,
|
return fmt.Sprintf("parsing %v ...|%s|... at %s", iter.head,
|
||||||
string(iter.buf[peekStart: iter.head]), string(iter.buf[0:iter.tail]))
|
string(iter.buf[peekStart: iter.head]), string(iter.buf[0:iter.tail]))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -549,33 +562,31 @@ func (iter *Iterator) ReadObject() (ret string) {
|
|||||||
|
|
||||||
func (iter *Iterator) readObjectField() (ret string) {
|
func (iter *Iterator) readObjectField() (ret string) {
|
||||||
str := iter.ReadStringAsBytes()
|
str := iter.ReadStringAsBytes()
|
||||||
field := *(*string)(unsafe.Pointer(&str))
|
if iter.skipWhitespacesWithoutLoadMore() {
|
||||||
if iter.nextToken() != ':' {
|
if ret == "" {
|
||||||
iter.ReportError("ReadObject", "expect : after object field")
|
ret = string(str);
|
||||||
return
|
|
||||||
}
|
}
|
||||||
// skip whitespaces, and detect if buffer is rotated
|
|
||||||
for {
|
|
||||||
for i := iter.head; i < iter.tail; i++ {
|
|
||||||
c := iter.buf[i]
|
|
||||||
switch c {
|
|
||||||
case ' ', '\n', '\t', '\r':
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
iter.head = i
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if iter.head == iter.tail {
|
|
||||||
// no longer safe to reuse buffer
|
|
||||||
field = string(str)
|
|
||||||
if !iter.loadMore() {
|
if !iter.loadMore() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
break
|
if iter.buf[iter.head] != ':' {
|
||||||
|
iter.ReportError("ReadObject", "expect : after object field")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
iter.head++
|
||||||
|
if iter.skipWhitespacesWithoutLoadMore() {
|
||||||
|
if ret == "" {
|
||||||
|
ret = string(str);
|
||||||
|
}
|
||||||
|
if !iter.loadMore() {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return field
|
if ret == "" {
|
||||||
|
return *(*string)(unsafe.Pointer(&str))
|
||||||
|
}
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) ReadFloat32() (ret float32) {
|
func (iter *Iterator) ReadFloat32() (ret float32) {
|
||||||
|
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user