Trait encode_unicode::CharExt [] [src]

pub trait CharExt: Sized {
    fn to_utf8(self) -> Utf8Char;
    fn to_utf16(self) -> Utf16Char;
    fn iter_utf8_bytes(self) -> Utf8Iterator;
    fn iter_utf16_units(self) -> Utf16Iterator;
    fn to_utf8_slice(self, dst: &mut [u8]) -> Option<usize>;
    fn to_utf16_slice(self, dst: &mut [u16]) -> Option<usize>;
    fn to_utf8_array(self) -> ([u8; 4], usize);
    fn to_utf16_tuple(self) -> (u16, Option<u16>);
    fn from_utf8_slice(src: &[u8]) -> Result<(Self, usize)InvalidUtf8Slice>;
    fn from_utf16_slice(src: &[u16]) -> Result<(Self, usize)InvalidUtf16Slice>;
    fn from_utf8_array(utf8: [u8; 4]) -> Result<Self, InvalidUtf8Array>;
    fn from_utf16_tuple(utf16: (u16, Option<u16>)) -> Result<Self, InvalidUtf16Tuple>;
    unsafe fn from_utf8_exact_slice_unchecked(src: &[u8]) -> Self;
    unsafe fn from_utf16_tuple_unchecked(utf16: (u16, Option<u16>)) -> Self;
    fn from_u32_detailed(c: u32) -> Result<Self, InvalidCodePoint>;
}

Extension trait for char that adds methods for converting to and from UTF-8 or UTF-16.

Required Methods

fn to_utf8(self) -> Utf8Char

Get the UTF-8 representation of this codepoint.

Utf8Char is to [u8;4] what char is to u32: a restricted type that cannot be mutated internally.

fn to_utf16(self) -> Utf16Char

Get the UTF-16 representation of this codepoint.

Utf16Char is to (u16,Option<u16>) what char is to u32: a restricted type that cannot be mutated internally.

fn iter_utf8_bytes(self) -> Utf8Iterator

Iterate over or read the one to four bytes in the UTF-8 representation of this codepoint.

fn iter_utf16_units(self) -> Utf16Iterator

Iterate over the one or two units in the UTF-16 representation of this codepoint.

fn to_utf8_slice(self, dst: &mut [u8]) -> Option<usize>

Convert this char to UTF-8, and then returns the number of bytes written.

None is returned if the buffer is too small; then the buffer is left unmodified. A buffer of length four is always large enough.

Similar to the unstable .encode_utf8(), but that method somehow still exist on stable, so I have to use a different name.

fn to_utf16_slice(self, dst: &mut [u16]) -> Option<usize>

Convert this char to UTF-16, and then returns the number of units written.

None is returned if the buffer is too small; then the buffer is left unmodified. A buffer of length two is always large enough.

Similar to the unstable .encode_utf16(), but that method somehow still exist on stable, so I have to use a different name.

fn to_utf8_array(self) -> ([u8; 4], usize)

Convert this char to an UTF-8 array and lenght, The returned array is left-aligned, and the usize is how many bytes are used. The unused bytes are zero.

fn to_utf16_tuple(self) -> (u16, Option<u16>)

Convert this char to UTF-16. The second u16 is Some if a surrogate pair is required.

fn from_utf8_slice(src: &[u8]) -> Result<(Self, usize)InvalidUtf8Slice>

Create a char from the start of a slice intepreted as UTF-8, and return how many bytes were needed.

fn from_utf16_slice(src: &[u16]) -> Result<(Self, usize)InvalidUtf16Slice>

Read one or two UTF-16 units into a char, and also return how many units were needed.

fn from_utf8_array(utf8: [u8; 4]) -> Result<Self, InvalidUtf8Array>

Convert an UTF-8 sequence as returned from .to_utf8_array() into a char

fn from_utf16_tuple(utf16: (u16, Option<u16>)) -> Result<Self, InvalidUtf16Tuple>

Convert a UTF-16 pair as returned from .to_utf16_tuple() into a char.

unsafe fn from_utf8_exact_slice_unchecked(src: &[u8]) -> Self

Convert an UTF-8 sequence into a char. The length of the slice is the length of the sequence, should be 1,2,3 or 4.

Panics:

If the slice is empty

unsafe fn from_utf16_tuple_unchecked(utf16: (u16, Option<u16>)) -> Self

Convert a UTF-16 tuple as returned from .to_utf16_tuple() into a char.

fn from_u32_detailed(c: u32) -> Result<Self, InvalidCodePoint>

Perform some extra validations compared to char::from_u32_unchecked()

Failures:

  • the value is greater than 0x10ffff
  • the value is between 0xd800 and 0xdfff (inclusive)

Implementors