(mut self)
| 369 | } |
| 370 | |
| 371 | fn parse_bytes(mut self) -> Result<StringType, LexicalError> { |
| 372 | if let Some(index) = self.source.as_bytes().find_non_ascii_byte() { |
| 373 | let ch = self.source.chars().nth(index).unwrap(); |
| 374 | return Err(LexicalError::new( |
| 375 | LexicalErrorType::InvalidByteLiteral, |
| 376 | TextRange::at( |
| 377 | self.compute_position(index), |
| 378 | TextSize::try_from(ch.len_utf8()).unwrap(), |
| 379 | ), |
| 380 | )); |
| 381 | } |
| 382 | |
| 383 | if self.flags.is_raw_string() { |
| 384 | // For raw strings, no escaping is necessary. |
| 385 | return Ok(StringType::Bytes(ast::BytesLiteral { |
| 386 | value: self.source.as_bytes().into(), |
| 387 | range: self.range, |
| 388 | flags: self.flags.into(), |
| 389 | node_index: AtomicNodeIndex::NONE, |
| 390 | })); |
| 391 | } |
| 392 | |
| 393 | let Some(mut escape) = memchr::memchr(b'\\', self.source.as_bytes()) else { |
| 394 | // If the string doesn't contain any escape sequences, return the owned string. |
| 395 | return Ok(StringType::Bytes(ast::BytesLiteral { |
| 396 | value: self.source.as_bytes().into(), |
| 397 | range: self.range, |
| 398 | flags: self.flags.into(), |
| 399 | node_index: AtomicNodeIndex::NONE, |
| 400 | })); |
| 401 | }; |
| 402 | |
| 403 | // If the string contains escape sequences, we need to parse them. |
| 404 | let mut value = Vec::with_capacity(self.source.len()); |
| 405 | loop { |
| 406 | // Add the characters before the escape sequence to the string. |
| 407 | let before_with_slash = self.skip_bytes(escape + 1); |
| 408 | let before = &before_with_slash[..before_with_slash.len() - 1]; |
| 409 | value.extend_from_slice(before.as_bytes()); |
| 410 | |
| 411 | // Add the escaped character to the string. |
| 412 | match self.parse_escaped_char()? { |
| 413 | None => {} |
| 414 | Some(EscapedChar::Literal(c)) => value.push(c as u8), |
| 415 | Some(EscapedChar::Escape(c)) => { |
| 416 | value.push(b'\\'); |
| 417 | value.push(c as u8); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | let Some(next_escape) = memchr::memchr(b'\\', &self.source.as_bytes()[self.cursor..]) |
| 422 | else { |
| 423 | // Add the rest of the string to the value. |
| 424 | let rest = &self.source[self.cursor..]; |
| 425 | value.extend_from_slice(rest.as_bytes()); |
| 426 | break; |
| 427 | }; |
| 428 |
no test coverage detected