api_compat.go raw
1 // +build !amd64,!arm64 go1.25 !go1.17 arm64,!go1.20
2
3 /*
4 * Copyright 2022 ByteDance Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package ast
20
21 import (
22 `encoding/json`
23 `unicode/utf8`
24
25 `github.com/bytedance/sonic/internal/native/types`
26 `github.com/bytedance/sonic/internal/rt`
27 `github.com/bytedance/sonic/internal/compat`
28 )
29
30 func init() {
31 compat.Warn("sonic/ast")
32 }
33
34 func quote(buf *[]byte, val string) {
35 quoteString(buf, val)
36 }
37
38 // unquote unescapes an internal JSON string (it doesn't count quotas at the beginning and end)
39 func unquote(src string) (string, types.ParsingError) {
40 sp := rt.IndexChar(src, -1)
41 out, ok := unquoteBytes(rt.BytesFrom(sp, len(src)+2, len(src)+2))
42 if !ok {
43 return "", types.ERR_INVALID_ESCAPE
44 }
45 return rt.Mem2Str(out), 0
46 }
47
48
49 func (self *Parser) decodeValue() (val types.JsonState) {
50 e, v := decodeValue(self.s, self.p, self.dbuf == nil)
51 if e < 0 {
52 return v
53 }
54 self.p = e
55 return v
56 }
57
58 func (self *Parser) skip() (int, types.ParsingError) {
59 e, s := skipValue(self.s, self.p)
60 if e < 0 {
61 return self.p, types.ParsingError(-e)
62 }
63 self.p = e
64 return s, 0
65 }
66
67 func (self *Parser) skipFast() (int, types.ParsingError) {
68 e, s := skipValueFast(self.s, self.p)
69 if e < 0 {
70 return self.p, types.ParsingError(-e)
71 }
72 self.p = e
73 return s, 0
74 }
75
76 func (self *Node) encodeInterface(buf *[]byte) error {
77 out, err := json.Marshal(self.packAny())
78 if err != nil {
79 return err
80 }
81 *buf = append(*buf, out...)
82 return nil
83 }
84
85 func (self *Parser) getByPath(validate bool, path ...interface{}) (int, types.ParsingError) {
86 for _, p := range path {
87 if idx, ok := p.(int); ok && idx >= 0 {
88 if err := self.searchIndex(idx); err != 0 {
89 return self.p, err
90 }
91 } else if key, ok := p.(string); ok {
92 if err := self.searchKey(key); err != 0 {
93 return self.p, err
94 }
95 } else {
96 panic("path must be either int(>=0) or string")
97 }
98 }
99
100 var start int
101 var e types.ParsingError
102 if validate {
103 start, e = self.skip()
104 } else {
105 start, e = self.skipFast()
106 }
107 if e != 0 {
108 return self.p, e
109 }
110 return start, 0
111 }
112
113 func validate_utf8(str string) bool {
114 return utf8.ValidString(str)
115 }
116