bitmask

package
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2022 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package bitmask handles a flag-like opt/bitmask system.

See https://yourbasic.org/golang/bitmask-flag-set-clear/ for more information.

To use this, set constants like thus:

package main

import (
	"r00t2.io/goutils/bitmask"
)

const OPTNONE bitmask.MaskBit = 0
const (
	OPT1 bitmask.MaskBit = 1 << iota
	OPT2
	OPT3
	// ...
)

var MyMask *bitmask.MaskBit

func main() {
	MyMask = bitmask.NewMaskBit()

	MyMask.AddFlag(OPT1)
	MyMask.AddFlag(OPT3)

	_ = MyMask
}

This would return true:

MyMask.HasFlag(OPT1)

As would this:

MyMask.HasFlag(OPT3)

But this would return false:

MyMask.HasFlag(OPT2)

If you need something with more flexibility (as always, at the cost of complexity), you may be interested in one of the following libraries:

. github.com/alvaroloes/enumer . github.com/abice/go-enum . github.com/jeffreyrichter/enum/enum

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MaskBit

type MaskBit uint

MaskBit is a flag container.

func NewMaskBit

func NewMaskBit() (m *MaskBit)

NewMaskBit is a convenience function. It will return a MaskBit with a (referenced) value of 0, so set your consts up accordingly.

It is highly recommended to set this default as a "None" flag (separate from your iotas!) as shown in the example.

func NewMaskBitExplicit added in v1.3.0

func NewMaskBitExplicit(value uint) (m *MaskBit)

NewMaskBitExplicit is like NewMaskBit, but allows you to specify a non-zero (0x0) value.

func (*MaskBit) AddFlag

func (m *MaskBit) AddFlag(flag MaskBit)

AddFlag adds MaskBit flag to m.

func (*MaskBit) Bytes added in v1.3.1

func (m *MaskBit) Bytes(trim bool) (b []byte)

Bytes returns the current value of a MasBit as a byte slice (big-endian).

If trim is false, b will (probably) be 4 bytes long if you're on a 32-bit size system, and b will (probably) be 8 bytes long if you're on a 64-bit size system. You can determine the size of the resulting slice via (math/)bits.UintSize / 8.

If trim is true, it will trim leading null bytes (if any). This will lead to an unpredictable byte slice length in b, but is most likely preferred for byte operations.

func (*MaskBit) ClearFlag

func (m *MaskBit) ClearFlag(flag MaskBit)

ClearFlag removes MaskBit flag from m.

func (*MaskBit) HasFlag

func (m *MaskBit) HasFlag(flag MaskBit) (r bool)

HasFlag is true if m has MaskBit flag set/enabled.

func (*MaskBit) ToggleFlag

func (m *MaskBit) ToggleFlag(flag MaskBit)

ToggleFlag switches MaskBit flag in m to its inverse; if true, it is now false and vice versa.

func (*MaskBit) Value added in v1.3.0

func (m *MaskBit) Value() (v uint)

Value returns the current raw uint value of a MaskBit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL