mirror of
https://github.com/jimeh/go-golden.git
synced 2026-02-19 11:16:47 +00:00
35 lines
761 B
Go
35 lines
761 B
Go
package sanitize
|
|
|
|
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 Filename(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
|
|
}
|