pub struct Utf32ByteCodec { /* private fields */ }Expand description
Combined byte-serialized UTF-32 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,
Utf32,
Utf32ByteCodec,
};
let codec = Utf32ByteCodec::new(ByteOrder::BigEndian);
assert_eq!(Charset::UTF_32BE, codec.charset());
assert_eq!(Utf32::MAX_BYTES_PER_CHAR, codec.max_units_per_char());
let mut output = [0_u8; Utf32::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-32BE"),
);Implementations§
Source§impl Utf32ByteCodec
impl Utf32ByteCodec
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-32 charset descriptor.
§Returns
Returns Charset::UTF_32LE or Charset::UTF_32BE 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-32 bytes for one character.
§Returns
Returns Utf32::MAX_BYTES_PER_CHAR.
Trait Implementations§
Source§impl CharsetCodec for Utf32ByteCodec
impl CharsetCodec for Utf32ByteCodec
Source§fn charset(&self) -> Charset
fn charset(&self) -> Charset
Returns the fixed-endian UTF-32 charset for the configured byte order.
§Returns
Returns Charset::UTF_32BE when configured with
ByteOrder::BigEndian, otherwise Charset::UTF_32LE.
Source§fn max_units_per_char(&self) -> usize
fn max_units_per_char(&self) -> usize
Returns the fixed size (4 bytes) for one serialized UTF-32 scalar value.
§Returns
Returns Utf32::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-32 scalar value from a byte-prefixed UTF-32 stream.
§Arguments
input- Byte-prefixed UTF-32 buffer.index- Start offset for parsing; must satisfyindex <= input.len().
§Returns
Ok(DecodeStatus::NeedMore { required, available })when fewer than four bytes remain.Ok(DecodeStatus::Complete { value, consumed })when one character is decoded.
§Errors
crate::CharsetDecodeErrorKind::MalformedSequencewhen byte index is invalid.crate::CharsetDecodeErrorKind::InvalidCodePointwhen bytes decode to an invalid scalar.
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-32 bytes at index.
§Arguments
ch- The Unicode scalar value to encode.output- Destination byte buffer.index- Start offset where 4 bytes are written; must satisfyindex <= output.len().
§Returns
Always returns Ok(4) on success.
§Errors
crate::CharsetEncodeErrorKind::BufferTooSmallif fewer than 4 bytes remain inoutput.
Source§impl Clone for Utf32ByteCodec
impl Clone for Utf32ByteCodec
Source§fn clone(&self) -> Utf32ByteCodec
fn clone(&self) -> Utf32ByteCodec
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 Utf32ByteCodec
impl Debug for Utf32ByteCodec
Source§impl Hash for Utf32ByteCodec
impl Hash for Utf32ByteCodec
Source§impl PartialEq for Utf32ByteCodec
impl PartialEq for Utf32ByteCodec
Source§fn eq(&self, other: &Utf32ByteCodec) -> bool
fn eq(&self, other: &Utf32ByteCodec) -> bool
self and other values to be equal, and is used by ==.