Files
go-golden/sanitize.go
Jim Myhrberg 3185b09d09 refactor(golden): major refactor and improvements
This includes sanitizing golden file names to avoid characters and names
which are invalid on some operating systems. So this should now work on
Linux, macOS, and Window.
2021-09-17 02:21:32 +01:00

35 lines
767 B
Go

package golden
import (
"regexp"
"strings"
)
var (
whitespaceChars = regexp.MustCompile(`\s`)
illegalChars = regexp.MustCompile(`[\/\?<>\\:\*\|"]`)
controlChars = regexp.MustCompile(`[\x00-\x1f\x80-\x9f]`)
reservedNames = regexp.MustCompile(`^\.+$`)
winReserved = regexp.MustCompile(
`(?i)^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$`,
)
)
func sanitizeFilename(name string) string {
if reservedNames.MatchString(name) || winReserved.MatchString(name) {
var b []byte
for i := 0; i < len(name); i++ {
b = append(b, byte('_'))
}
return string(b)
}
r := strings.TrimRight(name, ". ")
r = whitespaceChars.ReplaceAllString(r, "_")
r = illegalChars.ReplaceAllString(r, "_")
r = controlChars.ReplaceAllString(r, "_")
return r
}