mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-13 16:15:16 +00:00
Remove repetitive to_string in parse_escaped_char
This commit is contained in:
parent
50f191b2c7
commit
b6647b0171
1 changed files with 35 additions and 31 deletions
|
@ -130,39 +130,43 @@ impl<'a> StringParser<'a> {
|
||||||
|
|
||||||
fn parse_escaped_char(&mut self) -> Result<String, LexicalError> {
|
fn parse_escaped_char(&mut self) -> Result<String, LexicalError> {
|
||||||
match self.next_char() {
|
match self.next_char() {
|
||||||
Some(c) => Ok(match c {
|
Some(c) => {
|
||||||
'\\' => '\\'.to_string(),
|
let char = match c {
|
||||||
'\'' => '\''.to_string(),
|
'\\' => '\\',
|
||||||
'\"' => '"'.to_string(),
|
'\'' => '\'',
|
||||||
'\n' => "".to_string(),
|
'\"' => '"',
|
||||||
'a' => '\x07'.to_string(),
|
'a' => '\x07',
|
||||||
'b' => '\x08'.to_string(),
|
'b' => '\x08',
|
||||||
'f' => '\x0c'.to_string(),
|
'f' => '\x0c',
|
||||||
'n' => '\n'.to_string(),
|
'n' => '\n',
|
||||||
'r' => '\r'.to_string(),
|
'r' => '\r',
|
||||||
't' => '\t'.to_string(),
|
't' => '\t',
|
||||||
'v' => '\x0b'.to_string(),
|
'v' => '\x0b',
|
||||||
o @ '0'..='7' => self.parse_octet(o).to_string(),
|
o @ '0'..='7' => self.parse_octet(o),
|
||||||
'x' => self.parse_unicode_literal(2)?.to_string(),
|
'x' => self.parse_unicode_literal(2)?,
|
||||||
'u' if !self.kind.is_bytes() => self.parse_unicode_literal(4)?.to_string(),
|
'u' if !self.kind.is_bytes() => self.parse_unicode_literal(4)?,
|
||||||
'U' if !self.kind.is_bytes() => self.parse_unicode_literal(8)?.to_string(),
|
'U' if !self.kind.is_bytes() => self.parse_unicode_literal(8)?,
|
||||||
'N' if !self.kind.is_bytes() => self.parse_unicode_name()?.to_string(),
|
'N' if !self.kind.is_bytes() => self.parse_unicode_name()?,
|
||||||
c => {
|
// Special cases where the escape sequence is not a single character
|
||||||
if self.kind.is_bytes() && !c.is_ascii() {
|
'\n' => return Ok("".to_string()),
|
||||||
return Err(LexicalError::new(
|
c => {
|
||||||
LexicalErrorType::OtherError(
|
if self.kind.is_bytes() && !c.is_ascii() {
|
||||||
"bytes can only contain ASCII literal characters".to_owned(),
|
return Err(LexicalError {
|
||||||
),
|
error: LexicalErrorType::OtherError(
|
||||||
self.get_pos(),
|
"bytes can only contain ASCII literal characters".to_owned(),
|
||||||
));
|
),
|
||||||
|
location: self.get_pos(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return Ok(format!("\\{c}"));
|
||||||
}
|
}
|
||||||
format!("\\{c}")
|
};
|
||||||
}
|
Ok(char.to_string())
|
||||||
|
}
|
||||||
|
None => Err(LexicalError {
|
||||||
|
error: LexicalErrorType::StringError,
|
||||||
|
location: self.get_pos(),
|
||||||
}),
|
}),
|
||||||
None => Err(LexicalError::new(
|
|
||||||
LexicalErrorType::StringError,
|
|
||||||
self.get_pos(),
|
|
||||||
)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue