github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/thrift/binary_skip_amd64.go (about)

     1  //go:build amd64 && go1.16
     2  // +build amd64,go1.16
     3  
     4  /**
     5   * Copyright 2023 cloudwego 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 thrift
    21  
    22  import (
    23  	"io"
    24  
    25  	"github.com/cloudwego/dynamicgo/internal/native"
    26  	"github.com/cloudwego/dynamicgo/internal/native/types"
    27  )
    28  
    29  const MaxSkipDepth = types.TB_SKIP_STACK_SIZE-1
    30  
    31  // Skip skips over the value for the given type.
    32  func (p *BinaryProtocol) Skip(fieldType Type, useNative bool) (err error) {
    33  	if useNative {
    34  		return p.SkipNative(fieldType, MaxSkipDepth)
    35  	}
    36  	return p.SkipGo(fieldType, MaxSkipDepth)
    37  }
    38  
    39  // Skip skips over teh value for the given type using native C implementation.
    40  func (p *BinaryProtocol) SkipNative(fieldType Type, maxDepth int) (err error) {
    41  	if maxDepth >= types.TB_SKIP_STACK_SIZE {
    42  		return errExceedDepthLimit
    43  	}
    44  	left := len(p.Buf) - p.Read
    45  	if left <= 0 {
    46  		return io.EOF
    47  	}
    48  	fsm := types.NewTStateMachine()
    49  	ret := native.TBSkip(fsm, &p.Buf[p.Read], left, uint8(fieldType))
    50  	if ret < 0 {
    51  		return
    52  	}
    53  	p.Read += int(ret)
    54  	types.FreeTStateMachine(fsm)
    55  	return nil
    56  }