diff --git a/src/text_editor/buffer.h b/src/text_editor/buffer.h index 6cfedbb..f413b85 100644 --- a/src/text_editor/buffer.h +++ b/src/text_editor/buffer.h @@ -49,7 +49,7 @@ struct Buffer { struct FuzzyPair { int32_t index; - int32_t rating; + float rating; }; enum { diff --git a/src/text_editor/window_command.cpp b/src/text_editor/window_command.cpp index a17e66a..eb24fe1 100644 --- a/src/text_editor/window_command.cpp +++ b/src/text_editor/window_command.cpp @@ -95,26 +95,48 @@ void CommandWindowLayout(Rect2I *rect, Int wx, Int wy) { n->document_rect = n->total_rect = CutBottom(rect, barsize); } -int32_t FuzzyRate(String16 string, String16 with) { - ProfileFunction(); - if (with.len == 0) return 0; - int32_t points = 0; - int32_t consecutive = 0; - int32_t with_i = 0; - for (int32_t i = 0; i < string.len; i++) { - if (ToLowerCase(string.data[i]) == ToLowerCase(with[with_i])) { - consecutive += 1; - with_i += 1; - } else { - with_i = 0; - points += consecutive * consecutive; - consecutive = 0; +float NewFuzzyRate(String16 s, String16 p) { + float score = 0; + // try to do this: https://github.com/junegunn/fzf/blob/master/src/algo/algo.go + return score; +} + +float FuzzyRate(String16 s, String16 p) { + float score = 0; + for (Int outer_pi = 0; outer_pi < p.len; outer_pi += 1) { + String16 pit = Skip(p, outer_pi); + if (IsWhitespace(At(pit, 0))) { + continue; } - if (with_i >= with.len) with_i = 0; + float matching = 0; + for (Int outer_si = 0; outer_si < s.len; outer_si += 1) { + String16 sit = Skip(s, outer_si); + if (IsWhitespace(At(sit, 0))) { + continue; + } + + Int si = 0; + Int pi = 0; + for (;si < sit.len && pi < pit.len;) { + while (si < sit.len && IsWhitespace(sit[si])) si += 1; + while (pi < pit.len && IsWhitespace(pit[pi])) pi += 1; + if (pi >= pit.len) break; + if (si >= sit.len) break; + + if (ToLowerCase(sit[si]) == ToLowerCase(pit[pi])) { + matching += 1.0f; + } else { + break; + } + si += 1; + pi += 1; + } + } + score += matching; } - points += consecutive * consecutive; - return points; + score = score / (float)s.len; + return score; } inline bool MergeSortCompare(FuzzyPair *a, FuzzyPair *b) { @@ -139,7 +161,7 @@ Array FuzzySearchLines(Allocator allocator, Buffer *buffer, Int line_ } s = Trim(s); - int32_t rating = FuzzyRate(s, needle); + float rating = FuzzyRate(s, needle); Add(&ratings, {(int32_t)i, rating}); } Array temp = Copy(allocator, ratings);