util

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyOrDefault

func AnyOrDefault(val any, defVal any) any

func Column

func Column[T any, V any](list []T, mapFn func(obj T) (val V, find bool)) []V

Column alias of Map func

func DecodeBools

func DecodeBools(quantity uint16, in []byte) (out []bool)

DecodeBools Extract a specified number of Boolean values

func DurationOrDefault

func DurationOrDefault(val time.Duration, defVal time.Duration) time.Duration

func EncodeBools

func EncodeBools(in []bool) (out []byte)

EncodeBools Convert a boolean list to a byte array

func Filter

func Filter[T any](ls []T, filter ...MatchFunc[T]) []T

Filter given slice, default will filter zero value.

Usage:

// output: [a, b]
ss := arrutil.Filter([]string{"a", "", "b", ""})

func GetBoolAt

func GetBoolAt(b byte, pos uint) bool

GetBoolAt gets a boolean (bit) from a byte at position

func IndexOf

func IndexOf[T ~string | XintOrFloat](val T, list []T) int

IndexOf value in given slice.

func IntOrDefault

func IntOrDefault(val int, defVal int) int

func Invoke

func Invoke(fn interface{}, types []interface{}, params ...interface{})

func Map

func Map[T any, V any](list []T, mapFn MapFn[T, V]) []V

Map a list to new list

eg: mapping [object0{},object1{},...] to flatten list [object0.someKey, object1.someKey, ...]

func NumberToBytes

func NumberToBytes(n any) []byte

func NumberToBytesE

func NumberToBytesE(n any) ([]byte, error)

func Remove

func Remove[T Compared](ls []T, val T) []T

Remove give element from slice []T.

eg: []string{"site", "user", "info", "0"} -> []string{"site", "user", "info"}

func Reverse

func Reverse[T any](ls []T)

Reverse any T slice.

eg: []string{"site", "user", "info", "0"} -> []string{"0", "info", "user", "site"}

func SetBoolAt

func SetBoolAt(b byte, bitPos uint, data bool) byte

SetBoolAt sets a boolean (bit) within a byte at bit position without changing the other bits it returns the resulted byte

func StrOrDefault

func StrOrDefault(str string, defVal string) string

func Unique

func Unique[T ~string | XintOrFloat](list []T) []T

Unique value in the given slice data.

Types

type ComGroup

type ComGroup struct {
	Items []*ComItem
}

func ReadRecombination

func ReadRecombination(src []uint16, targetSize int, extraSize int, threshold int) []*ComGroup

func WriteRecombination

func WriteRecombination(src []uint16, targetSize int, extraSize int) []*ComGroup

type ComItem

type ComItem struct {
	// Index 数据索引
	Index int
	// Index 原始数据大小
	RawSize int
	// SplitOffset 分割点,即数据偏移索引
	SplitOffset int
	// RipeSize 分割后的数据大小
	RipeSize int
	// ExtraSize 额外需要的数据大小
	ExtraSize int
	// Threshold 阀值
	Threshold int
}

func (*ComItem) TotalLength

func (i *ComItem) TotalLength() int

type Compared

type Compared interface {
	Int | Uint | Float | ~string
}

Compared type. alias of constraints.SortedType type Compared = SortedType

type Float

type Float interface {
	~float32 | ~float64
}

Float interface type

type Int

type Int interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Int interface type

type IntOrFloat

type IntOrFloat interface {
	Int | Float
}

IntOrFloat interface type. all int and float types

type Integer

type Integer interface {
	Int | Uint
}

Integer interface type. all int or uint types

type MapFn

type MapFn[T any, V any] func(input T) (target V, find bool)

MapFn map handle function type.

type MatchFunc

type MatchFunc[T any] func(v T) bool

MatchFunc definition. implements Matcher interface

func (MatchFunc[T]) Match

func (fn MatchFunc[T]) Match(v T) bool

Match satisfies the Matcher interface

type Matcher

type Matcher[T any] interface {
	Match(s T) bool
}

Matcher interface

type ScalarType

type ScalarType interface {
	Int | Uint | Float | ~string | ~bool
}

ScalarType interface type.

TIP: has bool type, it cannot be ordered

contains: (x)int, float, ~string, ~bool types

type SimpleType

type SimpleType interface {
	Int | Uint | Float | ~string | ~bool
}

SimpleType interface type. alias of ScalarType

contains: (x)int, float, ~string, ~bool types

type SortedType

type SortedType interface {
	Int | Uint | Float | ~string
}

SortedType interface type. same of constraints.Ordered

it can be ordered, that supports the operators < <= >= >.

contains: (x)int, float, ~string types

type StringHandleFunc

type StringHandleFunc func(s string) string

StringHandleFunc definition

func (StringHandleFunc) Handle

func (fn StringHandleFunc) Handle(s string) string

Handle satisfies the StringHandler interface

type StringHandler

type StringHandler interface {
	Handle(s string) string
}

StringHandler interface

type StringMatchFunc

type StringMatchFunc func(s string) bool

StringMatchFunc definition

func (StringMatchFunc) Match

func (fn StringMatchFunc) Match(s string) bool

Match satisfies the StringMatcher interface

type StringMatcher

type StringMatcher interface {
	Match(s string) bool
}

StringMatcher interface

type Uint

type Uint interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Uint interface type

type Xint

type Xint interface {
	Int | Uint
}

Xint interface type. alias of Integer

type XintOrFloat

type XintOrFloat interface {
	Int | Uint | Float
}

XintOrFloat interface type. all int, uint and float types

Jump to

Keyboard shortcuts

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