[meld] Split MyersSequenceMatcher pre-processing steps for future subclassing



commit f3c1d0fee44ee699434c17303011ce2643d8922b
Author: Piotr Piastucki <leech miranda gmail com>
Date:   Sun May 20 10:00:32 2012 +1000

    Split MyersSequenceMatcher pre-processing steps for future subclassing

 meld/matchers.py |   40 ++++++++++++++++++++--------------------
 1 files changed, 20 insertions(+), 20 deletions(-)
---
diff --git a/meld/matchers.py b/meld/matchers.py
index f92d743..9182b4e 100644
--- a/meld/matchers.py
+++ b/meld/matchers.py
@@ -74,36 +74,27 @@ class MyersSequenceMatcher(difflib.SequenceMatcher):
     def get_difference_opcodes(self):
         return filter(lambda x: x[0] != "equal", self.get_opcodes())
 
-    def preprocess(self):
-        """
-        Pre-processing optimizations:
-        1) remove common prefix and common suffix
-        2) remove lines that do not match
-        """
-        a = self.a
-        b = self.b
-        aindex = self.aindex = {}
-        bindex = self.bindex = {}
-        n = len(a)
-        m = len(b)
+    def preprocess_remove_prefix_suffix(self, a, b):
         # remove common prefix and common suffix
         self.common_prefix = self.common_suffix = 0
         self.common_prefix = find_common_prefix(a, b)
         if self.common_prefix > 0:
             a = a[self.common_prefix:]
             b = b[self.common_prefix:]
-            n -= self.common_prefix
-            m -= self.common_prefix
 
-        if n > 0 and m > 0:
+        if len(a) > 0 and len(b) > 0:
             self.common_suffix = find_common_suffix(a, b)
             if self.common_suffix > 0:
-                a = a[:n - self.common_suffix]
-                b = b[:m - self.common_suffix]
-                n -= self.common_suffix
-                m -= self.common_suffix
-
+                a = a[:len(a) - self.common_suffix]
+                b = b[:len(b) - self.common_suffix]
+        return (a, b)
+    
+    def preprocess_discard_nonmatching_lines(self, a, b):
         # discard lines that do not match any line from the other file
+        aindex = self.aindex = {}
+        bindex = self.bindex = {}
+        n = len(a)
+        m = len(b)
         if n > 0 and m > 0:
             aset = frozenset(a)
             bset = frozenset(b)
@@ -129,6 +120,15 @@ class MyersSequenceMatcher(difflib.SequenceMatcher):
                 b = b2
         return (a, b)
 
+    def preprocess(self):
+        """
+        Pre-processing optimizations:
+        1) remove common prefix and common suffix
+        2) remove lines that do not match
+        """
+        a, b = self.preprocess_remove_prefix_suffix(self.a, self.b)
+        return self.preprocess_discard_nonmatching_lines(a, b)
+
     def postprocess(self):
         mb = [self.matching_blocks[-1]]
         i = len(self.matching_blocks) - 2



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]