strutil

package
v2.14.7 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: MIT Imports: 14 Imported by: 2

Documentation

Index

Constants

View Source
const (
	BOM_UTF8               = "\xEF\xBB\xBF"
	BOM_UTF16_BigEndian    = "\xFE\xFF"
	BOM_UTF16_LittleEndian = "\xFF\xFE"
	BOM_UTF32_BigEndian    = "\x00\x00\xFE\xFF"
	BOM_UTF32_LittleEndian = "\xFF\xFE\x00\x00"
)

Unicode byte order mark (BOM) constants.

Reference: https://en.wikipedia.org/wiki/Byte_order_mark and https://www.unicode.org/faq/utf_bom.html

View Source
const (
	Digits         = "0123456789"
	HexDigits      = "0123456789abcdefABCDEF"
	OctDigits      = "01234567"
	UpperLetters   = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	UpperAndDigits = UpperLetters + Digits
	LowerLetters   = "abcdefghijklmnopqrstuvwxyz"
	LowerAndDigits = LowerLetters + Digits
	Letters        = UpperLetters + LowerLetters
	AlphaDigits    = Letters + Digits

	PasswordSpecial = "!#$%&()*+-./:;=?@[]^_~"
)

Variables

This section is empty.

Functions

func ContainsFold

func ContainsFold(s, substr string) bool

ContainsFold is like strings.Contains but uses Unicode case-folding.

func DetectBOM

func DetectBOM(b []byte) (bom string)

DetectBOM detects BOM prefix from a byte slice.

func Format

func Format(format string, kwArgs any, posArgs ...any) string

Format mimics subset features of the python string.format function.

It formats the string using given keyword arguments and positional arguments. kwArgs can be a map[string]any, map[string]string or a struct or a pointer to a struct.

If bracket is needed in string it can be created by escaping (two brackets).

All standard formatting options from fmt work. To specify them, add colon after key name or position number and specify fmt package compatible formatting options. The percent sign is optional. For example:

// returns "3.14, 3.1416"
Format("{pi:%.2f}, {pi:.4f}", map[string]any{"pi": math.Pi})

If a replacement is not found in kwArgs and posArgs, the placeholder will be output as the same in the given format.

func HasPrefixFold

func HasPrefixFold(s, prefix string) bool

HasPrefixFold is like strings.HasPrefix but uses Unicode case-folding, matching case insensitively.

func HasSuffixFold

func HasSuffixFold(s, suffix string) bool

HasSuffixFold is like strings.HasSuffix but uses Unicode case-folding, matching case insensitively.

func IsASCII

func IsASCII(str string) bool

IsASCII returns true if the string is empty or all characters in the string are ASCII, false otherwise.

func IsASCIIDigit

func IsASCIIDigit(str string) bool

IsASCIIDigit returns true if all characters in the string are in range 0-9 and there is at least one character, false otherwise.

func IsDigit

func IsDigit(str string) bool

IsDigit returns true if all characters in the string are digits and there is at least one character, false otherwise

func IsLower

func IsLower(str string) bool

IsLower returns true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.

func IsPrintable

func IsPrintable(str string) bool

IsPrintable returns true if all characters in the string are printable or the string is empty, false otherwise.

func IsUpper

func IsUpper(str string) bool

IsUpper returns true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.

func Random

func Random(table string, length int) string

Random returns a random string of length consisting of characters from table. It panics if length <= 0 or len(table) <= 1.

func RandomCrypto

func RandomCrypto(table string, length int) string

RandomCrypto returns a random string of length consisting of characters from table. It panics if length <= 0 or len(table) <= 1.

func RandomHex

func RandomHex(length int) string

RandomHex returns a random hex string of length consisting of cryptographic-safe random bytes.

func Reverse

func Reverse(s string) string

Reverse returns a new string of the rune characters from the given string in reverse order.

func SkipBOMReader

func SkipBOMReader(rd io.Reader) io.Reader

SkipBOMReader detects and skips BOM prefix from the given io.Reader. It returns a *bufio.Reader.

func ToCamelCase

func ToCamelCase(in string) string

ToCamelCase converts the given string to CamelCase.

func ToLowerCamelCase

func ToLowerCamelCase(in string) string

ToLowerCamelCase converts the given string to lowerCamelCase.

func ToSnakeCase

func ToSnakeCase(in string) string

ToSnakeCase convert the given string to snake case following the Golang format: acronyms are converted to lower-case and preceded by an underscore.

func TrimBOM

func TrimBOM(b []byte) []byte

TrimBOM detects and trims BOM prefix from a byte slice, the returned byte slice shares the same underlying memory with the given slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

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