Skip to content

Commit

Permalink
Avoid generating C char literals with control characters
Browse files Browse the repository at this point in the history
Fixes #487
  • Loading branch information
maxbrunsfeld committed Nov 13, 2019
1 parent a2bbc73 commit 5767bbc
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions cli/src/generate/render.rs
Expand Up @@ -1282,28 +1282,31 @@ impl Generator {
match c {
'\"' => result += "\\\"",
'\\' => result += "\\\\",
'\t' => result += "\\t",
'\u{000c}' => result += "\\f",
'\n' => result += "\\n",
'\r' => result += "\\r",
'\t' => result += "\\t",
_ => result.push(c),
}
}
result
}

fn add_character(&mut self, c: char) {
if c.is_ascii() {
match c {
'\0' => add!(self, "0"),
'\'' => add!(self, "'\\''"),
'\\' => add!(self, "'\\\\'"),
'\t' => add!(self, "'\\t'"),
'\n' => add!(self, "'\\n'"),
'\r' => add!(self, "'\\r'"),
_ => add!(self, "'{}'", c),
match c {
'\'' => add!(self, "'\\''"),
'\\' => add!(self, "'\\\\'"),
'\u{000c}' => add!(self, "'\\f'"),
'\n' => add!(self, "'\\n'"),
'\t' => add!(self, "'\\t'"),
'\r' => add!(self, "'\\r'"),
_ => {
if c == ' ' || c.is_ascii_graphic() {
add!(self, "'{}'", c)
} else {
add!(self, "{}", c as u32)
}
}
} else {
add!(self, "{}", c as u32)
}
}
}
Expand Down

0 comments on commit 5767bbc

Please sign in to comment.