.github
alerting
client
config
controller
core
docs
examples
jsonpath
k8s
k8stest
metric
pattern
security
storage
util
vendor
cloud.google.com
github.com
TwinProduction
beorn7
cespare
davecgh
go-ping
gogo
golang
google
googleapis
gorilla
imdario
json-iterator
go
.codecov.yml
.gitignore
.travis.yml
Gopkg.lock
Gopkg.toml
LICENSE
README.md
adapter.go
any.go
any_array.go
any_bool.go
any_float.go
any_int32.go
any_int64.go
any_invalid.go
any_nil.go
any_number.go
any_object.go
any_str.go
any_uint32.go
any_uint64.go
build.sh
config.go
fuzzy_mode_convert_table.md
go.mod
go.sum
iter.go
iter_array.go
iter_float.go
iter_int.go
iter_object.go
iter_skip.go
iter_skip_sloppy.go
iter_skip_strict.go
iter_str.go
jsoniter.go
pool.go
reflect.go
reflect_array.go
reflect_dynamic.go
reflect_extension.go
reflect_json_number.go
reflect_json_raw_message.go
reflect_map.go
reflect_marshaler.go
reflect_native.go
reflect_optional.go
reflect_slice.go
reflect_struct_decoder.go
reflect_struct_encoder.go
stream.go
stream_float.go
stream_int.go
stream_str.go
test.sh
kballard
mattn
matttproud
miekg
modern-go
prometheus
remyoudompheng
spf13
go.etcd.io
golang.org
google.golang.org
gopkg.in
k8s.io
lukechampine.com
modernc.org
sigs.k8s.io
modules.txt
watchdog
web
.dockerignore
.gitattributes
.gitignore
Dockerfile
LICENSE.md
Makefile
README.md
config.yaml
go.mod
go.sum
main.go
112 lines
2.7 KiB
Go
112 lines
2.7 KiB
Go
package jsoniter
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"strconv"
|
|
)
|
|
|
|
var pow10 []uint64
|
|
|
|
func init() {
|
|
pow10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}
|
|
}
|
|
|
|
// WriteFloat32 write float32 to stream
|
|
func (stream *Stream) WriteFloat32(val float32) {
|
|
if math.IsInf(float64(val), 0) || math.IsNaN(float64(val)) {
|
|
stream.Error = fmt.Errorf("unsupported value: %f", val)
|
|
return
|
|
}
|
|
abs := math.Abs(float64(val))
|
|
fmt := byte('f')
|
|
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
|
|
if abs != 0 {
|
|
if float32(abs) < 1e-6 || float32(abs) >= 1e21 {
|
|
fmt = 'e'
|
|
}
|
|
}
|
|
stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 32)
|
|
}
|
|
|
|
// WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster
|
|
func (stream *Stream) WriteFloat32Lossy(val float32) {
|
|
if math.IsInf(float64(val), 0) || math.IsNaN(float64(val)) {
|
|
stream.Error = fmt.Errorf("unsupported value: %f", val)
|
|
return
|
|
}
|
|
if val < 0 {
|
|
stream.writeByte('-')
|
|
val = -val
|
|
}
|
|
if val > 0x4ffffff {
|
|
stream.WriteFloat32(val)
|
|
return
|
|
}
|
|
precision := 6
|
|
exp := uint64(1000000) // 6
|
|
lval := uint64(float64(val)*float64(exp) + 0.5)
|
|
stream.WriteUint64(lval / exp)
|
|
fval := lval % exp
|
|
if fval == 0 {
|
|
return
|
|
}
|
|
stream.writeByte('.')
|
|
for p := precision - 1; p > 0 && fval < pow10[p]; p-- {
|
|
stream.writeByte('0')
|
|
}
|
|
stream.WriteUint64(fval)
|
|
for stream.buf[len(stream.buf)-1] == '0' {
|
|
stream.buf = stream.buf[:len(stream.buf)-1]
|
|
}
|
|
}
|
|
|
|
// WriteFloat64 write float64 to stream
|
|
func (stream *Stream) WriteFloat64(val float64) {
|
|
if math.IsInf(val, 0) || math.IsNaN(val) {
|
|
stream.Error = fmt.Errorf("unsupported value: %f", val)
|
|
return
|
|
}
|
|
abs := math.Abs(val)
|
|
fmt := byte('f')
|
|
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
|
|
if abs != 0 {
|
|
if abs < 1e-6 || abs >= 1e21 {
|
|
fmt = 'e'
|
|
}
|
|
}
|
|
stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 64)
|
|
}
|
|
|
|
// WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster
|
|
func (stream *Stream) WriteFloat64Lossy(val float64) {
|
|
if math.IsInf(val, 0) || math.IsNaN(val) {
|
|
stream.Error = fmt.Errorf("unsupported value: %f", val)
|
|
return
|
|
}
|
|
if val < 0 {
|
|
stream.writeByte('-')
|
|
val = -val
|
|
}
|
|
if val > 0x4ffffff {
|
|
stream.WriteFloat64(val)
|
|
return
|
|
}
|
|
precision := 6
|
|
exp := uint64(1000000) // 6
|
|
lval := uint64(val*float64(exp) + 0.5)
|
|
stream.WriteUint64(lval / exp)
|
|
fval := lval % exp
|
|
if fval == 0 {
|
|
return
|
|
}
|
|
stream.writeByte('.')
|
|
for p := precision - 1; p > 0 && fval < pow10[p]; p-- {
|
|
stream.writeByte('0')
|
|
}
|
|
stream.WriteUint64(fval)
|
|
for stream.buf[len(stream.buf)-1] == '0' {
|
|
stream.buf = stream.buf[:len(stream.buf)-1]
|
|
}
|
|
}
|