/// Decodes a slice of bytes containing encoded text into a UTF-8 `str`. Trailing null terminators
/// are removed, and any invalid characters are replaced with the [U+FFFD REPLACEMENT CHARACTER].
Decodes a slice of bytes containing encoded text into a UTF-8 `str`. Trailing null terminators
are removed, and any invalid characters are replaced with the [U+FFFD REPLACEMENT CHARACTER].
Decodes a slice of bytes containing encoded text into a UTF - 8 ` str ` . Trailing null terminators are removed and any invalid characters are replaced with the [ U + FFFD REPLACEMENT CHARACTER ] .
fn decode_text(encoding: Encoding, data: &[u8]) -> Cow<'_, str> {
    let mut end = data.len();
    match encoding {
        Encoding::Iso8859_1 => {
            // The ID3v2 specification says that only ISO-8859-1 characters between 0x20 to 0xFF,
            // inclusive, are considered valid. Any null terminator(s) (trailing 0x00 byte for
            // ISO-8859-1) will also be removed.
            //
            // TODO: Improve this conversion by returning a copy-on-write str sliced from data if
            // all characters are > 0x1F and < 0x80. Fallback to the iterator approach otherwise.
            data.iter().filter(|&b| *b > 0x1f).map(|&b| b as char).collect()
        },
        Encoding::Utf8 => {
            // Remove any null terminator(s) (trailing 0x00 byte for UTF-8).
            while end > 0 {
                if data[end-1] != 0 { break; }
                end -= 1;
            }
            String::from_utf8_lossy(&data[..end])
        },
        Encoding::Utf16Bom  | Encoding::Utf16Be => {
            // Remove any null terminator(s) (trailing [0x00, 0x00] bytes for UTF-16 variants).
            while end > 1 {
                if data[end-2] != 0x0 || data[end-1] != 0x0 { break; }
                end -= 2;
            }
            // Decode UTF-16 to UTF-8. If a byte-order-mark is present, UTF_16BE.decode() will use
            // the indicated endianness. Otherwise, big endian is assumed.
            UTF_16BE.decode(&data[..end]).0
        }
    }
}
fn decode_text ( encoding : Encoding , data : & [ u8 ] ) -> Cow < ' _ , str > { let mut end = data . len ( ) ; match encoding { Encoding :: Iso8859_1 => { // The ID3v2 specification says that only ISO-8859-1 characters between 0x20 to 0xFF, // inclusive, are considered valid. Any null terminator(s) (trailing 0x00 byte for // ISO-8859-1) will also be removed. // // TODO: Improve this conversion by returning a copy-on-write str sliced from data if // all characters are > 0x1F and < 0x80. Fallback to the iterator approach otherwise. data . iter ( ) . filter ( | & b | * b > 0x1f ) . map ( | & b | b as char ) . collect ( ) } , Encoding :: Utf8 => { // Remove any null terminator(s) (trailing 0x00 byte for UTF-8). while end > 0 { if data [ end - 1 ] != 0 { break ; } end -= 1 ; } String :: from_utf8_lossy ( & data [ .. end ] ) } , Encoding :: Utf16Bom | Encoding :: Utf16Be => { // Remove any null terminator(s) (trailing [0x00, 0x00] bytes for UTF-16 variants). while end > 1 { if data [ end - 2 ] != 0x0 || data [ end - 1 ] != 0x0 { break ; } end -= 2 ; } // Decode UTF-16 to UTF-8. If a byte-order-mark is present, UTF_16BE.decode() will use // the indicated endianness. Otherwise, big endian is assumed. UTF_16BE . decode ( & data [ .. end ] ) . 0 } } }
Decodes a slice of bytes containing encoded text into a UTF-8 `str`.
Decodes a slice of bytes containing encoded text into a UTF - 8 ` str ` .
encoding
Encoding
data
&[u8]