-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
57 lines (47 loc) · 851 Bytes
/
path.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
54
55
56
57
package wikithing
import (
"strings"
"unicode"
)
type Path struct{ p []string }
func (p Path) Path() string {
return strings.Join(p.p, "/")
}
func (p Path) String() string {
return strings.Join(p.p, ".")
}
const AllowedSpecialChars = "-()"
func ParsePath(s string) Path {
p := Path{make([]string, 0)}
s = strings.ToLower(s)
s = strings.Map(func(r rune) rune {
switch r {
case '.', '/', '\\', ' ':
return '.'
}
if unicode.IsLetter(r) {
return r
}
if unicode.IsNumber(r) {
return r
}
if strings.ContainsAny(string(r), AllowedSpecialChars) {
return r
}
return -1
}, s)
buf := strings.Builder{}
for _, x := range s {
if x != '.' {
buf.WriteRune(x)
continue
}
if buf.Len() == 0 {
continue
}
p.p = append(p.p, buf.String())
buf.Reset()
}
p.p = append(p.p, buf.String())
return p
}