utils

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 16 Imported by: 2

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

View Source
const (
	EmailTagStart = "+"
	EmailAt       = "@"
)

Email parsing constants.

Variables

This section is empty.

Functions

func ABTest

func ABTest(data, salt []byte, groups ...uint64) uint64

ABTest assigns data to a group bucket using murmur3 hashing with the given salt and group sizes.

func Between

func Between(data string, keys ...string) string

Between function to get content between two keys

func ByteSliceToString

func ByteSliceToString(b []byte) string

ByteSliceToString cast given bytes to string, without allocation memory

func CRC16

func CRC16(field string) uint16

CRC16 returns the CRC-16/XModem checksum of field.

func CRC32

func CRC32(field string) uint32

CRC32 returns the CRC-32 checksum of field using the Castagnoli polynomial.

func CommonString

func CommonString(str string) string

CommonString strips all characters except letters, digits, and spaces from the string.

func EmailDomain

func EmailDomain(email string) string

EmailDomain returns the domain part of an email address (after the @).

func EmailUserName

func EmailUserName(email string) string

EmailUserName returns the local part of an email address (before the @).

func GetShortID

func GetShortID() ([]byte, error)

GetShortID return short id

func GetTinyID

func GetTinyID() ([]byte, error)

GetTinyID return tiny id

func HashName

func HashName(name string) string

HashName returns a short hash of the name: its first letter followed by a hex-encoded CRC-16.

func MaskField

func MaskField(str string, keepUnmaskedFront int, keepUnmaskedEnd int) string

MaskField replaces the middle portion of str with asterisks, keeping the specified number of characters at each end.

func NFDLowerString

func NFDLowerString(str string) string

NFDLowerString trims, NFD-normalizes, and lowercases the given string.

func RandStringBytes

func RandStringBytes(n int) string

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

func SanitizeEmail(email string) string

SanitizeEmail lowercases the email and strips any +tag portion from the local part.

func SimHash

func SimHash(data []byte) uint64

SimHash returns the simhash fingerprint of the given data.

func SimHashCompare

func SimHashCompare(val1, val2 uint64) uint8

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

func SplitBetweenTokens(data string, keys ...string) []string

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

func SplitByChunks(s string, chunkSize int) []string

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

func NewSafeList[K any](data ...K) *SafeList[K]

NewSafeList creates a new SafeList optionally initialized with the given elements.

func (*SafeList[V]) Add

func (s *SafeList[V]) Add(value V)

Add appends a value to the list.

func (*SafeList[V]) Clear

func (s *SafeList[V]) Clear() []V

Clear removes all elements and returns them.

func (*SafeList[V]) Count

func (s *SafeList[V]) Count() int

Count returns the number of elements in the list.

func (*SafeList[V]) List

func (s *SafeList[V]) List() []V

List returns a copy of all elements.

type SafeMap

type SafeMap[K string, V any] struct {
	// contains filtered or unexported fields
}

SafeMap is a thread-safe string-keyed map protected by a read-write mutex.

func NewSafeMap

func NewSafeMap[K string, V any](data map[K]V) *SafeMap[K, V]

NewSafeMap creates a new SafeMap initialized with a copy of the given data.

func (*SafeMap[K, V]) Exists

func (s *SafeMap[K, V]) Exists(name K) bool

Exists reports whether the given key is present.

func (*SafeMap[K, V]) Get

func (s *SafeMap[K, V]) Get(name K) (V, bool)

Get returns the value for the given key and whether it exists.

func (*SafeMap[K, V]) GetMap

func (s *SafeMap[K, V]) GetMap() map[K]V

GetMap returns a copy of the underlying map.

func (*SafeMap[K, V]) Remove

func (s *SafeMap[K, V]) Remove(name K)

Remove deletes the entry for the given key.

func (*SafeMap[K, V]) Set

func (s *SafeMap[K, V]) Set(name K, value V)

Set inserts or updates the value for the given key.

Jump to

Keyboard shortcuts

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