strings

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

README

stdx/strings

Import path: github.com/InsideGallery/core/stdx/strings

Overview

stdx/strings provides hashing, email cleanup, normalization, ID, masking, chunking, and password-display helpers. Import it with an alias when the standard library strings package is also needed.

Main APIs

  • CRC32, CRC16, SimHash, SimHashCompare, and HashName provide hash helpers.
  • ABTest maps input and salt bytes into a deterministic bucket below the sum of the supplied group weights.
  • EmailUserName, EmailDomain, and SanitizeEmail split and normalize email-like strings.
  • NFDLowerString, NFKDLowerString, CommonString, SplitBetweenTokens, and Between normalize or extract text.
  • ByteSliceToString casts bytes to string without allocation.
  • GetUniqueID, GetShortID, GetTinyID, and RandStringBytes create identifiers or random strings.
  • SafeGet, MaskField, and SplitByChunks provide small generic/string helpers.
  • ContextKey, Password, and Str are compatibility types for context keys and masked password values.

Usage

email := strx.SanitizeEmail("User+tag@example.com")
bucket := strx.ABTest([]byte("user-1"), []byte("experiment"), 50, 50)
masked := strx.Password("secret").String()

_ = email
_ = bucket
_ = masked

Notes

Password.String and JSON marshaling always return ********; use Value only when the real secret is needed. ByteSliceToString uses unsafe, so callers should not mutate the byte slice after casting. HashName expects a non-empty input string. SplitByChunks and shorthands in this package operate on byte indexes, not runes.

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func ABTest

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

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 strings, without allocation memory

func CRC16

func CRC16(field string) uint16

func CRC32

func CRC32(field string) uint32

func CommonString

func CommonString(str string) string

func EmailDomain

func EmailDomain(email string) string

func EmailUserName

func EmailUserName(email string) string

func GetShortID

func GetShortID() ([]byte, error)

GetShortID return short id

func GetTinyID

func GetTinyID() ([]byte, error)

GetTinyID return tiny id

func GetUniqueID

func GetUniqueID() string

GetUniqueID return unique id

func HashName

func HashName(name string) string

func MaskField

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

func NFDLowerString

func NFDLowerString(str string) string

func NFKDLowerString

func NFKDLowerString(str string) string

func RandStringBytes

func RandStringBytes(n int) string

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

func SimHash

func SimHash(data []byte) uint64

func SimHashCompare

func SimHashCompare(val1, val2 uint64) uint8

func SplitBetweenTokens

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

SplitBetweenTokens takes strings and one or two tokens, and cuts everything between two tokens, or between two copies of first token.

func SplitByChunks

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

Types

type ContextKey

type ContextKey string

ContextKey contains context key

type Password

type Password string

Password describe masked field

func (Password) MarshalJSON

func (p Password) MarshalJSON() ([]byte, error)

MarshalJSON block to show value in json

func (Password) String

func (p Password) String() string

String return hide password value

func (Password) Value

func (p Password) Value() string

Value return exactly password value

type Str

type Str struct {
	Pass Password
}

Jump to

Keyboard shortcuts

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