@@ -89,45 +89,111 @@ impl Ascii {
89
89
}
90
90
91
91
/// Check if the character is a control character
92
+ ///
93
+ /// # Examples
94
+ ///
95
+ /// ```
96
+ /// use ascii::AsciiCast;
97
+ /// assert_eq!('\0'.to_ascii().unwrap().is_control(), true);
98
+ /// assert_eq!('n'.to_ascii().unwrap().is_control(), false);
99
+ /// assert_eq!(' '.to_ascii().unwrap().is_control(), false);
100
+ /// assert_eq!('\n'.to_ascii().unwrap().is_control(), true);
101
+ /// ```
92
102
#[ inline]
93
103
pub fn is_control ( & self ) -> bool {
94
104
self . chr < 0x20 || self . chr == 0x7F
95
105
}
96
106
97
107
/// Checks if the character is printable (except space)
108
+ ///
109
+ /// # Examples
110
+ ///
111
+ /// ```
112
+ /// use ascii::AsciiCast;
113
+ /// assert_eq!('n'.to_ascii().unwrap().is_graph(), true);
114
+ /// assert_eq!(' '.to_ascii().unwrap().is_graph(), false);
115
+ /// assert_eq!('\n'.to_ascii().unwrap().is_graph(), false);
116
+ /// ```
98
117
#[ inline]
99
118
pub fn is_graph ( & self ) -> bool {
100
- ( self . chr - 0x21 ) < 0x5E
119
+ self . chr . wrapping_sub ( 0x21 ) < 0x5E
101
120
}
102
121
103
122
/// Checks if the character is printable (including space)
123
+ ///
124
+ /// # Examples
125
+ ///
126
+ /// ```
127
+ /// use ascii::AsciiCast;
128
+ /// assert_eq!('n'.to_ascii().unwrap().is_print(), true);
129
+ /// assert_eq!(' '.to_ascii().unwrap().is_print(), true);
130
+ /// assert_eq!('\n'.to_ascii().unwrap().is_print(), false);
131
+ /// ```
104
132
#[ inline]
105
133
pub fn is_print ( & self ) -> bool {
106
- ( self . chr - 0x20 ) < 0x5F
134
+ self . chr . wrapping_sub ( 0x20 ) < 0x5F
107
135
}
108
136
109
137
/// Checks if the character is alphabetic and lowercase
138
+ ///
139
+ /// # Examples
140
+ ///
141
+ /// ```
142
+ /// use ascii::AsciiCast;
143
+ /// assert_eq!('a'.to_ascii().unwrap().is_lowercase(), true);
144
+ /// assert_eq!('A'.to_ascii().unwrap().is_lowercase(), false);
145
+ /// assert_eq!('@'.to_ascii().unwrap().is_lowercase(), false);
146
+ /// ```
110
147
#[ inline]
111
148
pub fn is_lowercase ( & self ) -> bool {
112
- ( self . chr - b'a' ) < 26
149
+ self . chr . wrapping_sub ( b'a' ) < 26
113
150
}
114
151
115
152
/// Checks if the character is alphabetic and uppercase
153
+ ///
154
+ /// # Examples
155
+ ///
156
+ /// ```
157
+ /// use ascii::AsciiCast;
158
+ /// assert_eq!('A'.to_ascii().unwrap().is_uppercase(), true);
159
+ /// assert_eq!('a'.to_ascii().unwrap().is_uppercase(), false);
160
+ /// assert_eq!('@'.to_ascii().unwrap().is_uppercase(), false);
161
+ /// ```
116
162
#[ inline]
117
163
pub fn is_uppercase ( & self ) -> bool {
118
- ( self . chr - b'A' ) < 26
164
+ self . chr . wrapping_sub ( b'A' ) < 26
119
165
}
120
166
121
167
/// Checks if the character is punctuation
168
+ ///
169
+ /// # Examples
170
+ ///
171
+ /// ```
172
+ /// use ascii::AsciiCast;
173
+ /// assert_eq!('n'.to_ascii().unwrap().is_punctuation(), false);
174
+ /// assert_eq!(' '.to_ascii().unwrap().is_punctuation(), false);
175
+ /// assert_eq!('_'.to_ascii().unwrap().is_punctuation(), true);
176
+ /// assert_eq!('~'.to_ascii().unwrap().is_punctuation(), true);
177
+ /// ```
122
178
#[ inline]
123
179
pub fn is_punctuation ( & self ) -> bool {
124
180
self . is_graph ( ) && !self . is_alphanumeric ( )
125
181
}
126
182
127
183
/// Checks if the character is a valid hex digit
184
+ ///
185
+ /// # Examples
186
+ ///
187
+ /// ```
188
+ /// use ascii::AsciiCast;
189
+ /// assert_eq!('5'.to_ascii().unwrap().is_hex(), true);
190
+ /// assert_eq!('a'.to_ascii().unwrap().is_hex(), true);
191
+ /// assert_eq!('F'.to_ascii().unwrap().is_hex(), true);
192
+ /// assert_eq!(32u8.to_ascii().unwrap().is_hex(), false);
193
+ /// ```
128
194
#[ inline]
129
195
pub fn is_hex ( & self ) -> bool {
130
- self . is_digit ( ) || ( ( self . chr | 32u8 ) - b'a' ) < 6
196
+ self . is_digit ( ) || ( self . chr | 32u8 ) . wrapping_sub ( b'a' ) < 6
131
197
}
132
198
}
133
199
0 commit comments