Skip to content

Commit 8238a9e

Browse files
committed
Optimize regular expression for identifying line breaks in comments.
1 parent e660467 commit 8238a9e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

sqlparse/filters/others.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ def get_next_comment():
2222
def _get_insert_token(token):
2323
"""Returns either a whitespace or the line breaks from token."""
2424
# See issue484 why line breaks should be preserved.
25-
m = re.search(r'((\r\n|\r|\n)+) *$', token.value)
25+
# Note: The actual value for a line break is replaced by \n
26+
# in SerializerUnicode which will be executed in the
27+
# postprocessing state.
28+
m = re.search(r'((\r|\n)+) *$', token.value)
2629
if m is not None:
2730
return sql.Token(T.Whitespace.Newline, m.groups()[0])
2831
else:

tests/test_format.py

+17
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ def test_strip_comments_multi(self):
8484
res = sqlparse.format(sql, strip_comments=True)
8585
assert res == 'select (select 2)'
8686

87+
def test_strip_comments_preserves_linebreak(self):
88+
sql = 'select * -- a comment\r\nfrom foo'
89+
res = sqlparse.format(sql, strip_comments=True)
90+
assert res == 'select *\nfrom foo'
91+
sql = 'select * -- a comment\nfrom foo'
92+
res = sqlparse.format(sql, strip_comments=True)
93+
assert res == 'select *\nfrom foo'
94+
sql = 'select * -- a comment\rfrom foo'
95+
res = sqlparse.format(sql, strip_comments=True)
96+
assert res == 'select *\nfrom foo'
97+
sql = 'select * -- a comment\r\n\r\nfrom foo'
98+
res = sqlparse.format(sql, strip_comments=True)
99+
assert res == 'select *\n\nfrom foo'
100+
sql = 'select * -- a comment\n\nfrom foo'
101+
res = sqlparse.format(sql, strip_comments=True)
102+
assert res == 'select *\n\nfrom foo'
103+
87104
def test_strip_ws(self):
88105
f = lambda sql: sqlparse.format(sql, strip_whitespace=True)
89106
s = 'select\n* from foo\n\twhere ( 1 = 2 )\n'

0 commit comments

Comments
 (0)