util

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 14 Imported by: 2

Documentation

Index

Constants

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

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(a, b []int32) bool

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

func ExitOnErr

func ExitOnErr(e error)

ExitOnErr exit the software immediately if an error happens. Panics are not safe because panics print a stack trace, which may not be relevant to the error at all.

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

func IsDirEmpty

func IsDirEmpty(name string) bool

func IsDirNotExistsOrEmpty

func IsDirNotExistsOrEmpty(name string) bool

func IsFlagSet

func IsFlagSet(flags, mask int) bool

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

func IsValidDirPath

func IsValidDirPath(fp string) bool

func MakeAbs

func MakeAbs(path string) string

func Max32

func Max32(a, b int32) int32

Max32 returns the biggest of two 32-bits numbers.

func Max64

func Max64(a, b int64) int64

Max64 returns the biggest of two 64-bits numbers.

func MaxU32

func MaxU32(a, b uint32) uint32

MaxU32 returns the biggest of two 32-bits unsigned 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 Min32

func Min32(a, b int32) int32

Min32 returns the smallest of two 32-bits numbers.

func Min64

func Min64(a, b int64) int64

Min64 returns the smallest of two 64-bits numbers.

func MinU32

func MinU32(a, b uint32) uint32

MinU32 returns the smallest of two 32-bits unsigned 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(path string) bool

func RandInt16

func RandInt16(max int16) int16

RandInt32 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

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

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 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(flags, mask int) int

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 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 []int32, 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 Uint16ToSlice

func Uint16ToSlice(n uint16) []byte

func Uint32ToSlice

func Uint32ToSlice(n uint32) []byte

func Uint64ToSlice

func Uint64ToSlice(n uint64) []byte

func UnsetFlag

func UnsetFlag(flags, mask int) int

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) (n int, err 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) (n int, err 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