Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0704d0a

Browse files
authoredMar 13, 2024
feat:(ast) export some API for third-party JSON libs (bytedance#608)
1 parent 5d45952 commit 0704d0a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+14807
-12983
lines changed
 

‎ast/api.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build (amd64 && go1.16 && !go1.23) || (arm64 && go1.20 && !go1.23)
12
// +build amd64,go1.16,!go1.23 arm64,go1.20,!go1.23
23

34
/*
@@ -27,6 +28,7 @@ import (
2728
`github.com/bytedance/sonic/internal/native/types`
2829
`github.com/bytedance/sonic/internal/rt`
2930
uq `github.com/bytedance/sonic/unquote`
31+
`github.com/bytedance/sonic/utf8`
3032
)
3133

3234
var typeByte = rt.UnpackEface(byte(0)).Type
@@ -101,7 +103,7 @@ func (self *Parser) skip() (int, types.ParsingError) {
101103

102104
func (self *Node) encodeInterface(buf *[]byte) error {
103105
//WARN: NOT compatible with json.Encoder
104-
return encoder.EncodeInto(buf, self.packAny(), 0)
106+
return encoder.EncodeInto(buf, self.packAny(), encoder.NoEncoderNewline)
105107
}
106108

107109
func (self *Parser) skipFast() (int, types.ParsingError) {
@@ -112,13 +114,22 @@ func (self *Parser) skipFast() (int, types.ParsingError) {
112114
return start, 0
113115
}
114116

115-
func (self *Parser) getByPath(path ...interface{}) (int, types.ParsingError) {
116-
fsm := types.NewStateMachine()
117+
func (self *Parser) getByPath(validate bool, path ...interface{}) (int, types.ParsingError) {
118+
var fsm *types.StateMachine
119+
if validate {
120+
fsm = types.NewStateMachine()
121+
}
117122
start := native.GetByPath(&self.s, &self.p, &path, fsm)
118-
types.FreeStateMachine(fsm)
123+
if validate {
124+
types.FreeStateMachine(fsm)
125+
}
119126
runtime.KeepAlive(path)
120127
if start < 0 {
121128
return self.p, types.ParsingError(-start)
122129
}
123130
return start, 0
124131
}
132+
133+
func validate_utf8(str string) bool {
134+
return utf8.ValidateString(str)
135+
}

‎ast/api_compat.go

+50-41
Original file line numberDiff line numberDiff line change
@@ -19,67 +19,69 @@
1919
package ast
2020

2121
import (
22-
`encoding/json`
22+
`encoding/json`
23+
`unicode/utf8`
2324

24-
`github.com/bytedance/sonic/internal/native/types`
25-
`github.com/bytedance/sonic/internal/rt`
25+
`github.com/bytedance/sonic/internal/native/types`
26+
`github.com/bytedance/sonic/internal/rt`
2627
)
2728

2829
func init() {
29-
println("WARNING:(ast) sonic only supports Go1.16~1.22, but your environment is not suitable")
30+
println("WARNING:(ast) sonic only supports Go1.16~1.22, but your environment is not suitable")
3031
}
3132

3233
func quote(buf *[]byte, val string) {
33-
quoteString(buf, val)
34+
quoteString(buf, val)
3435
}
3536

37+
// unquote unescapes a internal JSON string (it doesn't count quotas at the begining and end)
3638
func unquote(src string) (string, types.ParsingError) {
37-
sp := rt.IndexChar(src, -1)
38-
out, ok := unquoteBytes(rt.BytesFrom(sp, len(src)+2, len(src)+2))
39-
if !ok {
40-
return "", types.ERR_INVALID_ESCAPE
41-
}
42-
return rt.Mem2Str(out), 0
39+
sp := rt.IndexChar(src, -1)
40+
out, ok := unquoteBytes(rt.BytesFrom(sp, len(src)+2, len(src)+2))
41+
if !ok {
42+
return "", types.ERR_INVALID_ESCAPE
43+
}
44+
return rt.Mem2Str(out), 0
4345
}
4446

4547

4648
func (self *Parser) decodeValue() (val types.JsonState) {
47-
e, v := decodeValue(self.s, self.p, self.dbuf == nil)
48-
if e < 0 {
49-
return v
50-
}
51-
self.p = e
52-
return v
49+
e, v := decodeValue(self.s, self.p, self.dbuf == nil)
50+
if e < 0 {
51+
return v
52+
}
53+
self.p = e
54+
return v
5355
}
5456

5557
func (self *Parser) skip() (int, types.ParsingError) {
56-
e, s := skipValue(self.s, self.p)
57-
if e < 0 {
58-
return self.p, types.ParsingError(-e)
59-
}
60-
self.p = e
61-
return s, 0
58+
e, s := skipValue(self.s, self.p)
59+
if e < 0 {
60+
return self.p, types.ParsingError(-e)
61+
}
62+
self.p = e
63+
return s, 0
6264
}
6365

6466
func (self *Parser) skipFast() (int, types.ParsingError) {
65-
e, s := skipValueFast(self.s, self.p)
66-
if e < 0 {
67-
return self.p, types.ParsingError(-e)
68-
}
69-
self.p = e
70-
return s, 0
67+
e, s := skipValueFast(self.s, self.p)
68+
if e < 0 {
69+
return self.p, types.ParsingError(-e)
70+
}
71+
self.p = e
72+
return s, 0
7173
}
7274

7375
func (self *Node) encodeInterface(buf *[]byte) error {
74-
out, err := json.Marshal(self.packAny())
75-
if err != nil {
76-
return err
77-
}
78-
*buf = append(*buf, out...)
79-
return nil
76+
out, err := json.Marshal(self.packAny())
77+
if err != nil {
78+
return err
79+
}
80+
*buf = append(*buf, out...)
81+
return nil
8082
}
8183

82-
func (self *Parser) getByPath(path ...interface{}) (int, types.ParsingError) {
84+
func (self *Parser) getByPath(validate bool, path ...interface{}) (int, types.ParsingError) {
8385
for _, p := range path {
8486
if idx, ok := p.(int); ok && idx >= 0 {
8587
if err := self.searchIndex(idx); err != 0 {
@@ -93,13 +95,20 @@ func (self *Parser) getByPath(path ...interface{}) (int, types.ParsingError) {
9395
panic("path must be either int(>=0) or string")
9496
}
9597
}
96-
start, e := self.skip()
98+
99+
var start int
100+
var e types.ParsingError
101+
if validate {
102+
start, e = self.skip()
103+
} else {
104+
start, e = self.skipFast()
105+
}
97106
if e != 0 {
98107
return self.p, e
99108
}
100-
// t := switchRawType(self.s[start])
101-
// if t == _V_NUMBER {
102-
// self.p = 1 + backward(self.s, self.p-1)
103-
// }
104109
return start, 0
105110
}
111+
112+
func validate_utf8(str string) bool {
113+
return utf8.ValidString(str)
114+
}

0 commit comments

Comments
 (0)
Please sign in to comment.