Skip to content

Commit

Permalink
Optimize regular expression for identifying line breaks in comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
andialbrecht committed Sep 10, 2021
1 parent e660467 commit 8238a9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion sqlparse/filters/others.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def get_next_comment():
def _get_insert_token(token):
"""Returns either a whitespace or the line breaks from token."""
# See issue484 why line breaks should be preserved.
m = re.search(r'((\r\n|\r|\n)+) *$', token.value)
# Note: The actual value for a line break is replaced by \n
# in SerializerUnicode which will be executed in the
# postprocessing state.
m = re.search(r'((\r|\n)+) *$', token.value)
if m is not None:
return sql.Token(T.Whitespace.Newline, m.groups()[0])
else:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ def test_strip_comments_multi(self):
res = sqlparse.format(sql, strip_comments=True)
assert res == 'select (select 2)'

def test_strip_comments_preserves_linebreak(self):
sql = 'select * -- a comment\r\nfrom foo'
res = sqlparse.format(sql, strip_comments=True)
assert res == 'select *\nfrom foo'
sql = 'select * -- a comment\nfrom foo'
res = sqlparse.format(sql, strip_comments=True)
assert res == 'select *\nfrom foo'
sql = 'select * -- a comment\rfrom foo'
res = sqlparse.format(sql, strip_comments=True)
assert res == 'select *\nfrom foo'
sql = 'select * -- a comment\r\n\r\nfrom foo'
res = sqlparse.format(sql, strip_comments=True)
assert res == 'select *\n\nfrom foo'
sql = 'select * -- a comment\n\nfrom foo'
res = sqlparse.format(sql, strip_comments=True)
assert res == 'select *\n\nfrom foo'

def test_strip_ws(self):
f = lambda sql: sqlparse.format(sql, strip_whitespace=True)
s = 'select\n* from foo\n\twhere ( 1 = 2 )\n'
Expand Down

0 comments on commit 8238a9e

Please sign in to comment.