Base16384

Struct Base16384 

Source
pub struct Base16384;
Expand description

Base16384 encoding and decoding.

Implementations§

Source§

impl Base16384

Source

pub const START: u16 = 19_968u16

The first code point of Base16384. It is the character ‘一’ (U+4E00).

Source

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).

Source

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);
Source

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, "婌焳廔萷尀㴁");
Source

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, "婌焳廔萷尀㴁");
Source

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);
Source

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);
Source

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");
Source

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");

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.