use encoding_rs::Encoding;
pub fn encoding_for_codepage(name: &str) -> Option<&'static Encoding> {
let upper = name.trim().to_ascii_uppercase();
if upper.is_empty() {
return None;
}
let label: String = match upper.as_str() {
"UTF-8" | "UTF8" => "utf-8".into(),
"ASCII" | "US-ASCII" => "windows-1252".into(),
"ANSI_932" | "DOS932" | "SHIFT_JIS" | "SJIS" => "shift_jis".into(),
"ANSI_936" | "DOS936" | "GB2312" | "GBK" => "gbk".into(),
"ANSI_949" | "DOS949" => "euc-kr".into(),
"ANSI_950" | "DOS950" | "BIG5" => "big5".into(),
"DOS866" | "CP866" => "ibm866".into(),
"MACINTOSH" | "MACROMAN" => "macintosh".into(),
"ANSI_874" | "DOS874" => "windows-874".into(),
_ => {
if let Some(num) = upper.strip_prefix("ANSI_") {
format!("windows-{num}")
} else if let Some(num) = upper.strip_prefix("ISO8859-") {
format!("iso-8859-{num}")
} else if let Some(num) = upper.strip_prefix("ISO-8859-") {
format!("iso-8859-{num}")
} else if let Some(num) = upper.strip_prefix("CP") {
format!("windows-{num}")
} else {
upper.to_ascii_lowercase()
}
}
};
Encoding::for_label(label.as_bytes())
}
fn mif_encoding(n: u8) -> Option<&'static Encoding> {
match n {
b'1' => Some(encoding_rs::SHIFT_JIS),
b'2' => Some(encoding_rs::BIG5),
b'3' => Some(encoding_rs::EUC_KR),
b'4' => Some(encoding_rs::GBK),
_ => None,
}
}
pub fn decode_escapes(s: &str) -> String {
cadkit_core::text::decode_escapes(s, &|n, pair| {
let enc = mif_encoding(n)?;
let (text, had_errors) = enc.decode_without_bom_handling(&pair);
(!had_errors).then(|| text.into_owned())
})
}
pub fn decode_carets(s: &str) -> String {
if !s.contains('^') {
return s.to_owned();
}
let mut out = String::with_capacity(s.len());
let mut chars = s.chars().peekable();
while let Some(c) = chars.next() {
if c == '^' {
match chars.peek().copied() {
Some(' ') => {
chars.next();
out.push('^');
continue;
}
Some(n) if ('@'..='_').contains(&n) => {
chars.next();
out.push(char::from((n as u8) - 64));
continue;
}
Some('?') => {
chars.next();
out.push('\u{7f}');
continue;
}
_ => {}
}
}
out.push(c);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn caret_notation() {
assert_eq!(decode_carets("a^Jb"), "a\nb");
assert_eq!(decode_carets("x^ y"), "x^y");
assert_eq!(decode_carets("2^3"), "2^3");
assert_eq!(decode_carets("end^"), "end^");
}
#[test]
fn unicode_escapes() {
assert_eq!(decode_escapes("a\\U+00E9b"), "a\u{e9}b");
assert_eq!(decode_escapes("\\U+D83D\\U+DE00"), "\u{1F600}");
assert_eq!(decode_escapes("\\U+D83Dx"), "\\U+D83Dx");
assert_eq!(decode_escapes("C:\\Users\\x"), "C:\\Users\\x");
assert_eq!(decode_escapes("\\U+12"), "\\U+12");
}
#[test]
fn mif_escape() {
assert_eq!(decode_escapes("\\M+182A0"), "\u{3042}");
assert_eq!(decode_escapes("\\M+982A0"), "\\M+982A0");
}
#[test]
fn codepages() {
assert_eq!(
encoding_for_codepage("ANSI_1252"),
Some(encoding_rs::WINDOWS_1252)
);
assert_eq!(
encoding_for_codepage("ANSI_1254"),
Some(encoding_rs::WINDOWS_1254)
);
assert_eq!(
encoding_for_codepage("ANSI_932"),
Some(encoding_rs::SHIFT_JIS)
);
assert_eq!(encoding_for_codepage("dos866"), Some(encoding_rs::IBM866));
assert_eq!(encoding_for_codepage("nonsense"), None);
}
}