plannotator-tui 0.11.0

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
//! Standard base64 without padding tricks, for OSC 52. Small enough not to be a dependency.

pub(crate) fn encode(bytes: &[u8]) -> String {
    const TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
    for chunk in bytes.chunks(3) {
        let n = chunk.iter().fold(0u32, |acc, b| (acc << 8) | u32::from(*b)) << (8 * (3 - chunk.len()));
        for i in 0..4 {
            let ch =
                if i <= chunk.len() { TABLE.get(((n >> (18 - 6 * i)) & 63) as usize).copied() } else { None };
            out.push(ch.map_or('=', char::from));
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn matches_reference_vectors() {
        assert_eq!(encode(b""), "");
        assert_eq!(encode(b"f"), "Zg==");
        assert_eq!(encode(b"fo"), "Zm8=");
        assert_eq!(encode(b"foobar"), "Zm9vYmFy");
    }
}