Make alphabet use bytes instead of characters
This commit is contained in:
parent
313b877553
commit
a5cac7522b
@ -65,7 +65,7 @@ impl<R: Read, W: Write> Decoder<R, W> {
|
|||||||
for (i, &c) in in_buf.iter().enumerate() {
|
for (i, &c) in in_buf.iter().enumerate() {
|
||||||
let c = c.into();
|
let c = c.into();
|
||||||
num <<= 6;
|
num <<= 6;
|
||||||
if c == self.alphabet.pad() {
|
if c == char::from(self.alphabet.pad()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if i != bytes {
|
if i != bytes {
|
||||||
|
@ -68,14 +68,21 @@ impl<R: Read, W: Write> Encoder<R, W> {
|
|||||||
}
|
}
|
||||||
if let Some(wrap) = self.wrap {
|
if let Some(wrap) = self.wrap {
|
||||||
for idx in 0..=3 {
|
for idx in 0..=3 {
|
||||||
write!(self.writer, "{}", obuf[idx])?;
|
write!(self.writer, "{}", char::from(obuf[idx]))?;
|
||||||
total += 1;
|
total += 1;
|
||||||
if total % wrap == 0 {
|
if total % wrap == 0 {
|
||||||
writeln!(self.writer)?;
|
writeln!(self.writer)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
write!(self.writer, "{}{}{}{}", obuf[0], obuf[1], obuf[2], obuf[3])?;
|
write!(
|
||||||
|
self.writer,
|
||||||
|
"{}{}{}{}",
|
||||||
|
char::from(obuf[0]),
|
||||||
|
char::from(obuf[1]),
|
||||||
|
char::from(obuf[2]),
|
||||||
|
char::from(obuf[3])
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
if outlen < 4 {
|
if outlen < 4 {
|
||||||
break;
|
break;
|
||||||
|
19
src/lib.rs
19
src/lib.rs
@ -10,19 +10,20 @@ pub use {decode::Decoder, encode::Encoder, error::Error as B64Error};
|
|||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
/// Defines the character set used for encoding and decoding
|
/// Defines the character set used for encoding and decoding
|
||||||
pub struct B64Alphabet {
|
pub struct B64Alphabet {
|
||||||
items: [char; 64],
|
items: [u8; 64],
|
||||||
pad: char,
|
pad: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The most common Base64 alphabet as defined in [RFC4648](https://www.rfc-editor.org/rfc/rfc4648)
|
/// The most common Base64 alphabet as defined in [RFC4648](https://www.rfc-editor.org/rfc/rfc4648)
|
||||||
pub static B64_RFC4648_ALPHABET: B64Alphabet = B64Alphabet {
|
pub static B64_RFC4648_ALPHABET: B64Alphabet = B64Alphabet {
|
||||||
items: [
|
items: [
|
||||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
|
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',
|
||||||
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
|
b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', b'Z', b'a', b'b', b'c', b'd',
|
||||||
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
|
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',
|
||||||
'2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
|
b't', b'u', b'v', b'w', b'x', b'y', b'z', b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7',
|
||||||
|
b'8', b'9', b'+', b'/',
|
||||||
],
|
],
|
||||||
pad: '=',
|
pad: b'=',
|
||||||
};
|
};
|
||||||
|
|
||||||
impl Default for B64Alphabet {
|
impl Default for B64Alphabet {
|
||||||
@ -34,14 +35,14 @@ impl Default for B64Alphabet {
|
|||||||
impl B64Alphabet {
|
impl B64Alphabet {
|
||||||
pub(crate) fn get(&self, c: char) -> Option<usize> {
|
pub(crate) fn get(&self, c: char) -> Option<usize> {
|
||||||
for (idx, x) in self.items.iter().enumerate() {
|
for (idx, x) in self.items.iter().enumerate() {
|
||||||
if *x == c {
|
if char::from(*x) == c {
|
||||||
return Some(idx);
|
return Some(idx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn pad(&self) -> char {
|
pub(crate) fn pad(&self) -> u8 {
|
||||||
self.pad
|
self.pad
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user