...

Source file src/github.com/bytedance/sonic/ast/api_amd64.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      `runtime`
    23      `unsafe`
    24  
    25      `github.com/bytedance/sonic/encoder`
    26      `github.com/bytedance/sonic/internal/native`
    27      `github.com/bytedance/sonic/internal/native/types`
    28      `github.com/bytedance/sonic/internal/rt`
    29      uq `github.com/bytedance/sonic/unquote`
    30      `github.com/chenzhuoyu/base64x`
    31  )
    32  
    33  var typeByte = rt.UnpackEface(byte(0)).Type
    34  
    35  //go:nocheckptr
    36  func quote(buf *[]byte, val string) {
    37      *buf = append(*buf, '"')
    38      if len(val) == 0 {
    39          *buf = append(*buf, '"')
    40          return
    41      }
    42  
    43      sp := rt.IndexChar(val, 0)
    44      nb := len(val)
    45      b := (*rt.GoSlice)(unsafe.Pointer(buf))
    46  
    47      // input buffer
    48      for nb > 0 {
    49          // output buffer
    50          dp := unsafe.Pointer(uintptr(b.Ptr) + uintptr(b.Len))
    51          dn := b.Cap - b.Len
    52          // call native.Quote, dn is byte count it outputs
    53          ret := native.Quote(sp, nb, dp, &dn, 0)
    54          // update *buf length
    55          b.Len += dn
    56  
    57          // no need more output
    58          if ret >= 0 {
    59              break
    60          }
    61  
    62          // double buf size
    63          *b = growslice(typeByte, *b, b.Cap*2)
    64          // ret is the complement of consumed input
    65          ret = ^ret
    66          // update input buffer
    67          nb -= ret
    68          sp = unsafe.Pointer(uintptr(sp) + uintptr(ret))
    69      }
    70  
    71      runtime.KeepAlive(buf)
    72      runtime.KeepAlive(sp)
    73      *buf = append(*buf, '"')
    74  }
    75  
    76  func unquote(src string) (string, types.ParsingError) {
    77      return uq.String(src)
    78  }
    79  
    80  func decodeBase64(src string) ([]byte, error) {
    81      return base64x.StdEncoding.DecodeString(src)
    82  }
    83  
    84  func encodeBase64(src []byte) string {
    85      return base64x.StdEncoding.EncodeToString(src)
    86  }
    87  
    88  func (self *Parser) decodeValue() (val types.JsonState) {
    89      sv := (*rt.GoString)(unsafe.Pointer(&self.s))
    90      flag := types.F_USE_NUMBER
    91      if self.dbuf != nil {
    92          flag = 0
    93          val.Dbuf = self.dbuf
    94          val.Dcap = types.MaxDigitNums
    95      }
    96      self.p = native.Value(sv.Ptr, sv.Len, self.p, &val, uint64(flag))
    97      return
    98  }
    99  
   100  func (self *Parser) skip() (int, types.ParsingError) {
   101      fsm := types.NewStateMachine()
   102      start := native.SkipOne(&self.s, &self.p, fsm, 0)
   103      types.FreeStateMachine(fsm)
   104  
   105      if start < 0 {
   106          return self.p, types.ParsingError(-start)
   107      }
   108      return start, 0
   109  }
   110  
   111  func (self *Node) encodeInterface(buf *[]byte) error {
   112      //WARN: NOT compatible with json.Encoder
   113      return encoder.EncodeInto(buf, self.packAny(), 0)
   114  }
   115  
   116  func (self *Parser) skipFast() (int, types.ParsingError) {
   117      start := native.SkipOneFast(&self.s, &self.p)
   118      if start < 0 {
   119          return self.p, types.ParsingError(-start)
   120      }
   121      return start, 0
   122  }
   123  
   124  func (self *Parser) getByPath(path ...interface{}) (int, types.ParsingError) {
   125      fsm := types.NewStateMachine()
   126      start := native.GetByPath(&self.s, &self.p, &path, fsm)
   127      types.FreeStateMachine(fsm)
   128      runtime.KeepAlive(path)
   129      if start < 0 {
   130          return self.p, types.ParsingError(-start)
   131      }
   132      return start, 0
   133  }
   134  

View as plain text