Skip to content

Commit 5c58dde

Browse files
committed
core: added char::is_digit (matching Nd, Nl, No)
1 parent b3444db commit 5c58dde

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/libcore/char.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,18 @@ pure fn is_alphanumeric(c: char) -> bool {
8585
unicode::general_category::No(c);
8686
}
8787

88+
#[doc( brief = "Indicates whether the character is an ASCII character" )]
8889
pure fn is_ascii(c: char) -> bool {
8990
c - ('\x7F' & c) == '\x00'
9091
}
9192

93+
#[doc( brief = "Indicates whether the character is numeric (Nd, Nl, or No)" )]
94+
pure fn is_digit(c: char) -> bool {
95+
ret unicode::general_category::Nd(c) ||
96+
unicode::general_category::Nl(c) ||
97+
unicode::general_category::No(c);
98+
}
99+
92100
#[doc(
93101
brief = "Convert a char to the corresponding digit. \
94102
Safety note: This function fails if `c` is not a valid char",
@@ -227,8 +235,18 @@ fn test_to_upper() {
227235
}
228236

229237
#[test]
230-
fn test_ascii() unsafe {
238+
fn test_is_ascii() unsafe {
231239
assert str::all("banana", char::is_ascii);
232240
assert ! str::all("ประเทศไทย中华Việt Nam", char::is_ascii);
233241
}
234242

243+
#[test]
244+
fn test_is_digit() {
245+
assert is_digit('2');
246+
assert is_digit('7');
247+
assert ! is_digit('c');
248+
assert ! is_digit('i');
249+
assert ! is_digit('z');
250+
assert ! is_digit('Q');
251+
}
252+

0 commit comments

Comments
 (0)