File tree Expand file tree Collapse file tree 1 file changed +10
-11
lines changed Expand file tree Collapse file tree 1 file changed +10
-11
lines changed Original file line number Diff line number Diff line change @@ -17,12 +17,11 @@ use ascii_char::{AsciiChar, ToAsciiChar};
17
17
pub fn caret_encode < C : Copy + Into < u8 > > ( c : C ) -> Option < AsciiChar > {
18
18
// The formula is explained in the Wikipedia article.
19
19
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
26
25
}
27
26
}
28
27
@@ -51,10 +50,10 @@ pub fn caret_encode<C: Copy + Into<u8>>(c: C) -> Option<AsciiChar> {
51
50
/// ```
52
51
pub fn caret_decode < C : Copy + Into < u8 > > ( c : C ) -> Option < AsciiChar > {
53
52
// 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 ,
59
58
}
60
59
}
You can’t perform that action at this time.
0 commit comments