mercurial/pure/bdiff.py
changeset 43077 687b865b95ad
parent 43076 2372284d9457
child 45863 68aedad4c11c
equal deleted inserted replaced
43076:2372284d9457 43077:687b865b95ad
    12 import struct
    12 import struct
    13 
    13 
    14 
    14 
    15 def splitnewlines(text):
    15 def splitnewlines(text):
    16     '''like str.splitlines, but only split on newlines.'''
    16     '''like str.splitlines, but only split on newlines.'''
    17     lines = [l + '\n' for l in text.split('\n')]
    17     lines = [l + b'\n' for l in text.split(b'\n')]
    18     if lines:
    18     if lines:
    19         if lines[-1] == '\n':
    19         if lines[-1] == b'\n':
    20             lines.pop()
    20             lines.pop()
    21         else:
    21         else:
    22             lines[-1] = lines[-1][:-1]
    22             lines[-1] = lines[-1][:-1]
    23     return lines
    23     return lines
    24 
    24 
    58 def bdiff(a, b):
    58 def bdiff(a, b):
    59     a = bytes(a).splitlines(True)
    59     a = bytes(a).splitlines(True)
    60     b = bytes(b).splitlines(True)
    60     b = bytes(b).splitlines(True)
    61 
    61 
    62     if not a:
    62     if not a:
    63         s = "".join(b)
    63         s = b"".join(b)
    64         return s and (struct.pack(">lll", 0, 0, len(s)) + s)
    64         return s and (struct.pack(b">lll", 0, 0, len(s)) + s)
    65 
    65 
    66     bin = []
    66     bin = []
    67     p = [0]
    67     p = [0]
    68     for i in a:
    68     for i in a:
    69         p.append(p[-1] + len(i))
    69         p.append(p[-1] + len(i))
    71     d = difflib.SequenceMatcher(None, a, b).get_matching_blocks()
    71     d = difflib.SequenceMatcher(None, a, b).get_matching_blocks()
    72     d = _normalizeblocks(a, b, d)
    72     d = _normalizeblocks(a, b, d)
    73     la = 0
    73     la = 0
    74     lb = 0
    74     lb = 0
    75     for am, bm, size in d:
    75     for am, bm, size in d:
    76         s = "".join(b[lb:bm])
    76         s = b"".join(b[lb:bm])
    77         if am > la or s:
    77         if am > la or s:
    78             bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s)
    78             bin.append(struct.pack(b">lll", p[la], p[am], len(s)) + s)
    79         la = am + size
    79         la = am + size
    80         lb = bm + size
    80         lb = bm + size
    81 
    81 
    82     return "".join(bin)
    82     return b"".join(bin)
    83 
    83 
    84 
    84 
    85 def blocks(a, b):
    85 def blocks(a, b):
    86     an = splitnewlines(a)
    86     an = splitnewlines(a)
    87     bn = splitnewlines(b)
    87     bn = splitnewlines(b)
    90     return [(i, i + n, j, j + n) for (i, j, n) in d]
    90     return [(i, i + n, j, j + n) for (i, j, n) in d]
    91 
    91 
    92 
    92 
    93 def fixws(text, allws):
    93 def fixws(text, allws):
    94     if allws:
    94     if allws:
    95         text = re.sub('[ \t\r]+', '', text)
    95         text = re.sub(b'[ \t\r]+', b'', text)
    96     else:
    96     else:
    97         text = re.sub('[ \t\r]+', ' ', text)
    97         text = re.sub(b'[ \t\r]+', b' ', text)
    98         text = text.replace(' \n', '\n')
    98         text = text.replace(b' \n', b'\n')
    99     return text
    99     return text