-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuxxy.go
More file actions
55 lines (43 loc) · 949 Bytes
/
fuxxy.go
File metadata and controls
55 lines (43 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package fuxxy
import "sort"
type (
match struct {
i int
points int
}
byPointsDesc []match
)
func (a byPointsDesc) Len() int { return len(a) }
func (a byPointsDesc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byPointsDesc) Less(i, j int) bool { return a[i].points > a[j].points }
func Matches(names []string, value string) []string {
if len(names) == 0 || len(value) == 0 {
return nil
}
var (
valueRunes = []rune(value)
matches = make([]match, 0, 8)
)
for i, name := range names {
var j, pts, in int
for _, r := range name {
if r == valueRunes[j] {
pts += in
j, in = j+1, in+1
if j == len(valueRunes) {
matches = append(matches, match{i: i, points: pts})
break
}
} else {
in = 0
pts--
}
}
}
sort.Stable(byPointsDesc(matches))
ret := make([]string, len(matches))
for i, match := range matches {
ret[i] = names[match.i]
}
return ret
}