stringcue 0.1.0

String Compressed Unicode Encoding - encode unicode more space efficient than utf-8 by sacrificing robustness like byte-boundaries.
Documentation
use super::*;
use alloc::string::String;

/// The encoded bits, most significant bit first.
fn bits_of(encoded: &[u8]) -> String {
    let mut bits = String::new();
    for byte in encoded {
        for shift in (0..8).rev() {
            bits.push(if byte & (1 << shift) != 0 { '1' } else { '0' });
        }
    }
    bits
}

/// The expected bits of a codepoint: 7 prefix bits plus its tail bits.
fn codepoint_bits(prefix: &str, tail: &str) -> String {
    [prefix, tail].concat()
}

#[test]
fn encode_returns_padded_bytes() {
    let encoded = encode("Hello, world!", Default::default());
    assert!(!encoded.is_empty());
}

#[test]
fn encode_all_separates_strings() {
    let blob = encode_all(["a", "b"], Default::default());
    assert_eq!(
        bits_of(&blob),
        ["1100001", "1111111", "1100010", "111"].concat()
    );
}

#[test]
fn encode_all_single_string_matches_encode() {
    let blob = encode_all(["a"], Default::default());
    assert_eq!(blob, encode("a", Default::default()));
}

#[test]
fn ascii_encodes_as_the_seven_bit_character_code() {
    assert_eq!(bits_of(&encode("a", Default::default())), "11000011");
    assert_eq!(
        bits_of(&encode("Hello, world!", Default::default())),
        [
            "1001000", "1100101", "1101100", "1101100", "1101111", "0101100", "0100000", "1110111",
            "1101111", "1110010", "1101100", "1100100", "0100001", "11111"
        ]
        .concat()
    );
}

#[test]
fn control_characters_encode_in_the_shortest_length_class() {
    assert_eq!(
        bits_of(&encode("\0", Default::default())),
        "0000000000000011"
    );
    assert_eq!(
        bits_of(&encode("\t", Default::default())),
        "0000000000100111"
    );
    assert_eq!(
        bits_of(&encode("\u{7F}", Default::default())),
        ["0000000", "1111111", "11"].concat()
    );
}

#[test]
fn multibyte_codepoints_encode_in_their_length_class() {
    let cases = [
        (0x80u32, "0000001", "0000000", "11"),
        (0x3C0, "0000111", "1000000", "11"),
        (0x7FF, "0001111", "11111111", "1"),
        (0xFFFF, "0010111", "1111111111111", "1111"),
        (0x10000, "0011000", "010000000000000000", "1111111"),
        (0x10FFFF, "0011100", "001111111111111111", "1111111"),
    ];
    for (codepoint, prefix, tail, padding) in cases {
        let mut buffer = [0u8; 4];
        let string = char::from_u32(codepoint)
            .expect("valid codepoint")
            .encode_utf8(&mut buffer);
        assert_eq!(
            bits_of(&encode(string, Default::default())),
            codepoint_bits(prefix, tail) + padding
        );
    }
}

#[test]
fn multibyte_codepoints_concatenate_without_alignment() {
    let string = "\u{80}\u{7FF}\u{FFFF}\u{10FFFF}";
    assert_eq!(
        bits_of(&encode(string, Default::default())),
        [
            "0000001",
            "0000000",
            "0001111",
            "11111111",
            "0010111",
            "1111111111111",
            "0011100",
            "001111111111111111",
            "111111"
        ]
        .concat()
    );
}

#[test]
fn empty_strings_and_containers_encode_to_empty_blobs_by_default() {
    assert!(encode("", Default::default()).is_empty());
    assert!(encode_all(["", ""], Default::default()).is_empty());
    assert!(encode_all(Vec::<&str>::new(), Default::default()).is_empty());
}

#[test]
fn empties_occupy_only_a_separator_when_encoded() {
    let options = EncodeOptions {
        terminator: EncodeTerminator::Omit(EncodeEmpties::Encode),
    };
    assert!(encode_all([""], options).is_empty());
    assert_eq!(bits_of(&encode_all(["", ""], options)), "11111111");
    assert_eq!(
        bits_of(&encode_all(["a", "", "b"], options)),
        ["1100001", "1111111", "1111111", "1100010", "1111"].concat()
    );
}

#[test]
fn skipped_empties_leave_no_separator() {
    let blob = encode_all(["a", "", "b"], Default::default());
    assert_eq!(blob, encode_all(["a", "b"], Default::default()));
}

#[test]
fn in_band_termination_writes_the_control_value() {
    let options = EncodeOptions {
        terminator: EncodeTerminator::Write,
    };
    assert_eq!(
        bits_of(&encode("a", options)),
        ["1100001", "1111111", "11"].concat()
    );
    assert_eq!(bits_of(&encode("", options)), "11111111");
    assert_eq!(
        bits_of(&encode_all(["a", "b"], options)),
        ["1100001", "1111111", "1100010", "1111111", "1111111", "11111"].concat()
    );
    let nothing: [&str; 0] = [];
    assert_eq!(
        bits_of(&encode_all(nothing, options)),
        ["11111111", "11111111"].concat()
    );
    assert_eq!(
        encode_all(["a", "", "b"], options),
        encode_all(["a", "b"], options)
    );
}

#[test]
fn padding_fills_up_to_the_next_byte_boundary_with_ones() {
    let bits = bits_of(&encode("Hello", Default::default()));
    assert_eq!(bits.len(), 8 * 5);
    assert_eq!(&bits[35..], "11111");
}

#[test]
fn multibyte_encoding_stays_below_utf_8() {
    let string = "\u{80}\u{7FF}\u{FFFF}\u{10FFFF}";
    let encoded = encode(string, Default::default());
    assert_eq!(encoded.len(), 10);
    assert_eq!(string.len(), 11);
}

#[test]
fn multibyte_encoding_costs_the_documented_bits() {
    let documented_bits = [
        (0x80u32, 14),
        (0x3C0, 14),
        (0x7FF, 15),
        (0xFFFF, 20),
        (0x10000, 25),
        (0x10FFFF, 25),
    ];
    for (codepoint, bits) in documented_bits {
        let mut buffer = [0u8; 4];
        let string = char::from_u32(codepoint)
            .expect("valid codepoint")
            .encode_utf8(&mut buffer);
        let encoded = encode(string, Default::default());
        assert_eq!(encoded.len(), (bits + 7) / 8);
    }
}