feat: deduplicate ship name on transfer

This commit is contained in:
Ilia Denisov
2026-02-17 22:02:17 +02:00
parent f394c105b0
commit de91d575d0
7 changed files with 100 additions and 30 deletions
+25 -3
View File
@@ -1,12 +1,16 @@
package util
import (
"fmt"
"math/rand"
"strings"
"unicode"
)
// Allowed special characters
const specialChars = "!@#$%^*-_=+~()[]{}"
const (
maxNameLength = 30
specialChars = "!@#$%^*-_=+~()[]{}" // Allowed special characters
)
var allowedSpecialChars map[rune]bool
@@ -28,7 +32,7 @@ func ValidateTypeName(input string) (string, bool) {
runes := []rune(trimmed)
if len(runes) > 30 {
if len(runes) > maxNameLength {
return "", false
}
@@ -75,3 +79,21 @@ func ValidateTypeName(input string) (string, bool) {
// Return the trimmed string and true if all conditions are met
return trimmed, true
}
func AppendRandomSuffix(v string) string {
return AppendRandomSuffixGenerator(v, RandomSuffixGenerator)
}
func AppendRandomSuffixGenerator(v string, s func() string) string {
suffix := []rune(s())
str := []rune(v)
max := maxNameLength - len(suffix)
if len(str) > max {
str = str[:max]
}
return string(append(str, suffix...))
}
func RandomSuffixGenerator() string {
return fmt.Sprintf("%04d", rand.Intn(9999))
}