Refactor canonicalization.py strip_trailing_lines to avoid using re for more consistent processing across python versions

This commit is contained in:
Jonathan Bastien-Filiatrault
2018-12-11 14:34:34 -05:00
committed by Scott Kitterman
parent 84efc4a1cb
commit 7dee16a5b1
+13 -7
View File
@@ -41,15 +41,21 @@ def compress_whitespace(content):
def strip_trailing_lines(content): def strip_trailing_lines(content):
content = re.sub(b"(\r\n)*$", b"\r\n", content) end = None
# Yes, this is horrible, but regex processing changed in python3.7 and it while content[:end].endswith(b"\r\n"):
# is the least horrible solution I came up with for python2.7, python3.7, if end is None:
# and python3 << 3.7 combined support. Better solution welcome. end = -2
if len(content) >= 4: else:
if (content[len(content)-4:] == b'\r\n\r\n'): end -= 2
content = content[:len(content)-2]
if end is None:
return content + b"\r\n"
end += 2
if end == 0:
return content return content
return content[:end]
def unfold_header_value(content): def unfold_header_value(content):
return re.sub(b"\r\n", b"", content) return re.sub(b"\r\n", b"", content)