Skip to content
Myr

Re-use existing regex for whitespace detection

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Whitespace regex could also detect newlines, but since we're only matching the part from last newline, it's safer (and more readable) to use it here.

authored by Matěj Volf · committed by David Lord · parent a12ad40 · Browse files · 1 file+3 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+3 added−6 removed

  • other:+3 added−6 removed

1 file+3 added−6 removed

modified:src/jinja2/lexer.py+3 added-6 removed
505 unmodified lines
506507 # block suffix if trimming is enabled508 block_suffix_re = "\\n?" if environment.trim_blocks else ""509510 # If lstrip is enabled, it should not be applied if there is any511 # non-whitespace between the newline and block.512 self.lstrip_unless_re = c(r"[^ \t]") if environment.lstrip_blocks else None510 self.lstrip_blocks = environment.lstrip_blocks511512 self.newline_sequence = environment.newline_sequence513 self.keep_trailing_newline = environment.keep_trailing_newline514
179 unmodified lines
694695 statetokens = self.rules[stack[-1]]696 source_length = len(source)697 balancing_stack: t.List[str] = []700 lstrip_unless_re = self.lstrip_unless_re698 newlines_stripped = 0699 line_starting = True700701 while True:
37 unmodified lines
739 elif (740 # Not marked for preserving whitespace.741 strip_sign != "+"742 # lstrip is enabled.746 and lstrip_unless_re is not None743 and self.lstrip_blocks744 # Not a variable expression.745 and not m.groupdict().get(TOKEN_VARIABLE_BEGIN)746 ):747 # The start of text between the last newline and the tag.
1 unmodified line
749750 if l_pos > 0 or line_starting:751 # If there's only whitespace between the newline and the752 # tag, strip it.756 if not lstrip_unless_re.search(text, l_pos):753 if whitespace_re.fullmatch(text, l_pos):754 groups = [text[:l_pos], *groups[1:]]755756 for idx, token in enumerate(tokens):757 # failure group
109 unmodified lines