Skip to content

Instantly share code, notes, and snippets.

@pzol
Last active August 29, 2015 14:10
Show Gist options
  • Save pzol/c783a69b6473a97340d6 to your computer and use it in GitHub Desktop.
Save pzol/c783a69b6473a97340d6 to your computer and use it in GitHub Desktop.
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<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())
}
}
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