1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-12 22:47:42 +02:00

optimize read float

This commit is contained in:
Tao Wen
2016-12-08 10:02:18 +08:00
parent 44a65aa13a
commit ec19f6de6a
2 changed files with 44 additions and 19 deletions

View File

@ -133,6 +133,8 @@ func (iter *Iterator) readByte() (ret byte) {
ret = iter.buf[iter.head]
iter.head++
return ret
} else {
return 0
}
}
ret = iter.buf[iter.head]
@ -557,16 +559,26 @@ func (iter *Iterator) readObjectField() (ret string) {
}
func (iter *Iterator) ReadFloat32() (ret float32) {
str := make([]byte, 0, 4)
for c := iter.readByte(); iter.Error == nil; c = iter.readByte() {
switch c {
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
str = append(str, c)
continue
default:
iter.unreadByte()
strBuf := [8]byte{}
str := strBuf[0:0]
hasMore := true
for(hasMore) {
for i := iter.head; i < iter.tail; i++ {
c := iter.buf[i]
switch c {
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
str = append(str, c)
continue
default:
hasMore = false
break
}
}
if hasMore {
if !iter.loadMore() {
break
}
}
break
}
if iter.Error != nil && iter.Error != io.EOF {
return
@ -580,16 +592,26 @@ func (iter *Iterator) ReadFloat32() (ret float32) {
}
func (iter *Iterator) ReadFloat64() (ret float64) {
str := make([]byte, 0, 4)
for c := iter.readByte(); iter.Error == nil; c = iter.readByte() {
switch c {
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
str = append(str, c)
continue
default:
iter.unreadByte()
strBuf := [8]byte{}
str := strBuf[0:0]
hasMore := true
for(hasMore) {
for i := iter.head; i < iter.tail; i++ {
c := iter.buf[i]
switch c {
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
str = append(str, c)
continue
default:
hasMore = false
break
}
}
if hasMore {
if !iter.loadMore() {
break
}
}
break
}
if iter.Error != nil && iter.Error != io.EOF {
return