From 2e2ac8154ebb54b4f9edd06b146792e296a12d25 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Fri, 10 Jan 2025 18:46:06 -0500 Subject: [PATCH] Change alphabet to use bytes rather than chars --- src/decode.rs | 16 +++++++++------- src/encode.rs | 14 ++------------ src/lib.rs | 15 ++++++++------- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/decode.rs b/src/decode.rs index 9fba040..6074401 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -1,5 +1,5 @@ -use std::io::{self, Read, Write}; use super::*; +use std::io::{self, Read, Write}; #[derive(Debug)] pub enum DecoderError { @@ -21,7 +21,11 @@ pub struct Decoder { impl Decoder { pub fn new(reader: R, writer: W, alphabet: Option) -> Self { - Self { reader, writer, alphabet: alphabet.unwrap_or_default() } + Self { + reader, + writer, + alphabet: alphabet.unwrap_or_default(), + } } pub fn decode(&mut self) -> Result<(), DecoderError> { @@ -34,10 +38,9 @@ impl Decoder { } } for c in &buf { - let c = char::from(*c); num <<= 5; - if !matches!(self.alphabet.pad(), Some(ch) if ch == c) { - let idx = self.alphabet.idx(c).ok_or(DecoderError::IllegalChar)?; + if !matches!(self.alphabet.pad(), Some(ch) if ch == *c) { + let idx = self.alphabet.idx(*c).ok_or(DecoderError::IllegalChar)?; num |= idx as u64; } } @@ -75,7 +78,7 @@ mod tests { #[test] fn get_idx() { - let idx = B32_RFC4648_ALPHABET.idx('S').unwrap(); + let idx = B32_RFC4648_ALPHABET.idx(b'S').unwrap(); assert_eq!(idx, 18); } @@ -88,4 +91,3 @@ mod tests { assert_eq!(HELLO, String::from_utf8(decoder.bytes()).unwrap()); } } - diff --git a/src/encode.rs b/src/encode.rs index 5da12ae..5afcdcb 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -123,20 +123,10 @@ mod test { let mut encoder = Encoder::new(reader, writer, None, None); encoder.encode().unwrap(); assert_eq!(encoder.output(), "JBSWY3DPFQQFO33SNRSA===="); - encoder = Encoder::new( - "Hello, World!".as_bytes(), - String::new(), - None, - None - ); + encoder = Encoder::new("Hello, World!".as_bytes(), String::new(), None, None); encoder.encode().unwrap(); assert_eq!(encoder.output(), "JBSWY3DPFQQFO33SNRSCC==="); - encoder = Encoder::new( - "Hello, World!\n".as_bytes(), - String::new(), - None, - None, - ); + encoder = Encoder::new("Hello, World!\n".as_bytes(), String::new(), None, None); encoder.encode().unwrap(); assert_eq!(encoder.output(), "JBSWY3DPFQQFO33SNRSCCCQ="); } diff --git a/src/lib.rs b/src/lib.rs index 87b5264..46ea3aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,16 +5,17 @@ pub mod error; #[derive(Clone, Copy)] pub struct B32Alphabet { - items: [char; 32], - pad: Option, + items: [u8; 32], + pad: Option, } pub static B32_RFC4648_ALPHABET: B32Alphabet = B32Alphabet { items: [ - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', - 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', + b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', + b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', b'Z', b'2', b'3', b'4', b'5', + b'6', b'7', ], - pad: Some('='), + pad: Some(b'='), }; impl Default for B32Alphabet { @@ -24,7 +25,7 @@ impl Default for B32Alphabet { } impl B32Alphabet { - pub(crate) fn idx(&self, c: char) -> Option { + pub(crate) fn idx(&self, c: u8) -> Option { for (idx, x) in self.items.iter().enumerate() { if *x == c { return Some(idx); @@ -33,7 +34,7 @@ impl B32Alphabet { None } - pub(crate) fn pad(&self) -> Option { + pub(crate) fn pad(&self) -> Option { self.pad } }