Incorporate patch to support isolated CR

This commit is contained in:
mtkennerly 2018-10-13 19:44:02 -04:00
parent 2ab5832ce1
commit e694a6cfc2
2 changed files with 8 additions and 4 deletions

View file

@ -15,13 +15,13 @@ def fix_file(file_obj):
return 0 return 0
last_character = file_obj.read(1) last_character = file_obj.read(1)
# last_character will be '' for an empty file # last_character will be '' for an empty file
if last_character != b'\n' and last_character != b'': if last_character not in {b'\n', b'\r'} and last_character != b'':
# Needs this seek for windows, otherwise IOError # Needs this seek for windows, otherwise IOError
file_obj.seek(0, os.SEEK_END) file_obj.seek(0, os.SEEK_END)
file_obj.write(b'\n') file_obj.write(b'\n')
return 1 return 1
while last_character == b'\n' or last_character == b'\r': while last_character in {b'\n', b'\r'}:
# Deal with the beginning of the file # Deal with the beginning of the file
if file_obj.tell() == 1: if file_obj.tell() == 1:
# If we've reached the beginning of the file and it is all # If we've reached the beginning of the file and it is all
@ -38,8 +38,10 @@ def fix_file(file_obj):
# newlines. If we find extraneous newlines, then backtrack and trim them. # newlines. If we find extraneous newlines, then backtrack and trim them.
position = file_obj.tell() position = file_obj.tell()
remaining = file_obj.read() remaining = file_obj.read()
for sequence in [b'\n', b'\r\n']: for sequence in (b'\n', b'\r\n', b'\r'):
if remaining.startswith(sequence) and len(remaining) > len(sequence): if remaining == sequence:
return 0
elif remaining.startswith(sequence):
file_obj.seek(position + len(sequence)) file_obj.seek(position + len(sequence))
file_obj.truncate() file_obj.truncate()
return 1 return 1

View file

@ -17,6 +17,8 @@ TESTS = (
(b'\xe2\x98\x83', 1, b'\xe2\x98\x83\n'), (b'\xe2\x98\x83', 1, b'\xe2\x98\x83\n'),
(b'foo\r\n', 0, b'foo\r\n'), (b'foo\r\n', 0, b'foo\r\n'),
(b'foo\r\n\r\n\r\n', 1, b'foo\r\n'), (b'foo\r\n\r\n\r\n', 1, b'foo\r\n'),
(b'foo\r', 0, b'foo\r'),
(b'foo\r\r\r\r', 1, b'foo\r'),
) )