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.
This commit is contained in:
parent
a1f87d6b51
commit
688abd0874
@ -34,11 +34,47 @@ func NewMaskBitExplicit(value uint) (m *MaskBit) {
|
|||||||
return
|
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) {
|
func (m *MaskBit) HasFlag(flag MaskBit) (r bool) {
|
||||||
|
|
||||||
var b MaskBit = *m
|
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 {
|
if b&flag != 0 {
|
||||||
r = true
|
r = true
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user