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