github.com/andeya/ameda@v1.5.3/itoa62.go (about)

     1  package ameda
     2  
     3  import "math/bits"
     4  
     5  // FormatUint returns the string representation of i in the given base,
     6  // for 2 <= base <= 62.
     7  // NOTE:
     8  //
     9  //	Compatible with standard package strconv.
    10  func FormatUint(i uint64, base int) string {
    11  	if fastSmalls && i < nSmalls && base == 10 {
    12  		return small(int(i))
    13  	}
    14  	_, s := formatBits(nil, i, base, false, false)
    15  	return s
    16  }
    17  
    18  // FormatInt returns the string representation of i in the given base,
    19  // for 2 <= base <= 62.
    20  // NOTE:
    21  //
    22  //	Compatible with standard package strconv.
    23  func FormatInt(i int64, base int) string {
    24  	if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
    25  		return small(int(i))
    26  	}
    27  	_, s := formatBits(nil, uint64(i), base, i < 0, false)
    28  	return s
    29  }
    30  
    31  // Itoa is equivalent to FormatInt(int64(i), 10).
    32  // NOTE:
    33  //
    34  //	Compatible with standard package strconv.
    35  func Itoa(i int) string {
    36  	return FormatInt(int64(i), 10)
    37  }
    38  
    39  // AppendInt appends the string form of the integer i,
    40  // as generated by FormatInt, to dst and returns the extended buffer.
    41  // NOTE:
    42  //
    43  //	Compatible with standard package strconv.
    44  func AppendInt(dst []byte, i int64, base int) []byte {
    45  	if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
    46  		return append(dst, small(int(i))...)
    47  	}
    48  	dst, _ = formatBits(dst, uint64(i), base, i < 0, true)
    49  	return dst
    50  }
    51  
    52  // AppendUint appends the string form of the unsigned integer i,
    53  // as generated by FormatUint, to dst and returns the extended buffer.
    54  // NOTE:
    55  //
    56  //	Compatible with standard package strconv.
    57  func AppendUint(dst []byte, i uint64, base int) []byte {
    58  	if fastSmalls && i < nSmalls && base == 10 {
    59  		return append(dst, small(int(i))...)
    60  	}
    61  	dst, _ = formatBits(dst, i, base, false, true)
    62  	return dst
    63  }
    64  
    65  const (
    66  	digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    67  )
    68  
    69  const (
    70  	fastSmalls   = true // enable fast path for small integers
    71  	nSmalls      = 100
    72  	smallsString = "00010203040506070809" +
    73  		"10111213141516171819" +
    74  		"20212223242526272829" +
    75  		"30313233343536373839" +
    76  		"40414243444546474849" +
    77  		"50515253545556575859" +
    78  		"60616263646566676869" +
    79  		"70717273747576777879" +
    80  		"80818283848586878889" +
    81  		"90919293949596979899"
    82  )
    83  
    84  // small returns the string for an i with 0 <= i < nSmalls.
    85  func small(i int) string {
    86  	if i < 10 {
    87  		return digits[i : i+1]
    88  	}
    89  	return smallsString[i*2 : i*2+2]
    90  }
    91  
    92  // formatBits computes the string representation of u in the given base.
    93  // If neg is set, u is treated as negative int64 value. If append_ is
    94  // set, the string is appended to dst and the resulting byte slice is
    95  // returned as the first result value; otherwise the string is returned
    96  // as the second result value.
    97  func formatBits(dst []byte, u uint64, base int, neg, append_ bool) (d []byte, s string) {
    98  	if base < 2 || base > len(digits) {
    99  		panic("ameda(strconv): illegal AppendInt/FormatInt base")
   100  	}
   101  	// 2 <= base && base <= len(digits)
   102  
   103  	var a [64 + 1]byte // +1 for sign of 64bit value in base 2
   104  	i := len(a)
   105  
   106  	if neg {
   107  		u = -u
   108  	}
   109  
   110  	// convert bits
   111  	// We use uint values where we can because those will
   112  	// fit into a single register even on a 32bit machine.
   113  	if base == 10 {
   114  		// common case: use constants for / because
   115  		// the compiler can optimize it into a multiply+shift
   116  
   117  		if Host32bit {
   118  			// convert the lower digits using 32bit operations
   119  			for u >= 1e9 {
   120  				// Avoid using r = a%b in addition to q = a/b
   121  				// since 64bit division and modulo operations
   122  				// are calculated by runtime functions on 32bit machines.
   123  				q := u / 1e9
   124  				us := uint(u - q*1e9) // u % 1e9 fits into a uint
   125  				for j := 4; j > 0; j-- {
   126  					is := us % 100 * 2
   127  					us /= 100
   128  					i -= 2
   129  					a[i+1] = smallsString[is+1]
   130  					a[i+0] = smallsString[is+0]
   131  				}
   132  
   133  				// us < 10, since it contains the last digit
   134  				// from the initial 9-digit us.
   135  				i--
   136  				a[i] = smallsString[us*2+1]
   137  
   138  				u = q
   139  			}
   140  			// u < 1e9
   141  		}
   142  
   143  		// u guaranteed to fit into a uint
   144  		us := uint(u)
   145  		for us >= 100 {
   146  			is := us % 100 * 2
   147  			us /= 100
   148  			i -= 2
   149  			a[i+1] = smallsString[is+1]
   150  			a[i+0] = smallsString[is+0]
   151  		}
   152  
   153  		// us < 100
   154  		is := us * 2
   155  		i--
   156  		a[i] = smallsString[is+1]
   157  		if us >= 10 {
   158  			i--
   159  			a[i] = smallsString[is]
   160  		}
   161  
   162  	} else if isPowerOfTwo(base) {
   163  		// Use shifts and masks instead of / and %.
   164  		// Base is a power of 2 and 2 <= base <= len(digits) where len(digits) is 62.
   165  		// The largest power of 2 below or equal to 62 is 32, which is 1 << 5;
   166  		// i.e., the largest possible shift count is 5. By &-ind that value with
   167  		// the constant 7 we tell the compiler that the shift count is always
   168  		// less than 8 which is smaller than any register width. This allows
   169  		// the compiler to generate better code for the shift operation.
   170  		shift := uint(bits.TrailingZeros(uint(base))) & 7
   171  		b := uint64(base)
   172  		m := uint(base) - 1 // == 1<<shift - 1
   173  		for u >= b {
   174  			i--
   175  			a[i] = digits[uint(u)&m]
   176  			u >>= shift
   177  		}
   178  		// u < base
   179  		i--
   180  		a[i] = digits[uint(u)]
   181  	} else {
   182  		// general case
   183  		b := uint64(base)
   184  		for u >= b {
   185  			i--
   186  			// Avoid using r = a%b in addition to q = a/b
   187  			// since 64bit division and modulo operations
   188  			// are calculated by runtime functions on 32bit machines.
   189  			q := u / b
   190  			a[i] = digits[uint(u-q*b)]
   191  			u = q
   192  		}
   193  		// u < base
   194  		i--
   195  		a[i] = digits[uint(u)]
   196  	}
   197  
   198  	// add sign, if any
   199  	if neg {
   200  		i--
   201  		a[i] = '-'
   202  	}
   203  
   204  	if append_ {
   205  		d = append(dst, a[i:]...)
   206  		return
   207  	}
   208  	s = string(a[i:])
   209  	return
   210  }
   211  
   212  func isPowerOfTwo(x int) bool {
   213  	return x&(x-1) == 0
   214  }