github.com/pingcap/tidb/parser@v0.0.0-20231013125129-93a834a6bf8d/test_driver/test_driver_helper.go (about)

     1  // Copyright 2019 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  //go:build !codes
    15  // +build !codes
    16  
    17  package test_driver
    18  
    19  import (
    20  	"math"
    21  )
    22  
    23  func isSpace(c byte) bool {
    24  	return c == ' ' || c == '\t'
    25  }
    26  
    27  func isDigit(c byte) bool {
    28  	return c >= '0' && c <= '9'
    29  }
    30  
    31  func myMin(a, b int) int {
    32  	if a < b {
    33  		return a
    34  	}
    35  	return b
    36  }
    37  
    38  func pow10(x int) int32 {
    39  	return int32(math.Pow10(x))
    40  }
    41  
    42  func Abs(n int64) int64 {
    43  	y := n >> 63
    44  	return (n ^ y) - y
    45  }
    46  
    47  // uintSizeTable is used as a table to do comparison to get uint length is faster than doing loop on division with 10
    48  var uintSizeTable = [21]uint64{
    49  	0, // redundant 0 here, so to make function StrLenOfUint64Fast to count from 1 and return i directly
    50  	9, 99, 999, 9999, 99999,
    51  	999999, 9999999, 99999999, 999999999, 9999999999,
    52  	99999999999, 999999999999, 9999999999999, 99999999999999, 999999999999999,
    53  	9999999999999999, 99999999999999999, 999999999999999999, 9999999999999999999,
    54  	math.MaxUint64,
    55  } // math.MaxUint64 is 18446744073709551615 and it has 20 digits
    56  
    57  // StrLenOfUint64Fast efficiently calculate the string character lengths of an uint64 as input
    58  func StrLenOfUint64Fast(x uint64) int {
    59  	for i := 1; ; i++ {
    60  		if x <= uintSizeTable[i] {
    61  			return i
    62  		}
    63  	}
    64  }
    65  
    66  // StrLenOfInt64Fast efficiently calculate the string character lengths of an int64 as input
    67  func StrLenOfInt64Fast(x int64) int {
    68  	size := 0
    69  	if x < 0 {
    70  		size = 1 // add "-" sign on the length count
    71  	}
    72  	return size + StrLenOfUint64Fast(uint64(Abs(x)))
    73  }