util

package
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Kbit = bit * 1000  // 10^3
	Mbit = Kbit * 1000 // 10^6
	Gbit = Mbit * 1000 // 10^9
	Tbit = Gbit * 1000 // 10^12
	Pbit = Tbit * 1000 // 10^15
	Ebit = Pbit * 1000 // 10^18
	Zbit = Ebit * 1000 // 10^21
	Ybit = Zbit * 1000 // 10^24
)
View Source
const (
	B  ByteSize = 1
	KB          = B * 1000  // 10^3
	MB          = KB * 1000 // 10^6
	GB          = MB * 1000 // 10^9
	TB          = GB * 1000 // 10^12
	PB          = TB * 1000 // 10^15
	EB          = PB * 1000 // 10^18
	ZB          = EB * 1000 // 10^21
	YB          = ZB * 1000 // 10^24
)

Variables

View Source
var (
	IsAMD64                bool // is this an x86 AMD64 CPU
	UseAVX2                bool // AVX-2 available
	UseAVX512_F            bool // AVX-512 Foundation Instructions
	UseAVX512_DQ           bool // AVX-512 Doubleword & Quadword Instrs
	UseAVX512_IFMA         bool // AVX-512 Integer Fused Multiply Add
	UseAVX512_PF           bool // AVX-512 Prefetch Instructions
	UseAVX512_ER           bool // AVX-512 Exponent & Reciprocal Instrs
	UseAVX512_CD           bool // AVX-512 Conflict Detection Instrs
	UseAVX512_BW           bool // AVX-512 Byte and Word Instructions
	UseAVX512_VL           bool // AVX-512 Vector Length Extensions
	UseAVX512_VBMI         bool // AVX-512 Vector Byte Manipulation Instrs
	UseAVX512_BITALG       bool // Support for VPOPCNT[B,W] and VPSHUFBITQMB
	UseAVX512_VPOPCNTDQ    bool // POPCNT for vectors of DW/QW
	UseAVX512_4VNNIW       bool // AVX512 Neural Network Instructions
	UseAVX512_4FMAPS       bool // AVX512 Multiply Accumulation Single Precision
	UseAVX512_VP2INTERSECT bool // VP2INTERSECT{D,Q} insns
	UseAVX512_BF16         bool // AVX512 BFloat16 Instructions
)
View Source
var (
	EnableAVX     = noop
	DisableAVX2   = noop
	DisableAVX512 = noop
)
View Source
var EInvalidBitrate = errors.New("invalid bitrate")
View Source
var EInvalidByteSize = errors.New("invalid byte size")
View Source
var TimeFormats []string = []string{
	time.RFC3339,
	"02.01.2006T15:04:05.999999999Z07:00",
	"02.01.2006T15:04:05Z07:00",
	"02.01.2006 15:04:05.999999999Z07:00",
	"02.01.2006 15:04:05Z07:00",
	"2006:01:02 15:04:05.999999999-07:00",
	"2006:01:02 15:04:05-07:00",
	"2006:01:02:15:04:05-07:00",
	"2006:01:02:15:04:05-07",
	"2006-01-02T15:04:05.999999999Z",
	"2006-01-02T15:04:05Z",
	"2006-01-02 15:04:05.999999999Z",
	"2006-01-02 15:04:05Z",
	"02.01.2006T15:04:05.999999999Z",
	"02.01.2006T15:04:05Z",
	"02.01.2006 15:04:05.999999999Z",
	"02.01.2006 15:04:05Z",
	"2006-01-02T15:04:05.999999999",
	"2006-01-02T15:04:05",
	"2006-01-02 15:04:05.999999999",
	"2006-01-02 15:04:05",
	"02.01.2006T15:04:05.999999999",
	"02.01.2006T15:04:05",
	"02.01.2006 15:04:05.999999999",
	"02.01.2006 15:04:05",
	"2006-01-02T15:04",
	"2006-01-02 15:04",
	"02.01.2006T15:04",
	"02.01.2006 15:04",
	"2006-01-02",
	"02.01.2006",
	"2006-01",
	"01.2006",
	"15:04:05",
	"15:04",
	"2006",
}

Functions

func Abs

func Abs[T constraints.Signed](n T) T

func Basename

func Basename(s string) string

func Clamp

func Clamp[T constraints.Ordered](val, minIn, maxIn T) T

func ClampTime

func ClampTime(val, min, max time.Time) time.Time

func ContainsString

func ContainsString(s string, list []string) bool

func DaysBetween

func DaysBetween(a, b time.Time) int

func DaysIn

func DaysIn(m time.Month, year int) int

DaysIn returns the number of days in a month for a given year.

func EscapeWhitespace

func EscapeWhitespace(s string) string

func FirstNonZeroTime

func FirstNonZeroTime(val time.Time, others ...time.Time) time.Time

func Fqdn

func Fqdn() string

func FromCamelCase

func FromCamelCase(src, sep string) string

func HashString

func HashString(s string) string

func HexString

func HexString(v interface{}) string

func Int16AsUint16Slice

func Int16AsUint16Slice(src []int16) []uint16

func Int32AsUint32Slice

func Int32AsUint32Slice(src []int32) []uint32

func Int64AsUint64Slice

func Int64AsUint64Slice(src []int64) []uint64

func Int8AsUint8Slice

func Int8AsUint8Slice(src []int8) []uint8

func IsASCII

func IsASCII(s string) bool

func IsBase64

func IsBase64(s string) bool

func JoinString

func JoinString(sep string, more ...string) string

func JsonString

func JsonString(v interface{}) string

func LimitString

func LimitString(s string, l int) string

limits the length of string to l UTF8 runes

func LimitStringEllipsis

func LimitStringEllipsis(s string, l int) string

func LogCPUFeatures

func LogCPUFeatures(l *log.Logger)

func MaxBytes

func MaxBytes(a, b []byte) []byte

func MaxDuration

func MaxDuration(a, b time.Duration) time.Duration

func MaxTime

func MaxTime(x, y time.Time) time.Time

func MaxTimeN

func MaxTimeN(t ...time.Time) time.Time

func MinBytes

func MinBytes(a, b []byte) []byte

func MinDuration

func MinDuration(a, b time.Duration) time.Duration

func MinMax

func MinMax[T constraints.Ordered](vals ...T) (T, T)

func MinMaxTime

func MinMaxTime(s []time.Time) (time.Time, time.Time)

func MinTime

func MinTime(x, y time.Time) time.Time

func MinTimeN

func MinTimeN(t ...time.Time) time.Time

func NonEmptyString

func NonEmptyString(s ...string) string

func NonZero

func NonZero[T constraints.Ordered](x ...T) T

func NonZeroMin

func NonZeroMin[T constraints.Ordered](x ...T) T

func NonZeroSince

func NonZeroSince(start time.Time) time.Duration

func Pow added in v0.2.8

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

Donald Knuth, The Art of Computer Programming, Volume 2, Section 4.6.3

func Pretty

func Pretty(val interface{}) string

func PrettyFloat64

func PrettyFloat64(f float64) string

func PrettyFloat64N

func PrettyFloat64N(f float64, decimals int) string

func PrettyInt

func PrettyInt(i int) string

func PrettyInt64

func PrettyInt64(i int64) string

func PrettyString

func PrettyString(s string) string

not pretty, but works: 1000000.123 -> 1,000,000.123

func PrettyUint64

func PrettyUint64(i uint64) string

func QuoteString

func QuoteString(s string) string

func RoundToDays

func RoundToDays(d time.Duration) int

func RoundToHours

func RoundToHours(d time.Duration) int64

func RoundToMillisecond

func RoundToMillisecond(d time.Duration) int64

func RoundToMinutes

func RoundToMinutes(d time.Duration) int64

func RoundToSeconds

func RoundToSeconds(d time.Duration) int64

func StepsBetween

func StepsBetween(t1, t2 time.Time, d time.Duration) []time.Time

func ToCamelCase

func ToCamelCase(src, sep string) string

func ToRawString

func ToRawString(t interface{}) (string, error)

func ToString

func ToString(t interface{}) string

func TrimAllSpace

func TrimAllSpace(str string) string

func TruncateDuration

func TruncateDuration(d, r time.Duration) time.Duration

func Uint16AsInt16Slice

func Uint16AsInt16Slice(src []uint16) []int16

func Uint32AsInt32Slice

func Uint32AsInt32Slice(src []uint32) []int32

func Uint64AsInt64Slice

func Uint64AsInt64Slice(src []uint64) []int64

func Uint8AsInt8Slice

func Uint8AsInt8Slice(src []uint8) []int8

func UnescapeWhitespace

func UnescapeWhitespace(s string) string

func UniqueStrings

func UniqueStrings(s []string) []string

func UnixMicro

func UnixMicro(t time.Time) int64

func UnixMicroNonZero

func UnixMicroNonZero(t time.Time) int64

func UnixMilli

func UnixMilli(t time.Time) int64

func UnixMilliNonZero

func UnixMilliNonZero(t time.Time) int64

func UnixNano

func UnixNano(t time.Time) int64

func UnixNanoNonZero

func UnixNanoNonZero(t time.Time) int64

func UnixNonZero

func UnixNonZero(t time.Time) int64

func UnsafeGetBytes

func UnsafeGetBytes(s string) []byte

go 1.20 versions

func UnsafeGetString

func UnsafeGetString(b []byte) string

Types

type AlignedTicker

type AlignedTicker struct {
	sync.Mutex
	sync.Once

	C <-chan time.Time
	// contains filtered or unexported fields
}

func NewAlignedTicker

func NewAlignedTicker(d time.Duration) *AlignedTicker

func (*AlignedTicker) Stop

func (t *AlignedTicker) Stop()

type BitRate

type BitRate float64
const (
	Kibit BitRate = 1 << (10 * iota) // 1 << (10*1)
	Mibit                            // 1 << (10*2)
	Gibit                            // 1 << (10*3)
	Tibit                            // 1 << (10*4)
	Pibit                            // 1 << (10*5)
	Eibit                            // 1 << (10*6)
	Zibit                            // 1 << (10*7)
	Yibit                            // 1 << (10*8)
)

func ParseBitRate

func ParseBitRate(r string) (BitRate, error)

func (BitRate) Int64

func (r BitRate) Int64() int64

func (BitRate) MarshalText

func (r BitRate) MarshalText() ([]byte, error)

func (BitRate) Size

func (r BitRate) Size(d time.Duration) ByteSize

func (BitRate) String

func (r BitRate) String() string

Text/JSON conversion

func (*BitRate) UnmarshalText

func (r *BitRate) UnmarshalText(data []byte) error

type ByteSize

type ByteSize float64
const (
	KiB ByteSize = 1 << (10 * iota) // 1 << (10*1)
	MiB                             // 1 << (10*2)
	GiB                             // 1 << (10*3)
	TiB                             // 1 << (10*4)
	PiB                             // 1 << (10*5)
	EiB                             // 1 << (10*6)
	ZiB                             // 1 << (10*7)
	YiB                             // 1 << (10*8)
)

func ParseByteSize

func ParseByteSize(r string) (ByteSize, error)

func (ByteSize) BitRate

func (b ByteSize) BitRate(d time.Duration) BitRate

func (ByteSize) Int64

func (r ByteSize) Int64() int64

func (ByteSize) MarshalText

func (r ByteSize) MarshalText() ([]byte, error)

func (ByteSize) String

func (b ByteSize) String() string

func (*ByteSize) UnmarshalText

func (r *ByteSize) UnmarshalText(data []byte) error

type Duration

type Duration time.Duration

func ParseDuration

func ParseDuration(d string) (Duration, error)

func (Duration) Duration

func (d Duration) Duration() time.Duration

func (Duration) MarshalText

func (d Duration) MarshalText() ([]byte, error)

func (Duration) RoundToDays

func (d Duration) RoundToDays() int

func (Duration) RoundToHours

func (d Duration) RoundToHours() int64

func (Duration) RoundToMillisecond

func (d Duration) RoundToMillisecond() int64

func (Duration) RoundToMinutes

func (d Duration) RoundToMinutes() int64

func (Duration) RoundToSeconds

func (d Duration) RoundToSeconds() int64

func (Duration) String

func (d Duration) String() string

func (Duration) Truncate

func (d Duration) Truncate(r time.Duration) Duration

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(data []byte) error

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(data []byte) error

type GenericPool

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

GenericPool is a pool of types that can be re-used. Items in this pool will not be garbage collected when not in use.

func NewGenericPool

func NewGenericPool(max int, fn func() interface{}) *GenericPool

NewGeneric returns a Generic pool with capacity for max items to be pool.

func (*GenericPool) Get

func (p *GenericPool) Get() interface{}

Get returns a item from the pool or a new instance if the pool is empty. Items returned may not be in the zero state and should be reset by the caller.

func (*GenericPool) Put

func (p *GenericPool) Put(c interface{})

Put returns an item back to the pool. If the pool is full, the item is discarded.

type SizedPool

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

Sized is a pool of types that can be re-used. Items in this pool will not be garbage collected when not in use.

func NewSizedPool

func NewSizedPool(max int, fn func(sz int) interface{}) *SizedPool

NewSized returns a Sized pool with capacity for max items to be pool.

func (*SizedPool) Get

func (p *SizedPool) Get(sz int) interface{}

Get returns a item from the pool or a new instance if the pool is empty. Items returned may not be in the zero state and should be reset by the caller.

func (*SizedPool) Put

func (p *SizedPool) Put(c interface{})

Put returns an item back to the pool. If the pool is full, the item is discarded.

type StringList

type StringList []string

func (*StringList) Add

func (l *StringList) Add(r string) StringList

func (*StringList) AddFront

func (l *StringList) AddFront(r string) StringList

func (*StringList) AddUnique

func (l *StringList) AddUnique(r string) StringList

func (*StringList) AddUniqueFront

func (l *StringList) AddUniqueFront(r string) StringList

func (StringList) AsInterface

func (l StringList) AsInterface() []interface{}

func (StringList) Contains

func (l StringList) Contains(r string) bool

func (StringList) Index

func (l StringList) Index(r string) int

func (StringList) MarshalText

func (l StringList) MarshalText() ([]byte, error)

func (StringList) String

func (l StringList) String() string

func (*StringList) UnmarshalText

func (l *StringList) UnmarshalText(data []byte) error

type Time

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

func Date

func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time

func NewTime

func NewTime(t time.Time) Time

func NewTimeFrom

func NewTimeFrom(ts int64, res time.Duration) Time

func Now

func Now() Time

func ParseTime

func ParseTime(value string) (Time, error)

func (Time) Add

func (t Time) Add(d time.Duration) Time

func (Time) AddDate

func (t Time) AddDate(years int, months int, days int) Time

func (Time) After

func (t Time) After(a Time) bool

func (Time) AppendFormat

func (t Time) AppendFormat(b []byte, layout string) []byte

func (Time) Before

func (t Time) Before(a Time) bool

func (Time) Date

func (t Time) Date() (int, time.Month, int)

func (Time) DaysSince

func (t Time) DaysSince(a Time) int

func (Time) EOD

func (f Time) EOD() Time

func (Time) EODTime

func (f Time) EODTime() time.Time

func (Time) Equal

func (t Time) Equal(t2 Time) bool

func (Time) Format

func (t Time) Format(layout string) string

func (Time) GetFormat

func (f Time) GetFormat() TimeFormat

func (Time) IsDate

func (f Time) IsDate() bool

func (Time) IsZero

func (t Time) IsZero() bool

func (Time) MarshalJSON

func (f Time) MarshalJSON() ([]byte, error)

func (Time) MarshalText

func (f Time) MarshalText() ([]byte, error)

func (*Time) SetFormat

func (t *Time) SetFormat(f TimeFormat) *Time

func (Time) String

func (f Time) String() string

func (Time) Time

func (f Time) Time() time.Time

func (Time) Truncate

func (t Time) Truncate(d time.Duration) Time

func (Time) Unix

func (t Time) Unix() int64

func (Time) UnixMicro

func (t Time) UnixMicro() int64

func (Time) UnixMilli

func (t Time) UnixMilli() int64

func (Time) UnixNano

func (t Time) UnixNano() int64

func (*Time) UnmarshalJSON

func (f *Time) UnmarshalJSON(data []byte) error

func (*Time) UnmarshalText

func (f *Time) UnmarshalText(data []byte) error

func (Time) Year

func (t Time) Year() int

type TimeFormat

type TimeFormat int
const (
	TimeFormatDefault TimeFormat = iota
	TimeFormatUnix
	TimeFormatUnixMicro
	TimeFormatUnixMilli
	TimeFormatUnixNano
	TimeFormatDate
)

func (TimeFormat) IsUnix

func (f TimeFormat) IsUnix() bool

type TimeUnit

type TimeUnit struct {
	Value int
	Unit  rune
}

func ParseTimeUnit

func ParseTimeUnit(s string) (TimeUnit, error)

func (TimeUnit) Add

func (c TimeUnit) Add(t time.Time) time.Time

func (TimeUnit) Base

func (c TimeUnit) Base() time.Duration

func (TimeUnit) Between

func (c TimeUnit) Between(from, to time.Time, limit int) []time.Time

func (TimeUnit) Duration

func (c TimeUnit) Duration() time.Duration

func (TimeUnit) MarshalText

func (c TimeUnit) MarshalText() ([]byte, error)

func (TimeUnit) Next

func (c TimeUnit) Next(t time.Time, n int) time.Time

func (TimeUnit) Steps

func (c TimeUnit) Steps(from, to time.Time, limit int) []time.Time

func (TimeUnit) String

func (c TimeUnit) String() string

func (TimeUnit) Sub

func (c TimeUnit) Sub(t time.Time) time.Time

func (TimeUnit) Truncate

func (c TimeUnit) Truncate(t time.Time) time.Time

Truncate truncates t to time unit ignoring its value, e.g. - minutes: full minute - hours: full hour - days: midnight UTC - weeks: midnight UTC on first day of week (Sunday) - months: midnight UTC on first day of month - quarters: midnight UTC on first day of quarter - years: midnight UTC on first day of year

func (*TimeUnit) UnmarshalText

func (c *TimeUnit) UnmarshalText(data []byte) error

type U64String

type U64String uint64

func DecodeU64String

func DecodeU64String(s string) (U64String, error)

func (U64String) Base64

func (u U64String) Base64() string

func (U64String) Hex

func (u U64String) Hex() string

func (U64String) MarshalText

func (u U64String) MarshalText() ([]byte, error)

func (U64String) String

func (u U64String) String() string

func (U64String) U64

func (u U64String) U64() uint64

func (*U64String) UnmarshalText

func (u *U64String) UnmarshalText(data []byte) error

type WallTicker

type WallTicker struct {
	sync.Mutex
	sync.Once
	C <-chan time.Time
	// contains filtered or unexported fields
}

More accurate Ticker from https://github.com/golang/go/issues/19810

func NewWallTicker

func NewWallTicker(align, offset time.Duration) *WallTicker

func (*WallTicker) Stop

func (w *WallTicker) Stop()

Jump to

Keyboard shortcuts

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