Skip to content

(core::char) Add is_ascii and is_digit functions #1811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 12, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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. \
Expand Down Expand Up @@ -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');
}