generated from tbistr/golang-vscode-devcontainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_algo.go
147 lines (127 loc) · 3.05 KB
/
default_algo.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package inc
import (
"strings"
"unicode/utf8"
)
// Algorithm is the interface for the algorithm used by the engine.
//
// Algorithm can be non transitive.
// So, you can implement a algorithm that calculates with whole query every time.
type Algorithm interface {
// AppendCands appends candidates to the engine.
AppendCands([]*Candidate)
// DeleteCands deletes candidates from the engine.
DeleteCands()
// GetQuery returns the current query.
GetQuery() []rune
// AddQuery adds a rune to the query.
AddQuery(rune)
// RmQuery removes a rune from the query.
RmQuery()
// DelQuery deletes (clears) the query.
DelQuery()
}
type defaultAlgo struct {
query []rune
cands []*Candidate
}
var _ Algorithm = (*defaultAlgo)(nil)
// findAndMark finds runes in the query from the candidate and marks them.
// This starts from the current last key rune.
func findAndMark(c *Candidate, query ...rune) {
lastKey := lastOr(c.KeyRunes, KeyRune{Pos: 0, Len: 0})
tail := c.String()[lastKey.Pos+lastKey.Len:]
for _, r := range query {
found := indexIgnoreCase(tail, r)
if found == -1 {
c.Matched = false
return
}
// head tail
// "123" + "四五六"
// if findAndMark('四') ->
// Pos = lPos + lLen + found = 2 + 1 + 0 = 3
// Len = RuneLen('四') = 3
lastKey = KeyRune{
Pos: lastKey.Pos + lastKey.Len + uint(found),
Len: uint(utf8.RuneLen(r)),
}
c.KeyRunes = append(c.KeyRunes, lastKey)
tail = tail[lastKey.Len:]
}
}
// indexIgnoreCase is a custom search function that ignores case.
// Only ASCII characters are supported.
//
// A <-> a
func indexIgnoreCase(s string, r rune) int {
switch {
case 'a' <= r && r <= 'z':
return strings.IndexFunc(s, func(ir rune) bool {
return ir == r || ir == r+('A'-'a')
})
case 'A' <= r && r <= 'Z':
return strings.IndexFunc(s, func(ir rune) bool {
return ir == r || ir == r+('a'-'A')
})
default:
return strings.IndexRune(s, r)
}
}
func (a *defaultAlgo) AppendCands(cands []*Candidate) {
for _, c := range cands {
c.Matched = true
c.KeyRunes = nil
findAndMark(c, a.query...)
}
a.cands = append(a.cands, cands...)
}
func (a *defaultAlgo) DeleteCands() {
a.cands = nil
}
func (d *defaultAlgo) GetQuery() []rune {
return d.query
}
// AddQuery adds a rune to the query.
func (a *defaultAlgo) AddQuery(r rune) {
a.query = append(a.query, r)
for _, c := range a.cands {
if c.Matched {
findAndMark(c, r)
}
}
}
// RmQuery removes the last rune from the query.
func (a *defaultAlgo) RmQuery() {
a.query = rmLast(a.query)
for _, c := range a.cands {
if c.Matched {
c.KeyRunes = rmLast(c.KeyRunes)
}
c.Matched = len(c.KeyRunes) == len(a.query)
}
}
// DelQuery removes all runes from the query.
// All candidates will be matched.
func (a *defaultAlgo) DelQuery() {
if len(a.query) == 0 {
return
}
a.query = nil
for _, c := range a.cands {
c.Matched = true
c.KeyRunes = nil
}
}
func lastOr[T any](ts []T, defaultV T) T {
if len(ts) == 0 {
return defaultV
}
return ts[len(ts)-1]
}
func rmLast[T any](ts []T) []T {
if len(ts) == 0 {
return ts
}
return ts[:len(ts)-1]
}