...
1
2
3
18
19 package ast
20
21 import (
22 `runtime`
23 `unsafe`
24
25 `github.com/bytedance/sonic/encoder`
26 `github.com/bytedance/sonic/internal/native`
27 `github.com/bytedance/sonic/internal/native/types`
28 `github.com/bytedance/sonic/internal/rt`
29 uq `github.com/bytedance/sonic/unquote`
30 `github.com/chenzhuoyu/base64x`
31 )
32
33 var typeByte = rt.UnpackEface(byte(0)).Type
34
35
36 func quote(buf *[]byte, val string) {
37 *buf = append(*buf, '"')
38 if len(val) == 0 {
39 *buf = append(*buf, '"')
40 return
41 }
42
43 sp := rt.IndexChar(val, 0)
44 nb := len(val)
45 b := (*rt.GoSlice)(unsafe.Pointer(buf))
46
47
48 for nb > 0 {
49
50 dp := unsafe.Pointer(uintptr(b.Ptr) + uintptr(b.Len))
51 dn := b.Cap - b.Len
52
53 ret := native.Quote(sp, nb, dp, &dn, 0)
54
55 b.Len += dn
56
57
58 if ret >= 0 {
59 break
60 }
61
62
63 *b = growslice(typeByte, *b, b.Cap*2)
64
65 ret = ^ret
66
67 nb -= ret
68 sp = unsafe.Pointer(uintptr(sp) + uintptr(ret))
69 }
70
71 runtime.KeepAlive(buf)
72 runtime.KeepAlive(sp)
73 *buf = append(*buf, '"')
74 }
75
76 func unquote(src string) (string, types.ParsingError) {
77 return uq.String(src)
78 }
79
80 func decodeBase64(src string) ([]byte, error) {
81 return base64x.StdEncoding.DecodeString(src)
82 }
83
84 func encodeBase64(src []byte) string {
85 return base64x.StdEncoding.EncodeToString(src)
86 }
87
88 func (self *Parser) decodeValue() (val types.JsonState) {
89 sv := (*rt.GoString)(unsafe.Pointer(&self.s))
90 flag := types.F_USE_NUMBER
91 if self.dbuf != nil {
92 flag = 0
93 val.Dbuf = self.dbuf
94 val.Dcap = types.MaxDigitNums
95 }
96 self.p = native.Value(sv.Ptr, sv.Len, self.p, &val, uint64(flag))
97 return
98 }
99
100 func (self *Parser) skip() (int, types.ParsingError) {
101 fsm := types.NewStateMachine()
102 start := native.SkipOne(&self.s, &self.p, fsm, 0)
103 types.FreeStateMachine(fsm)
104
105 if start < 0 {
106 return self.p, types.ParsingError(-start)
107 }
108 return start, 0
109 }
110
111 func (self *Node) encodeInterface(buf *[]byte) error {
112
113 return encoder.EncodeInto(buf, self.packAny(), 0)
114 }
115
116 func (self *Parser) skipFast() (int, types.ParsingError) {
117 start := native.SkipOneFast(&self.s, &self.p)
118 if start < 0 {
119 return self.p, types.ParsingError(-start)
120 }
121 return start, 0
122 }
123
124 func (self *Parser) getByPath(path ...interface{}) (int, types.ParsingError) {
125 fsm := types.NewStateMachine()
126 start := native.GetByPath(&self.s, &self.p, &path, fsm)
127 types.FreeStateMachine(fsm)
128 runtime.KeepAlive(path)
129 if start < 0 {
130 return self.p, types.ParsingError(-start)
131 }
132 return start, 0
133 }
134
View as plain text