summaryrefslogtreecommitdiff
path: root/src/algo
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2016-09-21 01:15:06 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2016-09-21 01:15:06 +0900
commit791076d3662f04a20386f6ea9d166f6eccbd1a81 (patch)
tree67f351fd9f320a577d3c9a429318475be70691d5 /src/algo
parent37f43fbb35b819501fe4db7844d17231789c55cd (diff)
downloadfzf-791076d3662f04a20386f6ea9d166f6eccbd1a81.tar.gz
Fix panic when pattern occurs after 2^15-th column
Fix #666
Diffstat (limited to 'src/algo')
-rw-r--r--src/algo/algo.go15
-rw-r--r--src/algo/algo_test.go10
2 files changed, 18 insertions, 7 deletions
diff --git a/src/algo/algo.go b/src/algo/algo.go
index 622c9607..1b85594a 100644
--- a/src/algo/algo.go
+++ b/src/algo/algo.go
@@ -257,13 +257,14 @@ func FuzzyMatchV2(caseSensitive bool, forward bool, input util.Chars, pattern []
}
// Reuse pre-allocated integer slice to avoid unnecessary sweeping of garbages
- offset := 0
+ offset16 := 0
+ offset32 := 0
// Bonus point for each position
- offset, B := alloc16(offset, slab, N, false)
+ offset16, B := alloc16(offset16, slab, N, false)
// The first occurrence of each character in the pattern
- offset, F := alloc16(offset, slab, M, false)
+ offset32, F := alloc32(offset32, slab, M, false)
// Rune array
- _, T := alloc32(0, slab, N, false)
+ offset32, T := alloc32(offset32, slab, N, false)
// Phase 1. Check if there's a match and calculate bonus for each point
pidx, lastIdx, prevClass := 0, 0, charNonWord
@@ -291,7 +292,7 @@ func FuzzyMatchV2(caseSensitive bool, forward bool, input util.Chars, pattern []
if pidx < M {
if char == pattern[pidx] {
lastIdx = idx
- F[pidx] = int16(idx)
+ F[pidx] = int32(idx)
pidx++
}
} else {
@@ -307,10 +308,10 @@ func FuzzyMatchV2(caseSensitive bool, forward bool, input util.Chars, pattern []
// Phase 2. Fill in score matrix (H)
// Unlike the original algorithm, we do not allow omission.
width := lastIdx - int(F[0]) + 1
- offset, H := alloc16(offset, slab, width*M, false)
+ offset16, H := alloc16(offset16, slab, width*M, false)
// Possible length of consecutive chunk at each position.
- offset, C := alloc16(offset, slab, width*M, false)
+ offset16, C := alloc16(offset16, slab, width*M, false)
maxScore, maxScorePos := int16(0), 0
for i := 0; i < M; i++ {
diff --git a/src/algo/algo_test.go b/src/algo/algo_test.go
index 7317eb18..fc24f6d9 100644
--- a/src/algo/algo_test.go
+++ b/src/algo/algo_test.go
@@ -1,6 +1,7 @@
package algo
import (
+ "math"
"sort"
"strings"
"testing"
@@ -154,3 +155,12 @@ func TestEmptyPattern(t *testing.T) {
assertMatch(t, SuffixMatch, true, dir, "foobar", "", 6, 6, 0)
}
}
+
+func TestLongString(t *testing.T) {
+ bytes := make([]byte, math.MaxUint16*2)
+ for i := range bytes {
+ bytes[i] = 'x'
+ }
+ bytes[math.MaxUint16] = 'z'
+ assertMatch(t, FuzzyMatchV2, true, true, string(bytes), "zx", math.MaxUint16, math.MaxUint16+2, scoreMatch*2+bonusConsecutive)
+}