github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/ast/api_compat.go (about)

     1  //go:build !amd64 || !go1.16 || go1.22
     2  // +build !amd64 !go1.16 go1.22
     3  
     4  /*
     5   * Copyright 2022 ByteDance Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package ast
    21  
    22  import (
    23  	"encoding/base64"
    24  	"encoding/json"
    25  	"fmt"
    26  
    27  	"github.com/goshafaq/sonic/internal/native/types"
    28  	"github.com/goshafaq/sonic/internal/rt"
    29  )
    30  
    31  func init() {
    32  	println("WARNING: sonic only supports Go1.16~1.20 && CPU amd64, but your environment is not suitable")
    33  }
    34  
    35  func quote(buf *[]byte, val string) {
    36  	quoteString(buf, val)
    37  }
    38  
    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  func decodeBase64(src string) ([]byte, error) {
    49  	return base64.StdEncoding.DecodeString(src)
    50  }
    51  
    52  func encodeBase64(src []byte) string {
    53  	return base64.StdEncoding.EncodeToString(src)
    54  }
    55  
    56  func (self *Parser) decodeValue() (val types.JsonState) {
    57  	e, v := decodeValue(self.s, self.p, self.dbuf == nil)
    58  	if e < 0 {
    59  		return v
    60  	}
    61  	self.p = e
    62  	return v
    63  }
    64  
    65  func (self *Parser) skip() (int, types.ParsingError) {
    66  	e, s := skipValue(self.s, self.p)
    67  	if e < 0 {
    68  		return self.p, types.ParsingError(-e)
    69  	}
    70  	self.p = e
    71  	return s, 0
    72  }
    73  
    74  func (self *Parser) skipFast() (int, types.ParsingError) {
    75  	e, s := skipValueFast(self.s, self.p)
    76  	if e < 0 {
    77  		return self.p, types.ParsingError(-e)
    78  	}
    79  	self.p = e
    80  	return s, 0
    81  }
    82  
    83  func (self *Node) encodeInterface(buf *[]byte) error {
    84  	out, err := json.Marshal(self.packAny())
    85  	if err != nil {
    86  		return err
    87  	}
    88  	*buf = append(*buf, out...)
    89  	return nil
    90  }
    91  
    92  func (self *Searcher) GetByPath(path ...interface{}) (Node, error) {
    93  	self.parser.p = 0
    94  
    95  	var err types.ParsingError
    96  	for _, p := range path {
    97  		if idx, ok := p.(int); ok && idx >= 0 {
    98  			if err = self.parser.searchIndex(idx); err != 0 {
    99  				return Node{}, self.parser.ExportError(err)
   100  			}
   101  		} else if key, ok := p.(string); ok {
   102  			if err = self.parser.searchKey(key); err != 0 {
   103  				return Node{}, self.parser.ExportError(err)
   104  			}
   105  		} else {
   106  			panic("path must be either int(>=0) or string")
   107  		}
   108  	}
   109  
   110  	var start = self.parser.p
   111  	if start, err = self.parser.skip(); err != 0 {
   112  		return Node{}, self.parser.ExportError(err)
   113  	}
   114  	ns := len(self.parser.s)
   115  	if self.parser.p > ns || start >= ns || start >= self.parser.p {
   116  		return Node{}, fmt.Errorf("skip %d char out of json boundary", start)
   117  	}
   118  
   119  	t := switchRawType(self.parser.s[start])
   120  	if t == _V_NONE {
   121  		return Node{}, self.parser.ExportError(err)
   122  	}
   123  
   124  	return newRawNode(self.parser.s[start:self.parser.p], t), nil
   125  }