Merge pull request #1811 from killerswan/char_funcs

(core::char) Add is_ascii and is_digit functions
This commit is contained in:
Brian Anderson 2012-02-11 17:14:49 -08:00
commit c82a0d7c3c

View file

@ -37,6 +37,7 @@ export is_alphabetic,
is_XID_start, is_XID_continue,
is_lowercase, is_uppercase,
is_whitespace, is_alphanumeric,
is_ascii, is_digit,
to_digit, to_lower, to_upper, maybe_digit, cmp;
import is_alphabetic = unicode::derived_property::Alphabetic;
@ -84,6 +85,17 @@ pure fn is_alphanumeric(c: char) -> bool {
unicode::general_category::No(c);
}
#[doc( brief = "Indicates whether the character is an ASCII character" )]
pure fn is_ascii(c: char) -> bool {
c - ('\x7F' & c) == '\x00'
}
#[doc( brief = "Indicates whether the character is numeric (Nd, Nl, or No)" )]
pure fn is_digit(c: char) -> bool {
ret unicode::general_category::Nd(c) ||
unicode::general_category::Nl(c) ||
unicode::general_category::No(c);
}
#[doc(
brief = "Convert a char to the corresponding digit. \
@ -221,3 +233,20 @@ fn test_to_upper() {
//assert (to_upper('ü') == 'Ü');
assert (to_upper('ß') == 'ß');
}
#[test]
fn test_is_ascii() unsafe {
assert str::all("banana", char::is_ascii);
assert ! str::all("ประเทศไทย中华Việt Nam", char::is_ascii);
}
#[test]
fn test_is_digit() {
assert is_digit('2');
assert is_digit('7');
assert ! is_digit('c');
assert ! is_digit('i');
assert ! is_digit('z');
assert ! is_digit('Q');
}