Fix #149 - panic on empty string filter

This commit is contained in:
Junegunn Choi 2015-03-19 13:06:20 +09:00
parent 3dddbfd8fa
commit a723977b9f
2 changed files with 20 additions and 0 deletions

View File

@ -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)

View File

@ -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)
}