patchutils.py: Speed improvement for hunk parsing (approximately by a factor of 2).

We don't need to keep the whole line in memory, the first character is sufficient.
Moreover, exception handling is faster in python than explicitly checking for None.
This commit is contained in:
Sebastian Lackner 2014-11-04 06:49:35 +01:00
parent 92fc5f6141
commit f66b06739e

View File

@ -218,27 +218,28 @@ def read_patch(filename):
raise PatchParserError("Empty hunk doesn't make sense.")
assert fp.read() == line
while srclines > 0 or dstlines > 0:
line = fp.read()
if line is None:
raise PatchParserError("Truncated patch.")
elif line.startswith(" "):
if srclines == 0 or dstlines == 0:
raise PatchParserError("Corrupted patch.")
srclines -= 1
dstlines -= 1
elif line.startswith("-"):
if srclines == 0:
raise PatchParserError("Corrupted patch.")
srclines -= 1
elif line.startswith("+"):
if dstlines == 0:
raise PatchParserError("Corrupted patch.")
dstlines -= 1
elif line.startswith("\\ "):
pass # ignore
else:
raise PatchParserError("Unexpected line in hunk.")
try:
while srclines > 0 or dstlines > 0:
line = fp.read()[0]
if line == " ":
if srclines == 0 or dstlines == 0:
raise PatchParserError("Corrupted patch.")
srclines -= 1
dstlines -= 1
elif line == "-":
if srclines == 0:
raise PatchParserError("Corrupted patch.")
srclines -= 1
elif line == "+":
if dstlines == 0:
raise PatchParserError("Corrupted patch.")
dstlines -= 1
elif line == "\\":
pass # ignore
else:
raise PatchParserError("Unexpected line in hunk.")
except TypeError: # triggered by None[0]
raise PatchParserError("Truncated patch.")
while True:
line = fp.peek()