flagged

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2025 License: Apache-2.0 Imports: 0 Imported by: 0

README

flagged is a lightweight Go library for working with typed, compact bitflags.

PkgGoDev Go Report Card Tests Go Coverage

It provides a minimal, extensible API for manipulating and inspecting compact bitflags, while remaining dependency-free and allocation-free.

It’s ideal for scenarios where you need efficient and compact boolean state representation — whether for generated flags, boolean configurations, or packed state machines.

Features:

  • Exposes typed wrappers for uint types: BitFlags8, BitFlags16, BitFlags32, BitFlags64 (matching uint8, uint16, uint32, uint64, respectively).
  • Unified interface: all exposed types implement a common BitFlags interface
  • Core bit operations, using only the bit index (normal integers, with no shifting required for inputs).
  • Pure Go implementation, no reflection, no dependencies, suitable for any application, in any environment.
  • Easy to use directly or as a backend for code generators (check github.com/asmsh/flagged/cmd/genflagged).

Installation:

go get github.com/asmsh/flagged

Usage:

Import the package:

import "github.com/asmsh/flagged"

Choose a bit width depending on your needs:

var flags flagged.BitFlags16
flags.Set(3)
if flags.Is(3) {
    fmt.Println("flags[3] is set")
}
flags.Reset(3)
if !flags.Is(3) {
    fmt.Println("flags[3] is not set")
}

Or work with the interface:

var f flagged.BitFlags = flagged.New(flagged.BitFlags32(0))
f.Set(1)
f.Set(5)
f.Toggle(1)
fmt.Println(f) // 00000000000000000000000000100000

Example:

package main

import (
	"fmt"

	"github.com/asmsh/flagged"
)

const (
	permissionReadBitIndex flagged.BitIndex = iota
	permissionWriteBitIndex
	permissionExecBitIndex
)

func main() {
	var permFlags flagged.BitFlags8
	permFlags.Set(permissionReadBitIndex)
	permFlags.Set(permissionExecBitIndex)

	fmt.Println(permFlags.Is(permissionReadBitIndex))  // true
	fmt.Println(permFlags.Is(permissionWriteBitIndex)) // false

	permFlags.Toggle(permissionWriteBitIndex)
	fmt.Println(permFlags.Is(permissionWriteBitIndex)) // true

	fmt.Println(permFlags) // 00000111
}

Documentation

Overview

Package flagged provides a minimal, extensible API for manipulating and inspecting compact bitflags, while remaining dependency- and allocation-free.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New[T BitFlags8 | BitFlags16 | BitFlags32 | BitFlags64](f T) *T

New is a helper function for creating pointer to one of the BitFlags types. It's useful for returning a value that implements the BitFlags interface.

Types

type BitFlags

type BitFlags interface {
	// Is reports whether the bit at index idx is set to true or not.
	// It panics if idx is out of the allowed range [0, Size-1].
	Is(idx BitIndex) (set bool)

	// Set sets the bit at index idx to true, returning its old value.
	// It panics if idx is out of the allowed range [0, Size-1].
	Set(idx BitIndex) (old bool)

	// Reset sets the bit at index idx to false, returning its old value.
	// It panics if idx is out of the allowed range [0, Size-1].
	Reset(idx BitIndex) (old bool)

	// SetTo sets the bit at index idx to new, returning its old value.
	// It panics if idx is out of the allowed range [0, Size-1].
	SetTo(idx BitIndex, new bool) (old bool)

	// Toggle toggles the bit at index idx, returning its new value.
	// It panics if idx is out of the allowed range [0, Size-1].
	Toggle(idx BitIndex) (new bool)

	// SetAll sets all bits to true.
	SetAll()

	// ResetAll sets all bits to false.
	ResetAll()

	// AnySet reports whether any of the bits are set to true.
	AnySet() bool

	// AllSet reports whether all the bits are set to true.
	AllSet() bool

	// AnyOf reports whether any of the bits at indexes idx are set to true.
	// If no indexes are passed, it acts as [BitFlags.AnySet].
	AnyOf(idx ...BitIndex) bool

	// AllOf reports whether all the bits at indexes idx are set to true.
	// If no indexes are passed, it acts as [BitFlags.AllSet].
	AllOf(idx ...BitIndex) bool

	// Size is the number of bits included in this [BitFlags] value.
	// It represents the bit width of the underlying uint.
	// It's one of 8, 16, 32, 64.
	Size() int

	// String returns the binary representation of this [BitFlags] value,
	// formatted like fmt's %b verb, but with leading zeros to preserve
	// the full bit width of the underlying type (Size).
	String() string

	// PrettyString returns a human-readable binary representation of
	// this [BitFlags] value, with 1 represented as 'I' and 0 as 'O', and
	// with '|' delimiter between each bit and '_' delimiter each 8 bits.
	//
	// Example:
	//
	//  String() // "0000010001000100"
	//  PrettyString() // "O|O|O|O|O|I|O|O_O|I|O|O|O|I|O|O"
	PrettyString() string
}

BitFlags represents the set of methods allowed on one of the typed bit flags types (BitFlags8, BitFlags16, BitFlags32, BitFlags64). It makes it easy to write generic flag-aware code, regardless of the underlying type or its size.

type BitFlags8

type BitFlags8 uint8

BitFlags8 is a wrapper for uint8 bit flags, carrying 8 flags at max.

func (BitFlags8) AllOf

func (f BitFlags8) AllOf(idx ...BitIndex) bool

func (BitFlags8) AllSet

func (f BitFlags8) AllSet() bool

func (BitFlags8) AnyOf

func (f BitFlags8) AnyOf(idx ...BitIndex) bool

func (BitFlags8) AnySet

func (f BitFlags8) AnySet() bool

func (*BitFlags8) BitFlags

func (f *BitFlags8) BitFlags() BitFlags

func (BitFlags8) Is

func (f BitFlags8) Is(idx BitIndex) (set bool)

func (BitFlags8) PrettyString

func (f BitFlags8) PrettyString() string

func (*BitFlags8) Reset

func (f *BitFlags8) Reset(idx BitIndex) (old bool)

func (*BitFlags8) ResetAll

func (f *BitFlags8) ResetAll()

func (*BitFlags8) Set

func (f *BitFlags8) Set(idx BitIndex) (old bool)

func (*BitFlags8) SetAll

func (f *BitFlags8) SetAll()

func (*BitFlags8) SetTo

func (f *BitFlags8) SetTo(idx BitIndex, new bool) (old bool)

func (BitFlags8) Size

func (BitFlags8) Size() int

func (BitFlags8) String

func (f BitFlags8) String() string

func (*BitFlags8) Toggle

func (f *BitFlags8) Toggle(idx BitIndex) (new bool)

type BitFlags16

type BitFlags16 uint16

BitFlags16 is a wrapper for uint16 bit flags, carrying 16 flags at max.

func (BitFlags16) AllOf

func (f BitFlags16) AllOf(idx ...BitIndex) bool

func (BitFlags16) AllSet

func (f BitFlags16) AllSet() bool

func (BitFlags16) AnyOf

func (f BitFlags16) AnyOf(idx ...BitIndex) bool

func (BitFlags16) AnySet

func (f BitFlags16) AnySet() bool

func (*BitFlags16) BitFlags

func (f *BitFlags16) BitFlags() BitFlags

func (BitFlags16) Is

func (f BitFlags16) Is(idx BitIndex) (set bool)

func (BitFlags16) PrettyString

func (f BitFlags16) PrettyString() string

func (*BitFlags16) Reset

func (f *BitFlags16) Reset(idx BitIndex) (old bool)

func (*BitFlags16) ResetAll

func (f *BitFlags16) ResetAll()

func (*BitFlags16) Set

func (f *BitFlags16) Set(idx BitIndex) (old bool)

func (*BitFlags16) SetAll

func (f *BitFlags16) SetAll()

func (*BitFlags16) SetTo

func (f *BitFlags16) SetTo(idx BitIndex, new bool) (old bool)

func (BitFlags16) Size

func (BitFlags16) Size() int

func (BitFlags16) String

func (f BitFlags16) String() string

func (*BitFlags16) Toggle

func (f *BitFlags16) Toggle(idx BitIndex) (new bool)

type BitFlags32

type BitFlags32 uint32

BitFlags32 is a wrapper for uint32 bit flags, carrying 32 flags at max.

func (BitFlags32) AllOf

func (f BitFlags32) AllOf(idx ...BitIndex) bool

func (BitFlags32) AllSet

func (f BitFlags32) AllSet() bool

func (BitFlags32) AnyOf

func (f BitFlags32) AnyOf(idx ...BitIndex) bool

func (BitFlags32) AnySet

func (f BitFlags32) AnySet() bool

func (*BitFlags32) BitFlags

func (f *BitFlags32) BitFlags() BitFlags

func (BitFlags32) Is

func (f BitFlags32) Is(idx BitIndex) (set bool)

func (BitFlags32) PrettyString

func (f BitFlags32) PrettyString() string

func (*BitFlags32) Reset

func (f *BitFlags32) Reset(idx BitIndex) (old bool)

func (*BitFlags32) ResetAll

func (f *BitFlags32) ResetAll()

func (*BitFlags32) Set

func (f *BitFlags32) Set(idx BitIndex) (old bool)

func (*BitFlags32) SetAll

func (f *BitFlags32) SetAll()

func (*BitFlags32) SetTo

func (f *BitFlags32) SetTo(idx BitIndex, new bool) (old bool)

func (BitFlags32) Size

func (BitFlags32) Size() int

func (BitFlags32) String

func (f BitFlags32) String() string

func (*BitFlags32) Toggle

func (f *BitFlags32) Toggle(idx BitIndex) (new bool)

type BitFlags64

type BitFlags64 uint64

BitFlags64 is a wrapper for uint64 bit flags, carrying 64 flags at max.

func (BitFlags64) AllOf

func (f BitFlags64) AllOf(idx ...BitIndex) bool

func (BitFlags64) AllSet

func (f BitFlags64) AllSet() bool

func (BitFlags64) AnyOf

func (f BitFlags64) AnyOf(idx ...BitIndex) bool

func (BitFlags64) AnySet

func (f BitFlags64) AnySet() bool

func (*BitFlags64) BitFlags

func (f *BitFlags64) BitFlags() BitFlags

func (BitFlags64) Is

func (f BitFlags64) Is(idx BitIndex) (set bool)

func (BitFlags64) PrettyString

func (f BitFlags64) PrettyString() string

func (*BitFlags64) Reset

func (f *BitFlags64) Reset(idx BitIndex) (old bool)

func (*BitFlags64) ResetAll

func (f *BitFlags64) ResetAll()

func (*BitFlags64) Set

func (f *BitFlags64) Set(idx BitIndex) (old bool)

func (*BitFlags64) SetAll

func (f *BitFlags64) SetAll()

func (*BitFlags64) SetTo

func (f *BitFlags64) SetTo(idx BitIndex, new bool) (old bool)

func (BitFlags64) Size

func (BitFlags64) Size() int

func (BitFlags64) String

func (f BitFlags64) String() string

func (*BitFlags64) Toggle

func (f *BitFlags64) Toggle(idx BitIndex) (new bool)

type BitIndex

type BitIndex = int

BitIndex is a marker type denoting that its values should be used as bit indexes, passed to the different BitFlags methods. If the value is outside BitFlags range, the methods will panic. The allowed range is [0, [BitFlags.Size]-1].

Example:

 const (
	myOption1 flagged.BitIndex = iota
	myOption2
	myOption3
)

Directories

Path Synopsis
cmd
genflagged module

Jump to

Keyboard shortcuts

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