Created
February 26, 2014 18:28
-
-
Save pzol/9235382 to your computer and use it in GitHub Desktop.
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
| struct UpperChars<'a> { | |
| chars: std::str::Chars<'a> | |
| } | |
| struct LowerChars<'a> { | |
| chars: std::str::Chars<'a> | |
| } | |
| fn lower_chars<'a>(s: &'a str) -> LowerChars<'a> { | |
| LowerChars { chars: s.chars() } | |
| } | |
| fn upper_chars<'a>(s: &'a str) -> UpperChars<'a> { | |
| UpperChars { chars: s.chars() } | |
| } | |
| impl<'a> Iterator<char> for LowerChars<'a> { | |
| fn next(&mut self) -> Option<char> { | |
| self.chars.next().map(|c| c.to_lowercase()) | |
| } | |
| } | |
| impl<'a> Iterator<char> for UpperChars<'a> { | |
| fn next(&mut self) -> Option<char> { | |
| self.chars.next().map(|c| c.to_uppercase()) | |
| } | |
| } | |
| fn eq_ignore_case(s: &str, needle: &str, ignore_case: bool) -> bool { | |
| if ignore_case == false { | |
| return s == needle; | |
| } else { | |
| let mut zip = upper_chars(s).zip(upper_chars(needle)); | |
| zip.all(|(left, right)| left == right) | |
| } | |
| } | |
| #[test] | |
| fn test_eq_ignore_case(){ | |
| let sl = "foobär"; | |
| let su = "FOOBÄR"; | |
| assert!(eq_ignore_case(sl, su, true)); | |
| assert!(!eq_ignore_case(sl, su, false)); | |
| } | |
| #[test] | |
| fn test_upper_chars(){ | |
| let gl = "στιγμας"; | |
| let gu = "ΣΤΙΓΜΑΣ"; | |
| let actual = upper_chars(gl).collect::<~str>(); | |
| assert_eq!(actual, gu.to_owned()); | |
| } | |
| #[test] | |
| fn test_lower_chars(){ | |
| let gl = "στιγμασ"; | |
| let gu = "ΣΤΙΓΜΑΣ"; | |
| let actual = lower_chars(gu).collect::<~str>(); | |
| assert_eq!(actual, gl.to_owned()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment