Skip to content

Commit 3ed61b4

Browse files
authored
Merge pull request python#56 from isidentical/dont-peek-unnecesarily
Don't peek unnecessarily when the first two quotes doesn't match
2 parents 7b9b973 + 9b1b8a8 commit 3ed61b4

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

Parser/tokenizer.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,8 +2435,6 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
24352435
// before it.
24362436
char start_char = tok_nextc(tok);
24372437
char peek1 = tok_nextc(tok);
2438-
char peek2 = tok_nextc(tok);
2439-
tok_backup(tok, peek2);
24402438
tok_backup(tok, peek1);
24412439
tok_backup(tok, start_char);
24422440

@@ -2448,27 +2446,28 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
24482446
return tok_get_normal_mode(tok, current_tok, token);
24492447
}
24502448

2451-
// Emit FSTRING_END in case we've reached the end of the string
2452-
if (start_char == current_tok->f_string_quote
2453-
&& (current_tok->f_string_quote_size != 3 || (start_char == peek1 && start_char == peek2))) {
2454-
// Advance the tokenizer state again to create a token out of the end quotes
2455-
for (int i = 0; i < current_tok->f_string_quote_size; i++) {
2456-
tok_nextc(tok);
2457-
}
2458-
2459-
if (current_tok->last_expr_buffer != NULL) {
2460-
PyMem_Free(current_tok->last_expr_buffer);
2461-
current_tok->last_expr_buffer = NULL;
2462-
current_tok->last_expr_size = 0;
2463-
current_tok->last_expr_end = -1;
2449+
// Check if we are at the end of the string
2450+
for (int i = 0; i < current_tok->f_string_quote_size; i++) {
2451+
char quote = tok_nextc(tok);
2452+
if (quote != current_tok->f_string_quote) {
2453+
tok_backup(tok, quote);
2454+
goto f_string_middle;
24642455
}
2456+
}
24652457

2466-
p_start = tok->start;
2467-
p_end = tok->cur;
2468-
tok->tok_mode_stack_index--;
2469-
return MAKE_TOKEN(FSTRING_END);
2458+
if (current_tok->last_expr_buffer != NULL) {
2459+
PyMem_Free(current_tok->last_expr_buffer);
2460+
current_tok->last_expr_buffer = NULL;
2461+
current_tok->last_expr_size = 0;
2462+
current_tok->last_expr_end = -1;
24702463
}
24712464

2465+
p_start = tok->start;
2466+
p_end = tok->cur;
2467+
tok->tok_mode_stack_index--;
2468+
return MAKE_TOKEN(FSTRING_END);
2469+
2470+
f_string_middle:
24722471
int end_quote_size = 0;
24732472
int unicode_escape = 0;
24742473
while (end_quote_size != current_tok->f_string_quote_size) {

0 commit comments

Comments
 (0)