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};
1717pub 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/// ```
5251pub 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}
You can’t perform that action at this time.
0 commit comments