From 5c58dde2f84edac1c24d48c050e23278206ac7c1 Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Wed, 8 Feb 2012 01:52:09 -0800 Subject: [PATCH] core: added char::is_digit (matching Nd, Nl, No) --- src/libcore/char.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 2544550bd57..87a9b24ce6d 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -85,10 +85,18 @@ 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. \ Safety note: This function fails if `c` is not a valid char", @@ -227,8 +235,18 @@ fn test_to_upper() { } #[test] -fn test_ascii() unsafe { +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'); +} +