github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/ast/api_compat.go (about)

     1  // +build !amd64,!arm64 go1.23 !go1.16 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  )
    28  
    29  func init() {
    30      println("WARNING:(ast) sonic only supports Go1.16~1.22, but your environment is not suitable")
    31  }
    32  
    33  func quote(buf *[]byte, val string) {
    34      quoteString(buf, val)
    35  }
    36  
    37  // unquote unescapes a internal JSON string (it doesn't count quotas at the begining and end)
    38  func unquote(src string) (string, types.ParsingError) {
    39      sp := rt.IndexChar(src, -1)
    40      out, ok := unquoteBytes(rt.BytesFrom(sp, len(src)+2, len(src)+2))
    41      if !ok {
    42          return "", types.ERR_INVALID_ESCAPE
    43      }
    44      return rt.Mem2Str(out), 0
    45  }
    46  
    47  
    48  func (self *Parser) decodeValue() (val types.JsonState) {
    49      e, v := decodeValue(self.s, self.p, self.dbuf == nil)
    50      if e < 0 {
    51          return v
    52      }
    53      self.p = e
    54      return v
    55  }
    56  
    57  func (self *Parser) skip() (int, types.ParsingError) {
    58      e, s := skipValue(self.s, self.p)
    59      if e < 0 {
    60          return self.p, types.ParsingError(-e)
    61      }
    62      self.p = e
    63      return s, 0
    64  }
    65  
    66  func (self *Parser) skipFast() (int, types.ParsingError) {
    67      e, s := skipValueFast(self.s, self.p)
    68      if e < 0 {
    69          return self.p, types.ParsingError(-e)
    70      }
    71      self.p = e
    72      return s, 0
    73  }
    74  
    75  func (self *Node) encodeInterface(buf *[]byte) error {
    76      out, err := json.Marshal(self.packAny())
    77      if err != nil {
    78          return err
    79      }
    80      *buf = append(*buf, out...)
    81      return nil
    82  }
    83  
    84  func (self *Parser) getByPath(validate bool, path ...interface{}) (int, types.ParsingError) {
    85      for _, p := range path {
    86          if idx, ok := p.(int); ok && idx >= 0 {
    87              if err := self.searchIndex(idx); err != 0 {
    88                  return self.p, err
    89              }
    90          } else if key, ok := p.(string); ok {
    91              if err := self.searchKey(key); err != 0 {
    92                  return self.p, err
    93              }
    94          } else {
    95              panic("path must be either int(>=0) or string")
    96          }
    97      }
    98  
    99      var start int
   100      var e types.ParsingError
   101      if validate {
   102          start, e = self.skip()
   103      } else {
   104          start, e = self.skipFast()
   105      }
   106      if e != 0 {
   107          return self.p, e
   108      }
   109      return start, 0
   110  }
   111  
   112  func validate_utf8(str string) bool {
   113      return utf8.ValidString(str)
   114  }