Commit eea27293 authored by Stefan Behnel's avatar Stefan Behnel

speed up character switching in f-string parser a little

parent 9020149c
...@@ -70,7 +70,7 @@ cdef bint check_for_non_ascii_characters(unicode string) ...@@ -70,7 +70,7 @@ cdef bint check_for_non_ascii_characters(unicode string)
cdef p_string_literal(PyrexScanner s, kind_override=*) cdef p_string_literal(PyrexScanner s, kind_override=*)
@cython.locals(i=Py_ssize_t, size=Py_ssize_t) @cython.locals(i=Py_ssize_t, size=Py_ssize_t)
cdef list p_f_string(PyrexScanner s, unicode_value, pos) cdef list p_f_string(PyrexScanner s, unicode_value, pos)
@cython.locals(i=Py_ssize_t, size=Py_ssize_t) @cython.locals(i=Py_ssize_t, size=Py_ssize_t, c=Py_UCS4, quote_char=Py_UCS4)
cdef tuple p_f_string_expr(PyrexScanner s, unicode_value, pos, Py_ssize_t starting_index) cdef tuple p_f_string_expr(PyrexScanner s, unicode_value, pos, Py_ssize_t starting_index)
cdef p_list_maker(PyrexScanner s) cdef p_list_maker(PyrexScanner s)
cdef p_comp_iter(PyrexScanner s, body) cdef p_comp_iter(PyrexScanner s, body)
......
...@@ -1032,9 +1032,10 @@ def p_f_string_expr(s, unicode_value, pos, starting_index): ...@@ -1032,9 +1032,10 @@ def p_f_string_expr(s, unicode_value, pos, starting_index):
size = len(unicode_value) size = len(unicode_value)
conversion_char = terminal_char = format_spec = None conversion_char = terminal_char = format_spec = None
format_spec_str = u'' format_spec_str = u''
NO_CHAR = 2**30
nested_depth = 0 nested_depth = 0
quote_char = None quote_char = NO_CHAR
in_triple_quotes = False in_triple_quotes = False
while True: while True:
...@@ -1042,17 +1043,17 @@ def p_f_string_expr(s, unicode_value, pos, starting_index): ...@@ -1042,17 +1043,17 @@ def p_f_string_expr(s, unicode_value, pos, starting_index):
s.error("missing '}' in format string expression") s.error("missing '}' in format string expression")
c = unicode_value[i] c = unicode_value[i]
if quote_char is not None: if quote_char != NO_CHAR:
if c == '\\': if c == '\\':
i += 1 i += 1
elif c == quote_char: elif c == quote_char:
if in_triple_quotes: if in_triple_quotes:
if i + 2 < size and unicode_value[i + 1] == c and unicode_value[i + 2] == c: if i + 2 < size and unicode_value[i + 1] == c and unicode_value[i + 2] == c:
in_triple_quotes = False in_triple_quotes = False
quote_char = None quote_char = NO_CHAR
i += 2 i += 2
else: else:
quote_char = None quote_char = NO_CHAR
elif c in '\'"': elif c in '\'"':
quote_char = c quote_char = c
if i + 2 < size and unicode_value[i + 1] == c and unicode_value[i + 2] == c: if i + 2 < size and unicode_value[i + 1] == c and unicode_value[i + 2] == c:
...@@ -1103,11 +1104,12 @@ def p_f_string_expr(s, unicode_value, pos, starting_index): ...@@ -1103,11 +1104,12 @@ def p_f_string_expr(s, unicode_value, pos, starting_index):
if nested_depth >= 1: if nested_depth >= 1:
s.error("nesting of '{' in format specifier is not allowed") s.error("nesting of '{' in format specifier is not allowed")
nested_depth += 1 nested_depth += 1
elif c == '}' and nested_depth == 0:
terminal_char = c
break
elif c == '}': elif c == '}':
nested_depth -= 1 if nested_depth > 0:
nested_depth -= 1
else:
terminal_char = c
break
if c in '\'"': if c in '\'"':
if not in_string and i + 2 < size and unicode_value[i + 1] == c and unicode_value[i + 2] == c: if not in_string and i + 2 < size and unicode_value[i + 1] == c and unicode_value[i + 2] == c:
in_triple_quotes = not in_triple_quotes in_triple_quotes = not in_triple_quotes
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment