| Crates.io | base122-rs |
| lib.rs | base122-rs |
| version | 0.1.4 |
| created_at | 2025-07-25 19:24:54.819758+00 |
| updated_at | 2025-08-16 03:49:16.29618+00 |
| description | High-performance Base122 encoding/decoding library with bitwise operations |
| homepage | https://github.com/kookyleo/base122 |
| repository | https://github.com/kookyleo/base122 |
| max_upload_size | |
| id | 1768121 |
| size | 55,314 |
A high-performance Base122 encoding/decoding library for Rust, based on the original kevinAlbs Base122 algorithm.
Base122 is a binary-to-text encoding that is approximately 14% more space-efficient than Base64, making it ideal for data URIs and other space-constrained applications.
Base122 uses a sophisticated bitwise approach:
Six characters are considered "dangerous" for transmission and are specially encoded:
\0 (null) - can truncate strings\n (newline) - breaks single-line formats\r (carriage return) - breaks single-line formats" (double quote) - conflicts with JSON/HTML attributes& (ampersand) - conflicts with HTML entities\ (backslash) - conflicts with escape sequencesAdd this to your Cargo.toml:
[dependencies]
base122-rs = "0.1"
use base122_rs::{encode, decode};
// Encode binary data
let data = b"Hello, World!";
let encoded = encode(data);
println!("Encoded: {}", encoded);
// Decode back to original
let decoded = decode(&encoded).unwrap();
assert_eq!(data, &decoded[..]);
use base122_rs::{encode, decode};
// Binary data with dangerous characters
let binary_data = vec![0, 10, 13, 34, 38, 92, 65, 66, 67];
let encoded = encode(&binary_data);
let decoded = decode(&encoded).unwrap();
assert_eq!(binary_data, decoded);
Build and run the demo:
cargo build --example demo
cargo run --example demo -- encode "Hello, World!"
cargo run --example demo -- decode "$(cargo run --example demo -- encode 'Hello, World!')"
| Encoding | Expansion Ratio | Efficiency | Use Case |
|---|---|---|---|
| Hexadecimal | 2.00x | 50% | Debug output |
| Base64 | 1.33x | 75% | Email, HTTP |
| Base122 | 1.14x | 87% | Data URIs, Space-constrained |
Size Encoded Ratio Efficiency vs Base64
--------------------------------------------------------
10 12 1.200 83.3% +16.7%
100 115 1.150 87.0% +13.8%
1000 1143 1.143 87.5% +14.3%
10000 11429 1.143 87.5% +14.3%
β Ideal for:
β Consider alternatives for:
use base122_rs::encode;
// Image data for CSS/HTML
let image_data = std::fs::read("image.png").unwrap();
let base122_uri = format!("data:image/png;base122,{}", encode(&image_data));
// ~14% smaller than equivalent Base64 data URI
use base122_rs::{encode, decode};
// Encode binary protocol message
let message = vec![0x01, 0x02, 0x03, 0x04];
let encoded = encode(&message);
// Send over text-based protocol
send_message(&encoded);
// Decode on receiver
let received = receive_message();
let decoded = decode(&received).unwrap();
The decode function returns a Result<Vec<u8>, String>:
use base122_rs::decode;
match decode("invalid input") {
Ok(data) => println!("Decoded: {:?}", data),
Err(e) => eprintln!("Decode error: {}", e),
}
Run all tests:
cargo test
Run with output for detailed benchmarks:
cargo test -- --nocapture
Run the example:
cargo run --example demo benchmark
cargo doc --open for local documentationThis project includes automated release scripts for easy version management:
π Full Release Process:
./release.sh
β‘ Quick Patch Release:
./quick-release.sh
π€ Automated Publishing:
Cargo.tomlgit commit -m "chore: bump version to x.y.z"git tag vx.y.zgit push origin vx.y.zContributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.