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 // FuzzyMatch performs fuzzy-match
func FuzzyMatch(caseSensitive bool, input *string, pattern []rune) (int, int) { func FuzzyMatch(caseSensitive bool, input *string, pattern []rune) (int, int) {
if len(pattern) == 0 {
return 0, 0
}
runes := []rune(*input) runes := []rune(*input)
// 0. (FIXME) How to find the shortest match? // 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. // ExactMatchStrings performs exact-match using strings package.
// Currently not used. // Currently not used.
func ExactMatchStrings(caseSensitive bool, input *string, pattern []rune) (int, int) { func ExactMatchStrings(caseSensitive bool, input *string, pattern []rune) (int, int) {
if len(pattern) == 0 {
return 0, 0
}
var str string var str string
if caseSensitive { if caseSensitive {
str = *input 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: // We might try to implement better algorithms in the future:
// http://en.wikipedia.org/wiki/String_searching_algorithm // http://en.wikipedia.org/wiki/String_searching_algorithm
func ExactMatchNaive(caseSensitive bool, input *string, pattern []rune) (int, int) { func ExactMatchNaive(caseSensitive bool, input *string, pattern []rune) (int, int) {
if len(pattern) == 0 {
return 0, 0
}
runes := []rune(*input) runes := []rune(*input)
numRunes := len(runes) numRunes := len(runes)
plen := len(pattern) 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, false, "fooBarbaz", "baz", 6, 9)
assertMatch(t, SuffixMatch, true, "fooBarbaz", "Baz", -1, -1) 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)
}