@pulsar-edit/fuzzy-native 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +78 -0
- package/binding.gyp +40 -0
- package/lib/main.js +1 -0
- package/lib/main.js.flow +45 -0
- package/package.json +30 -0
- package/src/MatcherBase.cpp +329 -0
- package/src/MatcherBase.h +93 -0
- package/src/binding.cpp +233 -0
- package/src/fuzzaldrin_score.cpp +112 -0
- package/src/fuzzaldrin_score.h +5 -0
- package/src/memrchr.h +179 -0
- package/src/score_match.cpp +278 -0
- package/src/score_match.h +32 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
#include "score_match.h"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This is mostly based on Greg Hurrell's implementation in
|
|
5
|
+
* https://github.com/wincent/command-t/blob/master/ruby/command-t/match.c
|
|
6
|
+
* with a few modifications and extra optimizations.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#include <algorithm>
|
|
10
|
+
#include <string>
|
|
11
|
+
#include <cstring>
|
|
12
|
+
|
|
13
|
+
// memrchr is a non-standard extension only available in glibc.
|
|
14
|
+
#if defined(__APPLE__) || defined(_WIN32) || defined(_WIN64)
|
|
15
|
+
#include "memrchr.h"
|
|
16
|
+
#endif
|
|
17
|
+
|
|
18
|
+
using namespace std;
|
|
19
|
+
|
|
20
|
+
// Initial multiplier when a gap is used.
|
|
21
|
+
const float BASE_DISTANCE_PENALTY = 0.6;
|
|
22
|
+
|
|
23
|
+
// penalty = BASE_DISTANCE_PENALTY - (dist - 1) * ADDITIONAL_DISTANCE_PENALTY.
|
|
24
|
+
const float ADDITIONAL_DISTANCE_PENALTY = 0.05;
|
|
25
|
+
|
|
26
|
+
// The lowest the distance penalty can go. Add epsilon for precision errors.
|
|
27
|
+
const float MIN_DISTANCE_PENALTY = 0.2;
|
|
28
|
+
|
|
29
|
+
// Bail if the state space exceeds this limit.
|
|
30
|
+
const size_t MAX_MEMO_SIZE = 10000;
|
|
31
|
+
|
|
32
|
+
// Convenience structure for passing around during recursion.
|
|
33
|
+
struct MatchInfo {
|
|
34
|
+
const char *haystack;
|
|
35
|
+
const char *haystack_case;
|
|
36
|
+
size_t haystack_len;
|
|
37
|
+
const char *needle;
|
|
38
|
+
const char *needle_case;
|
|
39
|
+
size_t needle_len;
|
|
40
|
+
int* last_match;
|
|
41
|
+
float *memo;
|
|
42
|
+
size_t *best_match;
|
|
43
|
+
bool smart_case;
|
|
44
|
+
size_t max_gap;
|
|
45
|
+
float min_score;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* This algorithm essentially looks for an optimal matching
|
|
50
|
+
* from needle characters to matching haystack characters. We assign a multiplier
|
|
51
|
+
* to each character in the needle, and multiply the scores together in the end.
|
|
52
|
+
*
|
|
53
|
+
* The key insight is that we wish to reduce the distance between adjacent
|
|
54
|
+
* matched characters in the haystack. Exact substring matches will receive a score
|
|
55
|
+
* of 1, while gaps incur significant multiplicative penalties.
|
|
56
|
+
*
|
|
57
|
+
* We reduce the penalty for word boundaries. This includes:
|
|
58
|
+
* - paths (a in /x/abc)
|
|
59
|
+
* - hyphens/underscores (a in x-a or x_a)
|
|
60
|
+
* - upper camelcase names (A in XyzAbc)
|
|
61
|
+
*
|
|
62
|
+
* See below for the exact cases and weights used.
|
|
63
|
+
*
|
|
64
|
+
* Computing the optimal matching is a relatively straight-forward
|
|
65
|
+
* dynamic-programming problem, similar to the classic Levenshtein distance.
|
|
66
|
+
* We use a memoized-recursive implementation, since the state space tends to
|
|
67
|
+
* be relatively sparse in most practical use cases.
|
|
68
|
+
*/
|
|
69
|
+
float recursive_match(const MatchInfo &m,
|
|
70
|
+
const size_t haystack_idx,
|
|
71
|
+
const size_t needle_idx,
|
|
72
|
+
const float cur_score) {
|
|
73
|
+
if (needle_idx == m.needle_len) {
|
|
74
|
+
return 1;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
float &memoized = m.memo[needle_idx * m.haystack_len + haystack_idx];
|
|
78
|
+
if (memoized >= 0) {
|
|
79
|
+
return memoized;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
float score = 0;
|
|
83
|
+
size_t best_match = 0;
|
|
84
|
+
char c = m.needle_case[needle_idx];
|
|
85
|
+
|
|
86
|
+
size_t lim = m.last_match[needle_idx];
|
|
87
|
+
if (needle_idx > 0 && m.max_gap && haystack_idx + m.max_gap < lim) {
|
|
88
|
+
lim = haystack_idx + m.max_gap;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// This is only used when needle_idx == haystack_idx == 0.
|
|
92
|
+
// It won't be accurate for any other run.
|
|
93
|
+
size_t last_slash = 0;
|
|
94
|
+
for (size_t j = haystack_idx; j <= lim; j++) {
|
|
95
|
+
char d = m.haystack_case[j];
|
|
96
|
+
bool is_path_sep = d == '/' || d == '\\';
|
|
97
|
+
|
|
98
|
+
if (needle_idx == 0 && is_path_sep) {
|
|
99
|
+
last_slash = j;
|
|
100
|
+
}
|
|
101
|
+
if (c == d || (is_path_sep && (c == '_' || c == '\\'))) {
|
|
102
|
+
// calculate score
|
|
103
|
+
float char_score = 1.0;
|
|
104
|
+
if (j > haystack_idx) {
|
|
105
|
+
char last = m.haystack[j - 1];
|
|
106
|
+
char curr = m.haystack[j]; // case matters, so get again
|
|
107
|
+
if (last == '/') {
|
|
108
|
+
char_score = 0.9;
|
|
109
|
+
} else if (last == '-' || last == '_' || last == ' ' ||
|
|
110
|
+
(last >= '0' && last <= '9')) {
|
|
111
|
+
char_score = 0.8;
|
|
112
|
+
} else if (last >= 'a' && last <= 'z' && curr >= 'A' && curr <= 'Z') {
|
|
113
|
+
char_score = 0.8;
|
|
114
|
+
} else if (last == '.') {
|
|
115
|
+
char_score = 0.7;
|
|
116
|
+
} else if (needle_idx == 0) {
|
|
117
|
+
char_score = BASE_DISTANCE_PENALTY;
|
|
118
|
+
} else {
|
|
119
|
+
char_score = max(
|
|
120
|
+
MIN_DISTANCE_PENALTY,
|
|
121
|
+
BASE_DISTANCE_PENALTY -
|
|
122
|
+
(j - haystack_idx - 1) * ADDITIONAL_DISTANCE_PENALTY
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Apply a severe penalty if the case doesn't match.
|
|
128
|
+
// This will make the exact matches have higher score than the case
|
|
129
|
+
// insensitive and the path insensitive matches.
|
|
130
|
+
if (
|
|
131
|
+
(m.smart_case || m.haystack[j] == '/') &&
|
|
132
|
+
m.needle[needle_idx] != m.haystack[j]
|
|
133
|
+
) {
|
|
134
|
+
char_score *= 0.001;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
float multiplier = char_score;
|
|
138
|
+
// Scale the score based on how much of the path was actually used.
|
|
139
|
+
// (We measure this via # of characters since the last slash.)
|
|
140
|
+
if (needle_idx == 0) {
|
|
141
|
+
multiplier /= float(m.haystack_len - last_slash);
|
|
142
|
+
}
|
|
143
|
+
float next_score = 1.0;
|
|
144
|
+
if (m.min_score > 0) {
|
|
145
|
+
next_score = cur_score * multiplier;
|
|
146
|
+
// Scores only decrease. If we can't pass the previous best, bail
|
|
147
|
+
if (next_score < m.min_score) {
|
|
148
|
+
// Ensure that score is non-zero:
|
|
149
|
+
// MatcherBase shouldn't exclude this from future searches.
|
|
150
|
+
if (score == 0) {
|
|
151
|
+
score = 1e-18;
|
|
152
|
+
}
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
float new_score =
|
|
157
|
+
multiplier * recursive_match(m, j + 1, needle_idx + 1, next_score);
|
|
158
|
+
if (new_score > score) {
|
|
159
|
+
score = new_score;
|
|
160
|
+
best_match = j;
|
|
161
|
+
// Optimization: can't score better than 1.
|
|
162
|
+
if (new_score == 1) {
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (m.best_match != nullptr) {
|
|
170
|
+
m.best_match[needle_idx * m.haystack_len + haystack_idx] = best_match;
|
|
171
|
+
}
|
|
172
|
+
return memoized = score;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
float score_match(const char *haystack,
|
|
176
|
+
const char *haystack_lower,
|
|
177
|
+
const char *needle,
|
|
178
|
+
const char *needle_lower,
|
|
179
|
+
const MatchOptions &options,
|
|
180
|
+
const float min_score,
|
|
181
|
+
vector<int> *match_indexes) {
|
|
182
|
+
if (!*needle) {
|
|
183
|
+
return 1.0;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
MatchInfo m;
|
|
187
|
+
m.haystack_len = strlen(haystack);
|
|
188
|
+
m.needle_len = strlen(needle);
|
|
189
|
+
m.haystack_case = options.case_sensitive ? haystack : haystack_lower;
|
|
190
|
+
m.needle_case = options.case_sensitive ? needle : needle_lower;
|
|
191
|
+
m.smart_case = options.smart_case;
|
|
192
|
+
m.max_gap = options.max_gap;
|
|
193
|
+
m.min_score = min_score;
|
|
194
|
+
|
|
195
|
+
#ifdef _WIN32
|
|
196
|
+
int *last_match = (int*)_alloca(m.needle_len * sizeof(int));
|
|
197
|
+
#else
|
|
198
|
+
int last_match[m.needle_len];
|
|
199
|
+
#endif
|
|
200
|
+
m.last_match = last_match;
|
|
201
|
+
|
|
202
|
+
// Check if the needle exists in the haystack at all.
|
|
203
|
+
// Simultaneously, we can figure out the last possible match for each needle
|
|
204
|
+
// character (which prunes the search space by a ton)
|
|
205
|
+
int hindex = m.haystack_len;
|
|
206
|
+
for (int i = m.needle_len - 1; i >= 0; i--) {
|
|
207
|
+
char* ptr = (char*)memrchr(m.haystack_case, m.needle_case[i], hindex);
|
|
208
|
+
if (ptr == nullptr) {
|
|
209
|
+
// Since we treat _ and \\ as path separators, we need to re-check if path
|
|
210
|
+
// separator exists on the string in case they exact chars are not found.
|
|
211
|
+
if (m.needle_case[i] == '_' || m.needle_case[i] == '\\') {
|
|
212
|
+
ptr = (char*)memrchr(m.haystack_case, '/', hindex);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (ptr == nullptr) {
|
|
216
|
+
return 0;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
hindex = ptr - m.haystack_case;
|
|
220
|
+
last_match[i] = hindex;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
m.haystack = haystack;
|
|
224
|
+
m.needle = needle;
|
|
225
|
+
|
|
226
|
+
size_t memo_size = m.haystack_len * m.needle_len;
|
|
227
|
+
if (memo_size >= MAX_MEMO_SIZE) {
|
|
228
|
+
// Just return the initial match.
|
|
229
|
+
float penalty = 1.0;
|
|
230
|
+
for (size_t i = 1; i < m.needle_len; i++) {
|
|
231
|
+
int gap = last_match[i] - last_match[i - 1];
|
|
232
|
+
if (gap > 1) {
|
|
233
|
+
penalty *= max(
|
|
234
|
+
MIN_DISTANCE_PENALTY,
|
|
235
|
+
BASE_DISTANCE_PENALTY - (gap - 1) * ADDITIONAL_DISTANCE_PENALTY
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (match_indexes != nullptr) {
|
|
240
|
+
*match_indexes = vector<int>(last_match, last_match + m.needle_len);
|
|
241
|
+
}
|
|
242
|
+
return penalty * m.needle_len / m.haystack_len;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (match_indexes != nullptr) {
|
|
246
|
+
m.best_match = new size_t[memo_size];
|
|
247
|
+
} else {
|
|
248
|
+
m.best_match = nullptr;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
#ifdef _WIN32
|
|
252
|
+
float *memo = (float*)_alloca(memo_size * sizeof(float));
|
|
253
|
+
#else
|
|
254
|
+
float memo[memo_size];
|
|
255
|
+
#endif
|
|
256
|
+
// This doesn't set the values to -2, but some negative number.
|
|
257
|
+
memset(memo, -2, sizeof(float) * memo_size);
|
|
258
|
+
m.memo = memo;
|
|
259
|
+
|
|
260
|
+
// Since we scaled by the length of haystack used,
|
|
261
|
+
// scale it back up by the needle length.
|
|
262
|
+
float score = m.needle_len * recursive_match(m, 0, 0, m.needle_len);
|
|
263
|
+
if (score <= 0) {
|
|
264
|
+
return 0.0;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (match_indexes != nullptr) {
|
|
268
|
+
match_indexes->resize(m.needle_len);
|
|
269
|
+
size_t curr_start = 0;
|
|
270
|
+
for (size_t i = 0; i < m.needle_len; i++) {
|
|
271
|
+
match_indexes->at(i) = m.best_match[i * m.haystack_len + curr_start];
|
|
272
|
+
curr_start = match_indexes->at(i) + 1;
|
|
273
|
+
}
|
|
274
|
+
delete[] m.best_match;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return score;
|
|
278
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cstddef>
|
|
4
|
+
#include <string>
|
|
5
|
+
#include <vector>
|
|
6
|
+
|
|
7
|
+
struct MatchOptions {
|
|
8
|
+
bool case_sensitive;
|
|
9
|
+
bool smart_case;
|
|
10
|
+
size_t max_gap;
|
|
11
|
+
bool fuzzaldrin = false;
|
|
12
|
+
std::string root_path;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Returns a matching score between 0-1.
|
|
17
|
+
* 0 represents no match at all, while 1 is a perfect match.
|
|
18
|
+
* See implementation for scoring details.
|
|
19
|
+
*
|
|
20
|
+
* If options.case_sensitive is false, haystack_lower and
|
|
21
|
+
* needle_lower must be provided.
|
|
22
|
+
*
|
|
23
|
+
* If match_indexes is non-null, the optimal match index in haystack
|
|
24
|
+
* will be computed for each value in needle (when score is non-zero).
|
|
25
|
+
*/
|
|
26
|
+
float score_match(const char *haystack,
|
|
27
|
+
const char *haystack_lower,
|
|
28
|
+
const char *needle,
|
|
29
|
+
const char *needle_lower,
|
|
30
|
+
const MatchOptions &options,
|
|
31
|
+
const float min_score,
|
|
32
|
+
std::vector<int> *match_indexes = nullptr);
|