Skip to content

Commit c82a0d7

Browse files
committed
Merge pull request #1811 from killerswan/char_funcs
(core::char) Add is_ascii and is_digit functions
2 parents 4339307 + 0121cd5 commit c82a0d7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/libcore/char.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export is_alphabetic,
3737
is_XID_start, is_XID_continue,
3838
is_lowercase, is_uppercase,
3939
is_whitespace, is_alphanumeric,
40+
is_ascii, is_digit,
4041
to_digit, to_lower, to_upper, maybe_digit, cmp;
4142

4243
import is_alphabetic = unicode::derived_property::Alphabetic;
@@ -84,6 +85,17 @@ pure fn is_alphanumeric(c: char) -> bool {
8485
unicode::general_category::No(c);
8586
}
8687

88+
#[doc( brief = "Indicates whether the character is an ASCII character" )]
89+
pure fn is_ascii(c: char) -> bool {
90+
c - ('\x7F' & c) == '\x00'
91+
}
92+
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+
}
8799

88100
#[doc(
89101
brief = "Convert a char to the corresponding digit. \
@@ -221,3 +233,20 @@ fn test_to_upper() {
221233
//assert (to_upper('ü') == 'Ü');
222234
assert (to_upper('ß') == 'ß');
223235
}
236+
237+
#[test]
238+
fn test_is_ascii() unsafe {
239+
assert str::all("banana", char::is_ascii);
240+
assert ! str::all("ประเทศไทย中华Việt Nam", char::is_ascii);
241+
}
242+
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)