pub struct Utf16ByteCodec { /* private fields */ }Expand description
Combined byte-serialized UTF-16 codec.
The codec uses one configured byte order for both decoding and encoding. It
does not detect, consume, or emit a BOM automatically; callers should use
crate::UnicodeBom when a byte stream may carry an explicit BOM.
§Examples
use qubit_text_codec::{
ByteOrder,
CharsetCodec,
DecodeStatus,
Charset,
Utf16,
Utf16ByteCodec,
};
let codec = Utf16ByteCodec::new(ByteOrder::LittleEndian);
assert_eq!(Charset::UTF_16LE, codec.charset());
assert_eq!(Utf16::MAX_BYTES_PER_CHAR, codec.max_units_per_char());
let mut output = [0_u8; Utf16::MAX_BYTES_PER_CHAR];
let written = codec.encode_one('😀', &mut output, 0).expect("buffer fits");
assert_eq!(
DecodeStatus::Complete {
value: '😀',
consumed: written,
},
codec.decode_one(&output[..written], 0).expect("valid UTF-16LE"),
);Implementations§
Source§impl Utf16ByteCodec
impl Utf16ByteCodec
Sourcepub const fn byte_order(self) -> ByteOrder
pub const fn byte_order(self) -> ByteOrder
Sourcepub const fn charset(self) -> Charset
pub const fn charset(self) -> Charset
Returns the fixed-endian UTF-16 charset descriptor.
§Returns
Returns Charset::UTF_16LE or Charset::UTF_16BE according to this
codec’s configured byte order.
Sourcepub const fn max_units_per_char(self) -> usize
pub const fn max_units_per_char(self) -> usize
Returns the maximum number of serialized UTF-16 bytes for one character.
§Returns
Returns Utf16::MAX_BYTES_PER_CHAR.
Trait Implementations§
Source§impl CharsetCodec for Utf16ByteCodec
impl CharsetCodec for Utf16ByteCodec
Source§fn charset(&self) -> Charset
fn charset(&self) -> Charset
Returns the fixed-endian UTF-16 charset for the configured byte order.
§Returns
Returns Charset::UTF_16BE when configured with
ByteOrder::BigEndian, otherwise Charset::UTF_16LE.
Source§fn max_units_per_char(&self) -> usize
fn max_units_per_char(&self) -> usize
Returns the maximum number of UTF-16 bytes for a single encoded character.
§Returns
Returns Utf16::MAX_BYTES_PER_CHAR.
Source§fn decode_one(
&self,
input: &[u8],
index: usize,
) -> CharsetDecodeResult<DecodeStatus>
fn decode_one( &self, input: &[u8], index: usize, ) -> CharsetDecodeResult<DecodeStatus>
Decodes one UTF-16 scalar value from a byte-prefixed UTF-16 stream.
§Arguments
input- Byte-prefixed UTF-16 buffer.index- Start offset for parsing; must satisfyindex <= input.len().
§Returns
Ok(DecodeStatus::NeedMore { required, available })when the current slice has only a partial unit/pair.Ok(DecodeStatus::Complete { value, consumed })when one Unicode scalar value is decoded.
§Errors
CharsetDecodeErrorwhen UTF-16 structure is malformed.
Source§fn encode_one(
&self,
ch: char,
output: &mut [u8],
index: usize,
) -> CharsetEncodeResult<usize>
fn encode_one( &self, ch: char, output: &mut [u8], index: usize, ) -> CharsetEncodeResult<usize>
Encodes one Unicode scalar value into UTF-16 bytes at index.
§Arguments
ch- The Unicode scalar value to encode.output- Destination byte buffer.index- Start offset where bytes are written; must satisfyindex <= output.len().
§Returns
Ok(usize) with the number of written bytes (2 for BMP and 4 for supplementary).
§Errors
crate::CharsetEncodeErrorKind::BufferTooSmallif output does not have enough space.
Source§impl Clone for Utf16ByteCodec
impl Clone for Utf16ByteCodec
Source§fn clone(&self) -> Utf16ByteCodec
fn clone(&self) -> Utf16ByteCodec
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Utf16ByteCodec
impl Debug for Utf16ByteCodec
Source§impl Hash for Utf16ByteCodec
impl Hash for Utf16ByteCodec
Source§impl PartialEq for Utf16ByteCodec
impl PartialEq for Utf16ByteCodec
Source§fn eq(&self, other: &Utf16ByteCodec) -> bool
fn eq(&self, other: &Utf16ByteCodec) -> bool
self and other values to be equal, and is used by ==.