1
- local MATCH_SCORE = 7
2
- local GAP_PENALTY = - 1
1
+ local MATCH_SCORE = 12
2
+ local GAP_OPEN_PENALTY = - 5
3
+ local GAP_EXTEND_PENALTY = - 1
3
4
4
5
-- bonus for matching the first character of the haystack
5
- local PREFIX_BONUS = 6
6
+ local PREFIX_BONUS = 12
6
7
-- bonus for matching character after a delimiter in the haystack (e.g. space, comma, underscore, slash, etc)
7
8
local DELIMITER_BONUS = 4
8
9
-- bonus for haystack == needle
9
10
local EXACT_MATCH_BONUS = 4
10
11
-- bonus for matching the case (upper or lower) of the haystack
11
- local MATCHING_CASE_BONUS = 1
12
+ local MATCHING_CASE_BONUS = 4
12
13
13
14
local DELIMITERS = {
14
15
[string.byte (' ' , 1 )] = true ,
@@ -43,7 +44,10 @@ local function match(needle, haystack)
43
44
score = score + MATCH_SCORE
44
45
45
46
-- gap penalty
46
- if needle_idx ~= 1 then score = score + GAP_PENALTY * (haystack_idx - haystack_start_idx ) end
47
+ if needle_idx ~= 1 then
48
+ local gap_length = haystack_idx - haystack_start_idx
49
+ if gap_length > 0 then score = score + GAP_OPEN_PENALTY + GAP_EXTEND_PENALTY * (gap_length - 1 ) end
50
+ end
47
51
48
52
-- bonuses
49
53
if needle_char == haystack_char then score = score + MATCHING_CASE_BONUS end
@@ -69,10 +73,10 @@ local function match(needle, haystack)
69
73
return score , exact
70
74
end
71
75
72
- assert (match (' fbb' , ' barbazfoobarbaz' ) == 20 , ' fbb should match barbazfoobarbaz with score 18 ' )
73
- assert (match (' foo' , ' _foobar' ) == 28 , ' foo should match foobar with score 29 ' )
74
- assert (match (' Foo' , ' foobar' ) == 29 , ' foo should match foobar with score 29 ' )
75
- assert (match (' foo' , ' foobar' ) == 30 , ' foo should match foobar with score 30 ' )
76
+ assert (match (' fbb' , ' barbazfoobarbaz' ) == 36 , ' fbb should match barbazfoobarbaz with score 36 ' )
77
+ assert (match (' foo' , ' _foobar' ) == 52 , ' foo should match foobar with score 52 ' )
78
+ assert (match (' Foo' , ' foobar' ) == 56 , ' foo should match foobar with score 56 ' )
79
+ assert (match (' foo' , ' foobar' ) == 60 , ' foo should match foobar with score 60 ' )
76
80
assert (match (' foo' , ' fobar' ) == nil , ' foo should not match fobar' )
77
81
78
82
return match
0 commit comments