base116/
lib.rs

1/*
2 * Copyright (C) 2021 taylor.fish <contact@taylor.fish>
3 *
4 * This file is part of Base116.
5 *
6 * Base116 is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Base116 is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with Base116. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20//! Base116 is like Base85, but it increases data size by only 1/6 instead of
21//! 1/4.
22//!
23//! Base116 exploits properties of UTF-8 to convert arbitrary binary data to
24//! valid, printable UTF-8, with a lower size overhead than is possible with
25//! any printable ASCII encoding.
26//!
27//! For example, this binary data (in hex):
28//!
29//! ```text
30//! 9329bd4b43da0bfdd1d97bdf081a2d42ec540155
31//! ```
32//!
33//! is encoded as:
34//!
35//! ```text
36//! DZ<Oȥґ|yO(WFic{2n㎨r~9*Dz
37//! ```
38//!
39//! Wrapping ‘DZ’ and ‘Dz’ characters are added by default to make encoded data
40//! easier to select, as the data may start or end with combining characters or
41//! characters from right-to-left scripts.
42//!
43//! This crate provides both a binary and a library.
44
45#![cfg_attr(not(feature = "std"), no_std)]
46#![cfg_attr(feature = "doc_cfg", feature(doc_cfg))]
47#![deny(unsafe_op_in_unsafe_fn)]
48
49#[macro_use]
50mod digit;
51use digit::Digit;
52
53pub mod decode;
54pub mod encode;
55mod iter;
56mod ranges;
57
58#[cfg(feature = "alloc")]
59extern crate alloc;
60
61const BYTES_PER_CHUNK: usize = 6;
62const DIGITS_PER_CHUNK: usize = 7;
63
64const START_CHAR: char = '\u{1f1}';
65const END_CHAR: char = '\u{1f2}';
66
67const L1_MULT: u16 = 116 + 1;
68const L2_MULT: u16 = 116 * L1_MULT + 1;
69
70pub use decode::decode_bytes;
71pub use decode::decode_chars;
72pub use decode::decode_str;
73#[cfg(feature = "alloc")]
74pub use decode::decode_to_vec;
75
76pub use encode::encode_to_bytes;
77pub use encode::encode_to_chars;
78#[cfg(feature = "alloc")]
79pub use encode::encode_to_string;