github.com/kamalshkeir/kencoding@v0.0.2-0.20230409043843-44b609a0475a/json/int.go (about)

     1  package json
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  var endianness int
     8  
     9  func init() {
    10  	var b [2]byte
    11  	*(*uint16)(unsafe.Pointer(&b)) = uint16(0xABCD)
    12  
    13  	switch b[0] {
    14  	case 0xCD:
    15  		endianness = 0 // LE
    16  	case 0xAB:
    17  		endianness = 1 // BE
    18  	default:
    19  		panic("could not determine endianness")
    20  	}
    21  }
    22  
    23  // "00010203...96979899" cast to []uint16
    24  var intLELookup = [100]uint16{
    25  	0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930,
    26  	0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931,
    27  	0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932,
    28  	0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933,
    29  	0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934,
    30  	0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935,
    31  	0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936,
    32  	0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937,
    33  	0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938,
    34  	0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939,
    35  }
    36  
    37  var intBELookup = [100]uint16{
    38  	0x3030, 0x3031, 0x3032, 0x3033, 0x3034, 0x3035, 0x3036, 0x3037, 0x3038, 0x3039,
    39  	0x3130, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, 0x3138, 0x3139,
    40  	0x3230, 0x3231, 0x3232, 0x3233, 0x3234, 0x3235, 0x3236, 0x3237, 0x3238, 0x3239,
    41  	0x3330, 0x3331, 0x3332, 0x3333, 0x3334, 0x3335, 0x3336, 0x3337, 0x3338, 0x3339,
    42  	0x3430, 0x3431, 0x3432, 0x3433, 0x3434, 0x3435, 0x3436, 0x3437, 0x3438, 0x3439,
    43  	0x3530, 0x3531, 0x3532, 0x3533, 0x3534, 0x3535, 0x3536, 0x3537, 0x3538, 0x3539,
    44  	0x3630, 0x3631, 0x3632, 0x3633, 0x3634, 0x3635, 0x3636, 0x3637, 0x3638, 0x3639,
    45  	0x3730, 0x3731, 0x3732, 0x3733, 0x3734, 0x3735, 0x3736, 0x3737, 0x3738, 0x3739,
    46  	0x3830, 0x3831, 0x3832, 0x3833, 0x3834, 0x3835, 0x3836, 0x3837, 0x3838, 0x3839,
    47  	0x3930, 0x3931, 0x3932, 0x3933, 0x3934, 0x3935, 0x3936, 0x3937, 0x3938, 0x3939,
    48  }
    49  
    50  var intLookup = [2]*[100]uint16{&intLELookup, &intBELookup}
    51  
    52  func appendInt(b []byte, n int64) []byte {
    53  	return formatInteger(b, uint64(n), n < 0)
    54  }
    55  
    56  func appendUint(b []byte, n uint64) []byte {
    57  	return formatInteger(b, n, false)
    58  }
    59  
    60  func formatInteger(out []byte, n uint64, negative bool) []byte {
    61  	if !negative {
    62  		if n < 10 {
    63  			return append(out, byte(n+'0'))
    64  		} else if n < 100 {
    65  			u := intLELookup[n]
    66  			return append(out, byte(u), byte(u>>8))
    67  		}
    68  	} else {
    69  		n = -n
    70  	}
    71  
    72  	lookup := intLookup[endianness]
    73  
    74  	var b [22]byte
    75  	u := (*[11]uint16)(unsafe.Pointer(&b))
    76  	i := 11
    77  
    78  	for n >= 100 {
    79  		j := n % 100
    80  		n /= 100
    81  		i--
    82  		u[i] = lookup[j]
    83  	}
    84  
    85  	i--
    86  	u[i] = lookup[n]
    87  
    88  	i *= 2 // convert to byte index
    89  	if n < 10 {
    90  		i++ // remove leading zero
    91  	}
    92  	if negative {
    93  		i--
    94  		b[i] = '-'
    95  	}
    96  
    97  	return append(out, b[i:]...)
    98  }