mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Flatten unescape_string a bit 🥞
This commit is contained in:
parent
4077a7c11e
commit
0ddab1c00d
@ -7,17 +7,24 @@ 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 != '\\' {
|
||||||
|
out.push(c);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
match iter.next() {
|
match iter.next() {
|
||||||
Some('\\') => out.push('\\'),
|
Some('\\') => out.push('\\'),
|
||||||
Some('"') => out.push('"'),
|
Some('"') => out.push('"'),
|
||||||
|
|
||||||
|
Some('n') => out.push('\n'),
|
||||||
|
Some('t') => out.push('\t'),
|
||||||
Some('u') if iter.peek() == Some(&'{') => {
|
Some('u') if iter.peek() == Some(&'{') => {
|
||||||
iter.next();
|
iter.next();
|
||||||
|
|
||||||
|
// TODO: Feedback if closing brace is missing.
|
||||||
let mut sequence = String::new();
|
let mut sequence = String::new();
|
||||||
let terminated = loop {
|
let terminated = loop {
|
||||||
match iter.peek() {
|
match iter.peek() {
|
||||||
// TODO: Feedback that closing brace is missing.
|
|
||||||
Some('}') => {
|
Some('}') => {
|
||||||
iter.next();
|
iter.next();
|
||||||
break true;
|
break true;
|
||||||
@ -30,10 +37,10 @@ pub fn unescape_string(string: &str) -> String {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: Feedback that escape sequence is wrong.
|
|
||||||
if let Some(c) = hex_to_char(&sequence) {
|
if let Some(c) = hex_to_char(&sequence) {
|
||||||
out.push(c);
|
out.push(c);
|
||||||
} else {
|
} else {
|
||||||
|
// TODO: Feedback that escape sequence is wrong.
|
||||||
out.push_str("\\u{");
|
out.push_str("\\u{");
|
||||||
out.push_str(&sequence);
|
out.push_str(&sequence);
|
||||||
if terminated {
|
if terminated {
|
||||||
@ -41,16 +48,11 @@ pub fn unescape_string(string: &str) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some('n') => out.push('\n'),
|
|
||||||
Some('t') => out.push('\t'),
|
other => {
|
||||||
Some(c) => {
|
|
||||||
out.push('\\');
|
out.push('\\');
|
||||||
out.push(c);
|
out.extend(other);
|
||||||
}
|
}
|
||||||
None => out.push('\\'),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
out.push(c);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user