Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

1.0 KB
Raw Blame
use std::borrow::Cow;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextEncoding {
    Ascii,
    Utf8,
    Latin1,
    Windows1252,
}

pub(crate) fn decode(bytes: &[u8], encoding: TextEncoding) -> Option<Cow<'_, str>> {
    if encoding == TextEncoding::Utf8 || bytes.is_ascii() {
        return std::str::from_utf8(bytes).ok().map(Cow::Borrowed);
    }
    if encoding == TextEncoding::Ascii {
        return None;
    }
    const C1: [char; 32] = [
        '€', '\u{81}', 'β€š', 'Ζ’', 'β€ž', '…', '†', '‑', 'Λ†', '‰', 'Ε ', 'β€Ή', 'Ε’', '\u{8d}', 'Ε½',
        '\u{8f}', '\u{90}', 'β€˜', '’', 'β€œ', '”', 'β€’', '–', 'β€”', '˜', 'β„’', 'Ε‘', 'β€Ί', 'Ε“', '\u{9d}',
        'ΕΎ', 'ΕΈ',
    ];
    Some(Cow::Owned(
        bytes
            .iter()
            .map(|&b| {
                if encoding == TextEncoding::Windows1252 && (0x80..=0x9f).contains(&b) {
                    C1[usize::from(b - 0x80)]
                } else {
                    char::from(b)
                }
            })
            .collect(),
    ))
}