improved string cleaning

This commit is contained in:
2025-07-03 01:37:15 -05:00
parent 73502669ea
commit c22772a0af

26
main.go
View File

@@ -9,7 +9,6 @@ import (
"log" "log"
"os" "os"
"path" "path"
"regexp"
"strings" "strings"
"text/template" "text/template"
"time" "time"
@@ -451,19 +450,28 @@ func moveFile(source, destination string) (err error) {
} }
func cleanPath(str string) string { func cleanPath(str string) string {
return doClean(str, `[<>:"|?*]`) return doClean(str, func(r rune) rune {
switch r {
case '<', '>', ':', '\'', '"', '|', '?', '*':
return '_'
}
return r
})
} }
func cleanTemplateVariable(str string) string { func cleanTemplateVariable(str string) string {
return doClean(str, `[<>:"|?*/\\.]`) return doClean(str, func(r rune) rune {
switch r {
case '<', '>', ':', '\'', '"', '|', '?', '*', '/', '\\', '.':
return '_'
}
return r
})
} }
func doClean(str string, reg string) string { func doClean(str string, mapping func(rune) rune) string {
re := regexp.MustCompile(reg) t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC, runes.Map(mapping))
s := re.ReplaceAllString(str, "") result, _, err := transform.String(t, str)
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, err := transform.String(t, s)
if err != nil { if err != nil {
panic(err) panic(err)
} }