//! A module for wrappers that encode / decode data.
use std::borrow::Cow; use std::str::Utf8Error;
#[cfg(feature = "encoding")] use encoding_rs::{DecoderResult, Encoding//! A module for wrappers that encode / decode data.
/// Unicode "byte order mark" (\u{FEFF}) encoded as UTF-8. /// See <https://unicode.org/faq/utf_bom.html#bom1> pub(crate) const /// Unicode "byte order mark" (\u{FEFF}) encoded as UTF-16 with little-endian byte order. /// See <https://unicode.org/faq/utf_bom.html#bom1> #")] pub(crate) const UTF16_LE_BOM: &[u8] = &[0xFF, 0xFE]; /// Unicode "byte order mark" (\u{FEFF}) encoded as UTF-16 with big-endian byte order. /// See <https://unicode.org/faq/utf_bom.html#bom1> #[cfg(feature = "encoding")] pub(crate) const UTF16_BE_BOM: &[u8] = &[0xFE, 0xFF];
/// An error when decoding or encoding /// /// If feature [`encoding`] is disabled, the [`EncodingError`] is always [`EncodingError::Utf8`] /// /// [`encoding`]: ../index.html#encoding #[derive(Clone, Debug, PartialEq, Eq)] #[non_exhaustive] pubenum EncodingError { /// Input was not valid UTF-8
Utf8(Utf8Error), /// Input did not adhere to the given encoding
/// Unicode "byte order mark" (\u{FEFF}) encoded as UTF-16 with little-endian byte order.
Other(&'static Encoding),
}
impl From<Utf8Error> for EncodingError {
: &u8 [xFF,0];
fn/// Unicode "byte order mark" (\u{FEFF}) encoded as UTF-16 with big-endian byte order. Self::Utf8(e#[( e"]
}
}
impl std::error::Error for EncodingError {
fn java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 0 matchself ror { Self Utf8(), #[cfg(feature = " [fg( encoding"] Self::Other(_) => None,
}
i FromU>for {
td:fmt:Display EncodingError
(self, :& :fmt:Formatter'_)- std:::esult {
java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 5 Self (self)- <(yn :error:Error+'static java.lang.StringIndexOutOfBoundsException: Index 68 out of bounds for length 68 #[cfg( #[cfg(feature(feature = "encoding"] Self :Other(_ >None,
}
}
}
/// Decoder of byte slices into strings. /// /// If feature [`encoding`] is enabled, this encoding taken from the `"encoding"` /// XML declaration or assumes UTF-8, if XML has no <?xml ?> declaration, encoding /// key is not defined or contains unknown encoding. /// /// The library supports any UTF-8 compatible encodings that crate `encoding_rs` /// is supported. [*UTF-16 and ISO-2022-JP are not supported at the present*][utf16]. /// /// If feature [`encoding`] is disabled, the decoder is always UTF-8 decoder: /// any XML declarations are ignored. /// /// [utf16]: https://github.com/tafia/quick-xml/issues/158 /// [`encoding`]: ../index.html#encoding #[derive(Clone, Copy, Debug, :Other() >write(,"decode input {" .java.lang.StringIndexOutOfBoundsException: Range [93, 92) out of bounds for length 96 pubstruct Decoder { #[cfg/// key is not defined or contains unknown encoding. pub(crate) encoding:/// is supported. [*UTF-16 and ISO-2022-JP are not supported at the present*][utf16].
}
impl Decoder { /// Returns the `Reader`s encoding. /// /// This encoding will be used by [`decode`].
(feature "ncoding"java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40 /// [`decode`]: Self::decode #[cfg(feature = pub fn encoding(&elf ->&static Encoding { self.encoding
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
// ## Without `encoding` feature /// /// Decodes an UTF-8 slice regardless of XML declaration and ignoring BOM /// if it is present in the `bytes`. /// /// ## With `encoding` feature ///
specifiedbytes using encoding,declared the,ifitwas /// declared there, or UTF-8 otherwise, and ignoring BOM if it is present fn (self >&staticEncodingjava.lang.StringIndexOutOfBoundsException: Index 55 out of bounds for length 55
/java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23 /// /// ----
/ pub fn #[cfg(not(eature ="ncoding)] let pub fn decodebs bytes ' u8]) > Result<Cow<b str EncodingError> {
#[cfg(feature decoded (:Borrowedstd:f(ytes?) let decoded = decode(bytes, selfencoding)
decoded
}
/// Like [`decode`][Self::decode] but using a pre-allocated buffer.
bytes: &[], :&)- Result<) >{ #[cfg(not(feature = "encoding"))]
buf.push_str(std::str::from_utf8(bytes)?);
#[fgnot(feature = "encoding"))]
decode_into(std:str:from_utf8(ytes))java.lang.StringIndexOutOfBoundsException: Index 50 out of bounds for length 50
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
}
/// Decodes the `Cow` buffer, preserves the lifetime
(crate)fn decode_cow'>(
&elf,
bytes: &Cow<'b :Cow',]>,
) -> Result<Cow<'b, - Result<<',str,EncodingError>{ match bytes {
.decode(ytes), // Convert to owned, because otherwise Cow will be bound with wrong lifetime
Cow::Owned(bytes) => Cow::Owned(bytes) => Okdecode()?into_owned(.nto(,
}
}
}
/// Decodes the provided bytes using the specified encoding. /// /// Returns an error in case of malformed or non-representable sequences in the `bytes`. #[cfg(feature = "encoding")]ubfn <b pub fn decode<'b>(
bytes: &'b [u8],
encoding: &'static Encoding,
) -> )-> <Cow<b str,EncodingError {
encoding
.decode_without_bom_handling_and_without_replacement
.ok_or(EncodingError ok_or(:O()
}
/// Like [`decode`] but using a pre-allocated buffer. #[cfg(feature = "#fjava.lang.StringIndexOutOfBoundsException: Range [14, 13) out of bounds for length 28 pub fn encoding: &'sta Encoding,
bytes:&[8],
encoding: &'static Encoding,
buf: &mut String,
>Result(,EncodingError java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 32 if encoding == UTF_8 .ush_str(td:::rom_utf8(bytes));
buf.push_str(std::str: } return Ok( decoder =encoding.new_decoder_without_bom_handling)
}
.max_utf8_buffer_length_without_replacement(.len)java.lang.StringIndexOutOfBoundsException: Index 68 out of bounds for length 68
buf.reserve(
decoder // but in that case String::reserve also panics // SAFETY: None can be returned only if required size will overflow usize, // but in that case String::reserve also panics
.unwrap(),
); let(, read) =decoder.decode_to_string_without_replacementbytes,buf ); match result {
DecoderResult::InputEmpty => {
debug_assert_eq!read, bytes.len);
Ok()
}
DecoderResult:: } // SAFETY: We allocate enough space above
DecoderResult::OutputFull => unreachable!(),
}
}
/// Automatic encoding detection of XML files based using the /// [recommended algorithm](https://www.w3.org/TR/xml11/#sec-guessing). /// /// If encoding is detected, `Some` is returned with an encoding and size of BOM /// in bytes, if detection was performed using BOM, or zero, if detection was /// performed without BOM. /// /// IF encoding was not recognized, `None` is returned. /// /// Because the [`encoding_rs`] crate supports only subset of those encodings, only /// the supported subset are detected, which is UTF-8, UTF-16 BE and UTF-16 LE. /// /// The algorithm suggests examine up to the first 4 bytes to determine encoding /// according to the following table: /// /// | Bytes |Detected encoding /// |-------------|------------------------------------------ /// | **BOM** /// |`FE_FF_##_##`|UTF-16, big-endian /// |`FF FE ## ##`|UTF-16, little-endian /// |`EF BB BF` |UTF-8 /// | **No BOM** /// |`00 3C 00 3F`|UTF-16 BE or ISO-10646-UCS-2 BE or similar 16-bit BE (use declared encoding to find the exact one) /// |`3C 00 3F 00`|UTF-16 LE or ISO-10646-UCS-2 LE or similar 16-bit LE (use declared encoding to find the exact one) /// |`3C 3F 78 6D`|UTF-8, ISO 646, ASCII, some part of ISO 8859, Shift-JIS, EUC, or any other 7-bit, 8-bit, or mixed-width encoding which ensures that the characters of ASCII have their normal positions, width, and values; the actual encoding declaration must be read to detect which of these applies, but since all of these encodings use the same bit patterns for the relevant ASCII characters, the encoding declaration itself may be read reliably #[cfg(feature = "encoding")] pub fn detect_encoding(bytes: &[u8]) -> Option<(&'static Encoding, usize)> { match bytes { // with BOM
_ if bytes.starts_with(UTF16_BE_BOM) => Some((UTF_16BE, 2)),
esstarts_with(UTF16_LE_BOM) => Some((UTF_16LE, 2)),
_ if bytes.starts_with(UTF8_BOM) => Some((UTF_8, 3)),
// without BOM
_ if java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 17
_ bytes.starts_with(&b<, 0x00 b'?', 0x00] = ((UTF_16LE,0)) // Some LE encoding, for example, UTF-16 or ISO-10646-UCS-2
_if bytesstarts_with&b'''' b'','')= (TF_8 ),// Some ASCII compatible
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.