fix: replace shoud replace the longest match

This commit is contained in:
hudahai
2023-02-04 15:31:16 +08:00
committed by Kevin Wan
parent b6f1bce695
commit d9a732a273
3 changed files with 73 additions and 61 deletions

View File

@@ -98,3 +98,45 @@ func (n *node) find(chars []rune) []scope {
return scopes
}
func (n *node) longestMatch(chars []rune, start int) (used int, jump *node, matched bool) {
cur := n
var matchedNode *node
for i := start; i < len(chars); i++ {
child, ok := cur.children[chars[i]]
if ok {
cur = child
if cur.end {
matchedNode = cur
}
} else {
if matchedNode != nil {
return matchedNode.depth, nil, true
}
if n.end {
return start, nil, true
}
var jump *node
for cur.fail != nil {
jump, ok = cur.fail.children[chars[i]]
if ok {
break
}
cur = cur.fail
}
if jump != nil {
return i + 1 - jump.depth, jump, false
}
return i + 1, nil, false
}
}
// this longest matched node
if matchedNode != nil {
return matchedNode.depth, nil, true
}
// last mathed node
if n.end {
return start, nil, true
}
return len(chars), nil, false
}