mirror of
https://github.com/soypat/lneto.git
synced 2026-08-19 22:24:03 +00:00
several bugfixes, add internal.IntLen, round up http-linux example with new router API
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package internal
|
||||
|
||||
// IntLen returns the number of bytes [strconv.AppendInt] emits for value in the
|
||||
// given base, including a leading minus sign for negatives. Lets callers size a
|
||||
// buffer, or test whether a value fits an existing slot, before writing a byte.
|
||||
// base must be in the range 2..36, as accepted by [strconv.AppendInt].
|
||||
func IntLen(value int64, base int) int {
|
||||
n := 1
|
||||
u := uint64(value)
|
||||
if value < 0 {
|
||||
n++ // Leading minus sign.
|
||||
u = -u // Two's-complement magnitude; correct even for math.MinInt64.
|
||||
}
|
||||
for u >= uint64(base) {
|
||||
u /= uint64(base)
|
||||
n++
|
||||
}
|
||||
return n
|
||||
}
|
||||
Reference in New Issue
Block a user