pub fn encode(data: &[u8]) -> StringExpand description
Encodes binary data into a Base122 string.
Base122 is an efficient binary-to-text encoding scheme that is more compact than Base64 (yielding ~14% overhead vs Base64’s 33%) while remaining valid UTF-8. It achieves this by utilizing 7-bit groups and escaping specific illegal UTF-8 sequences.
§Performance
This implementation is highly optimized for throughput, utilizing:
- Fast-path execution: Scans 8-byte chunks and uses SIMD-like logic to skip illegal character checks when possible.
- Branchless-style bit manipulation: Aggregates bits using 64-bit word operations.
- Zero-copy mindset: Direct pointer arithmetic and pre-allocated buffers.
§Examples
let data = b"hello world";
let encoded = base122_fast::encode(data);
println!("{}", encoded);