2024-03-25 10:31:19 +00:00
|
|
|
// +build !amd64,!arm64 go1.23 !go1.16 arm64,!go1.20
|
2023-02-25 12:12:40 +00:00
|
|
|
|
2023-06-01 22:20:16 +01:00
|
|
|
/*
|
2024-03-25 10:31:19 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2023-06-01 22:20:16 +01:00
|
|
|
|
2023-02-25 12:12:40 +00:00
|
|
|
package ast
|
|
|
|
|
|
|
|
import (
|
2024-03-25 10:31:19 +00:00
|
|
|
`encoding/json`
|
2023-02-25 12:12:40 +00:00
|
|
|
|
2024-03-25 10:31:19 +00:00
|
|
|
`github.com/bytedance/sonic/internal/native/types`
|
|
|
|
`github.com/bytedance/sonic/internal/rt`
|
2023-02-25 12:12:40 +00:00
|
|
|
)
|
|
|
|
|
2023-11-27 13:15:03 +00:00
|
|
|
func init() {
|
2024-03-25 10:31:19 +00:00
|
|
|
println("WARNING:(ast) sonic only supports Go1.16~1.22, but your environment is not suitable")
|
2023-11-27 13:15:03 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 12:12:40 +00:00
|
|
|
func quote(buf *[]byte, val string) {
|
2024-03-25 10:31:19 +00:00
|
|
|
quoteString(buf, val)
|
2023-02-25 12:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func unquote(src string) (string, types.ParsingError) {
|
2024-03-25 10:31:19 +00:00
|
|
|
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
|
2023-02-25 12:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (self *Parser) decodeValue() (val types.JsonState) {
|
2024-03-25 10:31:19 +00:00
|
|
|
e, v := decodeValue(self.s, self.p, self.dbuf == nil)
|
|
|
|
if e < 0 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
self.p = e
|
|
|
|
return v
|
2023-02-25 12:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Parser) skip() (int, types.ParsingError) {
|
2024-03-25 10:31:19 +00:00
|
|
|
e, s := skipValue(self.s, self.p)
|
|
|
|
if e < 0 {
|
|
|
|
return self.p, types.ParsingError(-e)
|
|
|
|
}
|
|
|
|
self.p = e
|
|
|
|
return s, 0
|
2023-02-25 12:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Parser) skipFast() (int, types.ParsingError) {
|
2024-03-25 10:31:19 +00:00
|
|
|
e, s := skipValueFast(self.s, self.p)
|
|
|
|
if e < 0 {
|
|
|
|
return self.p, types.ParsingError(-e)
|
|
|
|
}
|
|
|
|
self.p = e
|
|
|
|
return s, 0
|
2023-02-25 12:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Node) encodeInterface(buf *[]byte) error {
|
2024-03-25 10:31:19 +00:00
|
|
|
out, err := json.Marshal(self.packAny())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*buf = append(*buf, out...)
|
|
|
|
return nil
|
2023-02-25 12:12:40 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 10:12:06 +00:00
|
|
|
func (self *Parser) getByPath(path ...interface{}) (int, types.ParsingError) {
|
2023-02-25 12:12:40 +00:00
|
|
|
for _, p := range path {
|
2023-06-01 22:20:16 +01:00
|
|
|
if idx, ok := p.(int); ok && idx >= 0 {
|
2024-03-25 10:31:19 +00:00
|
|
|
if err := self.searchIndex(idx); err != 0 {
|
|
|
|
return self.p, err
|
2023-02-25 12:12:40 +00:00
|
|
|
}
|
2023-06-01 22:20:16 +01:00
|
|
|
} else if key, ok := p.(string); ok {
|
2024-03-25 10:31:19 +00:00
|
|
|
if err := self.searchKey(key); err != 0 {
|
|
|
|
return self.p, err
|
2023-02-25 12:12:40 +00:00
|
|
|
}
|
2023-06-01 22:20:16 +01:00
|
|
|
} else {
|
|
|
|
panic("path must be either int(>=0) or string")
|
2023-02-25 12:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-25 10:31:19 +00:00
|
|
|
start, e := self.skip()
|
|
|
|
if e != 0 {
|
|
|
|
return self.p, e
|
2023-02-25 12:12:40 +00:00
|
|
|
}
|
2024-03-25 10:31:19 +00:00
|
|
|
// t := switchRawType(self.s[start])
|
|
|
|
// if t == _V_NUMBER {
|
|
|
|
// self.p = 1 + backward(self.s, self.p-1)
|
|
|
|
// }
|
2024-03-11 10:12:06 +00:00
|
|
|
return start, 0
|
|
|
|
}
|