Skip to content

Instantly share code, notes, and snippets.

@pzol
Created February 24, 2014 11:19
Show Gist options
  • Save pzol/9186628 to your computer and use it in GitHub Desktop.
Save pzol/9186628 to your computer and use it in GitHub Desktop.
/* soon to be auto-generated */
use std::cmp::{Equal, Less, Greater};
use std::vec::ImmutableVector;
use std::option::None;
static LETTER_UPPERCASE : &'static [(char, char)] = &[
('\x41', '\x5a')
];
static LETTER_LOWERCASE : &'static [(char, char)] = &[
('\x61', '\x7a')
];
// table for common and simple case folding
// from data/6.3.0/CaseFolding.txt
static CASE_FOLDING_SIMPLE : &'static [(char, char)] = &[
('\U00000041', '\U00000061'), // 0041; C; 0061; # LATIN CAPITAL LETTER A
('\U00000042', '\U00000062'), // 0042; C; 0062; # LATIN CAPITAL LETTER B
('\U0001D6E2', '\U00000391') // '\U0001D6E2'//;MATHEMATICAL ITALIC CAPITAL ALPHA;Lu;0;L;<font> 0391;;;;N;;;;;'
];
fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
r.bsearch(|&(lo,hi)| {
if lo <= c && c <= hi { Equal }
else if hi < c { Less }
else { Greater }
}) != None
}
fn bsearch_lo(c: char, r: &'static [(char,char)]) -> Option<uint> {
r.bsearch(|&(x, _)| {
if x == c { Equal }
else if x < c { Less }
else { Greater }
})
}
fn bsearch_hi(c: char, r: &'static [(char,char)]) -> Option<uint> {
r.bsearch(|&(_, x)| {
if x == c { Equal }
else if x < c { Less }
else { Greater }
})
}
fn to_lower(c: char) -> char {
match bsearch_lo(c, CASE_FOLDING_SIMPLE) {
None => c,
Some(index) => CASE_FOLDING_SIMPLE[index].val1()
}
}
fn to_upper(c: char) -> char {
match bsearch_hi(c, CASE_FOLDING_SIMPLE) {
None => c,
Some(index) => CASE_FOLDING_SIMPLE[index].val0()
}
}
fn is_upper(c: char) -> bool {
bsearch_range_table(c, LETTER_UPPERCASE)
}
fn is_lower(c: char) -> bool {
bsearch_range_table(c, LETTER_LOWERCASE)
}
#[test]
fn test_is_upper(){
assert!(is_upper('\U00000041'));
assert!(is_upper('A'));
assert!(!is_upper('a'));
}
#[test]
fn test_is_lower(){
assert!(is_lower('\U00000061'));
assert!(is_lower('a'));
assert!(!is_lower('A'));
}
#[test]
fn test_to_lower(){
assert_eq!(to_lower('A'), 'a');
assert_eq!(to_lower('💩'), '💩');
}
#[test]
fn test_to_upper(){
assert_eq!(to_upper('a'), 'A');
assert_eq!(to_lower('💩'), '💩');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment