MCPcopy
hub / github.com/pquerna/ffjson / FormatBits2

Function FormatBits2

fflib/v1/iota.go:83–161  ·  view source on GitHub ↗

FormatBits2 computes the string representation of u in the given base. If neg is set, u is treated as negative int64 value. If append_ is set, the string is appended to dst and the resulting byte slice is returned as the first result value; otherwise the string is returned as the second result value

(dst FormatBitsWriter, u uint64, base int, neg bool)

Source from the content-addressed store, hash-verified

81// as the second result value.
82//
83func FormatBits2(dst FormatBitsWriter, u uint64, base int, neg bool) {
84 if base < 2 || base > len(digits) {
85 panic("strconv: illegal AppendInt/FormatInt base")
86 }
87 // fast path for small common numbers
88 if u <= 10 {
89 if neg {
90 dst.WriteByte('-')
91 }
92 dst.Write(smallNumbers[u])
93 return
94 }
95
96 // 2 <= base && base <= len(digits)
97
98 var a = makeSlice(65)
99 // var a [64 + 1]byte // +1 for sign of 64bit value in base 2
100 i := len(a)
101
102 if neg {
103 u = -u
104 }
105
106 // convert bits
107 if base == 10 {
108 // common case: use constants for / and % because
109 // the compiler can optimize it into a multiply+shift,
110 // and unroll loop
111 for u >= 100 {
112 i -= 2
113 q := u / 100
114 j := uintptr(u - q*100)
115 a[i+1] = digits01[j]
116 a[i+0] = digits10[j]
117 u = q
118 }
119 if u >= 10 {
120 i--
121 q := u / 10
122 a[i] = digits[uintptr(u-q*10)]
123 u = q
124 }
125
126 } else if s := shifts[base]; s > 0 {
127 // base is power of 2: use shifts and masks instead of / and %
128 b := uint64(base)
129 m := uintptr(b) - 1 // == 1<<s - 1
130 for u >= b {
131 i--
132 a[i] = digits[uintptr(u)&m]
133 u >>= s
134 }
135
136 } else {
137 // general case
138 b := uint64(base)
139 for u >= b {
140 i--

Callers 1

FormatBitsFunction · 0.85

Calls 4

WriteByteMethod · 0.80
makeSliceFunction · 0.70
PoolFunction · 0.70
WriteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…