Skip to content

Commit b3d68b2

Browse files
author
Thomas Bahn
committed
Merge pull request #15 from tormol/Ascii_is_fix
Fix panics in Ascii's `.is_xxx()` methods and add examples.
2 parents a0a7afa + c36f3dd commit b3d68b2

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

src/ascii.rs

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,45 +89,111 @@ impl Ascii {
8989
}
9090

9191
/// 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+
/// ```
92102
#[inline]
93103
pub fn is_control(&self) -> bool {
94104
self.chr < 0x20 || self.chr == 0x7F
95105
}
96106

97107
/// 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+
/// ```
98117
#[inline]
99118
pub fn is_graph(&self) -> bool {
100-
(self.chr - 0x21) < 0x5E
119+
self.chr.wrapping_sub(0x21) < 0x5E
101120
}
102121

103122
/// 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+
/// ```
104132
#[inline]
105133
pub fn is_print(&self) -> bool {
106-
(self.chr - 0x20) < 0x5F
134+
self.chr.wrapping_sub(0x20) < 0x5F
107135
}
108136

109137
/// 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+
/// ```
110147
#[inline]
111148
pub fn is_lowercase(&self) -> bool {
112-
(self.chr - b'a') < 26
149+
self.chr.wrapping_sub(b'a') < 26
113150
}
114151

115152
/// 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+
/// ```
116162
#[inline]
117163
pub fn is_uppercase(&self) -> bool {
118-
(self.chr - b'A') < 26
164+
self.chr.wrapping_sub(b'A') < 26
119165
}
120166

121167
/// 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+
/// ```
122178
#[inline]
123179
pub fn is_punctuation(&self) -> bool {
124180
self.is_graph() && !self.is_alphanumeric()
125181
}
126182

127183
/// 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+
/// ```
128194
#[inline]
129195
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
131197
}
132198
}
133199

0 commit comments

Comments
 (0)