gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

api_amd64.go (4124B)


      1 // +build amd64,go1.15,!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     `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     self.p = native.Value(sv.Ptr, sv.Len, self.p, &val, 0)
     91     return
     92 }
     93 
     94 func (self *Parser) skip() (int, types.ParsingError) {
     95     fsm := types.NewStateMachine()
     96     start := native.SkipOne(&self.s, &self.p, fsm, 0)
     97     types.FreeStateMachine(fsm)
     98 
     99     if start < 0 {
    100         return self.p, types.ParsingError(-start)
    101     }
    102     return start, 0
    103 }
    104 
    105 func (self *Node) encodeInterface(buf *[]byte) error {
    106     //WARN: NOT compatible with json.Encoder
    107     return encoder.EncodeInto(buf, self.packAny(), 0)
    108 }
    109 
    110 func (self *Parser) skipFast() (int, types.ParsingError) {
    111     start := native.SkipOneFast(&self.s, &self.p)
    112     if start < 0 {
    113         return self.p, types.ParsingError(-start)
    114     }
    115     return start, 0
    116 }
    117 
    118 func (self *Parser) getByPath(path ...interface{}) (int, types.ParsingError) {
    119     fsm := types.NewStateMachine()
    120     start := native.GetByPath(&self.s, &self.p, &path, fsm)
    121     types.FreeStateMachine(fsm)
    122     runtime.KeepAlive(path)
    123     if start < 0 {
    124         return self.p, types.ParsingError(-start)
    125     }
    126     return start, 0
    127 }
    128 
    129 func (self *Searcher) GetByPath(path ...interface{}) (Node, error) {
    130     var err types.ParsingError
    131     var start int
    132 
    133     self.parser.p = 0
    134     start, err = self.parser.getByPath(path...)
    135     if err != 0 {
    136         // for compatibility with old version
    137         if err == types.ERR_NOT_FOUND {
    138             return Node{}, ErrNotExist
    139         }
    140         if err == types.ERR_UNSUPPORT_TYPE {
    141             panic("path must be either int(>=0) or string")
    142         }
    143         return Node{}, self.parser.syntaxError(err)
    144     }
    145 
    146     t := switchRawType(self.parser.s[start])
    147     if t == _V_NONE {
    148         return Node{}, self.parser.ExportError(err)
    149     }
    150     return newRawNode(self.parser.s[start:self.parser.p], t), nil
    151 }