pub struct Utf8Codec;Expand description
UTF-8 byte-buffer charset codec.
§Examples
use qubit_text_codec::{
CharsetCodec,
DecodeStatus,
Charset,
Utf8,
Utf8Codec,
};
let codec = Utf8Codec;
assert_eq!(Charset::UTF_8, codec.charset());
assert_eq!(Utf8::MAX_UNITS_PER_CHAR, codec.max_units_per_char());
let mut output = [0_u8; Utf8::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-8"),
);Implementations§
Trait Implementations§
Source§impl CharsetCodec for Utf8Codec
impl CharsetCodec for Utf8Codec
Source§fn max_units_per_char(&self) -> usize
fn max_units_per_char(&self) -> usize
Returns the maximum number of UTF-8 bytes for one character.
§Returns
Returns Utf8::MAX_UNITS_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-8 character from a byte prefix.
§Arguments
input- UTF-8 byte slice.index- Start offset for decoding; must satisfyindex <= input.len().
§Returns
Ok(DecodeStatus::NeedMore { required, available })for partial input.Ok(DecodeStatus::Complete { value, consumed })for a decoded scalar value.
§Errors
crate::CharsetDecodeErrorKind::MalformedSequencefor invalid UTF-8 byte sequence.
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-8 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 encoded bytes (1..=4).
§Errors
crate::CharsetEncodeErrorKind::BufferTooSmallif output has insufficient bytes fromindex.
impl Copy for Utf8Codec
impl Eq for Utf8Codec
impl StructuralPartialEq for Utf8Codec
Auto Trait Implementations§
impl Freeze for Utf8Codec
impl RefUnwindSafe for Utf8Codec
impl Send for Utf8Codec
impl Sync for Utf8Codec
impl Unpin for Utf8Codec
impl UnsafeUnpin for Utf8Codec
impl UnwindSafe for Utf8Codec
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more