Merge pull request #1811 from killerswan/char_funcs
(core::char) Add is_ascii and is_digit functions
This commit is contained in:
commit
c82a0d7c3c
1 changed files with 29 additions and 0 deletions
|
@ -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');
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue