pub fn decode_latin1(bytes: &[u8]) -> String {
bytes.iter().map(|&b| b as char).collect()
}
pub fn decode_utf8_or_latin1(bytes: &[u8]) -> String {
match std::str::from_utf8(bytes) {
Ok(s) => s.to_string(),
Err(_) => decode_latin1(bytes),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_decode_latin1_ascii() {
assert_eq!(decode_latin1(b"hello"), "hello");
}
#[test]
fn test_decode_latin1_high_bytes() {
assert_eq!(decode_latin1(&[0xE9, 0xFC, 0xF1]), "éüñ");
}
#[test]
fn test_decode_latin1_full_range() {
assert_eq!(decode_latin1(&[0xA9, 0xAE, 0xF6]), "©®ö");
}
#[test]
fn test_decode_utf8_or_latin1_valid_utf8() {
assert_eq!(decode_utf8_or_latin1("café".as_bytes()), "café");
}
#[test]
fn test_decode_utf8_or_latin1_latin1_fallback() {
assert_eq!(decode_utf8_or_latin1(&[0x63, 0x61, 0x66, 0xE9]), "café");
}
#[test]
fn test_decode_utf8_or_latin1_pure_ascii() {
assert_eq!(decode_utf8_or_latin1(b"hello"), "hello");
}
#[test]
fn test_decode_utf8_or_latin1_empty() {
assert_eq!(decode_utf8_or_latin1(b""), "");
assert_eq!(decode_latin1(b""), "");
}
}