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

Function formatBits

fflib/v1/ftoa.go:464–542  ·  view source on GitHub ↗

formatBits computes the string representation of u in the given base. If neg is set, u is treated as negative int64 value.

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

Source from the content-addressed store, hash-verified

462// formatBits computes the string representation of u in the given base.
463// If neg is set, u is treated as negative int64 value.
464func formatBits(dst EncodingBuffer, u uint64, base int, neg bool) {
465 if base < 2 || base > len(digits) {
466 panic("strconv: illegal AppendInt/FormatInt base")
467 }
468 // 2 <= base && base <= len(digits)
469
470 var a [64 + 1]byte // +1 for sign of 64bit value in base 2
471 i := len(a)
472
473 if neg {
474 u = -u
475 }
476
477 // convert bits
478 if base == 10 {
479 // common case: use constants for / because
480 // the compiler can optimize it into a multiply+shift
481
482 if ^uintptr(0)>>32 == 0 {
483 for u > uint64(^uintptr(0)) {
484 q := u / 1e9
485 us := uintptr(u - q*1e9) // us % 1e9 fits into a uintptr
486 for j := 9; j > 0; j-- {
487 i--
488 qs := us / 10
489 a[i] = byte(us - qs*10 + '0')
490 us = qs
491 }
492 u = q
493 }
494 }
495
496 // u guaranteed to fit into a uintptr
497 us := uintptr(u)
498 for us >= 10 {
499 i--
500 q := us / 10
501 a[i] = byte(us - q*10 + '0')
502 us = q
503 }
504 // u < 10
505 i--
506 a[i] = byte(us + '0')
507
508 } else if s := shifts[base]; s > 0 {
509 // base is power of 2: use shifts and masks instead of / and %
510 b := uint64(base)
511 m := uintptr(b) - 1 // == 1<<s - 1
512 for u >= b {
513 i--
514 a[i] = digits[uintptr(u)&m]
515 u >>= s
516 }
517 // u < base
518 i--
519 a[i] = digits[uintptr(u)]
520
521 } else {

Callers 1

fmtBFunction · 0.85

Calls 1

WriteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…