| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
"unsafe" |
| 7 |
) |
| 8 |
|
| 9 |
type uintDecoder struct { |
| 10 |
typ reflect.Type |
| 11 |
kind reflect.Kind |
| 12 |
op func(unsafe.Pointer, uint64) |
| 13 |
structName string |
| 14 |
fieldName string |
| 15 |
} |
| 16 |
|
| 17 |
func newUintDecoder(typ reflect.Type, structName, fieldName string, op func(unsafe.Pointer, uint64)) *uintDecoder { |
| 18 |
return &uintDecoder{ |
| 19 |
typ: typ, |
| 20 |
kind: typ.Kind(), |
| 21 |
op: op, |
| 22 |
structName: structName, |
| 23 |
fieldName: fieldName, |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func (d *uintDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { |
| 28 |
buf := ctx.Buf |
| 29 |
cursor = skipWhiteSpace(buf, cursor) |
| 30 |
if buf[cursor] == 'n' { |
| 31 |
if err := validateNull(buf, cursor); err != nil { |
| 32 |
return 0, err |
| 33 |
} |
| 34 |
return cursor + 4, nil |
| 35 |
} |
| 36 |
if buf[cursor]-'0' > 9 { |
| 37 |
return d.decodeSlow(ctx, cursor, depth, p) |
| 38 |
} |
| 39 |
start := cursor |
| 40 |
u, next, digits := parseDigits(buf, cursor) |
| 41 |
if isFloatContinuation(buf[next]) || digits > maxUint64Digits+1 { |
| 42 |
return d.decodeSlow(ctx, start, depth, p) |
| 43 |
} |
| 44 |
if digits == maxUint64Digits+1 { |
| 45 |
// 20 digits: the value of the first 19 is exact, and the last one fits if the whole is less than 1<<64. |
| 46 |
var hi uint64 |
| 47 |
for i := int64(0); i < maxUint64Digits; i++ { |
| 48 |
hi = hi*10 + uint64(buf[start+i]-'0') |
| 49 |
} |
| 50 |
lo := uint64(buf[start+maxUint64Digits] - '0') |
| 51 |
if hi > (1<<64-1)/10 || (hi == (1<<64-1)/10 && lo > (1<<64-1)%10) { |
| 52 |
return d.decodeSlow(ctx, start, depth, p) |
| 53 |
} |
| 54 |
u = hi*10 + lo |
| 55 |
} |
| 56 |
switch d.kind { |
| 57 |
case reflect.Uint8: |
| 58 |
if (1 << 8) <= u { |
| 59 |
return d.decodeSlow(ctx, start, depth, p) |
| 60 |
} |
| 61 |
case reflect.Uint16: |
| 62 |
if (1 << 16) <= u { |
| 63 |
return d.decodeSlow(ctx, start, depth, p) |
| 64 |
} |
| 65 |
case reflect.Uint32: |
| 66 |
if (1 << 32) <= u { |
| 67 |
return d.decodeSlow(ctx, start, depth, p) |
| 68 |
} |
| 69 |
} |
| 70 |
d.op(p, u) |
| 71 |
return next, nil |
| 72 |
} |
| 73 |
|
| 74 |
// decodeSlow decodes the value at cursor, which Decode doesn't: a value of another kind, a negative number or |
| 75 |
// a number which is not an integer of the type, which are type errors, or a syntax error. It is a function of its |
| 76 |
// own, so that Decode keeps the size it had. |
| 77 |
// |
| 78 |
//go:noinline |
| 79 |
func (d *uintDecoder) decodeSlow(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { |
| 80 |
buf := ctx.Buf |
| 81 |
cursor = skipWhiteSpace(buf, cursor) |
| 82 |
switch c := buf[cursor]; { |
| 83 |
case c == 'n': |
| 84 |
if err := validateNull(buf, cursor); err != nil { |
| 85 |
return 0, err |
| 86 |
} |
| 87 |
return cursor + 4, nil |
| 88 |
case c == '-': |
| 89 |
// a negative number, which the error reports |
| 90 |
end, err := numberEnd(buf, cursor) |
| 91 |
if err != nil { |
| 92 |
return 0, err |
| 93 |
} |
| 94 |
ctx.numberTypeError(cursor, end, d.typ) |
| 95 |
return end, nil |
| 96 |
case c-'0' > 9: |
| 97 |
// a value of another kind |
| 98 |
return ctx.skipTypeError(cursor, depth, d.typ) |
| 99 |
} |
| 100 |
start := cursor |
| 101 |
u, next, digits := parseDigits(buf, cursor) |
| 102 |
if isFloatContinuation(buf[next]) || digits > maxUint64Digits+1 { |
| 103 |
// a number which is not an integer, or too large for any integer |
| 104 |
end, err := numberEnd(buf, start) |
| 105 |
if err != nil { |
| 106 |
return 0, err |
| 107 |
} |
| 108 |
ctx.numberTypeError(start, end, d.typ) |
| 109 |
return end, nil |
| 110 |
} |
| 111 |
if digits == maxUint64Digits+1 { |
| 112 |
// 20 digits: the value of the first 19 is exact, and the last one fits if the whole is less than 1<<64. |
| 113 |
var hi uint64 |
| 114 |
for i := int64(0); i < maxUint64Digits; i++ { |
| 115 |
hi = hi*10 + uint64(buf[start+i]-'0') |
| 116 |
} |
| 117 |
lo := uint64(buf[start+maxUint64Digits] - '0') |
| 118 |
if hi > (1<<64-1)/10 || (hi == (1<<64-1)/10 && lo > (1<<64-1)%10) { |
| 119 |
ctx.numberTypeError(start, next, d.typ) |
| 120 |
return next, nil |
| 121 |
} |
| 122 |
u = hi*10 + lo |
| 123 |
} |
| 124 |
switch d.kind { |
| 125 |
case reflect.Uint8: |
| 126 |
if (1 << 8) <= u { |
| 127 |
ctx.numberTypeError(start, next, d.typ) |
| 128 |
return next, nil |
| 129 |
} |
| 130 |
case reflect.Uint16: |
| 131 |
if (1 << 16) <= u { |
| 132 |
ctx.numberTypeError(start, next, d.typ) |
| 133 |
return next, nil |
| 134 |
} |
| 135 |
case reflect.Uint32: |
| 136 |
if (1 << 32) <= u { |
| 137 |
ctx.numberTypeError(start, next, d.typ) |
| 138 |
return next, nil |
| 139 |
} |
| 140 |
} |
| 141 |
d.op(p, u) |
| 142 |
return next, nil |
| 143 |
} |
| 144 |
|
| 145 |
func (d *uintDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) { |
| 146 |
return nil, 0, fmt.Errorf("json: uint decoder does not support decode path") |
| 147 |
} |