Documentation
¶
Overview ¶
Package utils provides hashing, sorting, and string helpers (stateless, safe for concurrent use) as well as SafeMap and SafeList which are thread-safe via read-write mutexes.
Index ¶
- Constants
- func ABTest(data, salt []byte, groups ...uint64) uint64
- func Between(data string, keys ...string) string
- func ByteSliceToString(b []byte) string
- func CRC16(field string) uint16
- func CRC32(field string) uint32
- func CommonString(str string) string
- func EmailDomain(email string) string
- func EmailUserName(email string) string
- func GetShortID() ([]byte, error)
- func GetTinyID() ([]byte, error)
- func HashName(name string) string
- func MaskField(str string, keepUnmaskedFront int, keepUnmaskedEnd int) string
- func NFDLowerString(str string) string
- func RandStringBytes(n int) string
- func SafeGet[T any](ptr *T, defaultValue T) T
- func SanitizeEmail(email string) string
- func SimHash(data []byte) uint64
- func SimHashCompare(val1, val2 uint64) uint8
- func Sort(values []interface{}, c comparator.Comparator) error
- func SplitBetweenTokens(data string, keys ...string) []string
- func SplitByChunks(s string, chunkSize int) []string
- type SafeList
- type SafeMap
Constants ¶
const ( EmailTagStart = "+" EmailAt = "@" )
Email parsing constants.
Variables ¶
This section is empty.
Functions ¶
func ABTest ¶
ABTest assigns data to a group bucket using murmur3 hashing with the given salt and group sizes.
func ByteSliceToString ¶
ByteSliceToString cast given bytes to string, without allocation memory
func CommonString ¶
CommonString strips all characters except letters, digits, and spaces from the string.
func EmailDomain ¶
EmailDomain returns the domain part of an email address (after the @).
func EmailUserName ¶
EmailUserName returns the local part of an email address (before the @).
func HashName ¶
HashName returns a short hash of the name: its first letter followed by a hex-encoded CRC-16.
func MaskField ¶
MaskField replaces the middle portion of str with asterisks, keeping the specified number of characters at each end.
func NFDLowerString ¶
NFDLowerString trims, NFD-normalizes, and lowercases the given string.
func RandStringBytes ¶
RandStringBytes returns a random lowercase alphabetic string of length n.
func SafeGet ¶
func SafeGet[T any](ptr *T, defaultValue T) T
SafeGet return value of pointer, and return default value if it nil
func SanitizeEmail ¶
SanitizeEmail lowercases the email and strips any +tag portion from the local part.
func SimHashCompare ¶
SimHashCompare returns the Hamming distance between two simhash fingerprints.
func Sort ¶
func Sort(values []interface{}, c comparator.Comparator) error
Sort sorts values (in-place) with respect to the given comparator. Returns the first comparator error encountered during sorting, if any.
Uses Go's sort (hybrid of quicksort for large and then insertion sort for smaller slices).
func SplitBetweenTokens ¶
SplitBetweenTokens takes a string and one or two tokens, and cuts everything between the two tokens (or two copies of the first token).
func SplitByChunks ¶
SplitByChunks splits the string into chunks of the given size.
Types ¶
type SafeList ¶
type SafeList[V any] struct { // contains filtered or unexported fields }
SafeList is a thread-safe append-only list protected by a read-write mutex.
func NewSafeList ¶
NewSafeList creates a new SafeList optionally initialized with the given elements.
func (*SafeList[V]) Clear ¶
func (s *SafeList[V]) Clear() []V
Clear removes all elements and returns them.
type SafeMap ¶
SafeMap is a thread-safe string-keyed map protected by a read-write mutex.
func NewSafeMap ¶
NewSafeMap creates a new SafeMap initialized with a copy of the given data.
func (*SafeMap[K, V]) GetMap ¶
func (s *SafeMap[K, V]) GetMap() map[K]V
GetMap returns a copy of the underlying map.