feat: deduplicate ship name on transfer
This commit is contained in:
+25
-3
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user