MCPcopy Index your code
hub / github.com/RustPython/RustPython / parse_string

Method parse_string

crates/codegen/src/string_parser.rs:230–272  ·  view source on GitHub ↗
(mut self)

Source from the content-addressed store, hash-verified

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
275pub(crate) fn parse_string_literal(source: &str, flags: ast::AnyStringFlags) -> Box<Wtf8> {

Callers 1

parse_string_literalFunction · 0.80

Calls 8

skip_bytesMethod · 0.80
parse_escaped_charMethod · 0.80
as_bytesMethod · 0.45
lenMethod · 0.45
push_strMethod · 0.45
pushMethod · 0.45
push_charMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected