package decoder
import (
"math"
"reflect"
"strconv"
"unsafe"
"github.com/goccy/go-json/internal/errors"
)
type floatDecoder struct {
op func (unsafe .Pointer , float64 )
structName string
fieldName string
is32 bool
typ reflect .Type
}
func newFloatDecoder(structName , fieldName string , op func (unsafe .Pointer , float64 )) *floatDecoder {
return &floatDecoder {op : op , structName : structName , fieldName : fieldName , typ : reflect .TypeOf (float64 (0 ))}
}
func overflowsFloat32(f float64 ) bool {
if f < 0 {
f = -f
}
return math .MaxFloat32 < f && f <= math .MaxFloat64
}
var (
floatTable = [256 ]bool {
'0' : true ,
'1' : true ,
'2' : true ,
'3' : true ,
'4' : true ,
'5' : true ,
'6' : true ,
'7' : true ,
'8' : true ,
'9' : true ,
'.' : true ,
'e' : true ,
'E' : true ,
'+' : true ,
'-' : true ,
}
validEndNumberChar = [256 ]bool {
nul : true ,
' ' : true ,
'\t' : true ,
'\r' : true ,
'\n' : true ,
',' : true ,
':' : true ,
'}' : true ,
']' : true ,
}
)
func (d *floatDecoder ) decodeByte (buf []byte , cursor int64 ) ([]byte , int64 , error ) {
for {
switch buf [cursor ] {
case ' ' , '\n' , '\t' , '\r' :
cursor ++
continue
case '-' , '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' :
start := cursor
cursor ++
for floatTable [buf [cursor ]] {
cursor ++
}
num := buf [start :cursor ]
return num , cursor , nil
case 'n' :
if err := validateNull (buf , cursor ); err != nil {
return nil , 0 , err
}
cursor += 4
return nil , cursor , nil
default :
return nil , 0 , errors .ErrUnexpectedEndOfJSON ("float" , cursor )
}
}
}
func (d *floatDecoder ) Decode (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
buf := ctx .Buf
cursor = skipWhiteSpace (buf , cursor )
if f , next , ok := parseFloatFast (buf , cursor ); ok && validEndNumberChar [buf [next ]] {
d .op (p , f )
return next , nil
}
return d .decodeSlow (ctx , cursor , depth , p )
}
type float32Decoder struct {
floatDecoder
}
func (d *float32Decoder ) Decode (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
buf := ctx .Buf
cursor = skipWhiteSpace (buf , cursor )
if f , next , ok := parseFloatFast (buf , cursor ); ok && validEndNumberChar [buf [next ]] && !overflowsFloat32 (f ) {
d .op (p , f )
return next , nil
}
return d .decodeSlow (ctx , cursor , depth , p )
}
func (d *floatDecoder ) decodeSlow (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
buf := ctx .Buf
switch c := buf [cursor ]; {
case c == 'n' :
if err := validateNull (buf , cursor ); err != nil {
return 0 , err
}
if ctx .Option .V2 &(V2Semantics |V2MergeWithLegacySemantics ) == V2Semantics {
d .op (p , 0 )
}
return cursor + 4 , nil
case c == '-' || c -'0' <= 9 :
end , err := numberEnd (buf , cursor )
if err != nil {
return 0 , err
}
b := buf [cursor :end ]
f , parseErr := strconv .ParseFloat (*(*string )(unsafe .Pointer (&b )), 64 )
if inRange := parseErr == nil && !(d .is32 && overflowsFloat32 (f )); !inRange {
return ctx .numberError (cursor , end , d .typ )
}
d .op (p , f )
return end , nil
case isOtherValue (c , numberValue ):
return ctx .skipTypeError (cursor , depth , d .typ )
}
return 0 , errors .ErrUnexpectedEndOfJSON ("float" , cursor )
}
func (d *floatDecoder ) DecodePath (ctx *RuntimeContext , cursor , depth int64 ) ([][]byte , int64 , error ) {
buf := ctx .Buf
bytes , c , err := d .decodeByte (buf , cursor )
if err != nil {
return nil , 0 , err
}
if bytes == nil {
return [][]byte {nullbytes }, c , nil
}
return [][]byte {bytes }, c , nil
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .