MCPcopy Index your code
hub / github.com/python/mypy / join_formatted_strings

Function join_formatted_strings

mypyc/irbuild/format_str_tokenizer.py:168–208  ·  view source on GitHub ↗

Merge the list of literals and the list of substitutions alternatively using 'str_build_op'. `substitutions` is the result value of formatting conversions. If the `literals` is set to None, we simply join the substitutions; Otherwise, the `literals` is the literal substrings of the

(
    builder: IRBuilder, literals: list[str] | None, substitutions: list[Value], line: int
)

Source from the content-addressed store, hash-verified

166
167
168def join_formatted_strings(
169 builder: IRBuilder, literals: list[str] | None, substitutions: list[Value], line: int
170) -> Value:
171 """Merge the list of literals and the list of substitutions
172 alternatively using 'str_build_op'.
173
174 `substitutions` is the result value of formatting conversions.
175
176 If the `literals` is set to None, we simply join the substitutions;
177 Otherwise, the `literals` is the literal substrings of the original
178 format string and its length should be exactly one more than
179 substitutions.
180
181 For example:
182 (1) 'This is a %s and the value is %d'
183 -> literals: ['This is a ', ' and the value is', '']
184 (2) '{} and the value is {}'
185 -> literals: ['', ' and the value is', '']
186 """
187 # The first parameter for str_build_op is the total size of
188 # the following PyObject*
189 result_list: list[Value] = [Integer(0, c_pyssize_t_rprimitive)]
190
191 if literals is not None:
192 for a, b in zip(literals, substitutions):
193 if a:
194 result_list.append(builder.load_str(a))
195 result_list.append(b)
196 if literals[-1]:
197 result_list.append(builder.load_str(literals[-1]))
198 else:
199 result_list.extend(substitutions)
200
201 # Special case for empty string and literal string
202 if len(result_list) == 1:
203 return builder.load_str("")
204 if not substitutions and len(result_list) == 2:
205 return result_list[1]
206
207 result_list[0] = Integer(len(result_list) - 1, c_pyssize_t_rprimitive)
208 return builder.call_c(str_build_op, result_list, line)
209
210
211def convert_format_expr_to_bytes(

Callers 3

translate_str_formatFunction · 0.90
translate_fstringFunction · 0.90

Calls 7

IntegerClass · 0.90
zipFunction · 0.85
lenFunction · 0.85
appendMethod · 0.80
extendMethod · 0.80
load_strMethod · 0.45
call_cMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…