From c509b817482a735fba47c22ad7ab070b59ed3cae Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Mon, 21 Sep 2020 02:06:43 +0800 Subject: [PATCH] todo --- main_test.go | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) 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//././/..")) }