use std::borrow::Cow;
use std::fmt::Write;
const ESCAPES: &[char] = &[
'&', '\'', '\"', '<', '>', '\u{00D}', '\u{00A}', '\u{0085}', '\u{2028}',
];
pub(crate) fn escape(s: &str) -> Cow<'_, str> {
let mut remaining = s;
if !s.contains(ESCAPES) {
return Cow::Borrowed(s);
}
let mut out = String::new();
while let Some(idx) = remaining.find(ESCAPES) {
out.push_str(&remaining[..idx]);
remaining = &remaining[idx..];
let mut idxs = remaining.char_indices();
let (_, chr) = idxs.next().expect("must not be none");
match chr {
'>' => out.push_str(">"),
'<' => out.push_str("<"),
'\'' => out.push_str("'"),
'"' => out.push_str("""),
'&' => out.push_str("&"),
other => {
write!(&mut out, "&#x{:X};", other as u32).expect("write to string cannot fail")
}
};
match idxs.next() {
None => remaining = "",
Some((idx, _)) => remaining = &remaining[idx..],
}
}
out.push_str(remaining);
Cow::Owned(out)
}
#[cfg(test)]
mod test {
#[test]
fn escape_basic() {
let inp = "<helo>&\"'";
assert_eq!(escape(inp), "<helo>&"'");
}
#[test]
fn escape_eol_encoding_sep() {
let test_cases = vec![
("CiAK", "
 
"), ("YQ0KIGIKIGMN", "a
 b
 c
"), ("YQ3ChSBiwoU", "a
… b…"), ("YQ3igKggYsKFIGPigKg=", "a

 b… c
"), ];
for (base64_encoded, expected_xml_output) in test_cases {
let bytes = base64::decode(base64_encoded).expect("valid base64");
let input = String::from_utf8(bytes).expect("valid utf-8");
assert_eq!(escape(&input), expected_xml_output);
}
}
use crate::escape::escape;
use proptest::proptest;
proptest! {
#[test]
fn round_trip(s: String) {
let encoded = escape(&s);
let decoded = crate::unescape::unescape(&encoded).expect("encoded should be valid decoded");
assert_eq!(decoded, s);
}
}
}