#[derive(Debug, Clone, Copy)]
pub(crate) enum Form {
Utf16 { big_endian: bool, bom: bool },
SingleByte(fn(char) -> Option<u8>),
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Target {
pub(crate) label: &'static str,
pub(crate) declared: Option<&'static str>,
pub(crate) form: Form,
}
impl Target {
pub(crate) const fn form_is_utf16(&self) -> bool {
matches!(self.form, Form::Utf16 { .. })
}
}
pub(crate) const TARGETS: [Target; 8] = [
Target {
label: "utf-16le-bom",
declared: None,
form: Form::Utf16 {
big_endian: false,
bom: true,
},
},
Target {
label: "utf-16be-bom",
declared: Some("UTF-16"),
form: Form::Utf16 {
big_endian: true,
bom: true,
},
},
Target {
label: "utf-16le",
declared: Some("UTF-16LE"),
form: Form::Utf16 {
big_endian: false,
bom: false,
},
},
Target {
label: "utf-16be",
declared: Some("utf-16"),
form: Form::Utf16 {
big_endian: true,
bom: false,
},
},
Target {
label: "iso-8859-1",
declared: Some("ISO-8859-1"),
form: Form::SingleByte(latin1),
},
Target {
label: "iso-8859-15",
declared: Some("latin9"),
form: Form::SingleByte(latin9),
},
Target {
label: "windows-1252",
declared: Some("windows-1252"),
form: Form::SingleByte(windows_1252),
},
Target {
label: "us-ascii",
declared: Some("US-ASCII"),
form: Form::SingleByte(ascii),
},
];
fn ascii(c: char) -> Option<u8> {
u8::try_from(c).ok().filter(u8::is_ascii)
}
fn latin1(c: char) -> Option<u8> {
u8::try_from(c).ok()
}
fn latin9(c: char) -> Option<u8> {
const REPLACED: [(char, u8); 8] = [
('\u{20AC}', 0xA4),
('\u{0160}', 0xA6),
('\u{0161}', 0xA8),
('\u{017D}', 0xB4),
('\u{017E}', 0xB8),
('\u{0152}', 0xBC),
('\u{0153}', 0xBD),
('\u{0178}', 0xBE),
];
if let Some(&(_, b)) = REPLACED.iter().find(|(r, _)| *r == c) {
return Some(b);
}
latin1(c).filter(|b| !REPLACED.iter().any(|(_, r)| r == b))
}
fn windows_1252(c: char) -> Option<u8> {
const HIGH: [(char, u8); 27] = [
('\u{20AC}', 0x80),
('\u{201A}', 0x82),
('\u{0192}', 0x83),
('\u{201E}', 0x84),
('\u{2026}', 0x85),
('\u{2020}', 0x86),
('\u{2021}', 0x87),
('\u{02C6}', 0x88),
('\u{2030}', 0x89),
('\u{0160}', 0x8A),
('\u{2039}', 0x8B),
('\u{0152}', 0x8C),
('\u{017D}', 0x8E),
('\u{2018}', 0x91),
('\u{2019}', 0x92),
('\u{201C}', 0x93),
('\u{201D}', 0x94),
('\u{2022}', 0x95),
('\u{2013}', 0x96),
('\u{2014}', 0x97),
('\u{02DC}', 0x98),
('\u{2122}', 0x99),
('\u{0161}', 0x9A),
('\u{203A}', 0x9B),
('\u{0153}', 0x9C),
('\u{017E}', 0x9E),
('\u{0178}', 0x9F),
];
if let Some(&(_, b)) = HIGH.iter().find(|(h, _)| *h == c) {
return Some(b);
}
latin1(c).filter(|b| !(0x80..=0x9F).contains(b))
}
pub(crate) fn with_declaration(text: &str, declared: Option<&str>) -> Option<String> {
let Some(rest) = text.strip_prefix("<?xml") else {
return Some(match declared {
Some(name) => format!("<?xml version=\"1.0\" encoding=\"{name}\"?>{text}"),
None => text.to_owned(),
});
};
let end = rest.find("?>")? + "<?xml".len();
let declaration = &text[..end];
let rest = &text[end..];
let version_end = {
let start = declaration.find("version")?;
let after = &declaration[start..];
let open = after.find(['"', '\''])?;
let quote = after[open..].chars().next()?;
start + open + 1 + after[open + 1..].find(quote)? + 1
};
let tail = &declaration[version_end..];
let tail = match tail.find("encoding") {
Some(start) => {
let after = &tail[start..];
let open = after.find(['"', '\''])?;
let quote = after[open..].chars().next()?;
let close = open + 1 + after[open + 1..].find(quote)?;
format!("{}{}", tail[..start].trim_end(), &after[close + 1..])
}
None => tail.to_owned(),
};
let encoding = declared.map_or_else(String::new, |name| format!(" encoding=\"{name}\""));
Some(format!(
"{}{encoding}{tail}{rest}",
&declaration[..version_end]
))
}
pub(crate) fn reencode(text: &str, target: &Target) -> Option<Vec<u8>> {
let text = with_declaration(text, target.declared)?;
match target.form {
Form::Utf16 { big_endian, bom } => {
if !bom && !text.starts_with("<?") {
return None;
}
let mut out = Vec::with_capacity(text.len() * 2 + 2);
let units = bom.then_some(0xFEFF).into_iter().chain(text.encode_utf16());
for unit in units {
out.extend(if big_endian {
unit.to_be_bytes()
} else {
unit.to_le_bytes()
});
}
Some(out)
}
Form::SingleByte(encode) => text.chars().map(encode).collect(),
}
}