This commit is contained in:
eson 2020-09-21 02:06:43 +08:00
parent a32f5b55f9
commit c509b81748

View File

@ -15,7 +15,7 @@ const (
type Node struct {
Prev *Node
Next *Node
Name []byte
Name []rune
Type Type
}
@ -36,33 +36,62 @@ type Node struct {
// return string(path)
// }
func xPath(spath string) {
var path []byte = make([]byte, len(spath)+1)
copy(path, spath)
func toString(root *Node) string {
var content string
for root != nil {
content += string(root.Name)
if root.Type == TypeAllChildren {
content += "//"
} else {
content += "/"
}
root = root.Next
}
return content
}
func xPath(spath string) string {
var path []rune = []rune(spath)
path = append(path, ' ')
root := &Node{}
cur := root
for i := 0; i < len(spath); i++ {
c := path[i]
if c == '/' {
switch c {
case '/':
if path[i+1] == '/' {
cur.Type = TypeAllChildren
i++
} else {
cur.Type = TypeChild
}
cur.Next = &Node{Next: cur}
if len(cur.Name) == 0 {
continue
}
cur.Next = &Node{Prev: cur}
cur = cur.Next
// case '(': 先拿括号
default:
cur.Name = append(cur.Name, c)
}
cur.Name = append(cur.Name, c)
}
return toString(root)
}
func TestMain(t *testing.T) {
// t.Error(xPath("/a/../../b/../c//.//"))
// t.Error(xPath("/a/../../b/../c//.//"))
// t.Error(xPath("/a/./b/../../c/"))
// t.Error(xPath("/"))
// t.Error(xPath("/a//b////c/d//././/.."))
t.Error(xPath("/a/./b/../../c/"))
// t.Error(xPath("/a//b////c/d//././/.."))
}