-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
72 lines (60 loc) · 1.39 KB
/
match.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package fuzzaldrin
import (
"math"
"os"
"unicode"
)
func basenameMatch(str, query string) []int {
strRunes := []rune(str)
index := len(strRunes) - 1
for index >= 0 && os.IsPathSeparator(uint8(strRunes[index])) {
index--
}
slashCount := 0
lastCharacter := index
var base string
for index >= 0 {
if os.IsPathSeparator(uint8(strRunes[index])) {
slashCount++
if base == "" {
base = string(strRunes[index+1 : lastCharacter+1])
}
} else if index == 0 {
if base == "" {
if lastCharacter < len(strRunes)-1 {
base = string(strRunes[0 : lastCharacter+1])
} else {
base = string(strRunes)
}
}
}
index--
}
return match(base, query, len(str)-len(base))
}
func match(str, query string, offset int) (indexes []int) {
strRunes := []rune(str)
if str == query {
for i := offset; i < len(strRunes)+offset; i++ {
indexes = append(indexes, i)
}
return
}
indexInStr := 0
for _, ch := range query {
lcIndex := findIndex(strRunes, unicode.ToLower(ch))
ucIndex := findIndex(strRunes, unicode.ToUpper(ch))
minIndex := int(math.Min(float64(lcIndex), float64(ucIndex)))
if minIndex == -1 {
minIndex = int(math.Max(float64(lcIndex), float64(ucIndex)))
}
if minIndex == -1 {
return
}
indexes = append(indexes, offset+minIndex)
indexInStr = minIndex
offset += indexInStr + 1
strRunes = strRunes[indexInStr+1 : len(strRunes)]
}
return
}