/
names.go
48 lines (45 loc) · 834 Bytes
/
names.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
package forms
import "strings"
func JoinNames(path ...string) string {
var buf strings.Builder
for _, s := range path {
if s == "" {
continue
}
if buf.Len() == 0 {
buf.WriteString(s)
} else {
j := strings.IndexByte(s, '[')
if j < 0 {
buf.WriteByte('[')
buf.WriteString(s)
buf.WriteByte(']')
} else {
if j > 0 {
buf.WriteByte('[')
buf.WriteString(s[:j])
buf.WriteByte(']')
}
buf.WriteString(s[j:])
}
}
}
return buf.String()
}
func SplitName(name string) []string {
var path []string
for {
component, suffix, _ := strings.Cut(name, "[")
if component != "" {
path = append(path, component)
}
if suffix == "" {
break
}
component, name, _ = strings.Cut(suffix, "]")
if component != "" {
path = append(path, component)
}
}
return path
}