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