(mut self)
| 439 | } |
| 440 | |
| 441 | fn parse_string(mut self) -> Result<StringType, LexicalError> { |
| 442 | if self.flags.is_raw_string() { |
| 443 | // For raw strings, no escaping is necessary. |
| 444 | return Ok(StringType::Str(ast::StringLiteral { |
| 445 | value: self.source.into(), |
| 446 | range: self.range, |
| 447 | flags: self.flags.into(), |
| 448 | node_index: AtomicNodeIndex::NONE, |
| 449 | })); |
| 450 | } |
| 451 | |
| 452 | let Some(mut escape) = memchr::memchr(b'\\', self.source.as_bytes()) else { |
| 453 | // If the string doesn't contain any escape sequences, return the owned string. |
| 454 | return Ok(StringType::Str(ast::StringLiteral { |
| 455 | value: self.source.into(), |
| 456 | range: self.range, |
| 457 | flags: self.flags.into(), |
| 458 | node_index: AtomicNodeIndex::NONE, |
| 459 | })); |
| 460 | }; |
| 461 | |
| 462 | // If the string contains escape sequences, we need to parse them. |
| 463 | let mut value = String::with_capacity(self.source.len()); |
| 464 | |
| 465 | loop { |
| 466 | // Add the characters before the escape sequence to the string. |
| 467 | let before_with_slash = self.skip_bytes(escape + 1); |
| 468 | let before = &before_with_slash[..before_with_slash.len() - 1]; |
| 469 | value.push_str(before); |
| 470 | |
| 471 | // Add the escaped character to the string. |
| 472 | match self.parse_escaped_char()? { |
| 473 | None => {} |
| 474 | Some(EscapedChar::Literal(c)) => value.push(c), |
| 475 | Some(EscapedChar::Escape(c)) => { |
| 476 | value.push('\\'); |
| 477 | value.push(c); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | let Some(next_escape) = self.source[self.cursor..].find('\\') else { |
| 482 | // Add the rest of the string to the value. |
| 483 | let rest = &self.source[self.cursor..]; |
| 484 | value.push_str(rest); |
| 485 | break; |
| 486 | }; |
| 487 | |
| 488 | // Update the position of the next escape sequence. |
| 489 | escape = next_escape; |
| 490 | } |
| 491 | |
| 492 | Ok(StringType::Str(ast::StringLiteral { |
| 493 | value: value.into_boxed_str(), |
| 494 | range: self.range, |
| 495 | flags: self.flags.into(), |
| 496 | node_index: AtomicNodeIndex::NONE, |
| 497 | })) |
| 498 | } |
no test coverage detected