diff --git a/src/root.zig b/src/root.zig index 921a85a..3aacebd 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,5 +1,6 @@ //! `Fuzzig` the fuzzy search library. Matching algorithm implementation is based on -//! [ms-edit](https://github.com/microsoft/edit/blob/main/src/fuzzy.rs) MIT-Licensed. +//! [ms-edit](https://github.com/microsoft/edit/blob/main/crates/edit/src/fuzzy.rs) +//! MIT-Licensed. /// Result of the calculated score. The `Result` holds memory that needs to be /// freed once the `Result` is no longer required. @@ -120,7 +121,7 @@ pub fn match(gpa: Allocator, target: []const u8, query: []const u8, index: usize matches_sequence_len, ); - if (score != 0 and diag_score + score >= left_score) { + if (score != 0 and diag_score + score >= left_score and (idx > 0 or std.mem.startsWith(u8, target_lower[target_index..], query_lower))) { matches[current_idx] = matches_sequence_len + 1; scores[current_idx] = diag_score + score; } else { @@ -156,7 +157,8 @@ pub fn match(gpa: Allocator, target: []const u8, query: []const u8, index: usize /// Compute the score for a given character, while taking into account the previous character, the already matching length of a (sub-)sequence. fn compute_char_score(query: u8, query_lower: u8, target_prev: ?u8, target_curr: u8, target_curr_lower: u8, matches_sequence_len: usize) usize { var score: usize = 0; - if (!(query_lower == target_curr_lower or query_lower == '/' or query_lower == '\\' and target_curr_lower == '/' or target_curr_lower == '\\')) return score; + // Special case path separators: ignore platform differences + if (!(query_lower == target_curr_lower or (query_lower == '/' and target_curr_lower == '\\') or (query_lower == '/' and target_curr_lower == '\\'))) return score; score += 1; @@ -165,8 +167,10 @@ fn compute_char_score(query: u8, query_lower: u8, target_prev: ?u8, target_curr: if (query == target_curr) score += 1; if (target_prev) |prev| { - score += score_separator_at_pos(prev); - if (target_curr != target_curr_lower and matches_sequence_len == 0) score += 2; + const separator_bonus = score_separator_at_pos(prev); + if (separator_bonus > 0) + score += separator_bonus + else if (target_curr != target_curr_lower and matches_sequence_len == 0) score += 2; } else { score += 8; }