Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[update] read string memory allocation optimization #650

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion iter_str.go
Expand Up @@ -3,16 +3,22 @@ package jsoniter
import (
"fmt"
"unicode/utf16"
"unsafe"
)

// bytesToString convert []byte to string
func bytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

// ReadString read string from iterator
func (iter *Iterator) ReadString() (ret string) {
c := iter.nextToken()
if c == '"' {
for i := iter.head; i < iter.tail; i++ {
c := iter.buf[i]
if c == '"' {
ret = string(iter.buf[iter.head:i])
ret = bytesToString(iter.buf[iter.head:i])
iter.head = i + 1
return ret
} else if c == '\\' {
Expand Down