Use byte replacement strategy

This commit is contained in:
mvdbeek
2019-05-17 15:41:13 -04:00
committed by John Chilton
parent 6887398a49
commit 06aa7d21a8
+18 -2
View File
@@ -112,8 +112,24 @@ def convert_newlines(fname, in_place=True, tmp_dir=None, tmp_prefix="gxupload",
i = 0
with io.open(fd, mode="wb") as fp:
with io.open(fname, mode="rb") as fi:
for i, line in enumerate(fi):
fp.write(b"%s\n" % line.rstrip(b"\r\n"))
line = b''
clean_line = b""
last_char = None
block = fi.read(block_size)
while block:
if last_char == "\r" and block.startswith(b"\n"):
# last block ended with "\r", new block startswith "\n"
# since we replace "\r" with "\n" in the previous iteration we skip the first byte
block = block[1:]
# splitlines(True) splits at line terminators but keeps them so we can replace them
lines = block.splitlines(True)
for line in lines:
clean_line = line.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
fp.write(clean_line)
last_char = util.unicodify(line, error='replace')[-1]
block = fi.read(block_size)
if not clean_line.endswith(b"\n"):
fp.write(b"\n")
if in_place:
shutil.move(temp_name, fname)
# Return number of lines in file.