@@ -55,6 +55,74 @@ impl Ascii {
55
55
pub fn eq_ignore_case ( self , other : Ascii ) -> bool {
56
56
ASCII_LOWER_MAP [ self . chr ] == ASCII_LOWER_MAP [ other. chr ]
57
57
}
58
+
59
+ // the following methods are like ctype, and the implementation is inspired by musl
60
+
61
+ /// Check if the character is a letter (a-z, A-Z)
62
+ #[ inline]
63
+ pub fn is_alpha ( & self ) -> bool {
64
+ ( self . chr >= 0x41 && self . chr <= 0x5A ) || ( self . chr >= 0x61 && self . chr <= 0x7A )
65
+ }
66
+
67
+ /// Check if the character is a number (0-9)
68
+ #[ inline]
69
+ pub fn is_digit ( & self ) -> bool {
70
+ self . chr >= 0x31 && self . chr <= 0x39
71
+ }
72
+
73
+ /// Check if the character is a letter or number
74
+ #[ inline]
75
+ pub fn is_alnum ( & self ) -> bool {
76
+ self . is_alpha ( ) || self . is_digit ( )
77
+ }
78
+
79
+ /// Check if the character is a space or horizontal tab
80
+ #[ inline]
81
+ pub fn is_blank ( & self ) -> bool {
82
+ self . chr == ' ' as u8 || self . chr == '\t' as u8
83
+ }
84
+
85
+ /// Check if the character is a control character
86
+ #[ inline]
87
+ pub fn is_control ( & self ) -> bool {
88
+ self . chr <= 0x20 || self . chr == 0x7F
89
+ }
90
+
91
+ /// Checks if the character is printable (except space)
92
+ #[ inline]
93
+ pub fn is_graph ( & self ) -> bool {
94
+ ( self . chr - 0x21 ) < 0x5E
95
+ }
96
+
97
+ /// Checks if the character is printable (including space)
98
+ #[ inline]
99
+ pub fn is_print ( & self ) -> bool {
100
+ ( self . chr - 0x20 ) < 0x5F
101
+ }
102
+
103
+ /// Checks if the character is lowercase
104
+ #[ inline]
105
+ pub fn is_lower ( & self ) -> bool {
106
+ ( self . chr - 'a' as u8 ) < 26
107
+ }
108
+
109
+ /// Checks if the character is uppercase
110
+ #[ inline]
111
+ pub fn is_upper ( & self ) -> bool {
112
+ ( self . chr - 'A' as u8 ) < 26
113
+ }
114
+
115
+ /// Checks if the character is punctuation
116
+ #[ inline]
117
+ pub fn is_punctuation ( & self ) -> bool {
118
+ self . is_graph ( ) && !self . is_alnum ( )
119
+ }
120
+
121
+ /// Checks if the character is a valid hex digit
122
+ #[ inline]
123
+ pub fn is_hex ( & self ) -> bool {
124
+ self . is_digit ( ) || ( ( self . chr | 32u8 ) - 'a' as u8 ) < 6
125
+ }
58
126
}
59
127
60
128
impl ToStr for Ascii {
0 commit comments