diff --git a/base32ct/src/encoding.rs b/base32ct/src/encoding.rs index 8b87f08fe..c0dfec610 100644 --- a/base32ct/src/encoding.rs +++ b/base32ct/src/encoding.rs @@ -220,14 +220,9 @@ impl Encoding for T { } } + #[inline] fn encoded_len(bytes: &[u8]) -> usize { - if bytes.is_empty() { - 0 - } else if Self::PADDED { - ((bytes.len() - 1) / 5 + 1) * 8 - } else { - (bytes.len() * 8 + 4) / 5 - } + encoded_len::(bytes.len()) } } @@ -259,6 +254,17 @@ fn remove_padding(mut input: &[u8]) -> Result<&[u8]> { Ok(input) } +/// Get the length of Base32 produced by encoding the given amount of bytes. +pub const fn encoded_len(length: usize) -> usize { + if length == 0 { + 0 + } else if T::PADDED { + ((length - 1) / 5 + 1) * 8 + } else { + (length * 8 + 4) / 5 + } +} + #[cfg(all(test, feature = "alloc"))] mod tests { use crate::{Base32, Base32Unpadded, Encoding}; diff --git a/base32ct/src/lib.rs b/base32ct/src/lib.rs index 539af2cce..cc5c8e0c4 100644 --- a/base32ct/src/lib.rs +++ b/base32ct/src/lib.rs @@ -59,6 +59,6 @@ mod error; pub use crate::{ alphabet::rfc4648::{Base32, Base32Unpadded, Base32Upper, Base32UpperUnpadded}, - encoding::Encoding, + encoding::{encoded_len, Encoding}, error::{Error, Result}, };