1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-15 22:50:24 +02:00

int lazy any

This commit is contained in:
Tao Wen
2017-01-22 23:29:48 +08:00
parent 9df37bbd68
commit ba410b045b
7 changed files with 248 additions and 0 deletions

57
feature_any.go Normal file
View File

@ -0,0 +1,57 @@
package jsoniter
import "fmt"
type Any interface {
LastError() error
ToBool() bool
ToInt() int
ToInt32() int32
ToInt64() int64
ToFloat32() float32
ToFloat64() float64
ToString() string
}
func (iter *Iterator) ReadAny() Any {
valueType := iter.WhatIsNext()
switch valueType {
case Nil:
iter.skipFixedBytes(4)
return &nilAny{}
case Number:
dotFound, lazyBuf := iter.skipNumber()
if dotFound {
return &floatLazyAny{lazyBuf, nil, nil}
} else {
return &intLazyAny{lazyBuf, nil, nil, 0}
}
}
iter.reportError("ReadAny", fmt.Sprintf("unexpected value type: %v", valueType))
return nil
}
func (iter *Iterator) skipNumber() (bool, []byte) {
dotFound := false
var lazyBuf []byte
for {
for i := iter.head; i < iter.tail; i++ {
c := iter.buf[i]
if c == '.' {
dotFound = true
continue
}
switch c {
case ' ', '\n', '\r', '\t', ',', '}', ']':
lazyBuf = append(lazyBuf, iter.buf[iter.head:i]...)
iter.head = i
return dotFound, lazyBuf
}
}
lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
if !iter.loadMore() {
iter.head = iter.tail;
return dotFound, lazyBuf
}
}
}