utils

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: MIT Imports: 21 Imported by: 0

README

Utils

Documentation

Index

Constants

View Source
const (
	SMALL_LINE_SEP = "------------------------------"
)
View Source
const UPPER_HEX_DIGITS = "0123456789ABCDEF"

Variables

View Source
var ANSI_ESCAPE_SEQUENCE_REGEX = regexp.MustCompile("[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))")
View Source
var (
	ErrInvalidPercentEncodedString = errors.New("invalid percent encoded string")
)
View Source
var ErrOutOfSpace = errors.New("out of space")
View Source
var (
	MATCHALL_REGEX = regexp.MustCompile(".*")
)

Functions

func Abs

func Abs[T constraints.Integer](a T) T

func AddCarriageReturnAfterNewlines

func AddCarriageReturnAfterNewlines(s string) string

func AssertNoGoroutineLeak

func AssertNoGoroutineLeak(t *testing.T, startCount int)

AssertNoGoroutineLeak checks

func AssertNoMemoryLeak

func AssertNoMemoryLeak(t *testing.T, startStats *runtime.MemStats, maxAllocDelta uint64, opts ...AssertNoMemoryLeakOptions)

AssertNoMemoryLeak checks that at most maxAllocDelta bytes have been allocated since the passed memory stats have been collected. This function should be called at the end of a long test suite or test case.

func BytesAsString

func BytesAsString(b []byte) string

func Catch

func Catch(fn func()) (result error)

func CombineErrors

func CombineErrors(errs ...error) error

CombineErrors combines errors into a single error with a multiline message.

func CombineErrorsWithPrefixMessage

func CombineErrorsWithPrefixMessage(prefixMsg string, errs ...error) error

CombineErrorsWithPrefixMessage combines errors into a single error with a multiline message.

func ConvertPanicValueToError

func ConvertPanicValueToError(v any) error

func ConvertStringSlice

func ConvertStringSlice[A ~string, B ~string](s []A) []B

func CountDigits

func CountDigits(n int64) int

func CountPrevBackslashes

func CountPrevBackslashes[T constraints.Integer](s []T, i int32) int32

func DefaultIfZero

func DefaultIfZero[T constraints.Integer](v, defaultValue T) T

func EmptySliceIfNil

func EmptySliceIfNil[T any](slice []T) []T

func EqualMaps

func EqualMaps[K comparable, V any](m1 map[K]V, m2 map[K]V, eql func(v1, v2 V) bool) bool

func FilterMapSlice

func FilterMapSlice[T any, U any](s []T, mapper func(e T) (U, bool)) []U

func FilterSlice

func FilterSlice[T any](s []T, filter func(e T) bool) []T

func FilterSliceByType

func FilterSliceByType[T any, V any](s []T, filter V) []V

func FindClosest

func FindClosest[T any](search ClosestSearch[T]) (sourceIndex int, minDistance int)

func FindClosestString

func FindClosestString(ctx context.Context, candidates []string, v string, maxDifferences int) (string, int, bool)

func FindDoubleLineSequence

func FindDoubleLineSequence(bytes []byte) (index int, length int)

FindDoubleLineSequence returns the index of a double line sequence (see further), and the sequence's length in bytes. If no double lines sequence is present, the index will be negative. A double line sequence is one of the following sequences: \r\r, \n\n, \r\n\n, \n\r\n, \r\n\r\n

func FindLongestCommonPrefix

func FindLongestCommonPrefix(strs []string) string

func GetByteSize

func GetByteSize[T any]() uintptr

func HexDigitToByte

func HexDigitToByte(c byte) byte

func If

func If[T any](cond bool, a, b T) T

If cond is true a is returned, else b is returned.

func Implements

func Implements[T any](v any) bool

func IndentLines

func IndentLines(s string, indent string) string

func InefficientlyWaitUntilFalse

func InefficientlyWaitUntilFalse(b *atomic.Bool, timeout time.Duration) bool

InefficientlyWaitUntilFalse checks every ~millisecond if *b is false, it returns true if *b is false or returns false on timeout.

func InefficientlyWaitUntilTrue

func InefficientlyWaitUntilTrue(b *atomic.Bool, timeout time.Duration) bool

InefficientlyWaitUntilTrue checks every ~millisecond if *b is true, it returns true if *b is true or returns false on timeout.

func IsContextDone

func IsContextDone(ctx context.Context) bool

func IsHexDigit

func IsHexDigit(c byte) bool

func IsWholeInt64

func IsWholeInt64[F ~float64](f F) bool

func MapSlice

func MapSlice[T any, U any](s []T, mapper func(e T) U) []U

func MapSliceIndexed

func MapSliceIndexed[T any, U any](s []T, mapper func(e T, i int) U) []U

func MarshalIndentJsonNoHTMLEspace

func MarshalIndentJsonNoHTMLEspace(v any, prefix, indent string) ([]byte, error)

func MarshalJsonNoHTMLEspace

func MarshalJsonNoHTMLEspace(v any) ([]byte, error)

func Max

func Max[T constraints.Ordered](a, b T) T

func Min

func Min[T constraints.Ordered](a T, b T) T

func MinMaxPossibleRuneCount

func MinMaxPossibleRuneCount(byteCount int) (int, int)

func MoveCursorNextLine

func MoveCursorNextLine(writer io.Writer, n int)

func Must

func Must[T any](a T, err error) T

func Must2

func Must2[T any, U any](a T, b U, err error) (T, U)

func MustGet

func MustGet[T any](a T, found bool) T

func MustWriteMany

func MustWriteMany[W io.Writer](w W, slices ...[]byte)

func New

func New[T any](v T) *T

func PanicIfErr

func PanicIfErr(err error)

func PanicIfErrAmong

func PanicIfErrAmong(errs ...error)

func PercentDecode

func PercentDecode(s string, allowNotEncoded bool) (string, error)

func PercentEncode

func PercentEncode(s string) string

func PrintSmallLineSeparator

func PrintSmallLineSeparator(w io.Writer)

func RandFloat

func RandFloat(low, high float64, seed uint64) float64

func Recover

func Recover() any

Recover only calls recover. From recover()'s doc:

If recover is called outside the deferred function it will
not stop a panicking sequence.

func RemoveIndexOfSlice

func RemoveIndexOfSlice[T any](s []T, index int) []T

func Repeat

func Repeat[T any](n int, fn func(index int) T) []T

func RepeatValue

func RepeatValue[T any](n int, v T) []T

func Ret

func Ret[V any](v V) func() V

func Ret0

func Ret0[A, B any](a A, b B) A

func Ret1

func Ret1[A, B any](a A, b B) B

func ReversedSlice

func ReversedSlice[T any](s []T) []T

func SameKeys

func SameKeys[K comparable, V any](m1 map[K]V, m2 map[K]V) bool

func SamePointer

func SamePointer(a, b interface{}) bool

func ShrinkSliceIfWastedCapacity

func ShrinkSliceIfWastedCapacity[E any](slice []E, minShrinkableStackLength int, stackShrinkDivider int) []E

func SliceContains

func SliceContains[T constraints.Ordered](slice []T, v T) bool

func Some

func Some[T any](s []T, predicate func(e T) bool) bool

func StringAsBytes

func StringAsBytes[T ~string](s T) []byte

func StripANSISequences

func StripANSISequences(str string) string

func WriteMany

func WriteMany[W io.Writer](w W, slices ...[]byte) error

Types

type AssertNoMemoryLeakOptions

type AssertNoMemoryLeakOptions struct {
	// duration of sleep before collection of memory stats,
	// defaults to defaultPreSleepDuration.
	PreSleepDurationMillis uint8

	// if true AssertNoMemoryLeak checks that the number of goroutines has not increased.
	CheckGoroutines bool

	// number of goroutines at the beginning,
	GoroutineCount int

	MaxGoroutineCountDelta uint8
}

type ClosestSearch

type ClosestSearch[T any] struct {
	MaxDifferences           int
	Target                   T
	GetSourceI               func(i int) (source T, ok bool)
	OptionalFilter           func(source T, target T) bool
	ComputeLevenshteinMatrix func(source T, target T) [][]int
	IsRelevant               func(candidate T, distance int) bool //can be nil
	Context                  context.Context
}

type FixedBufferWriter

type FixedBufferWriter []byte

FixedBufferWriter writes data in the wrapper byte slice, it never allocates a new slice. ErrOutOfSpace is returned if there not enough space.

func (*FixedBufferWriter) Write

func (w *FixedBufferWriter) Write(p []byte) (int, error)

type FnReader

type FnReader func(p []byte) (n int, err error)

func (FnReader) Read

func (fn FnReader) Read(p []byte) (n int, err error)

type FnReaderWriter

type FnReaderWriter struct {
	WriteFn func(p []byte) (n int, err error)
	ReadFn  func(p []byte) (n int, err error)
}

func (FnReaderWriter) Read

func (w FnReaderWriter) Read(p []byte) (n int, err error)

func (FnReaderWriter) Write

func (w FnReaderWriter) Write(p []byte) (n int, err error)

type FnReaderWriterCloser

type FnReaderWriterCloser struct {
	WriteFn func(p []byte) (n int, err error)
	ReadFn  func(p []byte) (n int, err error)
	CloseFn func() error
}

func (FnReaderWriterCloser) Close

func (w FnReaderWriterCloser) Close() error

func (FnReaderWriterCloser) Read

func (w FnReaderWriterCloser) Read(p []byte) (n int, err error)

func (FnReaderWriterCloser) Write

func (w FnReaderWriterCloser) Write(p []byte) (n int, err error)

type FnWriter

type FnWriter struct {
	WriteFn func(p []byte) (n int, err error)
}

func (FnWriter) Write

func (writer FnWriter) Write(p []byte) (n int, err error)

type TestWriter

type TestWriter struct {
	T *testing.T
}

func (*TestWriter) Write

func (w *TestWriter) Write(p []byte) (n int, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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