mirror of
https://github.com/jimeh/go-golden.git
synced 2026-02-19 03:16:38 +00:00
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.
35 lines
767 B
Go
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
|
|
}
|