Add decoder of float

This commit is contained in:
Masaaki Goshima 2020-04-24 16:46:12 +09:00
parent 8409d60bdf
commit cfde002d29
3 changed files with 81 additions and 0 deletions

View File

@ -150,6 +150,10 @@ func (d *Decoder) compile(typ reflect.Type) (decoder, error) {
return d.compileString()
case reflect.Bool:
return d.compileBool()
case reflect.Float32:
return d.compileFloat32()
case reflect.Float64:
return d.compileFloat64()
}
return nil, nil
}
@ -222,6 +226,18 @@ func (d *Decoder) compileUint64() (decoder, error) {
}), nil
}
func (d *Decoder) compileFloat32() (decoder, error) {
return newFloatDecoder(func(p uintptr, v float64) {
*(*float32)(unsafe.Pointer(p)) = float32(v)
}), nil
}
func (d *Decoder) compileFloat64() (decoder, error) {
return newFloatDecoder(func(p uintptr, v float64) {
*(*float64)(unsafe.Pointer(p)) = v
}), nil
}
func (d *Decoder) compileString() (decoder, error) {
return newStringDecoder(), nil
}

55
decode_float.go Normal file
View File

@ -0,0 +1,55 @@
package json
import (
"errors"
"strconv"
"unsafe"
)
type floatDecoder struct {
op func(uintptr, float64)
}
func newFloatDecoder(op func(uintptr, float64)) *floatDecoder {
return &floatDecoder{op: op}
}
func (d *floatDecoder) decodeByte(ctx *context) ([]byte, error) {
buf := ctx.buf
cursor := ctx.cursor
buflen := ctx.buflen
for ; cursor < buflen; cursor++ {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := cursor
cursor++
for ; cursor < buflen; cursor++ {
tk := int(buf[cursor])
if (int('0') <= tk && tk <= int('9')) || tk == '.' || tk == 'e' || tk == 'E' {
continue
}
break
}
num := ctx.buf[start:cursor]
ctx.cursor = cursor
return num, nil
}
}
return nil, errors.New("unexpected error number")
}
func (d *floatDecoder) decode(ctx *context, p uintptr) error {
bytes, err := d.decodeByte(ctx)
if err != nil {
return err
}
s := *(*string)(unsafe.Pointer(&bytes))
f64, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
d.op(p, f64)
return nil
}

View File

@ -74,6 +74,16 @@ func Test_Decoder(t *testing.T) {
assertErr(t, json.Unmarshal([]byte(`"hello"`), &v))
assertEq(t, "string", "hello", v)
})
t.Run("float32", func(t *testing.T) {
var v float32
assertErr(t, json.Unmarshal([]byte(`3.14`), &v))
assertEq(t, "float32", float32(3.14), v)
})
t.Run("float64", func(t *testing.T) {
var v float64
assertErr(t, json.Unmarshal([]byte(`3.14`), &v))
assertEq(t, "float64", float64(3.14), v)
})
t.Run("struct", func(t *testing.T) {
type T struct {
AA int `json:"aa"`