use std::io;
use std::io::Write;
pub fn write_escaped<W>(mut out: W, string: &str) -> io::Result<()>
where
W: Write,
{
for c in string.chars() {
match c {
'&' => out.write_all(b"\\&")?,
'%' => out.write_all(b"\\%")?,
'$' => out.write_all(b"\\$")?,
'#' => out.write_all(b"\\#")?,
'_' => out.write_all(b"\\_")?,
'{' => out.write_all(b"\\{")?,
'}' => out.write_all(b"\\}")?,
'~' => out.write_all(b"\\textasciitilde{}")?,
'^' => out.write_all(b"\\textasciicircum{}")?,
'\\' => out.write_all(b"\\textbackslash{}")?,
'\n' => out.write_all(b"\\hfill\\break\n")?,
'<' => out.write_all(b"\\textless{}")?,
'>' => out.write_all(b"\\textgreater{}")?,
'|' => out.write_all(b"\\textbar{}")?,
'"' => out.write_all(b"\\textquotedbl{}")?,
'[' => out.write_all(b"{[}")?,
']' => out.write_all(b"{]}")?,
_ => write!(out, "{}", c)?,
}
}
Ok(())
}