1%
This commit is contained in:
parent
c509b81748
commit
414d979931
201
main_test.go
201
main_test.go
|
@ -1,97 +1,188 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Type int
|
// type Type int
|
||||||
|
|
||||||
|
// const (
|
||||||
|
// TypeUnknown Type = iota
|
||||||
|
|
||||||
|
// TypeNode
|
||||||
|
// TypeText
|
||||||
|
// TypeAttribute
|
||||||
|
// )
|
||||||
|
|
||||||
|
// // type Attribute struct {
|
||||||
|
// // Name []rune
|
||||||
|
// // Value []rune
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// type Node struct {
|
||||||
|
// Parent *Node
|
||||||
|
// Children []*Node
|
||||||
|
|
||||||
|
// Attributes []*Node
|
||||||
|
|
||||||
|
// Name []rune
|
||||||
|
// Value []rune
|
||||||
|
|
||||||
|
// Type Type
|
||||||
|
// }
|
||||||
|
|
||||||
|
type Selection int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TypeChild Type = iota
|
SelUnknown Selection = iota
|
||||||
TypeChildren
|
SelRoot // 根节点
|
||||||
TypeAllChildren
|
SelSelf // 自身
|
||||||
|
SelParent // 父节点
|
||||||
|
SelChildren // 孩子
|
||||||
|
SelAllChildRen // 所有子孙
|
||||||
|
SelMethod // 函数类型
|
||||||
|
SelAttribute // 属性
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Predicates struct {
|
||||||
Prev *Node
|
|
||||||
Next *Node
|
|
||||||
Name []rune
|
Name []rune
|
||||||
Type Type
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// func extractPath(cur *Node) string {
|
type Node struct {
|
||||||
// var path []byte
|
Name []rune
|
||||||
|
Value []rune
|
||||||
// if cur.Next.Next == nil {
|
Next *Node
|
||||||
// return "/"
|
Sel Selection
|
||||||
// }
|
Pred Predicates
|
||||||
|
|
||||||
// for ; cur != nil; cur = cur.Next {
|
|
||||||
// path = append(path, cur.Name...)
|
|
||||||
// if cur.Next.Next == nil {
|
|
||||||
// break
|
|
||||||
// }
|
|
||||||
// path = append(path, '/')
|
|
||||||
// }
|
|
||||||
// return string(path)
|
|
||||||
// }
|
|
||||||
|
|
||||||
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 compile(spath string) (head *Node, tail *Node) {
|
||||||
}
|
var path []rune = []rune(strings.TrimSpace(spath))
|
||||||
|
|
||||||
func xPath(spath string) string {
|
|
||||||
|
|
||||||
var path []rune = []rune(spath)
|
|
||||||
path = append(path, ' ')
|
path = append(path, ' ')
|
||||||
|
|
||||||
root := &Node{}
|
var cur *Node
|
||||||
cur := root
|
if path[0] == '/' {
|
||||||
|
head = &Node{}
|
||||||
|
head.Sel = SelRoot
|
||||||
|
head.Next = cur
|
||||||
|
} else {
|
||||||
|
head = cur
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < len(spath); i++ {
|
// for i := 0; i < len(spath); i++ {
|
||||||
|
var i = 0
|
||||||
c := path[i]
|
c := path[i]
|
||||||
switch c {
|
switch c {
|
||||||
case '/':
|
case '/':
|
||||||
if path[i+1] == '/' {
|
if path[i+1] == '/' {
|
||||||
cur.Type = TypeAllChildren
|
|
||||||
i++
|
i++
|
||||||
|
head.Sel = SelAllChildRen
|
||||||
} else {
|
} else {
|
||||||
cur.Type = TypeChild
|
head.Sel = SelChildren
|
||||||
|
}
|
||||||
|
case '.':
|
||||||
|
head.Sel = SelSelf
|
||||||
|
case '(':
|
||||||
|
// 进入递归
|
||||||
|
i++
|
||||||
|
start, end := getBrackets(path, len(spath), &i)
|
||||||
|
h, t := compile(string(path[start:end]))
|
||||||
|
cur.Next = h
|
||||||
|
cur = t
|
||||||
|
case '|':
|
||||||
|
// 递归
|
||||||
|
default:
|
||||||
|
head.Sel = SelUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cur.Name) == 0 {
|
return head, cur
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cur.Next = &Node{Prev: cur}
|
func getAxes(path []rune, limit int, ii *int, cur *Node) {
|
||||||
cur = cur.Next
|
i := *ii
|
||||||
// case '(': 先拿括号
|
|
||||||
|
|
||||||
|
for ; i < limit; i++ {
|
||||||
|
c := path[i]
|
||||||
|
switch c {
|
||||||
|
case '[':
|
||||||
|
case '/':
|
||||||
|
case '@': //
|
||||||
|
cur.Sel = SelAttribute
|
||||||
|
cur.Name = getAttributeName(path, limit, ii)
|
||||||
|
case '.': // 获取节点
|
||||||
|
if path[i+1] == '.' {
|
||||||
|
i++
|
||||||
|
cur.Sel = SelParent
|
||||||
|
} else {
|
||||||
|
cur.Sel = SelSelf
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case '(': // function
|
||||||
|
s, e := getBrackets(path, limit, ii)
|
||||||
|
cur.Value = path[s:e]
|
||||||
|
cur.Sel = SelMethod
|
||||||
|
return
|
||||||
default:
|
default:
|
||||||
cur.Name = append(cur.Name, c)
|
cur.Name = append(cur.Name, c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return toString(root)
|
func getAttributeName(path []rune, limit int, ii *int) []rune {
|
||||||
|
i := *ii
|
||||||
|
|
||||||
|
for start := i; i < limit; i++ {
|
||||||
|
if c, ok := Escape(path, &i); !ok {
|
||||||
|
switch {
|
||||||
|
case c >= ' ' && c <= '/':
|
||||||
|
fallthrough
|
||||||
|
case c >= ':' && c <= '@':
|
||||||
|
fallthrough
|
||||||
|
case c >= '[' && c <= '`':
|
||||||
|
fallthrough
|
||||||
|
case c >= '{' && c <= '~':
|
||||||
|
return path[start:i]
|
||||||
|
default:
|
||||||
|
panic("get attribute error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
panic("get attribute error")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBrackets(path []rune, limit int, ii *int) (start int, end int) {
|
||||||
|
i := *ii
|
||||||
|
open := 1
|
||||||
|
for start := i; i < limit; i++ {
|
||||||
|
if c, ok := Escape(path, &i); !ok {
|
||||||
|
switch c {
|
||||||
|
case '(':
|
||||||
|
open++
|
||||||
|
case ')':
|
||||||
|
open--
|
||||||
|
}
|
||||||
|
if open == 0 {
|
||||||
|
return start, i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
panic("can't find ')' close?")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Escape
|
||||||
|
func Escape(data []rune, i *int) (rune, bool) {
|
||||||
|
if data[*i] == '\\' {
|
||||||
|
*i++
|
||||||
|
return data[*i], true
|
||||||
|
}
|
||||||
|
return data[*i], false
|
||||||
}
|
}
|
||||||
|
|
||||||
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/"))
|
// t.Error(xPath("/a/./b/../../c/"))
|
||||||
// t.Error(xPath("/a//b////c/d//././/.."))
|
// t.Error(xPath("/a//b////c/d//././/.."))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user