Skip to content

Commit c36f3dd

Browse files
committed
Add examples to Ascii's .is_xxx() methods.
To test subtractions and shom some of the less obvious cases. Some of them probably fit better in tests/test.rs
1 parent 77aae74 commit c36f3dd

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/ascii.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,42 +88,108 @@ impl Ascii {
8888
}
8989

9090
/// Check if the character is a control character
91+
///
92+
/// # Examples
93+
///
94+
/// ```
95+
/// use ascii::AsciiCast;
96+
/// assert_eq!('\0'.to_ascii().unwrap().is_control(), true);
97+
/// assert_eq!('n'.to_ascii().unwrap().is_control(), false);
98+
/// assert_eq!(' '.to_ascii().unwrap().is_control(), false);
99+
/// assert_eq!('\n'.to_ascii().unwrap().is_control(), true);
100+
/// ```
91101
#[inline]
92102
pub fn is_control(&self) -> bool {
93103
self.chr < 0x20 || self.chr == 0x7F
94104
}
95105

96106
/// Checks if the character is printable (except space)
107+
///
108+
/// # Examples
109+
///
110+
/// ```
111+
/// use ascii::AsciiCast;
112+
/// assert_eq!('n'.to_ascii().unwrap().is_graph(), true);
113+
/// assert_eq!(' '.to_ascii().unwrap().is_graph(), false);
114+
/// assert_eq!('\n'.to_ascii().unwrap().is_graph(), false);
115+
/// ```
97116
#[inline]
98117
pub fn is_graph(&self) -> bool {
99118
self.chr.wrapping_sub(0x21) < 0x5E
100119
}
101120

102121
/// Checks if the character is printable (including space)
122+
///
123+
/// # Examples
124+
///
125+
/// ```
126+
/// use ascii::AsciiCast;
127+
/// assert_eq!('n'.to_ascii().unwrap().is_print(), true);
128+
/// assert_eq!(' '.to_ascii().unwrap().is_print(), true);
129+
/// assert_eq!('\n'.to_ascii().unwrap().is_print(), false);
130+
/// ```
103131
#[inline]
104132
pub fn is_print(&self) -> bool {
105133
self.chr.wrapping_sub(0x20) < 0x5F
106134
}
107135

108136
/// Checks if the character is alphabetic and lowercase
137+
///
138+
/// # Examples
139+
///
140+
/// ```
141+
/// use ascii::AsciiCast;
142+
/// assert_eq!('a'.to_ascii().unwrap().is_lowercase(), true);
143+
/// assert_eq!('A'.to_ascii().unwrap().is_lowercase(), false);
144+
/// assert_eq!('@'.to_ascii().unwrap().is_lowercase(), false);
145+
/// ```
109146
#[inline]
110147
pub fn is_lowercase(&self) -> bool {
111148
self.chr.wrapping_sub(b'a') < 26
112149
}
113150

114151
/// Checks if the character is alphabetic and uppercase
152+
///
153+
/// # Examples
154+
///
155+
/// ```
156+
/// use ascii::AsciiCast;
157+
/// assert_eq!('A'.to_ascii().unwrap().is_uppercase(), true);
158+
/// assert_eq!('a'.to_ascii().unwrap().is_uppercase(), false);
159+
/// assert_eq!('@'.to_ascii().unwrap().is_uppercase(), false);
160+
/// ```
115161
#[inline]
116162
pub fn is_uppercase(&self) -> bool {
117163
self.chr.wrapping_sub(b'A') < 26
118164
}
119165

120166
/// Checks if the character is punctuation
167+
///
168+
/// # Examples
169+
///
170+
/// ```
171+
/// use ascii::AsciiCast;
172+
/// assert_eq!('n'.to_ascii().unwrap().is_punctuation(), false);
173+
/// assert_eq!(' '.to_ascii().unwrap().is_punctuation(), false);
174+
/// assert_eq!('_'.to_ascii().unwrap().is_punctuation(), true);
175+
/// assert_eq!('~'.to_ascii().unwrap().is_punctuation(), true);
176+
/// ```
121177
#[inline]
122178
pub fn is_punctuation(&self) -> bool {
123179
self.is_graph() && !self.is_alphanumeric()
124180
}
125181

126182
/// Checks if the character is a valid hex digit
183+
///
184+
/// # Examples
185+
///
186+
/// ```
187+
/// use ascii::AsciiCast;
188+
/// assert_eq!('5'.to_ascii().unwrap().is_hex(), true);
189+
/// assert_eq!('a'.to_ascii().unwrap().is_hex(), true);
190+
/// assert_eq!('F'.to_ascii().unwrap().is_hex(), true);
191+
/// assert_eq!(32u8.to_ascii().unwrap().is_hex(), false);
192+
/// ```
127193
#[inline]
128194
pub fn is_hex(&self) -> bool {
129195
self.is_digit() || (self.chr | 32u8).wrapping_sub(b'a') < 6

0 commit comments

Comments
 (0)