@@ -37,6 +37,7 @@ export is_alphabetic,
37
37
is_XID_start, is_XID_continue,
38
38
is_lowercase, is_uppercase,
39
39
is_whitespace, is_alphanumeric,
40
+ is_ascii, is_digit,
40
41
to_digit, to_lower, to_upper, maybe_digit, cmp;
41
42
42
43
import is_alphabetic = unicode:: derived_property:: Alphabetic ;
@@ -84,6 +85,17 @@ pure fn is_alphanumeric(c: char) -> bool {
84
85
unicode:: general_category:: No ( c) ;
85
86
}
86
87
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
+ }
87
99
88
100
#[ doc(
89
101
brief = "Convert a char to the corresponding digit. \
@@ -221,3 +233,20 @@ fn test_to_upper() {
221
233
//assert (to_upper('ü') == 'Ü');
222
234
assert ( to_upper ( 'ß' ) == 'ß' ) ;
223
235
}
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