@@ -323,17 +323,24 @@ def _find_balanced_parens(self, code: str, open_paren_pos: int) -> tuple[str | N
323323 in_string = False
324324 string_char = None
325325
326- while pos < len (code ) and depth > 0 :
327- char = code [pos ]
326+ s = code # local alias for speed
327+ s_len = len (s )
328+ quotes = "\" '`"
329+
330+ while pos < s_len and depth > 0 :
331+ char = s [pos ]
328332
329333 # Handle string literals
330- if char in "\" '`" and (pos == 0 or code [pos - 1 ] != "\\ " ):
331- if not in_string :
332- in_string = True
333- string_char = char
334- elif char == string_char :
335- in_string = False
336- string_char = None
334+ # Note: preserve original escaping semantics (only checks immediate preceding char)
335+ if char in quotes :
336+ prev_char = s [pos - 1 ] if pos > 0 else None
337+ if prev_char != "\\ " :
338+ if not in_string :
339+ in_string = True
340+ string_char = char
341+ elif char == string_char :
342+ in_string = False
343+ string_char = None
337344 elif not in_string :
338345 if char == "(" :
339346 depth += 1
@@ -345,7 +352,8 @@ def _find_balanced_parens(self, code: str, open_paren_pos: int) -> tuple[str | N
345352 if depth != 0 :
346353 return None , - 1
347354
348- return code [open_paren_pos + 1 : pos - 1 ], pos
355+ # slice once
356+ return s [open_paren_pos + 1 : pos - 1 ], pos
349357
350358 def _parse_bracket_standalone_call (self , code : str , match : re .Match ) -> StandaloneCallMatch | None :
351359 """Parse a complete standalone obj['func'](...) call with bracket notation."""
@@ -367,10 +375,12 @@ def _parse_bracket_standalone_call(self, code: str, match: re.Match) -> Standalo
367375 # Check for trailing semicolon
368376 end_pos = close_pos
369377 # Skip whitespace
370- while end_pos < len (code ) and code [end_pos ] in " \t " :
378+ s = code
379+ s_len = len (s )
380+ while end_pos < s_len and s [end_pos ] in " \t " :
371381 end_pos += 1
372382
373- has_trailing_semicolon = end_pos < len ( code ) and code [end_pos ] == ";"
383+ has_trailing_semicolon = end_pos < s_len and s [end_pos ] == ";"
374384 if has_trailing_semicolon :
375385 end_pos += 1
376386
0 commit comments