(mut self)
| 228 | } |
| 229 | |
| 230 | fn parse_string(mut self) -> Result<Box<Wtf8>, LexicalError> { |
| 231 | if self.flags.is_raw_string() { |
| 232 | // For raw strings, no escaping is necessary. |
| 233 | return Ok(self.source.into()); |
| 234 | } |
| 235 | |
| 236 | let Some(mut escape) = memchr::memchr(b'\\', self.source.as_bytes()) else { |
| 237 | // If the string doesn't contain any escape sequences, return the owned string. |
| 238 | return Ok(self.source.into()); |
| 239 | }; |
| 240 | |
| 241 | // If the string contains escape sequences, we need to parse them. |
| 242 | let mut value = Wtf8Buf::with_capacity(self.source.len()); |
| 243 | |
| 244 | loop { |
| 245 | // Add the characters before the escape sequence to the string. |
| 246 | let before_with_slash = self.skip_bytes(escape + 1); |
| 247 | let before = &before_with_slash[..before_with_slash.len() - 1]; |
| 248 | value.push_str(before); |
| 249 | |
| 250 | // Add the escaped character to the string. |
| 251 | match self.parse_escaped_char()? { |
| 252 | None => {} |
| 253 | Some(EscapedChar::Literal(c)) => value.push(c), |
| 254 | Some(EscapedChar::Escape(c)) => { |
| 255 | value.push_char('\\'); |
| 256 | value.push_char(c); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | let Some(next_escape) = self.source[self.cursor..].find('\\') else { |
| 261 | // Add the rest of the string to the value. |
| 262 | let rest = &self.source[self.cursor..]; |
| 263 | value.push_str(rest); |
| 264 | break; |
| 265 | }; |
| 266 | |
| 267 | // Update the position of the next escape sequence. |
| 268 | escape = next_escape; |
| 269 | } |
| 270 | |
| 271 | Ok(value.into()) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | pub(crate) fn parse_string_literal(source: &str, flags: ast::AnyStringFlags) -> Box<Wtf8> { |
no test coverage detected