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 { type Node struct {
Prev *Node Prev *Node
Next *Node Next *Node
Name []byte Name []rune
Type Type Type Type
} }
@ -36,33 +36,62 @@ type Node struct {
// return string(path) // return string(path)
// } // }
func xPath(spath string) { func toString(root *Node) string {
var path []byte = make([]byte, len(spath)+1) var content string
copy(path, spath)
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{} root := &Node{}
cur := root cur := root
for i := 0; i < len(spath); i++ { for i := 0; i < len(spath); i++ {
c := path[i] c := path[i]
if c == '/' { switch c {
case '/':
if path[i+1] == '/' { if path[i+1] == '/' {
cur.Type = TypeAllChildren cur.Type = TypeAllChildren
i++
} else { } else {
cur.Type = TypeChild cur.Type = TypeChild
} }
cur.Next = &Node{Next: cur} if len(cur.Name) == 0 {
continue
}
cur.Next = &Node{Prev: cur}
cur = cur.Next 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) { 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("/a/./b/../../c/"))
// t.Error(xPath("/")) // t.Error(xPath("/"))
// t.Error(xPath("/a//b////c/d//././/..")) t.Error(xPath("/a/./b/../../c/"))
// t.Error(xPath("/a//b////c/d//././/.."))
} }