util

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: MIT Imports: 16 Imported by: 1

Documentation

Index

Constants

View Source
const (
	MaxUint16 = ^uint16(0)
	MinUint16 = 0
	MaxInt16  = int16(MaxUint16 >> 1)
	MinInt16  = -MaxInt16 - 1
)
View Source
const (
	MaxUint32 = ^uint32(0)
	MinUint32 = 0
	MaxInt32  = int32(MaxUint32 >> 1)
	MinInt32  = -MaxInt32 - 1
)
View Source
const (
	MaxUint64 = ^uint64(0)
	MinUint64 = 0
	MaxInt64  = int64(MaxUint64 >> 1)
	MinInt64  = -MaxInt64 - 1
)

Variables

This section is empty.

Functions

func CompressBuffer

func CompressBuffer(data []byte) ([]byte, error)

func Contains

func Contains[T comparable](slice []T, item T) bool

Contains checks whether the given slice has a specific item.

func DecompressBuffer

func DecompressBuffer(s []byte) ([]byte, error)

func Equal

func Equal[T comparable](a, b []T) bool

Equal tells whether a and b contain the same elements. A nil argument is equivalent to an empty slice.

func Extend added in v0.15.0

func Extend[T any](slice []T, length int) []T

Extend extends the slice to the given length by appending zero-valued elements.

func FormatBytesToHumanReadable added in v1.4.0

func FormatBytesToHumanReadable(bytes uint64) string

func I2OSP added in v1.1.0

func I2OSP(num *big.Int, len int) []byte

I2OSP converts a nonnegative integer to an octet string of a specified length. https://datatracker.ietf.org/doc/html/rfc8017#section-4.1

func Int16ToSlice

func Int16ToSlice(n int16) []byte

func Int32ToSlice

func Int32ToSlice(n int32) []byte

func Int64ToSlice

func Int64ToSlice(n int64) []byte

func IsAbsPath

func IsAbsPath(path string) bool

func IsDirEmpty

func IsDirEmpty(path string) bool

IsDirEmpty checks if a directory is empty.

func IsDirNotExistsOrEmpty

func IsDirNotExistsOrEmpty(path string) bool

IsDirNotExistsOrEmpty checks if the path exists and, if so, whether the directory is empty.

func IsFlagSet

func IsFlagSet[T constraints.Integer](flags, mask T) bool

IsFlagSet checks if the mask is set for the given flags.

func IsSubset added in v0.15.0

func IsSubset[T comparable](parentSet, subSet []T) bool

IsSubset checks if subSet is a subset of parentSet. It returns true if all elements of subSet are in parentSet.

func IsValidDirPath

func IsValidDirPath(path string) bool

func ListFilesInDir added in v1.6.0

func ListFilesInDir(dir string) ([]string, error)

ListFilesInDir return list of files in directory.

func LogScale added in v0.17.0

func LogScale(val int) int

LogScale computes 2^⌈log₂(val)⌉, where ⌈x⌉ represents the ceiling of x. For more information, refer to: https://en.wikipedia.org/wiki/Logarithmic_scale

func MakeAbs

func MakeAbs(path string) string

func Max added in v0.15.0

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

Max returns the biggest of two integer numbers.

func Merge added in v0.10.0

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

Merge accepts multiple slices and returns a single merged slice.

func Min added in v0.15.0

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

Min returns the smallest of two integer numbers.

func Mkdir

func Mkdir(path string) error

func MoveDirectory added in v1.4.0

func MoveDirectory(srcDir, dstDir string) error

MoveDirectory moves a directory from srcDir to dstDir, including all its contents. If dstDir already exists and is not empty, it returns an error. If the parent directory of dstDir does not exist, it will be created.

func OS2IP

func OS2IP(x []byte) *big.Int

OS2IP converts an octet string to a nonnegative integer. OS2IP: https://datatracker.ietf.org/doc/html/rfc8017#section-4.2

func PathExists

func PathExists(path string) bool

func RandInt16

func RandInt16(max int16) int16

RandInt16 returns a random int16 in between 0 and max. If max set to zero or negative, the max will set to MaxInt16.

func RandInt32

func RandInt32(max int32) int32

RandInt32 returns a random int32 in between 0 and max. If max set to zero or negative, the max will set to MaxInt32.

func RandInt64

func RandInt64(max int64) int64

RandInt64 returns a random int64 in between 0 and max. If max set to zero or negative, the max will set to MaxInt64.

func RandUint16

func RandUint16(max uint32) uint16

RandUint16 returns a random uint16 in between 0 and max. If max set to zero or negative, the max will set to MaxUint16.

func RandUint32

func RandUint32(max uint32) uint32

RandUint32 returns a random uint32 in between 0 and max. If max set to zero or negative, the max will set to MaxUint32.

func RandUint64

func RandUint64(max uint64) uint64

RandUint64 returns a random uint64 in between 0 and max. If max set to zero or negative, the max will set to MaxUint64.

func ReadFile

func ReadFile(filename string) ([]byte, error)

func RemoveFirstOccurrenceOf added in v0.15.0

func RemoveFirstOccurrenceOf[T comparable](slice []T, element T) ([]T, bool)

RemoveFirstOccurrenceOf removes the first occurrence of element from slice. It returns the modified slice and a boolean indicating whether an element was removed.

func Reverse

func Reverse[S ~[]E, E any](slice S)

Reverse replace the contents of a slice with the same elements but in reverse order.

func RoundNow

func RoundNow(sec int) time.Time

RoundNow returns the result of rounding sec to the current time in UTC. The rounding behavior is rounding down.

func SafeCmp

func SafeCmp(left, right []byte) bool

SafeCmp compares two slices with constant time. Note that we are using the subtle.ConstantTimeCompare() function for this to help prevent timing attacks.

func SanitizeArchivePath added in v1.4.0

func SanitizeArchivePath(baseDir, archivePath string) (fullPath string, err error)

SanitizeArchivePath mitigates the "Zip Slip" vulnerability by sanitizing archive file paths. It ensures that the file path is contained within the specified base directory to prevent directory traversal attacks. For more details on the vulnerability, see https://snyk.io/research/zip-slip-vulnerability.

func SetFlag

func SetFlag[T constraints.Integer](flags, mask T) T

SetFlag applies mask to the flags.

func Shuffle added in v1.2.0

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

Shuffle shuffles a slice of any type.

func SliceToInt16

func SliceToInt16(bs []byte) int16

func SliceToInt32

func SliceToInt32(bs []byte) int32

func SliceToInt64

func SliceToInt64(bs []byte) int64

func SliceToUint16

func SliceToUint16(bs []byte) uint16

func SliceToUint32

func SliceToUint32(bs []byte) uint32

func SliceToUint64

func SliceToUint64(bs []byte) uint64

func StringToBytes added in v0.15.0

func StringToBytes(s string) []byte

StringToBytes converts a string to a slice of bytes.

func Subtracts

func Subtracts(slice1, slice2 []int32) []int32

Subtracts subtracts slice2 from slice1 in order. Examples:

[1,2,3,4] - [1,2] = [3,4]
[1,2,3,4] - [2,4] = [1,3]
[1,2,3,4] - [4,2] = [1,3]
[1,2,3,4] - [4,5] = [1,2,3]

.

func TempDirPath

func TempDirPath() string

func TempFilePath

func TempFilePath() string

func Trim added in v0.15.0

func Trim[T any](slice []T, newLength int) []T

func Uint16ToSlice

func Uint16ToSlice(n uint16) []byte

func Uint32ToSlice

func Uint32ToSlice(n uint32) []byte

func Uint64ToSlice

func Uint64ToSlice(n uint64) []byte

func UnsetFlag

func UnsetFlag[T constraints.Integer](flags, mask T) T

UnsetFlag removes mask from the flags.

func WriteFile

func WriteFile(filename string, data []byte) error

Types

type FixedReader

type FixedReader struct {
	// contains filtered or unexported fields
}

FixedReader implements the io.Reader interface and intentionally allows testing of error paths by forcing short reads.

func NewFixedReader

func NewFixedReader(max int, data []byte) *FixedReader

NewFixedReader returns a new io.Reader that will error once more bytes than the specified max have been read.

func (*FixedReader) Read

func (fr *FixedReader) Read(p []byte) (int, error)

Read reads the next len(p) bytes from the fixed reader. When the number of bytes read would exceed the maximum number of allowed bytes to be read from the fixed writer, an error is returned.

This satisfies the io.Reader interface.

type FixedWriter

type FixedWriter struct {
	// contains filtered or unexported fields
}

FixedWriter implements the io.Writer interface and intentionally allows testing of error paths by forcing short writes.

func NewFixedWriter

func NewFixedWriter(max int) *FixedWriter

NewFixedWriter returns a new io.Writer that will error once more bytes than the specified max have been written.

func (*FixedWriter) Bytes

func (w *FixedWriter) Bytes() []byte

Bytes returns the bytes already written to the fixed writer.

func (*FixedWriter) Write

func (w *FixedWriter) Write(data []byte) (int, error)

Write writes the contents of p to w. When the contents of p would cause the writer to exceed the maximum allowed size of the fixed writer, io.ErrShortWrite is returned and the writer is left unchanged.

This satisfies the io.Writer interface.

Jump to

Keyboard shortcuts

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