Skip to content

Instantly share code, notes, and snippets.

@juliarose
Last active April 3, 2024 20:16
Show Gist options
  • Save juliarose/875a43724c966a101a7063d11790fa96 to your computer and use it in GitHub Desktop.
Save juliarose/875a43724c966a101a7063d11790fa96 to your computer and use it in GitHub Desktop.
// A possibly more efficient way to match pluralized strings case-insensitively.
// For example matching "burgers" and "burger" with fewer iterations.
// Untested
fn eq_ignore_ascii_case_with_tail(mut lhs: &str, rhs: &str, tail: char) -> bool {
let mut lhs_len = lhs.len();
let rhs_len = rhs.len();
if lhs_len == rhs_len + 1 && lhs.ends_with(tail) {
lhs = &lhs[..lhs_len - 1];
lhs_len -= 1;
}
if lhs_len != rhs_len {
return false;
}
std::iter::zip(lhs.as_bytes(), rhs.as_bytes()).all(|(a, b)| a.eq_ignore_ascii_case(b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment