stringcue 0.1.0

String Compressed Unicode Encoding - encode unicode more space efficient than utf-8 by sacrificing robustness like byte-boundaries.
Documentation
//! The 7-bit prefix that starts every codepoint in a stringcue blob, with
//! the concrete wire value assignments.

use arbitrary_int::{u3, u7};

/// The 7-bit prefix that starts every codepoint in a stringcue blob.
///
/// The 128-value prefix space is used up exactly. Concrete assignments:
///
/// - `0x20..=0x7E`: [`Prefix::Ascii`], the 95 printable ASCII characters,
///   each encoded as its 7-bit character code
/// - `0x7F`: [`Prefix::Control`]
/// - `0x00..=0x07`: [`Prefix::Tail7`], 7 additional bits follow
/// - `0x08..=0x0F`: [`Prefix::Tail8`], 8 additional bits follow
/// - `0x10..=0x17`: [`Prefix::Tail13`], 13 additional bits follow
/// - `0x18..=0x1F`: [`Prefix::Tail18`], 18 additional bits follow
///
/// The tail variants store the first 3 bits of the codepoint as their low 3
/// bits and their length class as their top 2 bits, so both translation
/// directions are pure bit arithmetic.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum Prefix {
    /// One of the 95 printable ASCII characters (`0x20` to `0x7E`), stored
    /// as its 7-bit character code.
    Ascii(u7),
    /// The non-unicode `CONTROL` value, used as separator and terminator.
    Control,
    /// The first 3 bits of a codepoint with 7 additional bits following.
    Tail7 {
        /// The first 3 bits of the codepoint.
        payload: u3,
    },
    /// The first 3 bits of a codepoint with 8 additional bits following.
    Tail8 {
        /// The first 3 bits of the codepoint.
        payload: u3,
    },
    /// The first 3 bits of a codepoint with 13 additional bits following.
    Tail13 {
        /// The first 3 bits of the codepoint.
        payload: u3,
    },
    /// The first 3 bits of a codepoint with 18 additional bits following.
    Tail18 {
        /// The first 3 bits of the codepoint.
        payload: u3,
    },
}

#[allow(dead_code)]
impl Prefix {
    /// Parses a 7-bit prefix value.
    ///
    /// This is total: the 128-value prefix space is used up exactly, so every
    /// possible `u7` maps to a variant.
    pub(crate) fn from_bits(bits: u7) -> Self {
        let value = bits.value();
        if value < 0x20 {
            let payload = u3::new(value & 0b111);
            match value >> 3 {
                0 => Self::Tail7 { payload },
                1 => Self::Tail8 { payload },
                2 => Self::Tail13 { payload },
                _ => Self::Tail18 { payload },
            }
        } else if value < 0x7F {
            Self::Ascii(bits)
        } else {
            Self::Control
        }
    }

    /// The 7-bit prefix value of this prefix.
    pub(crate) fn to_bits(self) -> u7 {
        match self {
            Self::Tail7 { payload } => u7::new(payload.value()),
            Self::Tail8 { payload } => u7::new(0b01000 | payload.value()),
            Self::Tail13 { payload } => u7::new(0b10000 | payload.value()),
            Self::Tail18 { payload } => u7::new(0b11000 | payload.value()),
            Self::Ascii(code) => code,
            Self::Control => u7::new(0x7F),
        }
    }

    /// The number of bits following the prefix, or `None` for the
    /// self-contained [`Prefix::Ascii`] and [`Prefix::Control`].
    pub(crate) fn follow_bits(self) -> Option<u8> {
        match self {
            Self::Tail7 { .. } => Some(7),
            Self::Tail8 { .. } => Some(8),
            Self::Tail13 { .. } => Some(13),
            Self::Tail18 { .. } => Some(18),
            Self::Ascii(_) | Self::Control => None,
        }
    }
}

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

    #[test]
    fn bits_roundtrip_for_every_valid_prefix_value() {
        for bits in 0..=0x7F {
            let prefix = Prefix::from_bits(u7::new(bits));
            assert_eq!(prefix.to_bits(), u7::new(bits));
        }
    }

    #[test]
    fn tail_prefixes_carry_payload_in_the_low_bits() {
        for payload in 0..=0b111u8 {
            for prefix in [
                Prefix::Tail7 {
                    payload: u3::new(payload),
                },
                Prefix::Tail8 {
                    payload: u3::new(payload),
                },
                Prefix::Tail13 {
                    payload: u3::new(payload),
                },
                Prefix::Tail18 {
                    payload: u3::new(payload),
                },
            ] {
                assert_eq!(prefix.to_bits().value() & 0b111, payload);
            }
        }
    }

    #[test]
    fn follow_bits_match_the_length_classes() {
        for payload in 0..=0b111u8 {
            assert_eq!(
                Prefix::Tail7 { payload: u3::new(payload) }.follow_bits(),
                Some(7)
            );
            assert_eq!(
                Prefix::Tail8 { payload: u3::new(payload) }.follow_bits(),
                Some(8)
            );
            assert_eq!(
                Prefix::Tail13 { payload: u3::new(payload) }.follow_bits(),
                Some(13)
            );
            assert_eq!(
                Prefix::Tail18 { payload: u3::new(payload) }.follow_bits(),
                Some(18)
            );
        }
        assert_eq!(Prefix::Ascii(u7::new(0x20)).follow_bits(), None);
        assert_eq!(Prefix::Control.follow_bits(), None);
    }
}