util

package
v1.0.0-rc-0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2024 License: MIT Imports: 15 Imported by: 2

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 ChangeToCoin

func ChangeToCoin(change int64) float64

ChangeToCoin converts a change value to its corresponding coin amount. Example: ChangeToCoin(2750000000) returns 2.75.

func ChangeToString

func ChangeToString(change int64) string

ChangeToString converts a change value to its string representation. Example: ChangeToString(2750000000) returns "2.75".

func ChangeToStringWithTrailingZeros added in v0.11.0

func ChangeToStringWithTrailingZeros(change int64) string

ChangeToStringWithTrailingZeros converts a change value to its string representation with trailing zeros. The returned string will have up to 9 decimal places. Example: ChangeToStringWithTrailingZeros(2750000000) returns "2.750000000".

func CoinToChange

func CoinToChange(coin float64) int64

CoinToChange converts a coin amount to its corresponding change value. Example: CoinToChange(2.75) returns 2750000000.

func CompressBuffer

func CompressBuffer(s []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](s *[]T, n int)

Extend extends the slice 's' to length 'n' by appending zero-valued elements.

func IS2OP

func IS2OP(x *big.Int, xLen 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(p string) bool

func IsDirEmpty

func IsDirEmpty(name string) bool

func IsDirNotExistsOrEmpty

func IsDirNotExistsOrEmpty(name string) bool

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(fp string) bool

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(p 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(dir string) error

func Now

func Now() time.Time

Now returns the rounded current time in UTC. The rounding behavior is rounding down.

func OS2IP

func OS2IP(os []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(p 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](s []T, e T) ([]T, bool)

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

func Reverse

func Reverse[S ~[]E, E any](s 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(s1, s2 []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 SetFlag

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

SetFlag applies mask to the flags.

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 StringToChange

func StringToChange(amount string) (int64, error)

StringToChange converts a string representation of a coin amount to its corresponding change value. It returns the change value as an int64 and an error if the string cannot be parsed. Example: StringToChange("2.75") returns 2750000000, nil.

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](s []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, buf []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(p []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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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