mirror of
https://github.com/json-iterator/go.git
synced 2025-05-19 21:53:05 +02:00
float any
This commit is contained in:
parent
ba410b045b
commit
d49ea1bc49
@ -22,7 +22,7 @@ func (iter *Iterator) ReadAny() Any {
|
|||||||
case Number:
|
case Number:
|
||||||
dotFound, lazyBuf := iter.skipNumber()
|
dotFound, lazyBuf := iter.skipNumber()
|
||||||
if dotFound {
|
if dotFound {
|
||||||
return &floatLazyAny{lazyBuf, nil, nil}
|
return &floatLazyAny{lazyBuf, nil, nil, 0}
|
||||||
} else {
|
} else {
|
||||||
return &intLazyAny{lazyBuf, nil, nil, 0}
|
return &intLazyAny{lazyBuf, nil, nil, 0}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,31 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
type floatLazyAny struct {
|
type floatLazyAny struct {
|
||||||
buf []byte
|
buf []byte
|
||||||
iter *Iterator
|
iter *Iterator
|
||||||
err error
|
err error
|
||||||
|
cache float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *floatLazyAny) fillCache() {
|
||||||
|
if any.err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
iter := any.iter
|
||||||
|
if iter == nil {
|
||||||
|
iter = NewIterator()
|
||||||
|
}
|
||||||
|
iter.ResetBytes(any.buf)
|
||||||
|
any.cache = iter.ReadFloat64()
|
||||||
|
if iter.Error != io.EOF {
|
||||||
|
iter.reportError("floatLazyAny", "there are bytes left")
|
||||||
|
}
|
||||||
|
any.err = iter.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *floatLazyAny) LastError() error {
|
func (any *floatLazyAny) LastError() error {
|
||||||
@ -11,29 +33,34 @@ func (any *floatLazyAny) LastError() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (any *floatLazyAny) ToBool() bool {
|
func (any *floatLazyAny) ToBool() bool {
|
||||||
return false
|
return any.ToFloat64() != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *floatLazyAny) ToInt() int {
|
func (any *floatLazyAny) ToInt() int {
|
||||||
return 0
|
any.fillCache()
|
||||||
|
return int(any.cache)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *floatLazyAny) ToInt32() int32 {
|
func (any *floatLazyAny) ToInt32() int32 {
|
||||||
return 0
|
any.fillCache()
|
||||||
|
return int32(any.cache)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *floatLazyAny) ToInt64() int64 {
|
func (any *floatLazyAny) ToInt64() int64 {
|
||||||
return 0
|
any.fillCache()
|
||||||
|
return int64(any.cache)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *floatLazyAny) ToFloat32() float32 {
|
func (any *floatLazyAny) ToFloat32() float32 {
|
||||||
return 0
|
any.fillCache()
|
||||||
|
return float32(any.cache)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *floatLazyAny) ToFloat64() float64 {
|
func (any *floatLazyAny) ToFloat64() float64 {
|
||||||
return 0
|
any.fillCache()
|
||||||
|
return any.cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *floatLazyAny) ToString() string {
|
func (any *floatLazyAny) ToString() string {
|
||||||
return ""
|
return *(*string)(unsafe.Pointer(&any.buf))
|
||||||
}
|
}
|
@ -45,12 +45,21 @@ func Test_read_float(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_float_via_read(t *testing.T) {
|
func Test_read_float_as_interface(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`12.3`)
|
iter := ParseString(`12.3`)
|
||||||
should.Equal(float64(12.3), iter.Read())
|
should.Equal(float64(12.3), iter.Read())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_read_float_as_any(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
any, err := UnmarshalAnyFromString("12.3")
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(float64(12.3), any.ToFloat64())
|
||||||
|
should.Equal("12.3", any.ToString())
|
||||||
|
should.True(any.ToBool())
|
||||||
|
}
|
||||||
|
|
||||||
func Test_write_float32(t *testing.T) {
|
func Test_write_float32(t *testing.T) {
|
||||||
vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
|
vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
|
||||||
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
|
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user