From cfde002d29aac1f63b3faf8a67af991410e57146 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 24 Apr 2020 16:46:12 +0900 Subject: [PATCH] Add decoder of float --- decode.go | 16 ++++++++++++++ decode_float.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ decode_test.go | 10 +++++++++ 3 files changed, 81 insertions(+) create mode 100644 decode_float.go diff --git a/decode.go b/decode.go index 280cdca..8966c67 100644 --- a/decode.go +++ b/decode.go @@ -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 } diff --git a/decode_float.go b/decode_float.go new file mode 100644 index 0000000..43eadd1 --- /dev/null +++ b/decode_float.go @@ -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 +} diff --git a/decode_test.go b/decode_test.go index 533664f..a391d37 100644 --- a/decode_test.go +++ b/decode_test.go @@ -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"`