>> deal with invalid UTF-8 in filenames

> make restrictRuneset a method on context
This commit is contained in:
Thomas Lindop 2025-05-24 10:58:03 +01:00
parent e8f7c9b411
commit eab2cf9f67

View file

@ -132,10 +132,10 @@ func isFatValid(r rune) bool {
return false return false
} }
func restrictRuneset(s, strategy string) string { func (ctx *context) restrictRuneset(s string) string {
result := s result := s
toValidSubs := make(map[rune]string) toValidSubs := make(map[rune]string)
if strategy == "remove" { if ctx.Strategy == "remove" {
for _, r := range result { for _, r := range result {
if !isFatValid(r) { if !isFatValid(r) {
toValidSubs[r] = "" toValidSubs[r] = ""
@ -250,7 +250,10 @@ func main() {
for _, file := range ctx.FileList { for _, file := range ctx.FileList {
oldName := fpath.Base(file) oldName := fpath.Base(file)
dirName := fpath.Dir(file) dirName := fpath.Dir(file)
newPath := fpath.Join(dirName, restrictRuneset(oldName, ctx.Strategy)) newName := ctx.restrictRuneset(
strings.ToValidUTF8(oldName, "_INVALID_"),
)
newPath := fpath.Join(dirName, newName)
if newPath == file { if newPath == file {
continue continue
} }