Skip to main content

Crate stringcue

Crate stringcue 

Source
Expand description

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

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 valuesEncoding
0x20..=0x7Ethe 95 printable ASCII characters, each as its 7-bit character code
0x7Fthe CONTROL value
0x00..=0x07the first 3 bits of the codepoint, with 7 additional bits following
0x08..=0x0Fthe first 3 bits of the codepoint, with 8 additional bits following
0x10..=0x17the first 3 bits of the codepoint, with 13 additional bits following
0x18..=0x1Fthe 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.

Structs§

DecodeAll
Iterator over the strings of a decoded stringcue blob; see decode_all.
DecodeOptions
Options for decoding; see crate::decode and crate::decode_all.
EncodeOptions
Options for encoding; see crate::encode and crate::encode_all.

Enums§

DecodeEmpties
Treatment of empty elements by decode_all.
DecodeError
An error produced while decoding a stringcue blob.
DecodeTerminator
Whether a CONTROL terminator is required, tolerated or refused when decoding a blob, and how empty elements are read without one.
EncodeEmpties
Treatment of empty elements by encode_all.
EncodeTerminator
Whether a CONTROL terminator is appended to the encoded blob, and how empty elements are treated.

Functions§

decode
Decodes a single string from a stringcue blob.
decode_all
Decodes a container of strings from one compact stringcue blob.
encode
Encodes a single string as a stringcue blob.
encode_all
Encodes a container of strings as one compact stringcue blob.