changeset 962:930c9e82a60b

Optimised diff for text files.
author Alexander Schremmer <alex AT alexanderweb DOT de>
date Sat, 01 Jul 2006 01:47:42 +0200
parents 21eb4cb11e2c
children 1083861bd16c
files MoinMoin/util/bdiff.py
diffstat 1 files changed, 19 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/util/bdiff.py	Sat Jul 01 01:28:46 2006 +0200
+++ b/MoinMoin/util/bdiff.py	Sat Jul 01 01:47:42 2006 +0200
@@ -19,9 +19,12 @@
     return zlib.decompress(bin)
 
 def diff(a, b):
-    """ Generates a binary diff of the passed strings. """
+    """ Generates a binary diff of the passed strings.
+        Note that you can pass arrays of strings as well.
+        This might give you better results for text files. """
     if not a:
-        return b and (struct.pack(BDIFF_PATT, 0, 0, len(b)) + b)
+        s = "".join(b)
+        return s and (struct.pack(BDIFF_PATT, 0, 0, len(s)) + s)
 
     bin = []
     la = lb = 0
@@ -38,6 +41,10 @@
     
     return "".join(bin)
 
+def textdiff(a, b):
+    """ A diff function optimised for text files. Works with binary files as well. """
+    return diff(a.splitlines(1), b.splitlines(1))
+
 def patchtext(bin):
     """ Returns the new hunks that are contained in a binary diff."""
     pos = 0
@@ -67,13 +74,18 @@
     return "".join(r)
 
 def test():
-    a = "fo" * 30
-    b = "br" * 30
+    a = ("foo\n" * 30)
+    b = ("  fao" * 30)
+    
+    a = file(r"C:\Dokumente und Einstellungen\Administrator\Eigene Dateien\Progra\Python\MoinMoin\moin-1.6-sync\MoinMoin\util\test.1").read()
+    b = file(r"C:\Dokumente und Einstellungen\Administrator\Eigene Dateien\Progra\Python\MoinMoin\moin-1.6-sync\MoinMoin\util\test.2").read()
+    a = a.splitlines(1)
+    b = b.splitlines(1)
+    
     d = diff(a, b)
     z = compress(d)
     print `patchtext(d)`
-    #print `d`
-    print b == patch(a, d)
+    print `d`
+    print "".join(b) == patch("".join(a), d)
     print len(d), len(z)
 
-test()
\ No newline at end of file