From 1666c7a5cb9bddf23dd12fec70cc96b1a2cd93c1 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 6 Jan 2024 15:59:34 -0500 Subject: [PATCH] Add size hints to string parser (#9413) --- crates/ruff_python_parser/src/string.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/ruff_python_parser/src/string.rs b/crates/ruff_python_parser/src/string.rs index 3b62197f15..80f42e453b 100644 --- a/crates/ruff_python_parser/src/string.rs +++ b/crates/ruff_python_parser/src/string.rs @@ -107,6 +107,7 @@ impl<'a> StringParser<'a> { _ => std::char::from_u32(p).ok_or(unicode_error), } } + fn parse_octet(&mut self, o: u8) -> char { let mut radix_bytes = [o, 0, 0]; let mut len = 1; @@ -203,7 +204,7 @@ impl<'a> StringParser<'a> { } fn parse_fstring_middle(&mut self) -> Result { - let mut value = String::new(); + let mut value = String::with_capacity(self.rest.len()); while let Some(ch) = self.next_char() { match ch { // We can encounter a `\` as the last character in a `FStringMiddle` @@ -246,7 +247,7 @@ impl<'a> StringParser<'a> { } fn parse_bytes(&mut self) -> Result { - let mut content = String::new(); + let mut content = String::with_capacity(self.rest.len()); while let Some(ch) = self.next_char() { match ch { '\\' if !self.kind.is_raw() => { @@ -265,7 +266,6 @@ impl<'a> StringParser<'a> { } } } - Ok(StringType::Bytes(ast::BytesLiteral { value: content.chars().map(|c| c as u8).collect::>(), range: self.range, @@ -273,8 +273,7 @@ impl<'a> StringParser<'a> { } fn parse_string(&mut self) -> Result { - let mut value = String::new(); - + let mut value = String::with_capacity(self.rest.len()); if self.kind.is_raw() { value.push_str(self.skip_bytes(self.rest.len())); } else {