diff --git a/main_test.go b/main_test.go index 5beb534..faaec26 100644 --- a/main_test.go +++ b/main_test.go @@ -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//././/..")) }