Created
January 26, 2014 19:24
-
-
Save pzol/8637904 to your computer and use it in GitHub Desktop.
compare two strings ignoring case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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