mirror of
https://github.com/abhinavxd/libredesk.git
synced 2025-11-05 06:23:27 +00:00
84 lines
2.1 KiB
Go
84 lines
2.1 KiB
Go
// Package stringutil provides string utility functions.
|
|
package stringutil
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/k3a/html2text"
|
|
)
|
|
|
|
var (
|
|
regexpNonAlNum = regexp.MustCompile(`[^a-zA-Z0-9\-_\.]+`)
|
|
regexpSpaces = regexp.MustCompile(`[\s]+`)
|
|
)
|
|
|
|
// Trim removes HTML tags, trims whitespace, makes the text human-readable, and shortens the content to a specified maximum length, appending "..." if truncated.
|
|
func Trim(content string, maxLen int) string {
|
|
plain := strings.TrimSpace(html2text.HTML2Text(content))
|
|
if len(plain) > maxLen {
|
|
plain = plain[:maxLen] + "..."
|
|
}
|
|
return plain
|
|
}
|
|
|
|
// AppendSuffixToFilename adds a string suffix to the filename while keeping the file extension.
|
|
func AppendSuffixToFilename(filename, suffix string) string {
|
|
ext := filepath.Ext(filename)
|
|
name := strings.TrimSuffix(filename, ext)
|
|
return fmt.Sprintf("%s_%s%s", name, suffix, ext)
|
|
}
|
|
|
|
// SanitizeFilename sanitizes the provided filename.
|
|
func SanitizeFilename(fName string) string {
|
|
// Trim whitespace.
|
|
name := strings.TrimSpace(fName)
|
|
|
|
// Replace whitespace and "/" with "-"
|
|
name = regexpSpaces.ReplaceAllString(name, "-")
|
|
|
|
// Remove or replace any non-alphanumeric characters
|
|
name = regexpNonAlNum.ReplaceAllString(name, "")
|
|
|
|
// Convert to lowercase
|
|
name = strings.ToLower(name)
|
|
return filepath.Base(name)
|
|
}
|
|
|
|
// RandomAlNumString generates a random alphanumeric string of length n.
|
|
func RandomAlNumString(n int) (string, error) {
|
|
const dictionary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
for k, v := range bytes {
|
|
bytes[k] = dictionary[v%byte(len(dictionary))]
|
|
}
|
|
|
|
return string(bytes), nil
|
|
}
|
|
|
|
// RandomNumericString generates a random numeric string of length n.
|
|
func RandomNumericString(n int) (string, error) {
|
|
const dictionary = "0123456789"
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
for k, v := range bytes {
|
|
bytes[k] = dictionary[v%byte(len(dictionary))]
|
|
}
|
|
|
|
return string(bytes), nil
|
|
}
|