summaryrefslogtreecommitdiff
path: root/vendor/github.com/bytedance/sonic/ast
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/bytedance/sonic/ast')
-rw-r--r--vendor/github.com/bytedance/sonic/ast/api.go65
-rw-r--r--vendor/github.com/bytedance/sonic/ast/api_compat.go14
-rw-r--r--vendor/github.com/bytedance/sonic/ast/decode.go17
-rw-r--r--vendor/github.com/bytedance/sonic/ast/encode.go10
-rw-r--r--vendor/github.com/bytedance/sonic/ast/node.go4
-rw-r--r--vendor/github.com/bytedance/sonic/ast/parser.go16
-rw-r--r--vendor/github.com/bytedance/sonic/ast/stubs.go3
-rw-r--r--vendor/github.com/bytedance/sonic/ast/visitor.go5
8 files changed, 41 insertions, 93 deletions
diff --git a/vendor/github.com/bytedance/sonic/ast/api.go b/vendor/github.com/bytedance/sonic/ast/api.go
index 36151f270..b9d3c58ee 100644
--- a/vendor/github.com/bytedance/sonic/ast/api.go
+++ b/vendor/github.com/bytedance/sonic/ast/api.go
@@ -1,5 +1,5 @@
-//go:build (amd64 && go1.17 && !go1.25) || (arm64 && go1.20 && !go1.25)
-// +build amd64,go1.17,!go1.25 arm64,go1.20,!go1.25
+//go:build (amd64 && go1.17 && !go1.26) || (arm64 && go1.20 && !go1.26)
+// +build amd64,go1.17,!go1.26 arm64,go1.20,!go1.26
/*
* Copyright 2022 ByteDance Inc.
@@ -20,62 +20,21 @@
package ast
import (
- `runtime`
- `unsafe`
-
- `github.com/bytedance/sonic/encoder`
- `github.com/bytedance/sonic/internal/native`
- `github.com/bytedance/sonic/internal/native/types`
- `github.com/bytedance/sonic/internal/rt`
- uq `github.com/bytedance/sonic/unquote`
- `github.com/bytedance/sonic/utf8`
+ "runtime"
+ "unsafe"
+
+ "github.com/bytedance/sonic/encoder"
+ "github.com/bytedance/sonic/internal/encoder/alg"
+ "github.com/bytedance/sonic/internal/native"
+ "github.com/bytedance/sonic/internal/native/types"
+ "github.com/bytedance/sonic/internal/rt"
+ "github.com/bytedance/sonic/utf8"
)
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)
- nb := len(val)
- b := (*rt.GoSlice)(unsafe.Pointer(buf))
-
- // input buffer
- for nb > 0 {
- // output buffer
- dp := unsafe.Pointer(uintptr(b.Ptr) + uintptr(b.Len))
- dn := b.Cap - b.Len
- // call native.Quote, dn is byte count it outputs
- ret := native.Quote(sp, nb, dp, &dn, 0)
- // update *buf length
- b.Len += dn
-
- // no need more output
- if ret >= 0 {
- break
- }
-
- // double buf size
- *b = rt.GrowSlice(typeByte, *b, b.Cap*2)
- // ret is the complement of consumed input
- ret = ^ret
- // update input buffer
- nb -= ret
- sp = unsafe.Pointer(uintptr(sp) + uintptr(ret))
- }
-
- runtime.KeepAlive(buf)
- runtime.KeepAlive(sp)
- *buf = append(*buf, '"')
-}
-
-func unquote(src string) (string, types.ParsingError) {
- return uq.String(src)
+ *buf = alg.Quote(*buf, val, false)
}
func (self *Parser) decodeValue() (val types.JsonState) {
diff --git a/vendor/github.com/bytedance/sonic/ast/api_compat.go b/vendor/github.com/bytedance/sonic/ast/api_compat.go
index 74119fed6..c6a540cbf 100644
--- a/vendor/github.com/bytedance/sonic/ast/api_compat.go
+++ b/vendor/github.com/bytedance/sonic/ast/api_compat.go
@@ -1,4 +1,4 @@
-// +build !amd64,!arm64 go1.25 !go1.17 arm64,!go1.20
+// +build !amd64,!arm64 go1.26 !go1.17 arm64,!go1.20
/*
* Copyright 2022 ByteDance Inc.
@@ -23,7 +23,6 @@ import (
`unicode/utf8`
`github.com/bytedance/sonic/internal/native/types`
- `github.com/bytedance/sonic/internal/rt`
`github.com/bytedance/sonic/internal/compat`
)
@@ -35,17 +34,6 @@ func quote(buf *[]byte, val string) {
quoteString(buf, val)
}
-// unquote unescapes an internal JSON string (it doesn't count quotas at the beginning and end)
-func unquote(src string) (string, types.ParsingError) {
- sp := rt.IndexChar(src, -1)
- out, ok := unquoteBytes(rt.BytesFrom(sp, len(src)+2, len(src)+2))
- if !ok {
- return "", types.ERR_INVALID_ESCAPE
- }
- return rt.Mem2Str(out), 0
-}
-
-
func (self *Parser) decodeValue() (val types.JsonState) {
e, v := decodeValue(self.s, self.p, self.dbuf == nil)
if e < 0 {
diff --git a/vendor/github.com/bytedance/sonic/ast/decode.go b/vendor/github.com/bytedance/sonic/ast/decode.go
index 135ee6eb8..45f5e2d2b 100644
--- a/vendor/github.com/bytedance/sonic/ast/decode.go
+++ b/vendor/github.com/bytedance/sonic/ast/decode.go
@@ -25,10 +25,9 @@ import (
"github.com/bytedance/sonic/internal/native/types"
"github.com/bytedance/sonic/internal/rt"
"github.com/bytedance/sonic/internal/utils"
+ "github.com/bytedance/sonic/unquote"
)
-// Hack: this is used for both checking space and cause friendly compile errors in 32-bit arch.
-const _Sonic_Not_Support_32Bit_Arch__Checking_32Bit_Arch_Here = (1 << ' ') | (1 << '\t') | (1 << '\r') | (1 << '\n')
var bytesNull = []byte("null")
@@ -40,17 +39,13 @@ const (
bytesArray = "[]"
)
-func isSpace(c byte) bool {
- return (int(1<<c) & _Sonic_Not_Support_32Bit_Arch__Checking_32Bit_Arch_Here) != 0
-}
-
//go:nocheckptr
func skipBlank(src string, pos int) int {
se := uintptr(rt.IndexChar(src, len(src)))
sp := uintptr(rt.IndexChar(src, pos))
for sp < se {
- if !isSpace(*(*byte)(unsafe.Pointer(sp))) {
+ if !utils.IsSpace(*(*byte)(unsafe.Pointer(sp))) {
break
}
sp += 1
@@ -107,13 +102,13 @@ func decodeString(src string, pos int) (ret int, v string) {
return ret, v
}
- vv, ok := unquoteBytes(rt.Str2Mem(src[pos:ret]))
- if !ok {
+ result, err := unquote.String(src[pos:ret])
+ if err != 0 {
return -int(types.ERR_INVALID_CHAR), ""
}
runtime.KeepAlive(src)
- return ret, rt.Mem2Str(vv)
+ return ret, result
}
func decodeBinary(src string, pos int) (ret int, v []byte) {
@@ -549,7 +544,7 @@ func _DecodeString(src string, pos int, needEsc bool, validStr bool) (v string,
return str, p.p, true
}
/* unquote the string */
- out, err := unquote(str)
+ out, err := unquote.String(str)
/* check for errors */
if err != 0 {
return "", -int(err), true
diff --git a/vendor/github.com/bytedance/sonic/ast/encode.go b/vendor/github.com/bytedance/sonic/ast/encode.go
index eae0bd258..9401a6610 100644
--- a/vendor/github.com/bytedance/sonic/ast/encode.go
+++ b/vendor/github.com/bytedance/sonic/ast/encode.go
@@ -20,8 +20,9 @@ import (
"sync"
"unicode/utf8"
+ "github.com/bytedance/gopkg/lang/dirtmake"
"github.com/bytedance/sonic/internal/rt"
- "github.com/bytedance/sonic/option"
+ "github.com/bytedance/sonic/option"
)
func quoteString(e *[]byte, s string) {
@@ -95,6 +96,11 @@ func (self *Node) MarshalJSON() ([]byte, error) {
return bytesNull, nil
}
+ // fast path for raw node
+ if self.isRaw() {
+ return rt.Str2Mem(self.toString()), nil
+ }
+
buf := newBuffer()
err := self.encode(buf)
if err != nil {
@@ -105,7 +111,7 @@ func (self *Node) MarshalJSON() ([]byte, error) {
if !rt.CanSizeResue(cap(*buf)) {
ret = *buf
} else {
- ret = make([]byte, len(*buf))
+ ret = dirtmake.Bytes(len(*buf), len(*buf))
copy(ret, *buf)
freeBuffer(buf)
}
diff --git a/vendor/github.com/bytedance/sonic/ast/node.go b/vendor/github.com/bytedance/sonic/ast/node.go
index 1c5ff6439..6ea5f52ae 100644
--- a/vendor/github.com/bytedance/sonic/ast/node.go
+++ b/vendor/github.com/bytedance/sonic/ast/node.go
@@ -64,8 +64,8 @@ type Node struct {
// UnmarshalJSON is just an adapter to json.Unmarshaler.
// If you want better performance, use Searcher.GetByPath() directly
func (self *Node) UnmarshalJSON(data []byte) (err error) {
- *self = NewRaw(string(data))
- return self.Check()
+ *self = newRawNode(rt.Mem2Str(data), switchRawType(data[0]), false)
+ return nil
}
/** Node Type Accessor **/
diff --git a/vendor/github.com/bytedance/sonic/ast/parser.go b/vendor/github.com/bytedance/sonic/ast/parser.go
index aee96f86a..f10b43eaf 100644
--- a/vendor/github.com/bytedance/sonic/ast/parser.go
+++ b/vendor/github.com/bytedance/sonic/ast/parser.go
@@ -23,6 +23,8 @@ import (
"github.com/bytedance/sonic/internal/native/types"
"github.com/bytedance/sonic/internal/rt"
+ "github.com/bytedance/sonic/internal/utils"
+ "github.com/bytedance/sonic/unquote"
)
const (
@@ -113,13 +115,13 @@ func (self *Parser) array() types.ParsingError {
func (self *Parser) lspace(sp int) int {
ns := len(self.s)
- for ; sp<ns && isSpace(self.s[sp]); sp+=1 {}
+ for ; sp<ns && utils.IsSpace(self.s[sp]); sp+=1 {}
return sp
}
func (self *Parser) backward() {
- for ; self.p >= 0 && isSpace(self.s[self.p]); self.p-=1 {}
+ for ; self.p >= 0 && utils.IsSpace(self.s[self.p]); self.p-=1 {}
}
func (self *Parser) decodeArray(ret *linkedNodes) (Node, types.ParsingError) {
@@ -217,7 +219,7 @@ func (self *Parser) decodeObject(ret *linkedPairs) (Node, types.ParsingError) {
/* check for escape sequence */
if njs.Ep != -1 {
- if key, err = unquote(key); err != 0 {
+ if key, err = unquote.String(key); err != 0 {
return Node{}, err
}
}
@@ -282,7 +284,7 @@ func (self *Parser) decodeString(iv int64, ep int) (Node, types.ParsingError) {
}
/* unquote the string */
- out, err := unquote(s)
+ out, err := unquote.String(s)
/* check for errors */
if err != 0 {
@@ -392,7 +394,7 @@ func (self *Parser) searchKey(match string) types.ParsingError {
/* check for escape sequence */
if njs.Ep != -1 {
- if key, err = unquote(key); err != 0 {
+ if key, err = unquote.String(key); err != 0 {
return err
}
}
@@ -573,7 +575,7 @@ func (self *Node) skipNextPair() (*Pair) {
/* check for escape sequence */
if njs.Ep != -1 {
- if key, err = unquote(key); err != 0 {
+ if key, err = unquote.String(key); err != 0 {
return newErrorPair(parser.syntaxError(err))
}
}
@@ -692,7 +694,7 @@ func (self *Parser) ExportError(err types.ParsingError) error {
}
func backward(src string, i int) int {
- for ; i>=0 && isSpace(src[i]); i-- {}
+ for ; i>=0 && utils.IsSpace(src[i]); i-- {}
return i
}
diff --git a/vendor/github.com/bytedance/sonic/ast/stubs.go b/vendor/github.com/bytedance/sonic/ast/stubs.go
index 9991cc89e..6ba1d7eba 100644
--- a/vendor/github.com/bytedance/sonic/ast/stubs.go
+++ b/vendor/github.com/bytedance/sonic/ast/stubs.go
@@ -26,6 +26,3 @@ import (
func mem2ptr(s []byte) unsafe.Pointer {
return (*rt.GoSlice)(unsafe.Pointer(&s)).Ptr
}
-
-//go:linkname unquoteBytes encoding/json.unquoteBytes
-func unquoteBytes(s []byte) (t []byte, ok bool)
diff --git a/vendor/github.com/bytedance/sonic/ast/visitor.go b/vendor/github.com/bytedance/sonic/ast/visitor.go
index fc71d40cb..53faeb9c0 100644
--- a/vendor/github.com/bytedance/sonic/ast/visitor.go
+++ b/vendor/github.com/bytedance/sonic/ast/visitor.go
@@ -21,6 +21,7 @@ import (
`errors`
`github.com/bytedance/sonic/internal/native/types`
+ `github.com/bytedance/sonic/unquote`
)
// Visitor handles the callbacks during preorder traversal of a JSON AST.
@@ -270,7 +271,7 @@ func (self *traverser) decodeObject() error {
/* check for escape sequence */
if njs.Ep != -1 {
- if key, err = unquote(key); err != 0 {
+ if key, err = unquote.String(key); err != 0 {
return err
}
}
@@ -320,7 +321,7 @@ func (self *traverser) decodeString(iv int64, ep int) error {
}
/* unquote the string */
- out, err := unquote(s)
+ out, err := unquote.String(s)
if err != 0 {
return err
}