utils

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: BSD-3-Clause Imports: 25 Imported by: 62

README

Lux Utilities Package

Common utilities extracted from node package for use across all Lux packages.

Documentation

Overview

Package utils provides common utility functions used throughout the SDK.

Index

Constants

View Source
const CGOEnabled = true

CGOEnabled indicates whether CGO is available. This file is only compiled when CGO is enabled.

Variables

This section is empty.

Functions

func AddSingleQuotes

func AddSingleQuotes(s []string) []string

AddSingleQuotes adds single quotes to each string in the given slice.

func AppendSlices

func AppendSlices[T any](slices ...[]T) []T

AppendSlices appends multiple slices into a single slice.

func Belongs

func Belongs[T comparable](input []T, elem T) bool

func CleanupString

func CleanupString(s string) string

CleanupString cleans up a string by trimming \r and \n characters.

func CleanupStrings

func CleanupStrings(s []string) []string

CleanupStrings cleans up a slice of strings by trimming \r and \n characters.

func DeleteIndex

func DeleteIndex[S ~[]E, E any](s S, i int) S

DeleteIndex moves the last element in the slice to index [i] and shrinks the size of the slice by 1.

This is an O(1) operation that allows the removal of an element from a slice when the order of the slice is not important.

If [i] is out of bounds, this function will panic.

func Detach

func Detach(ctx context.Context) context.Context

func DirExists

func DirExists(dirName string) bool

DirExists checks if a directory exists.

func Err

func Err(errors ...error) error

func ExpandHome

func ExpandHome(path string) string

ExpandHome expands ~ symbol to home directory

func FileExists

func FileExists(filename string) bool

FileExists checks if a file exists.

func FindErrorLogs

func FindErrorLogs(rootDirs ...string)

FindErrorLogs is a utility function, we will NOT do error handling, as this is supposed to be called during error handling itself we don't want to make it even more complex

func FormatAmount

func FormatAmount(amount *big.Int, decimals uint8) string

FormatAmount formats an amount of base units as a string representing the amount in the given denomination. (i.e. An amount of 54321 with a decimals value of 3 results in the string "54.321")

func GetAPIContext

func GetAPIContext() (context.Context, context.CancelFunc)

GetAPIContext returns a context for API requests.

func GetAPILargeContext

func GetAPILargeContext() (context.Context, context.CancelFunc)

GetAPILargeContext returns a context for API requests with large timeout.

func GetSHA256FromDisk

func GetSHA256FromDisk(binPath string) (string, error)

func GetStacktrace

func GetStacktrace(all bool) string

func GetTimedContext

func GetTimedContext(timeout time.Duration) (context.Context, context.CancelFunc)

GetTimedContext returns a context with the given timeout.

func IsSortedAndUnique

func IsSortedAndUnique[T Sortable[T]](s []T) bool

Returns true iff the elements in [s] are unique and sorted.

func IsSortedAndUniqueByHash

func IsSortedAndUniqueByHash[T ~[]byte](s []T) bool

Returns true iff the elements in [s] are unique and sorted based by their hashes.

func IsSortedAndUniqueOrdered

func IsSortedAndUniqueOrdered[T cmp.Ordered](s []T) bool

Returns true iff the elements in [s] are unique and sorted.

func IsSortedBytes

func IsSortedBytes[T ~[]byte](s []T) bool

Returns true iff the elements in [s] are sorted.

func IsSubPath

func IsSubPath(childPath, parentPath string) bool

IsSubPath checks if childPath is inside parentPath. Both paths should be absolute paths. Returns true if childPath is a subdirectory or file inside parentPath.

func Map

func Map[T, U any](input []T, f func(T) U) []U

func PointersSlice

func PointersSlice[T any](values []T) []*T

PointersSlice converts a slice of values to a slice of pointers

func RandomBytes

func RandomBytes(n int) []byte

RandomBytes returns a slice of n random bytes Intended for use in testing

func Retry

func Retry[T any](
	fn func() (T, error),
	maxAttempts int,
	retryInterval time.Duration,
) (T, error)

Retry retries the given function until it succeeds or the maximum number of attempts is reached.

func RetryWithContextGen

func RetryWithContextGen[T any](
	ctxGen func() (context.Context, context.CancelFunc),
	fn func(context.Context) (T, error),
	maxAttempts int,
	retryInterval time.Duration,
) (T, error)

RetryWithContextGen retries the given function until it succeeds or the maximum number of attempts is reached. For each retry, it generates a fresh context to be used on the call.

func SearchSHA256File

func SearchSHA256File(file []byte, toSearch string) (string, error)

func Sort

func Sort[T Sortable[T]](s []T)

Sorts the elements of [s].

func SortByHash

func SortByHash[T ~[]byte](s []T)

Sorts the elements of [s] based on their hashes.

func SplitComaSeparatedInt

func SplitComaSeparatedInt(s string) []int

SplitComaSeparatedInt splits a comma-separated string into a slice of integers.

func SplitComaSeparatedString

func SplitComaSeparatedString(s string) []string

SplitComaSeparatedString splits and trims a comma-separated string into a slice of strings.

func SplitStringWithQuotes

func SplitStringWithQuotes(str string, r rune) []string

SplitStringWithQuotes splits a string by the given rune, ignoring quoted segments.

func TrimHexa

func TrimHexa(s string) string

TrimHexa removes the leading 0x/0X part of a hexadecimal string representation.

func Uint32Sort

func Uint32Sort(arr []uint32)

func Unique

func Unique[T comparable](arr []T) []T

Unique returns a new slice containing only the unique elements from the input slice.

func WrapContext

func WrapContext[T any](
	f func() (T, error),
) func(context.Context) (T, error)

WrapContext adds a context based timeout to a given function

func Zero

func Zero[T any]() T

Zero returns the zero value of any type T.

Types

type Atomic

type Atomic[T any] = atomic.Atomic[T]

Atomic is a re-export from the standalone atomic module for backward compatibility. Use github.com/luxfi/atomic directly for new code.

func NewAtomic

func NewAtomic[T any](value T) *Atomic[T]

NewAtomic creates a new Atomic with the given initial value. Use github.com/luxfi/atomic.NewAtomic directly for new code.

type BytesPool

type BytesPool [intSize]sync.Pool

BytesPool tracks buckets of available buffers to be allocated. Each bucket allocates buffers of the following length:

0 1 3 7 15 31 63 127 ... MaxInt

In order to allocate a buffer of length 19 (for example), we calculate the number of bits required to represent 19 (5). And therefore allocate a slice from bucket 5, which has length 31. This is the bucket which produces the smallest slices that are at least length 19.

When replacing a buffer of length 19, we calculate the number of bits required to represent 20 (5). And therefore place the slice into bucket 4, which has length 15. This is the bucket which produces the largest slices that a length 19 slice can be used for.

func NewBytesPool

func NewBytesPool() *BytesPool

func (*BytesPool) Get

func (p *BytesPool) Get(length int) *[]byte

Get returns a non-nil pointer to a slice with the requested length.

It is not guaranteed for the returned bytes to have been zeroed.

func (*BytesPool) Put

func (p *BytesPool) Put(bytes *[]byte)

Put takes ownership of a non-nil pointer to a slice of bytes.

Note: this function takes ownership of the underlying array. So, the length of the provided slice is ignored and only its capacity is used.

type Sortable

type Sortable[T any] interface {
	Compare(T) int
}

Directories

Path Synopsis
Package bimap provides a bidirectional map implementation.
Package bimap provides a bidirectional map implementation.
Package json provides JSON serialization utilities for numeric types and a gorilla/rpc Codec that normalizes lowercase method names.
Package json provides JSON serialization utilities for numeric types and a gorilla/rpc Codec that normalizes lowercase method names.

Jump to

Keyboard shortcuts

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