api_compat.go (3195B)
1 // +build !amd64 go1.21 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/base64` 23 `encoding/json` 24 `fmt` 25 26 `github.com/bytedance/sonic/internal/native/types` 27 `github.com/bytedance/sonic/internal/rt` 28 ) 29 30 func quote(buf *[]byte, val string) { 31 quoteString(buf, val) 32 } 33 34 func unquote(src string) (string, types.ParsingError) { 35 sp := rt.IndexChar(src, -1) 36 out, ok := unquoteBytes(rt.BytesFrom(sp, len(src)+2, len(src)+2)) 37 if !ok { 38 return "", types.ERR_INVALID_ESCAPE 39 } 40 return rt.Mem2Str(out), 0 41 } 42 43 func decodeBase64(src string) ([]byte, error) { 44 return base64.StdEncoding.DecodeString(src) 45 } 46 47 func encodeBase64(src []byte) string { 48 return base64.StdEncoding.EncodeToString(src) 49 } 50 51 func (self *Parser) decodeValue() (val types.JsonState) { 52 e, v := decodeValue(self.s, self.p) 53 if e < 0 { 54 return v 55 } 56 self.p = e 57 return v 58 } 59 60 func (self *Parser) skip() (int, types.ParsingError) { 61 e, s := skipValue(self.s, self.p) 62 if e < 0 { 63 return self.p, types.ParsingError(-e) 64 } 65 self.p = e 66 return s, 0 67 } 68 69 func (self *Parser) skipFast() (int, types.ParsingError) { 70 e, s := skipValueFast(self.s, self.p) 71 if e < 0 { 72 return self.p, types.ParsingError(-e) 73 } 74 self.p = e 75 return s, 0 76 } 77 78 func (self *Node) encodeInterface(buf *[]byte) error { 79 out, err := json.Marshal(self.packAny()) 80 if err != nil { 81 return err 82 } 83 *buf = append(*buf, out...) 84 return nil 85 } 86 87 func (self *Searcher) GetByPath(path ...interface{}) (Node, error) { 88 self.parser.p = 0 89 90 var err types.ParsingError 91 for _, p := range path { 92 if idx, ok := p.(int); ok && idx >= 0 { 93 if err = self.parser.searchIndex(idx); err != 0 { 94 return Node{}, self.parser.ExportError(err) 95 } 96 } else if key, ok := p.(string); ok { 97 if err = self.parser.searchKey(key); err != 0 { 98 return Node{}, self.parser.ExportError(err) 99 } 100 } else { 101 panic("path must be either int(>=0) or string") 102 } 103 } 104 105 var start = self.parser.p 106 if start, err = self.parser.skip(); err != 0 { 107 return Node{}, self.parser.ExportError(err) 108 } 109 ns := len(self.parser.s) 110 if self.parser.p > ns || start >= ns || start>=self.parser.p { 111 return Node{}, fmt.Errorf("skip %d char out of json boundary", start) 112 } 113 114 t := switchRawType(self.parser.s[start]) 115 if t == _V_NONE { 116 return Node{}, self.parser.ExportError(err) 117 } 118 119 return newRawNode(self.parser.s[start:self.parser.p], t), nil 120 }