Documentation
¶
Index ¶
- Constants
- Variables
- func GetString(packed []byte, pointer Pointer) (string, error)
- func GetStringUnsafe(packed []byte, pointer Pointer) (string, error)
- type PackedBlob
- type Pointer
- type StringMap
- func (s *StringMap) Add(str string) (int, error)
- func (s *StringMap) AddUnsafe(b []byte) (int, error)
- func (s *StringMap) GetString(index int) string
- func (s *StringMap) Length() int
- func (s *StringMap) Pack() (*PackedBlob, error)
- func (s *StringMap) Size() int
- func (s *StringMap) Strings() []string
Constants ¶
const ( // MaxStringLen is the maximum allowed length of a string to be packed. // This limit is constrained because the Pointer.Length field is a uint8. MaxStringLen = math.MaxUint8 )
Variables ¶
var ErrStringTooLong = fmt.Errorf("length exceeds max %d", MaxStringLen)
Functions ¶
func GetString ¶
Get returns a copied, independent string from the packed blob. It allocates a new underlying buffer to ensure the returned string can safely outlive the blob and remains isolated from any future mutations.
func GetStringUnsafe ¶
GetUnsafe returns a zero-copy string pointing directly into the blob's memory. It is fast but unsafe: the returned string's lifetime is tied to the blob, and it will reflect any future modifications made to the underlying slice.
Types ¶
type PackedBlob ¶
type PackedBlob struct {
// contains filtered or unexported fields
}
PackedBlob holds a single concatenated slice of bytes containing all the packed strings. Strings are retrieved using a Pointer.
func (*PackedBlob) Bytes ¶
func (s *PackedBlob) Bytes() []byte
Bytes returns the raw underlying byte slice of the PackedBlob. This slice should not be modified.
func (*PackedBlob) GetString ¶
func (s *PackedBlob) GetString(pointer Pointer) (string, error)
GetString returns a copied, independent string from the PackedBlob. It allocates a new underlying buffer to ensure the returned string can safely outlive the blob and remains isolated from any future mutations.
func (*PackedBlob) GetStringUnsafe ¶
func (s *PackedBlob) GetStringUnsafe(pointer Pointer) (string, error)
GetStringUnsafe returns a zero-copy string pointing directly into the blob's memory. It is fast but unsafe: the returned string's lifetime is tied to the blob, and it will reflect any future modifications made to the underlying slice.
func (*PackedBlob) Len ¶
func (s *PackedBlob) Len() int
Len returns the total length of the packed byte slice in the blob.
func (*PackedBlob) Pointers ¶
func (s *PackedBlob) Pointers() []Pointer
Pointers returns all pointers.
func (*PackedBlob) Size ¶
func (s *PackedBlob) Size() int
Size returns the total size of the packed bytes and pointers in memory.
type Pointer ¶
type Pointer struct {
// contains filtered or unexported fields
}
Pointer represents the location and length of a packed string within a PackedBlob. To maximize memory compression, it is packed as a 5-byte array to avoid struct alignment padding.
func NewPointer ¶
NewPointer initializes a new 5-byte packed Pointer.
func PointerFromBytes ¶
PointerFromBytes reconstructs a Pointer from a 5-byte array.
func PointerFromSlice ¶
PointerFromSlice reconstructs a Pointer from a slice. It returns an error if the slice contains fewer than 5 bytes.
type StringMap ¶
type StringMap struct {
// contains filtered or unexported fields
}
StringMap is a thread-safe collector for building a list of strings to be packed together into a PackedBlob.
func NewStringMap ¶
NewStringMap initializes a new StringMap with an optional pre-filled entries slice.
func (*StringMap) Add ¶
Add appends a string to the StringMap and returns its assigned index or ErrStringTooLong.
func (*StringMap) AddUnsafe ¶
AddUnsafe appends a string view of the provided byte slice to the StringMap and returns its assigned index or ErrStringTooLong. It avoids allocations by using unsafe-casting under the hood, but the caller must ensure the underlying byte slice is not mutated after this call.
func (*StringMap) GetString ¶
GetString returns the string at the specified index. It will panic if the index is out of bounds.
func (*StringMap) Length ¶
Length returns the number of strings currently collected in the StringMap.
func (*StringMap) Pack ¶
func (s *StringMap) Pack() (*PackedBlob, error)
Pack compresses all strings currently collected in the StringMap into a PackedBlob.
func (*StringMap) Size ¶
Size returns the total in-memory size of the entries slice and its strings.