pub struct Base16384;Expand description
Base16384 encoding and decoding.
Implementations§
Source§impl Base16384
impl Base16384
Sourcepub const START: u16 = 19_968u16
pub const START: u16 = 19_968u16
The first code point of Base16384. It is the character ‘一’ (U+4E00).
Sourcepub const PADDING_OFFSET: u16 = 15_616u16
pub const PADDING_OFFSET: u16 = 15_616u16
The start of the Base16384 padding code points. The padding code points are “㴀㴁㴂㴃㴄㴅㴆” (U+3D00 to U+3D06).
Sourcepub const fn encode_len(data_len: usize) -> usize
pub const fn encode_len(data_len: usize) -> usize
Returns the minimum number of u16s needed to encode the given number of bytes.
§Examples
use base16384::Base16384;
let data = b"12345678";
let len = Base16384::encode_len(data.len());
assert_eq!(len, 6);Sourcepub fn encode(data: &[u8]) -> Vec<u16>
pub fn encode(data: &[u8]) -> Vec<u16>
Encodes the given data as Base16384 in a new allocated vector.
§Examples
use base16384::Base16384;
let data = b"12345678";
let encoded = Base16384::encode(data);
let text = String::from_utf16(&encoded).unwrap();
assert_eq!(text, "婌焳廔萷尀㴁");Sourcepub fn encode_to_slice<'a>(data: &[u8], buf: &'a mut [u16]) -> &'a [u16]
pub fn encode_to_slice<'a>(data: &[u8], buf: &'a mut [u16]) -> &'a [u16]
Encodes the given data as Base16384 into the given buffer.
§Panics
Panics if the buffer is too small. Use Base16384::encode_len to get the required capacity.
§Examples
use base16384::Base16384;
let data = b"12345678";
let mut buf = [0u16; 6];
let encoded = Base16384::encode_to_slice(data, &mut buf);
let text = String::from_utf16(encoded).unwrap();
assert_eq!(text, "婌焳廔萷尀㴁");Sourcepub fn decode_len(data_len: usize, padding: Option<u16>) -> usize
pub fn decode_len(data_len: usize, padding: Option<u16>) -> usize
Returns the minimum number of bytes needed to decode the given number of u16s. The given offset is the padding code point of the last chunk (if exists).
§Panics
Panics if the given offset is out of the Base16384 padding code points range
(see Base16384::PADDING_OFFSET).
§Examples
use base16384::Base16384;
let data = "婌焳廔萷尀㴁".encode_utf16().collect::<Vec<_>>();
let padding = Base16384::padding(data.last().cloned().unwrap());
let len = Base16384::decode_len(data.len(), padding);
assert_eq!(len, 8);Sourcepub fn padding(last: u16) -> Option<u16>
pub fn padding(last: u16) -> Option<u16>
Gets the padding code point of the last chunk (if exists).
§Examples
use base16384::Base16384;
let data = "婌焳廔萷尀㴁".encode_utf16().collect::<Vec<_>>();
let padding = Base16384::padding(data.last().cloned().unwrap());
assert_eq!(padding, Some(0x3d01));
let data = "婌焳廔萷".encode_utf16().collect::<Vec<_>>();
let padding = Base16384::padding(data.last().cloned().unwrap());
assert_eq!(padding, None);Sourcepub fn decode(data: &[u16]) -> Result<Vec<u8>, Base16384DecodeError>
pub fn decode(data: &[u16]) -> Result<Vec<u8>, Base16384DecodeError>
Decodes the given Base16384 data into a new allocated vector.
§Examples
use base16384::Base16384;
let data = "婌焳廔萷尀㴁".encode_utf16().collect::<Vec<_>>();
let decoded = Base16384::decode(&data).unwrap();
assert_eq!(decoded, b"12345678");Sourcepub fn decode_to_slice<'a>(
data: &[u16],
buf: &'a mut [u8],
) -> Result<&'a [u8], Base16384DecodeError>
pub fn decode_to_slice<'a>( data: &[u16], buf: &'a mut [u8], ) -> Result<&'a [u8], Base16384DecodeError>
Decodes the given Base16384 data into the given buffer.
§Panics
Panics if the buffer is too small. Use Base16384::decode_len to get the required capacity.
§Examples
use base16384::Base16384;
let data = "婌焳廔萷尀㴁".encode_utf16().collect::<Vec<_>>();
let mut buf = [0u8; 8];
let decoded = Base16384::decode_to_slice(&data, &mut buf).unwrap();
assert_eq!(decoded, b"12345678");