Skip to content

Commit b0c4ec0

Browse files
committed
Documented all unsafe usages in free_functions module.
1 parent 1c4d1f9 commit b0c4ec0

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/free_functions.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ use ascii_char::{AsciiChar, ToAsciiChar};
1717
pub fn caret_encode<C: Copy + Into<u8>>(c: C) -> Option<AsciiChar> {
1818
// The formula is explained in the Wikipedia article.
1919
let c = c.into() ^ 0b0100_0000;
20-
unsafe {
21-
if c >= b'?' && c <= b'_' {
22-
Some(c.to_ascii_char_unchecked())
23-
} else {
24-
None
25-
}
20+
if c >= b'?' && c <= b'_' {
21+
// SAFETY: All bytes between '?' (0x3F) and '_' (0x5f) are valid ascii characters.
22+
Some(unsafe { c.to_ascii_char_unchecked() })
23+
} else {
24+
None
2625
}
2726
}
2827

@@ -51,10 +50,10 @@ pub fn caret_encode<C: Copy + Into<u8>>(c: C) -> Option<AsciiChar> {
5150
/// ```
5251
pub fn caret_decode<C: Copy + Into<u8>>(c: C) -> Option<AsciiChar> {
5352
// The formula is explained in the Wikipedia article.
54-
unsafe {
55-
match c.into() {
56-
b'?'..=b'_' => Some(AsciiChar::from_ascii_unchecked(c.into() ^ 0b0100_0000)),
57-
_ => None,
58-
}
53+
match c.into() {
54+
// SAFETY: All bytes between '?' (0x3F) and '_' (0x5f) after `xoring` with `0b0100_0000` are
55+
// valid bytes, as they represent characters between '␀' (0x0) and '␠' (0x1f) + '␡' (0x7f)
56+
b'?'..=b'_' => Some(unsafe { AsciiChar::from_ascii_unchecked(c.into() ^ 0b0100_0000) }),
57+
_ => None,
5958
}
6059
}

0 commit comments

Comments
 (0)