pub struct CharsetStringDecoder<C>where
C: CharsetCodec,{ /* private fields */ }Expand description
Convenience decoder for complete inputs that should become a String.
CharsetStringDecoder owns a CharsetDecoder and adapts the char
output expected by the transcode layer into UTF-8 String storage.
Lower-level streaming code should use CharsetDecoder directly; this
type is for closed, in-memory string conversions.
§Type Parameters
C: Charset codec used to decode source units into Unicode scalar values.
§Examples
use qubit_codec_text::Utf8Codec;
use qubit_io_text::CharsetStringDecoder;
let mut decoder = CharsetStringDecoder::new(Utf8Codec);
let decoded = decoder.decode_to_string("中文".as_bytes())?;
assert_eq!("中文", decoded);Implementations§
Source§impl<C> CharsetStringDecoder<C>where
C: CharsetCodec,
impl<C> CharsetStringDecoder<C>where
C: CharsetCodec,
Sourcepub fn new(codec: C) -> Self
pub fn new(codec: C) -> Self
Creates a string decoder with the default replacement policy.
§Parameters
codec: Charset codec used to decode input units.
§Returns
Returns a decoder using MalformedAction::Replace.
Sourcepub fn with_policy(codec: C, policy: CharsetDecodePolicy) -> Self
pub fn with_policy(codec: C, policy: CharsetDecodePolicy) -> Self
Sourcepub const fn malformed_action(&self) -> MalformedAction
pub const fn malformed_action(&self) -> MalformedAction
Returns the configured malformed-input action.
§Returns
The action used for malformed or incomplete input.
Sourcepub const fn replacement(&self) -> char
pub const fn replacement(&self) -> char
Returns the configured replacement character.
§Returns
The character emitted when the configured action replaces input.
Sourcepub const fn decoder(&self) -> &CharsetDecoder<C>
pub const fn decoder(&self) -> &CharsetDecoder<C>
Returns the wrapped charset decoder.
§Returns
A shared reference to the decoder owned by this adapter.
Sourcepub fn decoder_mut(&mut self) -> &mut CharsetDecoder<C>
pub fn decoder_mut(&mut self) -> &mut CharsetDecoder<C>
Returns a mutable reference to the wrapped charset decoder.
§Returns
A mutable reference to the decoder owned by this adapter.
Sourcepub fn into_decoder(self) -> CharsetDecoder<C>
pub fn into_decoder(self) -> CharsetDecoder<C>
Consumes this string decoder and returns the wrapped charset decoder.
§Returns
The decoder previously owned by this adapter.
Sourcepub fn decode_to_string(
&mut self,
input: &[C::Unit],
) -> Result<String, CharsetDecodeError>
pub fn decode_to_string( &mut self, input: &[C::Unit], ) -> Result<String, CharsetDecodeError>
Decodes a complete input slice into an owned String.
§Parameters
input: Complete source units.
§Returns
Returns decoded UTF-8 text.
§Errors
Returns CharsetDecodeError when decoding fails, output sizing
overflows, or the configured decode policy rejects malformed or
incomplete input.
Sourcepub fn decode_to_string_into(
&mut self,
input: &[C::Unit],
input_index: usize,
output: &mut String,
) -> Result<(), CharsetDecodeError>
pub fn decode_to_string_into( &mut self, input: &[C::Unit], input_index: usize, output: &mut String, ) -> Result<(), CharsetDecodeError>
Decodes a complete input slice and appends the decoded text to
output.
This method treats input[input_index..] as a closed stream. If the
underlying decoder requests more input, the tail is reported as
CharsetDecodeErrorKind::IncompleteSequence.
On success, decoded text is appended to output. If this method returns
Err, the destination string retains its original contents. This
guarantee applies to the destination only; it does not roll back
decoder state or cover panics. Streaming text-reader operations have
their own prefix-retention contracts.
§Parameters
input: Complete input slice visible to the decoder.input_index: Absolute index where decoding starts.output: String receiving decoded text.
§Returns
Returns () after appending all decoded text successfully.
§Errors
Returns CharsetDecodeError when input_index is outside input,
decoding fails, or output sizing overflows.