stringcue 0.1.1

String Compressed Unicode Encoding - encode unicode more space efficient than utf-8 by sacrificing robustness like byte-boundaries.
Documentation
#![no_std]
#![forbid(unsafe_code)]
#![deny(missing_docs, missing_debug_implementations)]
//! String Compressed Unicode Encoding.
//!
//! If you ever thought, "I wish I could be more space efficient than UTF-8,
//! but my strings are too short/too random to throw a generic compression at
//! it" this might be the crate for you.
//!
//! `stringcue` typically needs around an eighth less memory than UTF-8, by
//! sacrificing robustness and processability. It is only intended as a
//! space-efficient storage encoding, do not use it for processing. Since a
//! single bit error can corrupt the whole blob, ensure blob integrity with
//! e.g. an additional checksum.
//!
//! # Usage
//!
//! The crate provides four functions: [`encode`]/[`decode`] for a single
//! string and [`encode_all`]/[`decode_all`] for containers of strings.
//! Encoding a single string takes anything string-like (`impl AsRef<str>`)
//! and returns a `Vec<u8>`; decoding takes anything byte-like
//! (`impl AsRef<[u8]>`) and returns a `String`.
//! The container functions accept and return [`IntoIterator`] types. All
//! functions take an options argument, [`EncodeOptions`] to encode and
//! [`DecodeOptions`] to decode; the examples below use the defaults, which
//! are described in [How it works](#how-it-works).
//!
//! # How it works
//!
//! We completely ignore byte boundaries inside the encoded blob. Each
//! codepoint starts with 7 bits, which encode either:
//!
//! - the 95 printable ASCII characters directly
//! - a special non-unicode `CONTROL` value
//! - the first 3 bits of any other codepoint, and whether 7, 8, 13 or 18
//!   additional bits follow to complete it
//!
//! The 7-bit prefix space is used up exactly: 95 printable ASCII characters
//! plus `CONTROL` fill 96 of the 128 possible prefixes, and the remaining 32
//! divide evenly into the 4 length classes with 8 payload values each (the
//! first 3 bits of the codepoint). These follow lengths make the classes
//! cover codepoints of 8–10, 11, 12–16 and 17–21 bits, for total costs of
//! 14, 15, 20 and 25 bits per codepoint.
//!
//! The concrete assignments are:
//!
//! | Prefix values | Encoding |
//! |---------------|----------|
//! | `0x20..=0x7E` | the 95 printable ASCII characters, each as its 7-bit character code |
//! | `0x7F` | the `CONTROL` value |
//! | `0x00..=0x07` | the first 3 bits of the codepoint, with 7 additional bits following |
//! | `0x08..=0x0F` | the first 3 bits of the codepoint, with 8 additional bits following |
//! | `0x10..=0x17` | the first 3 bits of the codepoint, with 13 additional bits following |
//! | `0x18..=0x1F` | the first 3 bits of the codepoint, with 18 additional bits following |
//!
//! After the final encoded codepoint and potentially a `CONTROL` as
//! terminator, we pad with 1s up to the next byte boundary.
//!
//! To encode a container of strings as a compact blob, the `CONTROL` is used
//! as a separator. It can also be used as a terminator to read a blob
//! without external length determination. If needed both as separator and
//! terminator, two successive `CONTROL`s are used as terminator and
//! zero-length strings can't be stored.
//!
//! With the default options, the length of the given input serves as the
//! external length limit: encoding writes no terminator to the output, and
//! decoding accepts a superfluous terminator on input.

extern crate alloc;

mod decode;
mod encode;
mod error;
mod options;
mod prefix;

pub use decode::{decode, decode_all, DecodeAll};
pub use encode::{encode, encode_all};
pub use error::DecodeError;
pub use options::{
    DecodeEmpties, DecodeOptions, DecodeTerminator, EncodeEmpties, EncodeOptions,
    EncodeTerminator,
};

#[cfg(test)]
mod tests {
    use alloc::{string::String, vec::Vec};

    #[test]
    fn single_string_roundtrip() {
        let encoded = crate::encode("Hello, world!", Default::default());
        assert_eq!(
            crate::decode(&encoded, Default::default()),
            Ok(String::from("Hello, world!"))
        );
    }

    #[test]
    fn container_roundtrip() {
        let strings = ["Hello", "world", "¡hola, unicode!"];

        let blob = crate::encode_all(strings, Default::default());
        let decoded: Vec<String> = crate::decode_all(&blob, Default::default())
            .expect("decoding should roundtrip encoding")
            .collect();

        assert_eq!(decoded, strings);
    }
}