mirror of
https://github.com/json-iterator/go.git
synced 2025-05-19 21:53:05 +02:00
simplify read string, and support null
This commit is contained in:
parent
5488fde97f
commit
7e046e6aa7
@ -2,59 +2,37 @@ package jsoniter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"unicode/utf16"
|
"unicode/utf16"
|
||||||
"unsafe"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: avoid append
|
|
||||||
func (iter *Iterator) ReadString() (ret string) {
|
func (iter *Iterator) ReadString() (ret string) {
|
||||||
c := iter.nextToken()
|
c := iter.nextToken()
|
||||||
if c == '"' {
|
if c == '"' {
|
||||||
copied := make([]byte, 32)
|
for i := iter.head ; i < iter.tail; i++ {
|
||||||
j := 0
|
c := iter.buf[i]
|
||||||
fast_loop:
|
if c == '"' {
|
||||||
for {
|
ret = string(iter.buf[iter.head:i])
|
||||||
i := iter.head
|
iter.head = i + 1
|
||||||
for ; i < iter.tail && j < len(copied); i++ {
|
return ret
|
||||||
c := iter.buf[i]
|
} else if c == '\\' {
|
||||||
if c == '"' {
|
break
|
||||||
iter.head = i + 1
|
|
||||||
copied = copied[:j]
|
|
||||||
return *(*string)(unsafe.Pointer(&copied))
|
|
||||||
} else if c == '\\' {
|
|
||||||
iter.head = i
|
|
||||||
break fast_loop
|
|
||||||
}
|
|
||||||
copied[j] = c
|
|
||||||
j++
|
|
||||||
}
|
|
||||||
if i == iter.tail {
|
|
||||||
if iter.loadMore() {
|
|
||||||
i = iter.head
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
iter.reportError("ReadString", "incomplete string")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
iter.head = i
|
|
||||||
if j == len(copied) {
|
|
||||||
newBuf := make([]byte, len(copied) * 2)
|
|
||||||
copy(newBuf, copied)
|
|
||||||
copied = newBuf
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return iter.readStringSlowPath(copied[:j])
|
return iter.readStringSlowPath()
|
||||||
|
} else if c == 'n' {
|
||||||
|
iter.skipFixedBytes(3)
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
iter.reportError("ReadString", `expects " or n`)
|
iter.reportError("ReadString", `expects " or n`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) readStringSlowPath(str []byte) (ret string) {
|
func (iter *Iterator) readStringSlowPath() (ret string) {
|
||||||
|
var str []byte
|
||||||
var c byte
|
var c byte
|
||||||
for iter.Error == nil {
|
for iter.Error == nil {
|
||||||
c = iter.readByte()
|
c = iter.readByte()
|
||||||
if c == '"' {
|
if c == '"' {
|
||||||
return *(*string)(unsafe.Pointer(&str))
|
return string(str)
|
||||||
}
|
}
|
||||||
if c == '\\' {
|
if c == '\\' {
|
||||||
c = iter.readByte()
|
c = iter.readByte()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user