xpath/main_test.go
2020-09-18 19:19:56 +08:00

69 lines
1.0 KiB
Go

package main
import (
"testing"
)
type Type int
const (
TypeChild Type = iota
TypeChildren
TypeAllChildren
)
type Node struct {
Prev *Node
Next *Node
Name []byte
Type Type
}
// func extractPath(cur *Node) string {
// var path []byte
// if cur.Next.Next == nil {
// return "/"
// }
// for ; cur != nil; cur = cur.Next {
// path = append(path, cur.Name...)
// if cur.Next.Next == nil {
// break
// }
// path = append(path, '/')
// }
// return string(path)
// }
func xPath(spath string) {
var path []byte = make([]byte, len(spath)+1)
copy(path, spath)
root := &Node{}
cur := root
for i := 0; i < len(spath); i++ {
c := path[i]
if c == '/' {
if path[i+1] == '/' {
cur.Type = TypeAllChildren
} else {
cur.Type = TypeChild
}
cur.Next = &Node{Next: cur}
cur = cur.Next
}
cur.Name = append(cur.Name, c)
}
}
func TestMain(t *testing.T) {
// t.Error(xPath("/a/../../b/../c//.//"))
// t.Error(xPath("/a/./b/../../c/"))
// t.Error(xPath("/"))
// t.Error(xPath("/a//b////c/d//././/.."))
}