Skip to content
Myr

Fix panic when pattern occurs after 2^15-th column

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix #666

authored by Junegunn Choi · committed by Junegunn Choi · parent 37f43fb · Browse files · 2 files+18 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+18 added−7 removed

  • other:+18 added−7 removed

2 files+18 added−7 removed

modified:src/algo/algo_test.go+10 added
1package algo23import (4 "math"5 "sort"6 "strings"7 "testing"8
145 unmodified lines
154 assertMatch(t, PrefixMatch, true, dir, "foobar", "", 0, 0, 0)155 assertMatch(t, SuffixMatch, true, dir, "foobar", "", 6, 6, 0)156 }157}158159func TestLongString(t *testing.T) {160 bytes := make([]byte, math.MaxUint16*2)161 for i := range bytes {162 bytes[i] = 'x'163 }164 bytes[math.MaxUint16] = 'z'165 assertMatch(t, FuzzyMatchV2, true, true, string(bytes), "zx", math.MaxUint16, math.MaxUint16+2, scoreMatch*2+bonusConsecutive)166}
modified:src/algo/algo.go+8 added-7 removed
255 unmodified lines
256 return FuzzyMatchV1(caseSensitive, forward, input, pattern, withPos, slab)257 }258259 // Reuse pre-allocated integer slice to avoid unnecessary sweeping of garbages260 offset := 0260 offset16 := 0261 offset32 := 0262 // Bonus point for each position262 offset, B := alloc16(offset, slab, N, false)263 offset16, B := alloc16(offset16, slab, N, false)264 // The first occurrence of each character in the pattern264 offset, F := alloc16(offset, slab, M, false)265 offset32, F := alloc32(offset32, slab, M, false)266 // Rune array266 _, T := alloc32(0, slab, N, false)267 offset32, T := alloc32(offset32, slab, N, false)268269 // Phase 1. Check if there's a match and calculate bonus for each point270 pidx, lastIdx, prevClass := 0, 0, charNonWord271 for idx := 0; idx < N; idx++ {
19 unmodified lines
291292 if pidx < M {293 if char == pattern[pidx] {294 lastIdx = idx294 F[pidx] = int16(idx)295 F[pidx] = int32(idx)296 pidx++297 }298 } else {299 if char == pattern[M-1] {
7 unmodified lines
307308 // Phase 2. Fill in score matrix (H)309 // Unlike the original algorithm, we do not allow omission.310 width := lastIdx - int(F[0]) + 1310 offset, H := alloc16(offset, slab, width*M, false)311 offset16, H := alloc16(offset16, slab, width*M, false)312313 // Possible length of consecutive chunk at each position.313 offset, C := alloc16(offset, slab, width*M, false)314 offset16, C := alloc16(offset16, slab, width*M, false)315316 maxScore, maxScorePos := int16(0), 0317 for i := 0; i < M; i++ {318 I := i * width
382 unmodified lines