Flatten unescape_string a bit 🥞

This commit is contained in:
Laurenz 2020-09-30 19:07:45 +02:00
parent 4077a7c11e
commit 0ddab1c00d

View File

@ -7,50 +7,52 @@ pub fn unescape_string(string: &str) -> String {
let mut out = String::with_capacity(string.len()); let mut out = String::with_capacity(string.len());
while let Some(c) = iter.next() { while let Some(c) = iter.next() {
if c == '\\' { if c != '\\' {
match iter.next() { out.push(c);
Some('\\') => out.push('\\'), continue;
Some('"') => out.push('"'), }
Some('u') if iter.peek() == Some(&'{') => {
iter.next();
let mut sequence = String::new(); match iter.next() {
let terminated = loop { Some('\\') => out.push('\\'),
match iter.peek() { Some('"') => out.push('"'),
// TODO: Feedback that closing brace is missing.
Some('}') => { Some('n') => out.push('\n'),
iter.next(); Some('t') => out.push('\t'),
break true; Some('u') if iter.peek() == Some(&'{') => {
} iter.next();
Some(&c) if c.is_ascii_hexdigit() => {
iter.next(); // TODO: Feedback if closing brace is missing.
sequence.push(c); let mut sequence = String::new();
} let terminated = loop {
_ => break false, match iter.peek() {
Some('}') => {
iter.next();
break true;
} }
}; Some(&c) if c.is_ascii_hexdigit() => {
iter.next();
sequence.push(c);
}
_ => break false,
}
};
if let Some(c) = hex_to_char(&sequence) {
out.push(c);
} else {
// TODO: Feedback that escape sequence is wrong. // TODO: Feedback that escape sequence is wrong.
if let Some(c) = hex_to_char(&sequence) { out.push_str("\\u{");
out.push(c); out.push_str(&sequence);
} else { if terminated {
out.push_str("\\u{"); out.push('}');
out.push_str(&sequence);
if terminated {
out.push('}');
}
} }
} }
Some('n') => out.push('\n'),
Some('t') => out.push('\t'),
Some(c) => {
out.push('\\');
out.push(c);
}
None => out.push('\\'),
} }
} else {
out.push(c); other => {
out.push('\\');
out.extend(other);
}
} }
} }