Crates.io | tick-encoding |
lib.rs | tick-encoding |
version | 0.1.2 |
source | src |
created_at | 2024-01-28 13:30:27.721075 |
updated_at | 2024-01-29 00:56:45.257528 |
description | A simple encoding scheme to encode binary data into ASCII strings |
homepage | |
repository | https://github.com/kylewlacy/tick-encoding |
max_upload_size | |
id | 1117624 |
size | 51,951 |
Tick Encoding is a simple encoding scheme that encodes arbitrary binary data into an ASCII string, implemented as a Rust crate. It's primarily designed for stuffing usually-ASCII data into JSON strings. It's very similar to percent encoding / URL encoding, but with a few key differences:
%
) as the escape characterInstall the tick-encoding
crate as a Rust dependency by running cargo add tick-encoding
.
// Encode the input into a tick-encoded ASCII string
let encoded = tick_encoding::encode("hello, world! 🙂".as_bytes());
assert_eq!(encoded, "hello, world! `F0`9F`99`82");
// Decode it back into a UTF-8 string
let decoded = tick_encoding::decode(encoded.as_bytes()).unwrap();
let decoded_str = std::str::from_utf8(&decoded).unwrap();
assert_eq!(decoded_str, "hello, world! 🙂");
The tick-encoding
crate includes the following Cargo features:
std
(default): Enables functionality using Rust's standard library. Disable to build in #![no_std]
mode.alloc
(default): Enables functionality that depends on the global allocator. Disabling this will greatly limit what functions you can use!safe
: Avoid unsafe code. By default, a small amount of unsafe code is used (all checked with extensive unit tests, property tests, and Miri checks). Enabling this feature switches to purely safe code, and enables the #![deny(unsafe_code)]
lint at the crate level.The encoding scheme for Tick Encoding is straightforward:
0x21
to 0x5F
, and 0x61
to 0x7E
)0x09
, 0x0A
, 0x0D
, and 0x20
)0x60
becomes 0x60 0x60
)Decoding just reverses the process. To ensure that decoding and re-encoding produces the same output string, the encoded string is validated while decoding: