diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2015-03-19 13:06:20 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2015-03-19 13:06:20 +0900 |
commit | a723977b9f9ac95fb8ff2596e098b4dbb6017fc4 (patch) | |
tree | f01ab4011f58778ffb6d443a8d06d973f29ef8d5 /src | |
parent | 3dddbfd8fa45283b93c06f4263a30dfe6f7055f5 (diff) |
Fix #149 - panic on empty string filter
Diffstat (limited to 'src')
-rw-r--r-- | src/algo/algo.go | 12 | ||||
-rw-r--r-- | src/algo/algo_test.go | 8 |
2 files changed, 20 insertions, 0 deletions
diff --git a/src/algo/algo.go b/src/algo/algo.go index bc4e5382..60c436e5 100644 --- a/src/algo/algo.go +++ b/src/algo/algo.go @@ -12,6 +12,10 @@ import "strings" // FuzzyMatch performs fuzzy-match func FuzzyMatch(caseSensitive bool, input *string, pattern []rune) (int, int) { + if len(pattern) == 0 { + return 0, 0 + } + runes := []rune(*input) // 0. (FIXME) How to find the shortest match? @@ -66,6 +70,10 @@ func FuzzyMatch(caseSensitive bool, input *string, pattern []rune) (int, int) { // ExactMatchStrings performs exact-match using strings package. // Currently not used. func ExactMatchStrings(caseSensitive bool, input *string, pattern []rune) (int, int) { + if len(pattern) == 0 { + return 0, 0 + } + var str string if caseSensitive { str = *input @@ -88,6 +96,10 @@ func ExactMatchStrings(caseSensitive bool, input *string, pattern []rune) (int, // We might try to implement better algorithms in the future: // http://en.wikipedia.org/wiki/String_searching_algorithm func ExactMatchNaive(caseSensitive bool, input *string, pattern []rune) (int, int) { + if len(pattern) == 0 { + return 0, 0 + } + runes := []rune(*input) numRunes := len(runes) plen := len(pattern) diff --git a/src/algo/algo_test.go b/src/algo/algo_test.go index 363b6eee..ac7aad5a 100644 --- a/src/algo/algo_test.go +++ b/src/algo/algo_test.go @@ -42,3 +42,11 @@ func TestSuffixMatch(t *testing.T) { assertMatch(t, SuffixMatch, false, "fooBarbaz", "baz", 6, 9) assertMatch(t, SuffixMatch, true, "fooBarbaz", "Baz", -1, -1) } + +func TestEmptyPattern(t *testing.T) { + assertMatch(t, FuzzyMatch, true, "foobar", "", 0, 0) + assertMatch(t, ExactMatchStrings, true, "foobar", "", 0, 0) + assertMatch(t, ExactMatchNaive, true, "foobar", "", 0, 0) + assertMatch(t, PrefixMatch, true, "foobar", "", 0, 0) + assertMatch(t, SuffixMatch, true, "foobar", "", 6, 6) +} |