Skip to content

Commit

Permalink
utils: add is_ascii and char_len
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 30, 2023
1 parent 5b0d93d commit 6358a2e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/algos/fzf/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -108,3 +114,9 @@ fn find_last_unicode(needle: char, haystack: &str) -> Option<usize> {
.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()
}

0 comments on commit 6358a2e

Please sign in to comment.