Created
February 8, 2015 17:38
-
-
Save pzol/86d6de03ffc86aef34ea 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
use std::str; | |
struct UpperChars<'a> { | |
chars: str::Chars<'a> | |
} | |
struct LowerChars<'a> { | |
chars: str::Chars<'a> | |
} | |
fn upper_chars<'a>(s: &'a str) -> UpperChars<'a> { | |
UpperChars { chars: s.chars() } | |
} | |
fn lower_chars<'a>(s: &'a str) -> LowerChars<'a> { | |
LowerChars { chars: s.chars() } | |
} | |
impl<'a> Iterator for LowerChars<'a> { | |
type Item = char; | |
fn next(&mut self) -> Option<char> { | |
self.chars.next().map(|c| c.to_lowercase()) | |
} | |
} | |
impl<'a> Iterator for UpperChars<'a> { | |
type Item = char; | |
fn next(&mut self) -> Option<char> { | |
self.chars.next().map(|c| c.to_uppercase()) | |
} | |
} | |
pub trait CaseFolds { | |
fn to_uppercase(&self) -> String; | |
fn to_lowercase(&self) -> String; | |
} | |
impl<'a> CaseFolds for &'a String { | |
fn to_lowercase(&self) -> String { | |
lower_chars((*self).as_slice()).collect::<String>() | |
} | |
fn to_uppercase(&self) -> String { | |
upper_chars((*self).as_slice()).collect::<String>() | |
} | |
} | |
impl<'a> CaseFolds for &'a str { | |
fn to_lowercase(&self) -> String { | |
lower_chars((*self).as_slice()).collect::<String>() | |
} | |
fn to_uppercase(&self) -> String { | |
upper_chars((*self).as_slice()).collect::<String>() | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::CaseFolds; | |
#[test] | |
fn string_to_lowercase() { | |
let gl = String::from_str("στιγμασ"); | |
assert_eq!("ΣΤΙΓΜΑΣ".to_lowercase(), gl); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment