Option to escape spaces

This commit is contained in:
Tony Garnock-Jones 2021-08-05 15:54:41 +02:00
parent 661d96780d
commit cfd9898b4d
1 changed files with 9 additions and 0 deletions

View File

@ -24,6 +24,7 @@ pub struct TextWriter<W: io::Write> {
w: Suspendable<W>, w: Suspendable<W>,
pub comma_style: CommaStyle, pub comma_style: CommaStyle,
pub indentation: usize, pub indentation: usize,
pub escape_spaces: bool,
indent: String, indent: String,
} }
@ -54,10 +55,16 @@ impl<W: io::Write> TextWriter<W> {
w: Suspendable::new(w), w: Suspendable::new(w),
comma_style: CommaStyle::default(), comma_style: CommaStyle::default(),
indentation: 0, indentation: 0,
escape_spaces: false,
indent: "\n".to_owned(), indent: "\n".to_owned(),
} }
} }
pub fn set_escape_spaces(mut self, v: bool) -> Self {
self.escape_spaces = v;
self
}
pub fn suspend(&mut self) -> Self { pub fn suspend(&mut self) -> Self {
TextWriter { w: self.w.suspend(), indent: self.indent.clone(), .. *self } TextWriter { w: self.w.suspend(), indent: self.indent.clone(), .. *self }
} }
@ -229,6 +236,7 @@ impl<W: io::Write> Writer for TextWriter<W> {
for c in v.chars() { for c in v.chars() {
match c { match c {
'"' => write!(self.w, "\\\"")?, '"' => write!(self.w, "\\\"")?,
' ' if self.escape_spaces => write!(self.w, "\\u0020")?,
_ => self.write_stringlike_char(c)?, _ => self.write_stringlike_char(c)?,
} }
} }
@ -249,6 +257,7 @@ impl<W: io::Write> Writer for TextWriter<W> {
for c in v.chars() { for c in v.chars() {
match c { match c {
'|' => write!(self.w, "\\|")?, '|' => write!(self.w, "\\|")?,
' ' if self.escape_spaces => write!(self.w, "\\u0020")?,
_ => self.write_stringlike_char(c)?, _ => self.write_stringlike_char(c)?,
} }
} }