put character escape into a separate function
@@ -17,6 +17,7 @@ use shared::FileRange;
use shared::SpwnSource;
//use std::collections::HashMap;
use std::path::PathBuf;
+use std::str::Chars;
use errors::SyntaxError;
use internment::LocalIntern;
@@ -1829,28 +1830,18 @@ fn check_for_tag(
}
}
-pub fn str_content(
- mut inp: String,
+fn char_escape(
+ chars: &mut Chars<'_>,
tokens: &Tokens,
notes: &ParseNotes,
-) -> Result<(String, Option<StringFlags>), SyntaxError> {
- let first = inp.remove(0);
- inp.remove(inp.len() - 1);
-
- let mut out = (String::new(), None);
- let mut chars = inp.chars();
-
- match first {
- '\'' | '"' => {
- while let Some(c) = chars.next() {
- out.0.push(if c == '\\' {
+) -> Result<char, SyntaxError> {
match chars.next() {
- Some('n') => '\n',
- Some('r') => '\r',
- Some('t') => '\t',
- Some('"') => '\"',
- Some('\'') => '\'',
- Some('\\') => '\\',
+ Some('n') => Ok('\n'),
+ Some('r') => Ok('\r'),
+ Some('t') => Ok('\t'),
+ Some('"') => Ok('\"'),
+ Some('\'') => Ok('\''),
+ Some('\\') => Ok('\\'),
Some('u') => {
match chars.next() {
Some('{') => {}
@@ -1911,8 +1902,10 @@ pub fn str_content(
});
}
+ Ok(
std::char::from_u32(u32::from_str_radix(&hex, 16).unwrap())
.unwrap_or('\0')
+ )
}
Some(a) => {
return Err(SyntaxError::SyntaxError {
@@ -1923,6 +1916,24 @@ pub fn str_content(
}
None => unreachable!(),
}
+}
+
+pub fn str_content(
+ mut inp: String,
+ tokens: &mut Tokens,
+ notes: &mut ParseNotes,
+) -> Result<(String, Option<StringFlags>), SyntaxError> {
+ let first = inp.remove(0);
+ inp.remove(inp.len() - 1);
+
+ let mut out = (String::new(), None);
+ let mut chars = inp.chars();
+
+ match first {
+ '\'' | '"' => {
+ while let Some(c) = chars.next() {
+ out.0.push(if c == '\\' {
+ char_escape(&mut chars, tokens, notes)?
} else {
c
});
@@ -1944,22 +1955,7 @@ pub fn str_content(
while let Some(c) = chars.next() {
out_str.push(if c == '\\' {
- match chars.next() {
- Some('n') => '\n',
- Some('r') => '\r',
- Some('t') => '\t',
- Some('"') => '\"',
- Some('\'') => '\'',
- Some('\\') => '\\',
- Some(a) => {
- return Err(SyntaxError::SyntaxError {
- message: format!("Invalid escape: \\{}", a),
- pos: tokens.position(),
- file: notes.file.clone(),
- })
- }
- None => unreachable!(),
- }
+ char_escape(&mut chars, tokens, notes)?
} else {
c
});
