stringcue
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.)
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 = encode;
let decoded = decode.expect;
assert_eq!;
A roundtrip of a container of strings:
let strings = ;
let blob = encode_all;
let decoded: = decode_all.expect.collect;
assert_eq!;
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
CONTROLvalue - 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.