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

Method compile_fstring_elements_into

crates/codegen/src/compile.rs:10187–10267  ·  view source on GitHub ↗
(
        &mut self,
        flags: ast::FStringFlags,
        fstring_elements: &ast::InterpolatedStringElements,
        pending_literal: &mut Option<Wtf8Buf>,
        element_count: &mut u32,
    )

Source from the content-addressed store, hash-verified

10185 }
10186
10187 fn compile_fstring_elements_into(
10188 &mut self,
10189 flags: ast::FStringFlags,
10190 fstring_elements: &ast::InterpolatedStringElements,
10191 pending_literal: &mut Option<Wtf8Buf>,
10192 element_count: &mut u32,
10193 ) -> CompileResult<()> {
10194 for element in fstring_elements {
10195 match element {
10196 ast::InterpolatedStringElement::Literal(string) => {
10197 let value = self.compile_fstring_literal_value(string, flags);
10198 if let Some(pending) = pending_literal.as_mut() {
10199 pending.push_wtf8(value.as_ref());
10200 } else {
10201 *pending_literal = Some(value);
10202 }
10203 }
10204 ast::InterpolatedStringElement::Interpolation(fstring_expr) => {
10205 let mut conversion = match fstring_expr.conversion {
10206 ast::ConversionFlag::None => ConvertValueOparg::None,
10207 ast::ConversionFlag::Str => ConvertValueOparg::Str,
10208 ast::ConversionFlag::Repr => ConvertValueOparg::Repr,
10209 ast::ConversionFlag::Ascii => ConvertValueOparg::Ascii,
10210 };
10211
10212 if let Some(ast::DebugText { leading, trailing }) = &fstring_expr.debug_text {
10213 let range = fstring_expr.expression.range();
10214 let source = self.source_file.slice(range);
10215 let text = [
10216 strip_fstring_debug_comments(leading).as_str(),
10217 source,
10218 strip_fstring_debug_comments(trailing).as_str(),
10219 ]
10220 .concat();
10221
10222 let text: Wtf8Buf = text.into();
10223 pending_literal
10224 .get_or_insert_with(Wtf8Buf::new)
10225 .push_wtf8(text.as_ref());
10226
10227 // If debug text is present, apply repr conversion when no `format_spec` specified.
10228 // See action_helpers.c: fstring_find_expr_replacement
10229 if matches!(
10230 (conversion, &fstring_expr.format_spec),
10231 (ConvertValueOparg::None, None)
10232 ) {
10233 conversion = ConvertValueOparg::Repr;
10234 }
10235 }
10236
10237 self.emit_pending_fstring_literal(pending_literal, element_count, false);
10238
10239 self.compile_expression(&fstring_expr.expression)?;
10240
10241 match conversion {
10242 ConvertValueOparg::None => {}
10243 ConvertValueOparg::Str
10244 | ConvertValueOparg::Repr

Callers 2

Calls 12

push_wtf8Method · 0.80
compile_expressionMethod · 0.80
SomeClass · 0.50
as_mutMethod · 0.45
as_refMethod · 0.45
sliceMethod · 0.45
concatMethod · 0.45
as_strMethod · 0.45

Tested by

no test coverage detected