Skip to content

Commit

Permalink
fix #94
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed Mar 30, 2024
1 parent 6165646 commit 3667596
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ func getHashCode(n NodeNavigator) uint64 {
}
}
h := fnv.New64a()
h.Write([]byte(sb.String()))
h.Write(sb.Bytes())
return h.Sum64()
}

Expand Down
19 changes: 14 additions & 5 deletions xpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type NodeNavigator interface {
type NodeIterator struct {
node NodeNavigator
query query
table map[uint64]bool
}

// Current returns current node which matched.
Expand All @@ -83,14 +84,22 @@ func (t *NodeIterator) Current() NodeNavigator {

// MoveNext moves Navigator to the next match node.
func (t *NodeIterator) MoveNext() bool {
n := t.query.Select(t)
if n != nil {
for {
n := t.query.Select(t)
if n == nil {
return false
}
if !t.node.MoveTo(n) {
t.node = n.Copy()
}
// https://github.com/antchfx/xpath/issues/94
id := getHashCode(n.Copy())
if _, ok := t.table[id]; ok {
continue
}
t.table[id] = true
return true
}
return false
}

// Select selects a node set using the specified XPath expression.
Expand Down Expand Up @@ -121,14 +130,14 @@ func (expr *Expr) Evaluate(root NodeNavigator) interface{} {
val := expr.q.Evaluate(iteratorFunc(func() NodeNavigator { return root }))
switch val.(type) {
case query:
return &NodeIterator{query: expr.q.Clone(), node: root}
return &NodeIterator{query: expr.q.Clone(), node: root, table: make(map[uint64]bool)}
}
return val
}

// Select selects a node set using the specified XPath expression.
func (expr *Expr) Select(root NodeNavigator) *NodeIterator {
return &NodeIterator{query: expr.q.Clone(), node: root}
return &NodeIterator{query: expr.q.Clone(), node: root, table: make(map[uint64]bool)}
}

// String returns XPath expression string.
Expand Down

0 comments on commit 3667596

Please sign in to comment.