| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
"reflect" |
| 6 |
"strconv" |
| 7 |
"unsafe" |
| 8 |
|
| 9 |
"github.com/goccy/go-json/internal/errors" |
| 10 |
) |
| 11 |
|
| 12 |
type floatDecoder struct { |
| 13 |
op func(unsafe.Pointer, float64) |
| 14 |
structName string |
| 15 |
fieldName string |
| 16 |
// is32 is set for a float32, whose range is checked: a number out of it is a type error. |
| 17 |
is32 bool |
| 18 |
// typ is the type of the value, which the type errors report. |
| 19 |
typ reflect.Type |
| 20 |
} |
| 21 |
|
| 22 |
func newFloatDecoder(structName, fieldName string, op func(unsafe.Pointer, float64)) *floatDecoder { |
| 23 |
return &floatDecoder{op: op, structName: structName, fieldName: fieldName, typ: reflect.TypeOf(float64(0))} |
| 24 |
} |
| 25 |
|
| 26 |
// overflowsFloat32 reports whether f is out of the range of a float32, as reflect.Value.OverflowFloat does. |
| 27 |
func overflowsFloat32(f float64) bool { |
| 28 |
if f < 0 { |
| 29 |
f = -f |
| 30 |
} |
| 31 |
return math.MaxFloat32 < f && f <= math.MaxFloat64 |
| 32 |
} |
| 33 |
|
| 34 |
var ( |
| 35 |
floatTable = [256]bool{ |
| 36 |
'0': true, |
| 37 |
'1': true, |
| 38 |
'2': true, |
| 39 |
'3': true, |
| 40 |
'4': true, |
| 41 |
'5': true, |
| 42 |
'6': true, |
| 43 |
'7': true, |
| 44 |
'8': true, |
| 45 |
'9': true, |
| 46 |
'.': true, |
| 47 |
'e': true, |
| 48 |
'E': true, |
| 49 |
'+': true, |
| 50 |
'-': true, |
| 51 |
} |
| 52 |
|
| 53 |
validEndNumberChar = [256]bool{ |
| 54 |
nul: true, |
| 55 |
' ': true, |
| 56 |
'\t': true, |
| 57 |
'\r': true, |
| 58 |
'\n': true, |
| 59 |
',': true, |
| 60 |
':': true, |
| 61 |
'}': true, |
| 62 |
']': true, |
| 63 |
} |
| 64 |
) |
| 65 |
|
| 66 |
func (d *floatDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) { |
| 67 |
for { |
| 68 |
switch buf[cursor] { |
| 69 |
case ' ', '\n', '\t', '\r': |
| 70 |
cursor++ |
| 71 |
continue |
| 72 |
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 73 |
start := cursor |
| 74 |
cursor++ |
| 75 |
for floatTable[buf[cursor]] { |
| 76 |
cursor++ |
| 77 |
} |
| 78 |
num := buf[start:cursor] |
| 79 |
return num, cursor, nil |
| 80 |
case 'n': |
| 81 |
if err := validateNull(buf, cursor); err != nil { |
| 82 |
return nil, 0, err |
| 83 |
} |
| 84 |
cursor += 4 |
| 85 |
return nil, cursor, nil |
| 86 |
default: |
| 87 |
return nil, 0, errors.ErrUnexpectedEndOfJSON("float", cursor) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func (d *floatDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { |
| 93 |
buf := ctx.Buf |
| 94 |
cursor = skipWhiteSpace(buf, cursor) |
| 95 |
if f, next, ok := parseFloatFast(buf, cursor); ok && validEndNumberChar[buf[next]] { |
| 96 |
d.op(p, f) |
| 97 |
return next, nil |
| 98 |
} |
| 99 |
return d.decodeSlow(ctx, cursor, depth, p) |
| 100 |
} |
| 101 |
|
| 102 |
// float32Decoder is the decoder of a float32, whose range is checked: a number out of it is a type error. It is a |
| 103 |
// type of its own, so that the decoder of a float64 checks nothing more. |
| 104 |
type float32Decoder struct { |
| 105 |
floatDecoder |
| 106 |
} |
| 107 |
|
| 108 |
func (d *float32Decoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { |
| 109 |
buf := ctx.Buf |
| 110 |
cursor = skipWhiteSpace(buf, cursor) |
| 111 |
if f, next, ok := parseFloatFast(buf, cursor); ok && validEndNumberChar[buf[next]] && !overflowsFloat32(f) { |
| 112 |
d.op(p, f) |
| 113 |
return next, nil |
| 114 |
} |
| 115 |
return d.decodeSlow(ctx, cursor, depth, p) |
| 116 |
} |
| 117 |
|
| 118 |
// decodeSlow decodes the value at cursor which the fast parse doesn't: null, a number which it can't parse, a |
| 119 |
// number out of the range of the type, which is a type error, or a value of another kind, which is a type error |
| 120 |
// too. It is a function of its own, so that Decode keeps the size it had. |
| 121 |
// |
| 122 |
//go:noinline |
| 123 |
func (d *floatDecoder) decodeSlow(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { |
| 124 |
buf := ctx.Buf |
| 125 |
switch c := buf[cursor]; { |
| 126 |
case c == 'n': |
| 127 |
if err := validateNull(buf, cursor); err != nil { |
| 128 |
return 0, err |
| 129 |
} |
| 130 |
return cursor + 4, nil |
| 131 |
case c == '-' || c-'0' <= 9: |
| 132 |
end, err := numberEnd(buf, cursor) |
| 133 |
if err != nil { |
| 134 |
return 0, err |
| 135 |
} |
| 136 |
b := buf[cursor:end] |
| 137 |
f, parseErr := strconv.ParseFloat(*(*string)(unsafe.Pointer(&b)), 64) |
| 138 |
// a number of the grammar which ParseFloat fails is out of the range of float64 |
| 139 |
if inRange := parseErr == nil && !(d.is32 && overflowsFloat32(f)); !inRange { |
| 140 |
ctx.numberTypeError(cursor, end, d.typ) |
| 141 |
if storesFloatsOutOfRange { |
| 142 |
// ±Inf, which ParseFloat returns, or which the conversion to a float32 makes |
| 143 |
d.op(p, f) |
| 144 |
} |
| 145 |
return end, nil |
| 146 |
} |
| 147 |
d.op(p, f) |
| 148 |
return end, nil |
| 149 |
case isOtherValue(c, numberValue): |
| 150 |
return ctx.skipTypeError(cursor, depth, d.typ) |
| 151 |
} |
| 152 |
return 0, errors.ErrUnexpectedEndOfJSON("float", cursor) |
| 153 |
} |
| 154 |
|
| 155 |
func (d *floatDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) { |
| 156 |
buf := ctx.Buf |
| 157 |
bytes, c, err := d.decodeByte(buf, cursor) |
| 158 |
if err != nil { |
| 159 |
return nil, 0, err |
| 160 |
} |
| 161 |
if bytes == nil { |
| 162 |
return [][]byte{nullbytes}, c, nil |
| 163 |
} |
| 164 |
return [][]byte{bytes}, c, nil |
| 165 |
} |