-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace_forbidden_chars.go
53 lines (49 loc) · 1.18 KB
/
replace_forbidden_chars.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package fsutil
import "strings"
// ReplaceForbiddenChars replaces characters forbidden in some filesystems with
// dash (-).
//
// Characters replaced are:
// - < (less than)
// - > (greater than)
// - : (colon)
// - " (double quote)
// - / (slash)
// - \ (backslash)
// - | (pipe)
// - ? (question mark)
// - * (asterisk)
//
// In addition, all leading and trailing whitespace characters are trimmed.
func ReplaceForbiddenChars(name string) string {
return ReplaceForbiddenCharsWith(name, "-")
}
// ReplaceForbiddenChars replaces characters forbidden in some filesystems with
// an arbitrary character.
//
// Characters replaced are:
// - < (less than)
// - > (greater than)
// - : (colon)
// - " (double quote)
// - / (slash)
// - \ (backslash)
// - | (pipe)
// - ? (question mark)
// - * (asterisk)
//
// In addition, all leading and trailing whitespace characters are trimmed.
func ReplaceForbiddenCharsWith(name string, newchar string) string {
r := strings.NewReplacer(
"<", newchar,
">", newchar,
":", newchar,
"\"", newchar,
"/", newchar,
"\\", newchar,
"|", newchar,
"?", newchar,
"*", newchar,
)
return strings.TrimSpace(r.Replace(name))
}