diff options
Diffstat (limited to 'pkg/util/bitfield.go')
-rw-r--r-- | pkg/util/bitfield.go | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/pkg/util/bitfield.go b/pkg/util/bitfield.go index bf7812e..216837d 100644 --- a/pkg/util/bitfield.go +++ b/pkg/util/bitfield.go @@ -5,26 +5,45 @@ import ( "strings" ) -func BitfieldString[T ~uint8 | ~uint16 | ~uint32 | ~uint64](bitfield T, bitnames []string) string { +type BitfieldFormat uint8 + +const ( + HexNone = BitfieldFormat(iota) + HexLower + HexUpper +) + +func BitfieldString[T ~uint8 | ~uint16 | ~uint32 | ~uint64](bitfield T, bitnames []string, cfg BitfieldFormat) string { var out strings.Builder - fmt.Fprintf(&out, "0x%0x", uint64(bitfield)) + switch cfg { + case HexNone: + // do nothing + case HexLower: + fmt.Fprintf(&out, "0x%0x(", uint64(bitfield)) + case HexUpper: + fmt.Fprintf(&out, "0x%0X(", uint64(bitfield)) + } if bitfield == 0 { - out.WriteString("(none)") + out.WriteString("none") } else { rest := bitfield - sep := '(' + first := true for i := 0; rest != 0; i++ { if rest&(1<<i) != 0 { - out.WriteRune(sep) + if !first { + out.WriteRune('|') + } if i < len(bitnames) { out.WriteString(bitnames[i]) } else { fmt.Fprintf(&out, "(1<<%d)", i) } - sep = '|' + first = false } rest &^= 1 << i } + } + if cfg != HexNone { out.WriteRune(')') } return out.String() |