diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-09-30 19:07:45 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-09-30 19:07:45 +0200 |
| commit | 0ddab1c00d9a16b6c073575790cb91870a3378f1 (patch) | |
| tree | 6cbffb06494cabf6cf77ae095488d94868e81a9c /src/parse | |
| parent | 4077a7c11ea19b1b6b6b6fe3014b9018846cf21b (diff) | |
Flatten unescape_string a bit 🥞
Diffstat (limited to 'src/parse')
| -rw-r--r-- | src/parse/escaping.rs | 78 |
1 files changed, 40 insertions, 38 deletions
diff --git a/src/parse/escaping.rs b/src/parse/escaping.rs index a2ff963b..2e556d0c 100644 --- a/src/parse/escaping.rs +++ b/src/parse/escaping.rs @@ -7,50 +7,52 @@ pub fn unescape_string(string: &str) -> String { let mut out = String::with_capacity(string.len()); while let Some(c) = iter.next() { - if c == '\\' { - match iter.next() { - Some('\\') => out.push('\\'), - Some('"') => out.push('"'), - Some('u') if iter.peek() == Some(&'{') => { - iter.next(); - - let mut sequence = String::new(); - let terminated = loop { - match iter.peek() { - // TODO: Feedback that closing brace is missing. - Some('}') => { - iter.next(); - break true; - } - Some(&c) if c.is_ascii_hexdigit() => { - iter.next(); - sequence.push(c); - } - _ => break false, - } - }; + if c != '\\' { + out.push(c); + continue; + } - // TODO: Feedback that escape sequence is wrong. - if let Some(c) = hex_to_char(&sequence) { - out.push(c); - } else { - out.push_str("\\u{"); - out.push_str(&sequence); - if terminated { - out.push('}'); + match iter.next() { + Some('\\') => out.push('\\'), + Some('"') => out.push('"'), + + Some('n') => out.push('\n'), + Some('t') => out.push('\t'), + Some('u') if iter.peek() == Some(&'{') => { + iter.next(); + + // TODO: Feedback if closing brace is missing. + let mut sequence = String::new(); + let terminated = loop { + match iter.peek() { + Some('}') => { + iter.next(); + break true; + } + Some(&c) if c.is_ascii_hexdigit() => { + iter.next(); + sequence.push(c); } + _ => break false, } - } - Some('n') => out.push('\n'), - Some('t') => out.push('\t'), - Some(c) => { - out.push('\\'); + }; + + if let Some(c) = hex_to_char(&sequence) { out.push(c); + } else { + // TODO: Feedback that escape sequence is wrong. + out.push_str("\\u{"); + out.push_str(&sequence); + if terminated { + out.push('}'); + } } - None => out.push('\\'), } - } else { - out.push(c); + + other => { + out.push('\\'); + out.extend(other); + } } } |
