...

Source file src/github.com/bytedance/sonic/ast/api_compat.go

Documentation: github.com/bytedance/sonic/ast

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

View as plain text