Skip to content

Commit

Permalink
Add size hints to string parser (#9413)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 6, 2024
1 parent e80b3db commit 1666c7a
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions crates/ruff_python_parser/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -203,7 +204,7 @@ impl<'a> StringParser<'a> {
}

fn parse_fstring_middle(&mut self) -> Result<ast::FStringElement, LexicalError> {
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`
Expand Down Expand Up @@ -246,7 +247,7 @@ impl<'a> StringParser<'a> {
}

fn parse_bytes(&mut self) -> Result<StringType, LexicalError> {
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() => {
Expand All @@ -265,16 +266,14 @@ impl<'a> StringParser<'a> {
}
}
}

Ok(StringType::Bytes(ast::BytesLiteral {
value: content.chars().map(|c| c as u8).collect::<Vec<u8>>(),
range: self.range,
}))
}

fn parse_string(&mut self) -> Result<StringType, LexicalError> {
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 {
Expand Down

0 comments on commit 1666c7a

Please sign in to comment.