Skip to content

Instantly share code, notes, and snippets.

@pzol
Created January 26, 2014 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pzol/8637904 to your computer and use it in GitHub Desktop.
Save pzol/8637904 to your computer and use it in GitHub Desktop.
compare two strings ignoring case
fn compare_ci(x: &str, y: &str) -> bool {
if x.char_len() != y.char_len() {
return false;
}
let mut it = x.chars().zip(y.chars());
it.all(|(x,y)|
unsafe {
x.to_ascii_nocheck().to_lower() == y.to_ascii_nocheck().to_lower()
}
)
}
#[test]
fn test_compare_case_insensitive(){
let x = ~"Bilbo Bąggins";
let y = ~"bILbo bąGgins";
assert_eq!(compare_ci(x, y), true);
}
#[test]
fn test_compare_case_diff_length(){
let x = ~"Bilbo Bąggin";
let y = ~"bILbo bąGgins";
assert_eq!(compare_ci(x, y), false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment