Artifact 97c3fbf1adf6756b1bb09e6f877980fd257b1d887a49b0aa90e5d7c773eba92e:
- File
src/tui/fuzzy.c
— part of check-in
[9dabe49948]
at
2026-11-21 19:00:33
on branch amalgam
— Amalgamation: pic as one pic.c beside the vendored sources, built by one cc line
make amalgamation writes pic-amalgamation-VERSION/ and its tarball; make test-amal builds it from the README's own line and runs crash, print and tools against that binary, and is part of make test-pic. About 60 file-local names that collided in one translation unit take their file's prefix; test/pic/amalgam.sh fails on a new collision. (user: dmestas, size: 6221) [annotate] [blame] [check-ins using] [more...]
/* Port of Pi's fuzzy.ts; see fuzzy.h. * * JS approximations: strings are bytes, so indexes are byte offsets (Pi's are * UTF-16 units; they agree for ASCII) and toLowerCase is ASCII-only. A byte * of a multi-byte character never equals an ASCII word separator, so the * boundary rule is unaffected. */ #include "tui/fuzzy.h" #include <stdlib.h> #include <string.h> static char lower(char c) { return c >= 'A' && c <= 'Z' ? (char)(c + 32) : c; } static char *lowered(const char *s, size_t n) { char *r = malloc(n + 1); if (!r) return NULL; for (size_t i = 0; i < n; i++) r[i] = lower(s[i]); r[n] = 0; return r; } static int is_boundary(char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v' || c == '-' || c == '_' || c == '.' || c == '/' || c == ':'; } /* scorePositions */ static double score_positions(const char *text, const size_t *pos, size_t n) { double score = 0; long last = -1; long consecutive = 0; for (size_t k = 0; k < n; k++) { long i = (long)pos[k]; if (last == i - 1) { consecutive++; score -= (double)consecutive * 5; } else { consecutive = 0; if (last >= 0) score += (double)(i - last - 1) * 2; } if (i == 0 || is_boundary(text[i - 1])) score -= 10; score += (double)i * 0.1; last = i; } return score; } /* matchQuery on already-lowered strings; 1 and *score on a match. */ static int match_query(const char *q, size_t qn, const char *text, size_t tn, double *score) { *score = 0; if (qn == 0) return 1; if (qn > tn) return 0; size_t *pos = malloc(qn * sizeof *pos); if (!pos) return 0; /* Greedy alignment: each character at its first occurrence after the last. */ size_t from = 0; for (size_t k = 0; k < qn; k++) { const char *hit = from <= tn ? memchr(text + from, q[k], tn - from) : NULL; if (!hit) { free(pos); return 0; } pos[k] = (size_t)(hit - text); from = pos[k] + 1; } double best = score_positions(text, pos, qn); /* Also try every contiguous occurrence of the whole query. */ for (const char *hit = strstr(text, q); hit; hit = strstr(hit + 1, q)) { size_t start = (size_t)(hit - text); for (size_t k = 0; k < qn; k++) pos[k] = start + k; double s = score_positions(text, pos, qn); if (s < best) best = s; } free(pos); if (qn == tn && memcmp(q, text, qn) == 0) best -= 100; *score = best; return 1; } static int is_digit(char c) { return c >= '0' && c <= '9'; } static int is_alpha(char c) { return c >= 'a' && c <= 'z'; } /* "abc123" -> "123abc" and "123abc" -> "abc123"; NULL for anything else. */ static char *swapped(const char *q, size_t n) { size_t i = 0; int (*first)(char) = is_alpha, (*second)(char) = is_digit; if (n > 0 && is_digit(q[0])) first = is_digit, second = is_alpha; while (i < n && first(q[i])) i++; size_t split = i; while (i < n && second(q[i])) i++; if (split == 0 || split == n || i != n) return NULL; char *r = malloc(n + 1); if (!r) return NULL; memcpy(r, q + split, n - split); memcpy(r + n - split, q, split); r[n] = 0; return r; } int tui_fuzzy_match(const char *query, const char *text, double *score) { size_t qn = strlen(query), tn = strlen(text); char *q = lowered(query, qn), *t = lowered(text, tn); int ok = 0; double s = 0; if (q && t) { ok = match_query(q, qn, t, tn, &s); if (!ok) { char *sw = swapped(q, qn); double ss; if (sw && match_query(sw, qn, t, tn, &ss)) { ok = 1; s = ss + 5; } free(sw); } } free(q); free(t); *score = ok ? s : 0; return ok; } typedef struct { size_t idx; double score; } ranked; /* Ascending score; equal scores keep input order (JS sort is stable). */ static int by_score(const void *a, const void *b) { const ranked *x = a, *y = b; if (x->score < y->score) return -1; if (x->score > y->score) return 1; return x->idx < y->idx ? -1 : x->idx > y->idx; } static int fz_is_ws(char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v'; } size_t tui_fuzzy_filter(const char *query, const char *const *texts, size_t n, size_t *order) { size_t qn = strlen(query); char *q = malloc(qn + 1); ranked *res = n ? malloc(n * sizeof *res) : NULL; if (!q || (n && !res)) { free(q); free(res); return 0; } /* Tokens split on whitespace and '/'; empty ones are skipped. */ size_t nres = 0; int any = 0; for (size_t i = 0; i < qn; i++) if (!fz_is_ws(query[i]) && query[i] != '/') any = 1; int blank = 1; for (size_t i = 0; i < qn; i++) if (!fz_is_ws(query[i])) blank = 0; if (blank || !any) { for (size_t i = 0; i < n; i++) order[i] = i; free(q); free(res); return n; } for (size_t i = 0; i < n; i++) { double total = 0; int all = 1; size_t p = 0; while (all && p < qn) { while (p < qn && (fz_is_ws(query[p]) || query[p] == '/')) p++; size_t e = p; while (e < qn && !fz_is_ws(query[e]) && query[e] != '/') e++; if (e > p) { memcpy(q, query + p, e - p); q[e - p] = 0; double s; if (tui_fuzzy_match(q, texts[i], &s)) total += s; else all = 0; } p = e; } if (all) res[nres].idx = i, res[nres++].score = total; } qsort(res, nres, sizeof *res, by_score); for (size_t i = 0; i < nres; i++) order[i] = res[i].idx; free(q); free(res); return nres; }