From 688abd08744989098a9073ed707f45b8b7e51103 Mon Sep 17 00:00:00 2001 From: brent saner Date: Tue, 26 Aug 2025 20:39:29 -0400 Subject: [PATCH] v1.9.5 FIXED: * HasFlag would inappropriately report true for m = A, flag = A | B. This has been rectified, and this behavior is now explicitly exposed via IsOneOf. --- bitmask/bitmask.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/bitmask/bitmask.go b/bitmask/bitmask.go index 237fae8..95fcf3d 100644 --- a/bitmask/bitmask.go +++ b/bitmask/bitmask.go @@ -34,11 +34,47 @@ func NewMaskBitExplicit(value uint) (m *MaskBit) { return } -// HasFlag is true if m has MaskBit flag set/enabled. +/* + HasFlag is true if m has MaskBit flag set/enabled. + THIS WILL RETURN FALSE FOR OR'd FLAGS. + For example: + + flagA MaskBit = 0x01 + flagB MaskBit = 0x02 + flagComposite = flagA | flagB + + m *MaskBit = NewMaskBitExplicit(uint(flagA)) + + m.HasFlag(flagComposite) will return false even though flagComposite is an OR + that contains flagA. + Use [MaskBit.IsOneOf] instead if you do not desire this behavior, + and instead want to test composite flag *membership*. + (MaskBit.IsOneOf will also return true for non-composite equality.) +*/ func (m *MaskBit) HasFlag(flag MaskBit) (r bool) { var b MaskBit = *m + if b&flag == flag { + r = true + } + return +} + +/* + IsOneOf is like a "looser" form of [MaskBit.HasFlag] + in that it allows for testing composite membership. + + See [MaskBit.HasFlag] for more information. + + If composite is *not* an OR'd MaskBit (i.e. + it falls directly on a boundary -- 0, 1, 2, 4, 8, 16, etc.), + then IsOneOf will behave exactly like HasFlag. +*/ +func (m *MaskBit) IsOneOf(composite MaskBit) (r bool) { + + var b MaskBit = *m + if b&flag != 0 { r = true }