Skip to content

Instantly share code, notes, and snippets.

@arifd
Last active June 26, 2022 15:38
Show Gist options
  • Save arifd/5c62d3812ca9c42e9340a30c318208be to your computer and use it in GitHub Desktop.
Save arifd/5c62d3812ca9c42e9340a30c318208be to your computer and use it in GitHub Desktop.
uppercase, lowercase, Unicode, chars and Rust
fn main() {
let y = "y̆";
let mut chars = y.chars();
assert_eq!(Some('y'), chars.next()); // not 'y̆'
assert_eq!(Some('\u{0306}'), chars.next());
// INTERESTING: chars is unicode compatible, it just gives you code points in between your graphemes
for c in y.chars() {
println!("{}", c.is_lowercase()); // true, false
}
assert!("y̆"
.chars()
.filter(|c| c.is_alphabetic())
.all(|c| c.is_lowercase())); // true
assert!(is_lowercase("京"));
}
/// it is better to identify the presence of uppercase
/// because scripts that don't have the concept of case
/// will return false on `char::is_lowercase`
/// (...but are not uppercase either!)
fn is_lowercase(s: &str) -> bool {
!s.chars().any(|c| c.is_uppercase())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment