stringcue 0.1.1

String Compressed Unicode Encoding - encode unicode more space efficient than utf-8 by sacrificing robustness like byte-boundaries.
Documentation
  • Coverage
  • 100%
    27 out of 27 items documented4 out of 4 items with examples
  • Size
  • Source code size: 73.6 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 731.6 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Freddy404

stringcue

crates.io docs.rs

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.

Disclaimer: AI is used to develop this, but I'm checking its work extensively.

stringcue typically needs around an eighth less memory than UTF-8, by sacrificing robustness and processability. (This is only intended as a space-efficient storage encoding, do not use for processing. Since a single bit error can corrupt the whole blob, I'd recommend ensuring blob integrity with e.g. an additional checksum.)

Bits per code point vs code point bit length

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; the examples below use the defaults, which are described at the end of How it works.

A roundtrip of a single string:

let encoded = stringcue::encode("Hello, world!", Default::default());
let decoded = stringcue::decode(&encoded, Default::default()).expect("decoding should roundtrip encoding");

assert_eq!(decoded, "Hello, world!");

A roundtrip of a container of strings:

let strings = ["Hello", "world", "¡hola, unicode!"];

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

assert_eq!(decoded, strings);

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 CONTROLs 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.