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

Method parse_escaped_char

crates/codegen/src/string_parser.rs:120–154  ·  view source on GitHub ↗

Parse an escaped character, returning the new character.

(&mut self)

Source from the content-addressed store, hash-verified

118
119 /// Parse an escaped character, returning the new character.
120 fn parse_escaped_char(&mut self) -> Result<Option<EscapedChar>, LexicalError> {
121 let Some(first_char) = self.next_char() else {
122 unreachable!()
123 };
124
125 let new_char = match first_char {
126 '\\' => '\\'.into(),
127 '\'' => '\''.into(),
128 '\"' => '"'.into(),
129 'a' => '\x07'.into(),
130 'b' => '\x08'.into(),
131 'f' => '\x0c'.into(),
132 'n' => '\n'.into(),
133 'r' => '\r'.into(),
134 't' => '\t'.into(),
135 'v' => '\x0b'.into(),
136 o @ '0'..='7' => self.parse_octet(o as u8).into(),
137 'x' => self.parse_unicode_literal(2)?,
138 'u' if !self.flags.is_byte_string() => self.parse_unicode_literal(4)?,
139 'U' if !self.flags.is_byte_string() => self.parse_unicode_literal(8)?,
140 'N' if !self.flags.is_byte_string() => self.parse_unicode_name()?.into(),
141 // Special cases where the escape sequence is not a single character
142 '\n' => return Ok(None),
143 '\r' => {
144 if self.peek_byte() == Some(b'\n') {
145 self.next_byte();
146 }
147
148 return Ok(None);
149 }
150 _ => return Ok(Some(EscapedChar::Escape(first_char))),
151 };
152
153 Ok(Some(EscapedChar::Literal(new_char)))
154 }
155
156 fn parse_fstring_middle(mut self) -> Result<Box<Wtf8>, LexicalError> {
157 // Fast-path: if the f-string doesn't contain any escape sequences, return the literal.

Callers 2

parse_fstring_middleMethod · 0.80
parse_stringMethod · 0.80

Calls 9

EscapeInterface · 0.85
next_charMethod · 0.80
parse_octetMethod · 0.80
parse_unicode_literalMethod · 0.80
parse_unicode_nameMethod · 0.80
next_byteMethod · 0.80
SomeClass · 0.50
LiteralClass · 0.50
peek_byteMethod · 0.45

Tested by

no test coverage detected