mercurial/pure/bdiff.py
changeset 29833 a8933d992a71
parent 28389 9ab45fbe045e
child 29834 1ea77b75d266
equal deleted inserted replaced
29832:bac1829ec31f 29833:a8933d992a71
     9 
     9 
    10 import array
    10 import array
    11 import difflib
    11 import difflib
    12 import re
    12 import re
    13 import struct
    13 import struct
       
    14 
       
    15 from . import policy
       
    16 policynocffi = policy.policynocffi
       
    17 modulepolicy = policy.policy
    14 
    18 
    15 def splitnewlines(text):
    19 def splitnewlines(text):
    16     '''like str.splitlines, but only split on newlines.'''
    20     '''like str.splitlines, but only split on newlines.'''
    17     lines = [l + '\n' for l in text.split('\n')]
    21     lines = [l + '\n' for l in text.split('\n')]
    18     if lines:
    22     if lines:
    94         text = re.sub('[ \t\r]+', '', text)
    98         text = re.sub('[ \t\r]+', '', text)
    95     else:
    99     else:
    96         text = re.sub('[ \t\r]+', ' ', text)
   100         text = re.sub('[ \t\r]+', ' ', text)
    97         text = text.replace(' \n', '\n')
   101         text = text.replace(' \n', '\n')
    98     return text
   102     return text
       
   103 
       
   104 if modulepolicy not in policynocffi:
       
   105     try:
       
   106         from _bdiff_cffi import ffi, lib
       
   107     except ImportError:
       
   108         if modulepolicy == 'cffi': # strict cffi import
       
   109             raise
       
   110     else:
       
   111         def blocks(sa, sb):
       
   112             a = ffi.new("struct bdiff_line**")
       
   113             b = ffi.new("struct bdiff_line**")
       
   114             ac = ffi.new("char[]", sa)
       
   115             bc = ffi.new("char[]", sb)
       
   116             try:
       
   117                 an = lib.bdiff_splitlines(ac, len(sa), a)
       
   118                 bn = lib.bdiff_splitlines(bc, len(sb), b)
       
   119                 if not a[0] or not b[0]:
       
   120                     raise MemoryError
       
   121                 l = ffi.new("struct bdiff_hunk*")
       
   122                 count = lib.bdiff_diff(a[0], an, b[0], bn, l)
       
   123                 if count < 0:
       
   124                     raise MemoryError
       
   125                 rl = [None] * count
       
   126                 h = l.next
       
   127                 i = 0
       
   128                 while h:
       
   129                     rl[i] = (h.a1, h.a2, h.b1, h.b2)
       
   130                     h = h.next
       
   131                     i += 1
       
   132             finally:
       
   133                 lib.free(a[0])
       
   134                 lib.free(b[0])
       
   135                 lib.bdiff_freehunks(l.next)
       
   136             return rl