@@ -688,7 +688,6 @@ def make_naked(string: str, string_prefix: str) -> str:
688688 # NS: naked string
689689 # SS: next string
690690 # NSS: naked next string
691- S = ""
692691 NS = ""
693692 num_of_strings = 0
694693 next_str_idx = string_idx
@@ -709,14 +708,20 @@ def make_naked(string: str, string_prefix: str) -> str:
709708 has_prefix = bool (next_prefix )
710709 prefix_tracker .append (has_prefix )
711710
712- S = prefix + QUOTE + NS + NSS + BREAK_MARK + QUOTE
713- NS = make_naked (S , prefix )
711+ # Each NSS is already naked (prefix and quotes stripped, inner quotes
712+ # escaped, f-string expression quotes toggled), and the parts are
713+ # separated by BREAK_MARK which contains no quote or backslash, so the
714+ # naked group is just their concatenation. Re-running make_naked over the
715+ # whole accumulated string on every iteration rescans all previously
716+ # merged substrings, which is quadratic in the size of the group.
717+ NS = NS + NSS + BREAK_MARK
714718
715719 next_str_idx += 1
716720
717721 # Take a note on the index of the non-STRING leaf.
718722 non_string_idx = next_str_idx
719723
724+ S = prefix + QUOTE + NS + QUOTE
720725 S_leaf = Leaf (token .STRING , S )
721726 if self .normalize_strings :
722727 S_leaf .value = normalize_string_quotes (S_leaf .value )
0 commit comments