summaryrefslogtreecommitdiff
path: root/vendor/github.com/bytedance/sonic/ast
diff options
context:
space:
mode:
authorLibravatar dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>2023-06-01 22:20:16 +0100
committerLibravatar GitHub <noreply@github.com>2023-06-01 22:20:16 +0100
commit55aacaf4b07c1921061245cbaa3d307e97cf3c29 (patch)
treed969c5d9728566de1e794e19c5b19d3b660f790e /vendor/github.com/bytedance/sonic/ast
parent[chore/frontend] refactor header templating, add apple-touch-icon (#1850) (diff)
downloadgotosocial-55aacaf4b07c1921061245cbaa3d307e97cf3c29.tar.xz
[chore]: Bump github.com/gin-gonic/gin from 1.9.0 to 1.9.1 (#1855)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Diffstat (limited to 'vendor/github.com/bytedance/sonic/ast')
-rw-r--r--vendor/github.com/bytedance/sonic/ast/api_amd64.go29
-rw-r--r--vendor/github.com/bytedance/sonic/ast/api_compat.go38
-rw-r--r--vendor/github.com/bytedance/sonic/ast/decode.go161
-rw-r--r--vendor/github.com/bytedance/sonic/ast/node.go16
-rw-r--r--vendor/github.com/bytedance/sonic/ast/parser.go4
5 files changed, 221 insertions, 27 deletions
diff --git a/vendor/github.com/bytedance/sonic/ast/api_amd64.go b/vendor/github.com/bytedance/sonic/ast/api_amd64.go
index 6b3458aee..3047f59c3 100644
--- a/vendor/github.com/bytedance/sonic/ast/api_amd64.go
+++ b/vendor/github.com/bytedance/sonic/ast/api_amd64.go
@@ -1,5 +1,20 @@
// +build amd64,go1.15,!go1.21
+/*
+ * Copyright 2022 ByteDance Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package ast
@@ -17,10 +32,12 @@ import (
var typeByte = rt.UnpackEface(byte(0)).Type
+//go:nocheckptr
func quote(buf *[]byte, val string) {
*buf = append(*buf, '"')
if len(val) == 0 {
*buf = append(*buf, '"')
+ return
}
sp := rt.IndexChar(val, 0)
@@ -99,7 +116,9 @@ func (self *Parser) skipFast() (int, types.ParsingError) {
}
func (self *Parser) getByPath(path ...interface{}) (int, types.ParsingError) {
- start := native.GetByPath(&self.s, &self.p, &path)
+ fsm := types.NewStateMachine()
+ start := native.GetByPath(&self.s, &self.p, &path, fsm)
+ types.FreeStateMachine(fsm)
runtime.KeepAlive(path)
if start < 0 {
return self.p, types.ParsingError(-start)
@@ -107,7 +126,6 @@ func (self *Parser) getByPath(path ...interface{}) (int, types.ParsingError) {
return start, 0
}
-
func (self *Searcher) GetByPath(path ...interface{}) (Node, error) {
var err types.ParsingError
var start int
@@ -115,6 +133,13 @@ func (self *Searcher) GetByPath(path ...interface{}) (Node, error) {
self.parser.p = 0
start, err = self.parser.getByPath(path...)
if err != 0 {
+ // for compatibility with old version
+ if err == types.ERR_NOT_FOUND {
+ return Node{}, ErrNotExist
+ }
+ if err == types.ERR_UNSUPPORT_TYPE {
+ panic("path must be either int(>=0) or string")
+ }
return Node{}, self.parser.syntaxError(err)
}
diff --git a/vendor/github.com/bytedance/sonic/ast/api_compat.go b/vendor/github.com/bytedance/sonic/ast/api_compat.go
index 642330c51..b18b5ae8c 100644
--- a/vendor/github.com/bytedance/sonic/ast/api_compat.go
+++ b/vendor/github.com/bytedance/sonic/ast/api_compat.go
@@ -1,5 +1,21 @@
// +build !amd64 go1.21
+/*
+ * Copyright 2022 ByteDance Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package ast
import (
@@ -24,8 +40,6 @@ func unquote(src string) (string, types.ParsingError) {
return rt.Mem2Str(out), 0
}
-
-
func decodeBase64(src string) ([]byte, error) {
return base64.StdEncoding.DecodeString(src)
}
@@ -53,7 +67,12 @@ func (self *Parser) skip() (int, types.ParsingError) {
}
func (self *Parser) skipFast() (int, types.ParsingError) {
- return self.skip()
+ e, s := skipValueFast(self.s, self.p)
+ if e < 0 {
+ return self.p, types.ParsingError(-e)
+ }
+ self.p = e
+ return s, 0
}
func (self *Node) encodeInterface(buf *[]byte) error {
@@ -70,17 +89,16 @@ func (self *Searcher) GetByPath(path ...interface{}) (Node, error) {
var err types.ParsingError
for _, p := range path {
- switch p := p.(type) {
- case int:
- if err = self.parser.searchIndex(p); err != 0 {
+ if idx, ok := p.(int); ok && idx >= 0 {
+ if err = self.parser.searchIndex(idx); err != 0 {
return Node{}, self.parser.ExportError(err)
}
- case string:
- if err = self.parser.searchKey(p); err != 0 {
+ } else if key, ok := p.(string); ok {
+ if err = self.parser.searchKey(key); err != 0 {
return Node{}, self.parser.ExportError(err)
}
- default:
- panic("path must be either int or string")
+ } else {
+ panic("path must be either int(>=0) or string")
}
}
diff --git a/vendor/github.com/bytedance/sonic/ast/decode.go b/vendor/github.com/bytedance/sonic/ast/decode.go
index d54e98318..6a5f6fea3 100644
--- a/vendor/github.com/bytedance/sonic/ast/decode.go
+++ b/vendor/github.com/bytedance/sonic/ast/decode.go
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2022 ByteDance Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package ast
import (
@@ -24,6 +40,7 @@ func isSpace(c byte) bool {
return (int(1<<c) & _blankCharsMask) != 0
}
+//go:nocheckptr
func skipBlank(src string, pos int) int {
se := uintptr(rt.IndexChar(src, len(src)))
sp := uintptr(rt.IndexChar(src, pos))
@@ -77,6 +94,7 @@ func decodeFalse(src string, pos int) (ret int) {
return -int(types.ERR_INVALID_CHAR)
}
+//go:nocheckptr
func decodeString(src string, pos int) (ret int, v string) {
ret, ep := skipString(src, pos)
if ep == -1 {
@@ -112,6 +130,7 @@ func isDigit(c byte) bool {
return c >= '0' && c <= '9'
}
+//go:nocheckptr
func decodeInt64(src string, pos int) (ret int, v int64, err error) {
sp := uintptr(rt.IndexChar(src, pos))
ss := uintptr(sp)
@@ -161,6 +180,7 @@ func isNumberChars(c byte) bool {
return (c >= '0' && c <= '9') || c == '+' || c == '-' || c == 'e' || c == 'E' || c == '.'
}
+//go:nocheckptr
func decodeFloat64(src string, pos int) (ret int, v float64, err error) {
sp := uintptr(rt.IndexChar(src, pos))
ss := uintptr(sp)
@@ -255,6 +275,7 @@ func decodeValue(src string, pos int) (ret int, v types.JsonState) {
}
}
+//go:nocheckptr
func skipNumber(src string, pos int) (ret int) {
sp := uintptr(rt.IndexChar(src, pos))
se := uintptr(rt.IndexChar(src, len(src)))
@@ -281,7 +302,7 @@ func skipNumber(src string, pos int) (ret int) {
} else if nextNeedDigit {
return -int(types.ERR_INVALID_CHAR)
} else if c == '.' {
- if !lastIsDigit || pointer || sp == ss {
+ if !lastIsDigit || pointer || exponent || sp == ss {
return -int(types.ERR_INVALID_CHAR)
}
pointer = true
@@ -319,6 +340,7 @@ func skipNumber(src string, pos int) (ret int) {
return int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr))
}
+//go:nocheckptr
func skipString(src string, pos int) (ret int, ep int) {
if pos+1 >= len(src) {
return -int(types.ERR_EOF), -1
@@ -327,6 +349,7 @@ func skipString(src string, pos int) (ret int, ep int) {
sp := uintptr(rt.IndexChar(src, pos))
se := uintptr(rt.IndexChar(src, len(src)))
+ // not start with quote
if *(*byte)(unsafe.Pointer(sp)) != '"' {
return -int(types.ERR_INVALID_CHAR), -1
}
@@ -344,18 +367,16 @@ func skipString(src string, pos int) (ret int, ep int) {
}
sp += 1
if c == '"' {
- break
+ return int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr)), ep
}
}
- if sp > se {
- return -int(types.ERR_EOF), -1
- }
-
runtime.KeepAlive(src)
- return int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr)), ep
+ // not found the closed quote until EOF
+ return -int(types.ERR_EOF), -1
}
+//go:nocheckptr
func skipPair(src string, pos int, lchar byte, rchar byte) (ret int) {
if pos+1 >= len(src) {
return -int(types.ERR_EOF)
@@ -403,7 +424,7 @@ func skipPair(src string, pos int, lchar byte, rchar byte) (ret int) {
return int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr))
}
-func skipValue(src string, pos int) (ret int, start int) {
+func skipValueFast(src string, pos int) (ret int, start int) {
pos = skipBlank(src, pos)
if pos < 0 {
return pos, -1
@@ -428,3 +449,127 @@ func skipValue(src string, pos int) (ret int, start int) {
}
return ret, pos
}
+
+func skipValue(src string, pos int) (ret int, start int) {
+ pos = skipBlank(src, pos)
+ if pos < 0 {
+ return pos, -1
+ }
+ switch c := src[pos]; c {
+ case 'n':
+ ret = decodeNull(src, pos)
+ case '"':
+ ret, _ = skipString(src, pos)
+ case '{':
+ ret, _ = skipObject(src, pos)
+ case '[':
+ ret, _ = skipArray(src, pos)
+ case 't':
+ ret = decodeTrue(src, pos)
+ case 'f':
+ ret = decodeFalse(src, pos)
+ case '-', '+', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
+ ret = skipNumber(src, pos)
+ default:
+ ret = -int(types.ERR_INVALID_CHAR)
+ }
+ return ret, pos
+}
+
+func skipObject(src string, pos int) (ret int, start int) {
+ start = skipBlank(src, pos)
+ if start < 0 {
+ return start, -1
+ }
+
+ if src[start] != '{' {
+ return -int(types.ERR_INVALID_CHAR), -1
+ }
+
+ pos = start + 1
+ pos = skipBlank(src, pos)
+ if pos < 0 {
+ return pos, -1
+ }
+ if src[pos] == '}' {
+ return pos + 1, start
+ }
+
+ for {
+ pos, _ = skipString(src, pos)
+ if pos < 0 {
+ return pos, -1
+ }
+
+ pos = skipBlank(src, pos)
+ if pos < 0 {
+ return pos, -1
+ }
+ if src[pos] != ':' {
+ return -int(types.ERR_INVALID_CHAR), -1
+ }
+
+ pos++
+ pos, _ = skipValue(src, pos)
+ if pos < 0 {
+ return pos, -1
+ }
+
+ pos = skipBlank(src, pos)
+ if pos < 0 {
+ return pos, -1
+ }
+ if src[pos] == '}' {
+ return pos + 1, start
+ }
+ if src[pos] != ',' {
+ return -int(types.ERR_INVALID_CHAR), -1
+ }
+
+ pos++
+ pos = skipBlank(src, pos)
+ if pos < 0 {
+ return pos, -1
+ }
+
+ }
+}
+
+func skipArray(src string, pos int) (ret int, start int) {
+ start = skipBlank(src, pos)
+ if start < 0 {
+ return start, -1
+ }
+
+ if src[start] != '[' {
+ return -int(types.ERR_INVALID_CHAR), -1
+ }
+
+ pos = start + 1
+ pos = skipBlank(src, pos)
+ if pos < 0 {
+ return pos, -1
+ }
+ if src[pos] == ']' {
+ return pos + 1, start
+ }
+
+ for {
+ pos, _ = skipValue(src, pos)
+ if pos < 0 {
+ return pos, -1
+ }
+
+ pos = skipBlank(src, pos)
+ if pos < 0 {
+ return pos, -1
+ }
+ if src[pos] == ']' {
+ return pos + 1, start
+ }
+ if src[pos] != ',' {
+ return -int(types.ERR_INVALID_CHAR), -1
+ }
+ pos++
+ }
+}
diff --git a/vendor/github.com/bytedance/sonic/ast/node.go b/vendor/github.com/bytedance/sonic/ast/node.go
index 0d37baf12..6b5ad8a3e 100644
--- a/vendor/github.com/bytedance/sonic/ast/node.go
+++ b/vendor/github.com/bytedance/sonic/ast/node.go
@@ -1541,13 +1541,19 @@ var (
emptyObjectNode = Node{t: types.V_OBJECT}
)
-// NewRaw creates a node of raw json, and decides its type by first char.
+// NewRaw creates a node of raw json.
+// If the input json is invalid, NewRaw returns a error Node.
func NewRaw(json string) Node {
- if json == "" {
- panic("empty json string")
+ parser := NewParser(json)
+ start, err := parser.skip()
+ if err != 0 {
+ return *newError(err, err.Message())
+ }
+ it := switchRawType(parser.s[start])
+ if it == _V_NONE {
+ return Node{}
}
- it := switchRawType(json[0])
- return newRawNode(json, it)
+ return newRawNode(parser.s[start:parser.p], it)
}
// NewAny creates a node of type V_ANY if any's type isn't Node or *Node,
diff --git a/vendor/github.com/bytedance/sonic/ast/parser.go b/vendor/github.com/bytedance/sonic/ast/parser.go
index ebb7bb097..0a8e7b068 100644
--- a/vendor/github.com/bytedance/sonic/ast/parser.go
+++ b/vendor/github.com/bytedance/sonic/ast/parser.go
@@ -350,7 +350,7 @@ func (self *Parser) searchKey(match string) types.ParsingError {
/* skip value */
if key != match {
- if _, err = self.skip(); err != 0 {
+ if _, err = self.skipFast(); err != 0 {
return err
}
} else {
@@ -398,7 +398,7 @@ func (self *Parser) searchIndex(idx int) types.ParsingError {
for i := 0; i < idx; i++ {
/* decode the value */
- if _, err = self.skip(); err != 0 {
+ if _, err = self.skipFast(); err != 0 {
return err
}