Performs byte-wise subsequence matching with ASCII-only case folding. UTF-8 multi-byte sequences are preserved but matched as raw bytes. Only ASCII A-Z characters are case-folded to a-z.
264{
266 return -1;
267
268 size_t plen = strlen(pattern);
269 if (plen == 0)
270 return -1;
271
275
276 if (plen > (size_t) max_pattern)
277 return -1;
278
280
282
283 int pi = 0;
284 int ci = 0;
285 int score = 0;
286
287 int first = -1;
288 int last = -1;
289
290
292 {
293 const unsigned char pbyte = (unsigned char) pattern[pi];
294 if (pbyte < 0x80)
295 {
296 int pc =
lower_if(pattern[pi], fold);
298
299 if (pc == cc)
300 {
301 matchpos[pi] = ci;
302
303 if (first < 0)
304 first = ci;
305
306 last = ci;
307 pi++;
308 }
309
310 ci++;
311 continue;
312 }
313
314
316 const int premaining = (int) plen - pi;
317 if ((pchar_len <= 0) || (pchar_len > premaining))
318 return -1;
319 bool matched = false;
320
322 {
323 const unsigned char cbyte = (
unsigned char)
candidate[ci];
325 {
326 ci++;
327 continue;
328 }
329
331 if ((pchar_len == cchar_len) && (memcmp(pattern + pi,
candidate + ci, pchar_len) == 0))
332 {
333 for (int k = 0; k < pchar_len; k++)
334 matchpos[pi + k] = ci + k;
335
336 if (first < 0)
337 first = ci;
338
339 last = ci + pchar_len - 1;
340 pi += pchar_len;
341 ci += cchar_len;
342 matched = true;
343 break;
344 }
345
346 ci += cchar_len;
347 }
348
349 if (!matched)
350 break;
351 }
352
353 if (pi != (int) plen)
354 return -1;
355
356
357
358
359 score += plen * 10;
360
361
362 for (int i = 1; i < pi; i++)
363 {
364 int gap = matchpos[i] - matchpos[i - 1] - 1;
365
366 if (gap == 0)
367 score += 15;
368 else
369 score -= gap * 2;
370 }
371
372
373 int span = last - first + 1;
374 score -= span;
375
376
378 score += 40;
379
380
381 for (int i = 0; i < pi; i++)
382 {
383 int pos = matchpos[i];
384
385 if (pos == 0)
386 score += 30;
387 else
388 {
389 unsigned char prev = (
unsigned char)
candidate[pos - 1];
390 unsigned char curr = (
unsigned char)
candidate[pos];
391
392
393 if ((prev == '/') || (prev == '.') || (prev == '-') || (prev == '_'))
394 score += 15;
395
396 else if (((prev >= 'a') && (prev <= 'z')) && ((curr >= 'A') && (curr <= 'Z')))
397 score += 10;
398 }
399 }
400
401
403
404
405 if (score < 0)
406 score = 0;
407
408 if (out)
409 {
414 }
415
416 return score;
417}
bool candidate(struct CompletionData *cd, char *user, const char *src, char *dest, size_t dlen)
Helper function for completion.
int max_pattern
Safety bound (<=0 = default 256, capped at 256)
bool prefer_prefix
Extra weight for prefix matches.
int score
Score (<0 = no match)
int start
First match position.
int end
Last match position.
static int utf8_char_len(const char *s)
Get length of a UTF-8 codepoint at a byte offset.
static bool utf8_is_continuation(unsigned char c)
Check for UTF-8 continuation byte.
static int lower_if(int c, bool fold)
Convert character to lowercase conditionally.
static bool compute_case_mode(const char *pattern, const struct FuzzyOptions *opts)
Determine if case folding should be used.
#define DEFAULT_MAX_PATTERN
Default maximum pattern length.