feat: initial public version

This commit is contained in:
Anton
2023-11-25 04:02:45 +05:00
parent ebd1d47fc6
commit 8447197d0f
64 changed files with 3880 additions and 4 deletions

33
stringx/stringx.go Normal file
View File

@@ -0,0 +1,33 @@
// Package stringx provides helper functions for working with strings.
package stringx
import (
"crypto/rand"
"encoding/hex"
"regexp"
)
var compactRE = regexp.MustCompile(`\s+`)
// Shorten shortens a string to a specified number of characters.
func Shorten(s string, maxlen int) string {
var short = []rune(s)
if len(short) > maxlen {
short = short[:maxlen]
short = append(short, []rune(" [truncated]")...)
}
return string(short)
}
// Compact replaces consecutive whitespaces with a single space.
func Compact(s string) string {
return compactRE.ReplaceAllString(string(s), " ")
}
// RandString generates a random string.
// length must be even.
func RandString(length int) string {
b := make([]byte, length/2)
_, _ = rand.Read(b)
return hex.EncodeToString(b)
}