From 6358a2e9aaa57bbe19ec18a64c9c30e526a4f8ab Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Mon, 30 Oct 2023 15:04:48 +0100 Subject: [PATCH] utils: add `is_ascii` and `char_len` --- src/algos/fzf/v2.rs | 10 +++++----- src/utils.rs | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/algos/fzf/v2.rs b/src/algos/fzf/v2.rs index bf5e1bc..5cb5334 100644 --- a/src/algos/fzf/v2.rs +++ b/src/algos/fzf/v2.rs @@ -76,7 +76,7 @@ impl Metric for FzfV2 { return None; } - let is_candidate_ascii = candidate.is_ascii(); + let is_candidate_ascii = utils::is_ascii(candidate); let is_case_sensitive = match self.case_sensitivity { CaseSensitivity::Sensitive => true, @@ -168,7 +168,7 @@ fn matched_indices<'idx>( let char_offset = if is_candidate_ascii { byte_offset } else { - candidate[..byte_offset].chars().count() + utils::char_len(&candidate[..byte_offset]) }; last_matched_idx += char_offset; @@ -201,7 +201,7 @@ fn matched_indices<'idx>( let char_offset = if is_candidate_ascii { byte_offset } else { - candidate[..byte_offset].chars().count() + utils::char_len(&candidate[..byte_offset]) }; last_matched_idx += char_offset; @@ -322,7 +322,7 @@ fn score_first_row( let char_idx = if is_candidate_ascii { byte_idx } else { - candidate[..byte_idx].chars().count() + utils::char_len(&candidate[..byte_idx]) }; // TODO: explain what this does. @@ -413,7 +413,7 @@ fn score_remaining_rows( let char_offset = if is_candidate_ascii { byte_offset } else { - candidate[..byte_offset].chars().count() + utils::char_len(&candidate[..byte_offset]) }; // TODO: explain what this does. diff --git a/src/utils.rs b/src/utils.rs index fe96b8d..8001bf3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -18,6 +18,12 @@ pub fn case_sensitive_eq(lhs: char, rhs: char) -> bool { lhs == rhs } +/// TODO: docs +#[inline(always)] +pub fn char_len(s: &str) -> usize { + s.chars().count() +} + /// TODO: docs #[inline(always)] pub fn find_first( @@ -108,3 +114,9 @@ fn find_last_unicode(needle: char, haystack: &str) -> Option { .char_indices() .find_map(|(offset, ch)| (needle == ch).then_some(offset)) } + +/// TODO: docs +#[inline(always)] +pub fn is_ascii(s: &str) -> bool { + s.is_ascii() +}