From a1f87d6b51e7523ff8c17b1a2205f46c4c5722b5 Mon Sep 17 00:00:00 2001 From: brent saner Date: Sat, 23 Aug 2025 19:32:48 -0400 Subject: [PATCH] stubbing encoding/bit --- .encoding.TODO/bit/docs.go | 19 +++++++++++++++++++ .encoding.TODO/bit/funcs.go | 14 ++++++++++++++ .encoding.TODO/bit/types.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 .encoding.TODO/bit/docs.go create mode 100644 .encoding.TODO/bit/funcs.go create mode 100644 .encoding.TODO/bit/types.go diff --git a/.encoding.TODO/bit/docs.go b/.encoding.TODO/bit/docs.go new file mode 100644 index 0000000..9eee774 --- /dev/null +++ b/.encoding.TODO/bit/docs.go @@ -0,0 +1,19 @@ +/* +Package bit aims to provide feature parity with stdlib's [encoding/hex]. + +It's a ludicrous tragedy that hex/base16, base32, base64 all have libraries for converting +to/from string representations... but there's nothing for binary ('01010001' etc.) whatsoever. + +This package also provides some extra convenience functions and types in an attempt to provide +an abstracted bit-level fidelity in Go. A [Bit] is a bool type, in which that underlying bool +being false represents a 0 and that underlying bool being true represents a 1. + +Note that a [Bit] or arbitrary-length or non-octal-aligned [][Bit] may take up more bytes in memory +than expected; a [Bit] will actually always occupy a single byte -- thus representing +`00000000 00000000` as a [][Bit] or [16][Bit] will actually occupy *sixteen bytes* in memory, +NOT 2 bytes (nor, obviously, [2][Byte])! +It is recommended instead to use a [Bits] instead of a [Bit] slice or array, as it will try to properly align to the +smallest memory allocation possible (at the cost of a few extra CPU cycles on adding/removing one or more [Bit]). +It will properly retain any appended, prepended, leading, or trailing bits that do not currently align to a byte. +*/ +package bit diff --git a/.encoding.TODO/bit/funcs.go b/.encoding.TODO/bit/funcs.go new file mode 100644 index 0000000..080c002 --- /dev/null +++ b/.encoding.TODO/bit/funcs.go @@ -0,0 +1,14 @@ +package bit + +// TODO: Provide analogues of encoding/hex, encoding/base64, etc. functions etc. + +/* + TODO: Also provide interfaces for the following: + + * https://pkg.go.dev/encoding#BinaryAppender + * https://pkg.go.dev/encoding#BinaryMarshaler + * https://pkg.go.dev/encoding#BinaryUnmarshaler + * https://pkg.go.dev/encoding#TextAppender + * https://pkg.go.dev/encoding#TextMarshaler + * https://pkg.go.dev/encoding#TextUnmarshaler +*/ diff --git a/.encoding.TODO/bit/types.go b/.encoding.TODO/bit/types.go new file mode 100644 index 0000000..3962005 --- /dev/null +++ b/.encoding.TODO/bit/types.go @@ -0,0 +1,34 @@ +package bit + +type ( + // Bit aims to provide a native-like type for a single bit (Golang operates on the smallest fidelity level of *byte*/uint8). + Bit bool + + // Bits is an arbitrary length of bits. + Bits struct { + /* + leading is a series of Bit that do not cleanly align to the beginning of Bits.b. + They will always be the bits at the *beginning* of the sequence. + len(Bits.leading) will *never* be more than 7; + it's converted into a byte, prepended to Bits.b, and cleared if it reaches that point. + */ + leading []Bit + // b is the condensed/memory-aligned alternative to an [][8]Bit (or []Bit, or [][]Bit, etc.). + b []byte + /* + remaining is a series of Bit that do not cleanly align to the end of Bits.b. + They will always be the bits at the *end* of the sequence. + len(Bits.remaining) will *never* be more than 7; + it's converted into a byte, appended to Bits.b, and cleared if it reaches that point. + */ + remaining []Bit + // fixedLen, if 0, represents a "slice". If >= 1, it represents an "array". + fixedLen uint + } + + // Byte is this package's representation of a byte. It's primarily for convenience. + Byte byte + + // Bytes is defined as a type for convenience single-call functions. + Bytes []Byte +)