-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
decode_ptr.go
85 lines (78 loc) · 1.76 KB
/
decode_ptr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package json
import (
"unsafe"
)
type ptrDecoder struct {
dec decoder
typ *rtype
structName string
fieldName string
}
func newPtrDecoder(dec decoder, typ *rtype, structName, fieldName string) *ptrDecoder {
return &ptrDecoder{
dec: dec,
typ: typ,
structName: structName,
fieldName: fieldName,
}
}
func (d *ptrDecoder) contentDecoder() decoder {
dec, ok := d.dec.(*ptrDecoder)
if !ok {
return d.dec
}
return dec.contentDecoder()
}
//nolint:golint
//go:linkname unsafe_New reflect.unsafe_New
func unsafe_New(*rtype) unsafe.Pointer
func (d *ptrDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
s.skipWhiteSpace()
if s.char() == nul {
s.read()
}
if s.char() == 'n' {
if err := nullBytes(s); err != nil {
return err
}
*(*unsafe.Pointer)(p) = nil
return nil
}
newptr := unsafe_New(d.typ)
*(*unsafe.Pointer)(p) = newptr
if err := d.dec.decodeStream(s, newptr); err != nil {
return err
}
return nil
}
func (d *ptrDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] == 'n' {
buflen := int64(len(buf))
if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("null", cursor)
}
if buf[cursor+1] != 'u' {
return 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
}
if buf[cursor+2] != 'l' {
return 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
}
if buf[cursor+3] != 'l' {
return 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
}
if p != nil {
*(*unsafe.Pointer)(p) = nil
}
cursor += 4
return cursor, nil
}
newptr := unsafe_New(d.typ)
*(*unsafe.Pointer)(p) = newptr
c, err := d.dec.decode(buf, cursor, newptr)
if err != nil {
return 0, err
}
cursor = c
return cursor, nil
}