strutil

package
v0.0.0-...-b9f8b57 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 15 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareStringsShorterFirst

func CompareStringsShorterFirst[T ~string](a, b T) int

CompareStringsShorterFirst compares two strings by length first and then by lexicographical order.

func ConvertSlice

func ConvertSlice[T, S ~string](s []S) []T

ConvertSlice converts a slice of one string type type to another string type. It does this by reinterpreting the slice's underlying memory using unsafe.Pointer.

func CutTrimSpace

func CutTrimSpace(s, sep string) (before, after string, found bool)

CutTrimSpace slices s around the first instance of sep, returning the text before and after sep with all leading and trailing white space removed, as defined by Unicode. The found result reports whether sep appears in s. If sep does not appear in s, cut returns s, "", false.

func DerefPtr

func DerefPtr(ptr *string) string

DerefPtr returns the string ptr points to or an empty string if ptr is nil.

func EmptyStringToNil

func EmptyStringToNil(str string) any

EmptyStringToNil returns str or nil if it is empty.

func EqualJSON

func EqualJSON(a, b any) bool

EqualJSON returns true a and b are equal on a value basis, or if their marshalled JSON representation is equal.

func EqualPtrOrString

func EqualPtrOrString(a, b *string) bool

EqualPtrOrString returns if a and b are equal pointers or if the pointed to strings are equal

func HTMLEscapeSpecialRunes

func HTMLEscapeSpecialRunes(str string) string

func IndexInStrings

func IndexInStrings(str string, slice []string) int

IndexInStrings returns the index of where str can be found in slice, or -1 if it was not found.

func IsNorLetterOrDigit

func IsNorLetterOrDigit(r rune) bool

func IsWordSeparator

func IsWordSeparator(r rune) bool

func Join

func Join[T ~string](elems []T, sep string) string

Join concatenates the elements of its first argument to create a single string. The separator string sep is placed between elements in the resulting string.

func KeepRunes

func KeepRunes(str []byte, keepRunes ...IsRuneFunc) []byte

KeepRunes returns a copy of str where with all runes kept in it where any call to a keepRunes function reeturns true.

func KeepRunesString

func KeepRunesString(str string, keepRunes ...IsRuneFunc) string

KeepRunesString returns a copy of str where with all runes kept in it where any call to a keepRunes function reeturns true.

func MakeValidFileName

func MakeValidFileName(name string) string

MakeValidFileName replaces invalid filename characters with '_'. See also SanitizeFileName that does more than just replacing characters.

func MapRuneIsAfterWordSeparator

func MapRuneIsAfterWordSeparator(str string) []bool

func NormalizeExt

func NormalizeExt(ext string) string

func ParseDomainName

func ParseDomainName(word string) string

func ParseDomainNameIndex

func ParseDomainNameIndex(word string) (domain string, indices []int)

func Ptr

func Ptr(str string) *string

Ptr returns the passed string as pointer value.

func RandomString

func RandomString(length int) string

RandomString returns an URL compatible random string with the requested length.

func RandomStringBytes

func RandomStringBytes(length int) []byte

RandomStringBytes returns an URL compatible random string with the requested length as []byte slice, saving a string copy compared to RandomString.

func RemoveRunes

func RemoveRunes(str []byte, removeRunes ...IsRuneFunc) []byte

RemoveRunes returns a copy of str with all runes removed where any call to a removeRunes function reeturns true.

func RemoveRunesString

func RemoveRunesString(str string, removeRunes ...IsRuneFunc) string

RemoveRunesString returns a copy of str with all runes removed where any call to a removeRunes function reeturns true. If no rune was removed, the string is not copied.

func ReplaceTransliterations

func ReplaceTransliterations(str string) string

ReplaceTransliterations returns the string sanitized as valid UTF-8 with common European special characters transliterated to single or multiple ANSI characters.

func ReplaceTransliterationsMaxLen

func ReplaceTransliterationsMaxLen(str string, maxLen int) string

ReplaceTransliterationsMaxLen returns the string sanitized as valid UTF-8 with common European special characters transliterated to single or multiple ANSI characters.

The maxLen argument limits the number of runes returned. A negative value means no limit.

func SanitizeFileName

func SanitizeFileName(name string) string

SanitizeFileName creates a nice sane filename. It does more than just replacing invalid characters.

func SanitizeLineEndings

func SanitizeLineEndings(text string) string

SanitizeLineEndings converts all line endings to just '\n'

func SanitizeLineEndingsBytes

func SanitizeLineEndingsBytes(text []byte) []byte

SanitizeLineEndingsBytes converts all line endings to just '\n'

func SplitAndTrimIndex

func SplitAndTrimIndex(str []byte, isSplitRune, isTrimRune IsRuneFunc) (indices [][]int)

SplitAndTrimIndex first splits str into words not containing isSplitRune, then trims the words with isTrimRune.

func StringContainsAny

func StringContainsAny(str string, subStrings []string) bool

StringContainsAny returns if str is a sub string in any of subStrings.

func StringToPtrEmptyToNil

func StringToPtrEmptyToNil(str string) *string

StringToPtrEmptyToNil returns the address of a string or nil if the string is empty.

func SubStringIn

func SubStringIn(subString string, strs []string) bool

SubStringIn returns if subString is equal or a substring of any of strs.

func ToSnakeCase

func ToSnakeCase(s string) string

ToSnakeCase converts s to snake case by lower casing everything and inserting '_' before every new upper case character in s. Whitespace, symbol, and punctuation characters will be replace by '_'.

func Truncate

func Truncate(s string, i int) string

func TruncateWithEllipsis

func TruncateWithEllipsis(s string, i int) string

Types

type IsRuneFunc

type IsRuneFunc func(rune) bool

IsRuneFunc is function pionter for specifying if a rune matches a criteria

func IsRune

func IsRune(runes ...rune) IsRuneFunc

func IsRuneAll

func IsRuneAll(isRune ...IsRuneFunc) IsRuneFunc

func IsRuneAny

func IsRuneAny(isRune ...IsRuneFunc) IsRuneFunc

func IsRuneInverse

func IsRuneInverse(isRune IsRuneFunc) IsRuneFunc

type StrMutex

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

StrMutex manages a unique mutex for every locked string key. The mutex for a key exists as long as there are any locks waiting to be unlocked. This is equivalent to declaring a mutex variable for every key, except that the key and the number of mutexes are dynamic.

func NewStrMutex

func NewStrMutex() *StrMutex

NewStrMutex returns a new StrMutex

func (*StrMutex) Lock

func (m *StrMutex) Lock(key string)

Lock the mutex for a given key.

func (*StrMutex) Unlock

func (m *StrMutex) Unlock(key string)

Unlock the mutex for a given key.

type StringSet

type StringSet map[string]struct{}

func NewStringSet

func NewStringSet(strings ...string) StringSet

func NewStringSetMergeSlices

func NewStringSetMergeSlices(slices ...[]string) StringSet

func (StringSet) Add

func (set StringSet) Add(str string)

func (StringSet) AddSet

func (set StringSet) AddSet(other StringSet)

func (StringSet) AddSlice

func (set StringSet) AddSlice(s []string)

func (StringSet) Clear

func (set StringSet) Clear()

func (StringSet) Clone

func (set StringSet) Clone() StringSet

func (StringSet) Contains

func (set StringSet) Contains(str string) bool

func (StringSet) ContainsAny

func (set StringSet) ContainsAny(strs ...string) bool

func (StringSet) Delete

func (set StringSet) Delete(str string)

func (StringSet) DeleteSet

func (set StringSet) DeleteSet(other StringSet)

func (StringSet) DeleteSlice

func (set StringSet) DeleteSlice(s []string)

func (StringSet) Diff

func (set StringSet) Diff(other StringSet) StringSet

func (StringSet) Equal

func (set StringSet) Equal(other StringSet) bool

func (StringSet) Sorted

func (set StringSet) Sorted() (s []string)

func (StringSet) String

func (set StringSet) String() string

String implements the fmt.Stringer interface.

func (StringSet) StringContainsAnyOfSet

func (set StringSet) StringContainsAnyOfSet(str string) bool

StringContainsAnyOfSet returns true if the passed string contains any of the strings of the StringSet.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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