Repository navigation
Expand file tree
/
Copy pathindex.go
More file actions
285 lines (256 loc) · 7.01 KB
/
Copy pathindex.go
File metadata and controls
285 lines (256 loc) · 7.01 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* The MIT License (MIT)
*
* Copyright (c) 2022 Chance Dinkins <chanceusc@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, Subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or Substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// Package index contains a trie index used by StdConverter to lookup Replacements.
package index
import (
"github.com/chanced/caps/token"
)
// IndexedReplacement is a node in an Index
// created from a Replacement
type IndexedReplacement struct {
Screaming string
Camel string
Lower string
}
// IsEmpty reports whether or not ir is empty
func (ir IndexedReplacement) IsEmpty() bool {
return len(ir.Screaming) == 0
}
func (ir IndexedReplacement) HasValue() bool {
return !ir.IsEmpty()
}
// Index is a trie index used by Converter to lookup Replacements.
type Index struct {
value IndexedReplacement
nodes map[rune]*Index
lastMatch IndexedReplacement
partialMatches []rune
caser token.Caser
}
// Clone creates a copy of the Index
func (idx *Index) Clone() Index {
return Index{
value: idx.value,
nodes: idx.nodes,
lastMatch: idx.lastMatch,
partialMatches: idx.partialMatches,
caser: idx.caser,
}
}
// NewIndex creates a new Index of Replacements,
// internally represented as Trie
//
// reverseIndexes indicates whether or not
func New(caser token.Caser) *Index {
return &Index{
nodes: make(map[rune]*Index),
caser: token.CaserOrDefault(caser),
}
}
func (idx Index) Value() IndexedReplacement {
return idx.value
}
func (idx Index) Contains(s string) bool {
_, ok := idx.Get(s)
return ok
}
// PartialMatches returns
func (idx Index) PartialMatches() string {
return string(idx.partialMatches)
}
func (idx Index) PartialMatchRunes() []rune {
return idx.partialMatches
}
func (idx Index) HasPartialMatches() bool {
return len(idx.partialMatches) > 0
}
func (idx Index) HasMatched() bool {
return !idx.lastMatch.IsEmpty()
}
func (idx Index) LastMatch() IndexedReplacement {
return idx.lastMatch
}
// HasValue reports true if the current node has a value
func (idx Index) HasValue() bool {
return !idx.value.IsEmpty()
}
// Match searches the index for the given token, returning an Index node.
//
// If t is empty, the root node is returned.
//
// # If the Index does not contain the node, an empty Index is returned
func (idx Index) Match(s string) (Index, bool) {
var ok bool
if len(s) == 0 {
return idx, false
}
next := &idx
for _, r := range token.ToLower(idx.caser, s) {
if next, ok = next.nodes[r]; !ok || next == nil {
return Index{
partialMatches: idx.partialMatches,
lastMatch: idx.lastMatch,
caser: idx.caser,
}, false
}
idx = Index{
nodes: next.nodes,
value: next.value,
lastMatch: idx.lastMatch,
partialMatches: idx.partialMatches,
caser: idx.caser,
}
if next.HasValue() {
idx.lastMatch = next.value
idx.partialMatches = nil
} else {
if idx.partialMatches == nil {
idx.partialMatches = make([]rune, 0, 8)
}
idx.partialMatches = append(idx.partialMatches, r)
}
}
return idx, true
}
// Get searches the index for the t, returning the IndexedReplacement and true
// if found.
//
// To GetForward a reversed value, use GetReverse.
func (idx *Index) Get(s string) (IndexedReplacement, bool) {
if len(s) == 0 {
return idx.value, idx.value.HasValue()
}
node := idx
var ok bool
for _, r := range token.ToLower(idx.caser, s) {
if node, ok = node.nodes[r]; !ok {
return IndexedReplacement{}, false
}
}
return node.value, node.value.HasValue()
}
// Nodes returns all nodes in the Index
func (idx *Index) Nodes() []Index {
nodes := make([]Index, 0, len(idx.nodes))
nodes = append(nodes, *idx)
for _, node := range idx.nodes {
nodes = append(nodes, node.Nodes()...)
}
return nodes
}
// Values returns all IndexedReplacements in the Index
func (idx *Index) Values() []IndexedReplacement {
nodes := idx.Nodes()
values := make([]IndexedReplacement, 0, len(nodes))
for _, n := range nodes {
if n.HasValue() {
values = append(values, n.value)
}
}
return values
}
// Add inserts r into the Index, indexed by the lowercase variant of r.Camel AND
// r.Screaming.
//
// If the Index does not contains the IndexedReplacement
// repesentation of r, one is created, inserted, and true is returned.
// Otherwise, the previous value is replaced with a new IndexedReplacement.
//
// If idx.IsReversed is true, the IndexedReplacement is inserted into the
// Index with the key in reverse order (e.g. AnExample -> elpmaxena).
func (idx *Index) Add(camel string, screaming string) bool {
ir := IndexedReplacement{
Screaming: screaming,
Camel: camel,
Lower: token.ToLower(idx.caser, camel),
}
var exists bool
var er IndexedReplacement
var ok bool
if er, ok = idx.Get(ir.Screaming); ok {
exists = true
idx.Delete(er.Camel)
idx.Delete(er.Screaming)
}
if er, ok = idx.Get(ir.Camel); ok {
exists = true
idx.Delete(er.Screaming)
idx.Delete(er.Camel)
}
node := idx
for _, r := range ir.Lower {
if _, ok = node.nodes[r]; !ok {
node.nodes[r] = &Index{
nodes: make(map[rune]*Index),
caser: idx.caser,
}
}
node = node.nodes[r]
}
node.value = ir
skey := token.ToLower(idx.caser, ir.Screaming)
if ir.Lower != skey {
node = idx
for _, r := range skey {
if _, ok = node.nodes[r]; !ok {
node.nodes[r] = &Index{
nodes: make(map[rune]*Index),
caser: idx.caser,
}
}
node = node.nodes[r]
}
node.value = ir
}
return exists
}
// Delete deletes the IndexedReplacement indexed by key from the Index.
func (idx *Index) Delete(key string) bool {
node := idx
nodes := make([]*Index, len(key))
var ok bool
var i int
var r rune
for i, r = range token.ToLower(idx.caser, key) {
nodes[i] = node
if node, ok = node.nodes[r]; !ok || node == nil {
return false
}
}
node.value = IndexedReplacement{}
child := node
k := []rune(key)
for i := len(key) - 1; i >= 0; i-- {
r = k[i]
child = node
node = nodes[i]
if child.value.IsEmpty() && len(child.nodes) == 0 {
// safe to delete
delete(node.nodes, r)
} else {
break
}
}
return true
}