Documentation ¶
Index ¶
- Constants
- func CompressBuffer(data []byte) ([]byte, error)
- func Contains[T comparable](slice []T, item T) bool
- func DecompressBuffer(s []byte) ([]byte, error)
- func Equal[T comparable](a, b []T) bool
- func Extend[T any](slice []T, length int) []T
- func FormatBytesToHumanReadable(bytes uint64) string
- func I2OSP(num *big.Int, len int) []byte
- func Int16ToSlice(n int16) []byte
- func Int32ToSlice(n int32) []byte
- func Int64ToSlice(n int64) []byte
- func IsAbsPath(path string) bool
- func IsDirEmpty(path string) bool
- func IsDirNotExistsOrEmpty(path string) bool
- func IsFlagSet[T constraints.Integer](flags, mask T) bool
- func IsSubset[T comparable](parentSet, subSet []T) bool
- func IsValidDirPath(path string) bool
- func ListFilesInDir(dir string) ([]string, error)
- func LogScale(val int) int
- func MakeAbs(path string) string
- func Max[T constraints.Integer](a, b T) T
- func Merge[T any](slices ...[]T) []T
- func Min[T constraints.Integer](a, b T) T
- func Mkdir(path string) error
- func MoveDirectory(srcDir, dstDir string) error
- func OS2IP(x []byte) *big.Int
- func PathExists(path string) bool
- func RandInt16(max int16) int16
- func RandInt32(max int32) int32
- func RandInt64(max int64) int64
- func RandUint16(max uint32) uint16
- func RandUint32(max uint32) uint32
- func RandUint64(max uint64) uint64
- func ReadFile(filename string) ([]byte, error)
- func RemoveFirstOccurrenceOf[T comparable](slice []T, element T) ([]T, bool)
- func Reverse[S ~[]E, E any](slice S)
- func RoundNow(sec int) time.Time
- func SafeCmp(left, right []byte) bool
- func SanitizeArchivePath(baseDir, archivePath string) (fullPath string, err error)
- func SetFlag[T constraints.Integer](flags, mask T) T
- func Shuffle[T any](slice []T)
- func SliceToInt16(bs []byte) int16
- func SliceToInt32(bs []byte) int32
- func SliceToInt64(bs []byte) int64
- func SliceToUint16(bs []byte) uint16
- func SliceToUint32(bs []byte) uint32
- func SliceToUint64(bs []byte) uint64
- func StringToBytes(s string) []byte
- func Subtracts(slice1, slice2 []int32) []int32
- func TempDirPath() string
- func TempFilePath() string
- func Trim[T any](slice []T, newLength int) []T
- func Uint16ToSlice(n uint16) []byte
- func Uint32ToSlice(n uint32) []byte
- func Uint64ToSlice(n uint64) []byte
- func UnsetFlag[T constraints.Integer](flags, mask T) T
- func WriteFile(filename string, data []byte) error
- type FixedReader
- type FixedWriter
Constants ¶
const ( MaxUint16 = ^uint16(0) MinUint16 = 0 MaxInt16 = int16(MaxUint16 >> 1) MinInt16 = -MaxInt16 - 1 )
const ( MaxUint32 = ^uint32(0) MinUint32 = 0 MaxInt32 = int32(MaxUint32 >> 1) MinInt32 = -MaxInt32 - 1 )
const ( MaxUint64 = ^uint64(0) MinUint64 = 0 MaxInt64 = int64(MaxUint64 >> 1) MinInt64 = -MaxInt64 - 1 )
Variables ¶
This section is empty.
Functions ¶
func CompressBuffer ¶
func Contains ¶
func Contains[T comparable](slice []T, item T) bool
Contains checks whether the given slice has a specific item.
func DecompressBuffer ¶
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
Extend extends the slice to the given length by appending zero-valued elements.
func FormatBytesToHumanReadable ¶ added in v1.4.0
func I2OSP ¶ added in v1.1.0
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 Int32ToSlice ¶
func Int64ToSlice ¶
func IsDirNotExistsOrEmpty ¶
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 ListFilesInDir ¶ added in v1.6.0
ListFilesInDir return list of files in directory.
func LogScale ¶ added in v0.17.0
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 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 MoveDirectory ¶ added in v1.4.0
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 ¶
OS2IP converts an octet string to a nonnegative integer. OS2IP: https://datatracker.ietf.org/doc/html/rfc8017#section-4.2
func PathExists ¶
func RandInt16 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
RandUint64 returns a random uint64 in between 0 and max. If max set to zero or negative, the max will set to MaxUint64.
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 ¶
RoundNow returns the result of rounding sec to the current time in UTC. The rounding behavior is rounding down.
func SafeCmp ¶
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
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 SliceToInt16 ¶
func SliceToInt32 ¶
func SliceToInt64 ¶
func SliceToUint16 ¶
func SliceToUint32 ¶
func SliceToUint64 ¶
func StringToBytes ¶ added in v0.15.0
StringToBytes converts a string to a slice of bytes.
func Subtracts ¶
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 Uint16ToSlice ¶
func Uint32ToSlice ¶
func Uint64ToSlice ¶
func UnsetFlag ¶
func UnsetFlag[T constraints.Integer](flags, mask T) T
UnsetFlag removes mask from the flags.
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.