From a723977b9f9ac95fb8ff2596e098b4dbb6017fc4 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Thu, 19 Mar 2015 13:06:20 +0900 Subject: [PATCH] Fix #149 - panic on empty string filter --- src/algo/algo.go | 12 ++++++++++++ src/algo/algo_test.go | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/src/algo/algo.go b/src/algo/algo.go index bc4e538..60c436e 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 363b6ee..ac7aad5 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) +}