[jsword-svn] r1689 - in trunk/common/src: main/java/org/crosswire/common/diff test/java/org/crosswire/common/diff

dmsmith at www.crosswire.org dmsmith at www.crosswire.org
Wed Aug 22 08:56:00 MST 2007


Author: dmsmith
Date: 2007-08-22 08:55:59 -0700 (Wed, 22 Aug 2007)
New Revision: 1689

Modified:
   trunk/common/src/main/java/org/crosswire/common/diff/Match.java
   trunk/common/src/main/java/org/crosswire/common/diff/Patch.java
   trunk/common/src/test/java/org/crosswire/common/diff/PatchTest.java
Log:
Another bug fix in Diff.

Modified: trunk/common/src/main/java/org/crosswire/common/diff/Match.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/diff/Match.java	2007-08-22 13:34:22 UTC (rev 1688)
+++ trunk/common/src/main/java/org/crosswire/common/diff/Match.java	2007-08-22 15:55:59 UTC (rev 1689)
@@ -66,18 +66,18 @@
      */
     public int locate()
     {
+        if (text.equals(pattern))
+        {
+            // Shortcut (potentially not guaranteed by the algorithm)
+            return 0;
+        }
+
         if (text.length() == 0)
         {
             // Nothing to match.
             return -1;
         }
 
-        if (text.equals(pattern))
-        {
-            // Shortcut (potentially not guaranteed by the algorithm)
-            return 0;
-        }
-
         loc = Math.max(0, Math.min(loc, text.length() - pattern.length()));
         if (text.substring(loc, loc + pattern.length()).equals(pattern))
         {

Modified: trunk/common/src/main/java/org/crosswire/common/diff/Patch.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/diff/Patch.java	2007-08-22 13:34:22 UTC (rev 1688)
+++ trunk/common/src/main/java/org/crosswire/common/diff/Patch.java	2007-08-22 15:55:59 UTC (rev 1689)
@@ -25,6 +25,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 /**
@@ -419,20 +420,30 @@
     public Patch fromText(String input)
     {
         patches.clear();
-        String[] text = patchBoundaryPattern.split(input);
-        StringBuffer buf = new StringBuffer();
-        for (int patchCount = 0; patchCount < text.length; patchCount++)
+
+        Matcher m = patchBoundaryPattern.matcher(input);
+
+        // Add segments before each match found
+        int index = 0;
+        while (m.find())
         {
-            // Splitting removed @@ from the start of patches,
-            // so it needs to be added back in.
-            if (patchCount > 0)
-            {
-                // re-use the string buffer by using replace
-                buf.replace(0, buf.length(), "@@").append(text[patchCount]); //$NON-NLS-1$
-                text[patchCount] = buf.toString();
-            }
-            patches.add(new PatchEntry(text[patchCount]));
+            int start = m.start();
+            String match = input.substring(index, start);
+            patches.add(new PatchEntry(match));
+            index = start + 1;
         }
+
+        if (index == 0)
+        {
+            // No match was found, the patch consists of the entire string
+            patches.add(new PatchEntry(input));
+        }
+        else
+        {
+            // Add remaining segment
+            patches.add(new PatchEntry(input.substring(index)));
+        }
+
         return this;
     }
 

Modified: trunk/common/src/test/java/org/crosswire/common/diff/PatchTest.java
===================================================================
--- trunk/common/src/test/java/org/crosswire/common/diff/PatchTest.java	2007-08-22 13:34:22 UTC (rev 1688)
+++ trunk/common/src/test/java/org/crosswire/common/diff/PatchTest.java	2007-08-22 15:55:59 UTC (rev 1689)
@@ -61,6 +61,18 @@
         boolArray = results.getResults();
         resultStr = results.getText() + "\t" + boolArray[0] + "\t" + boolArray[1]; //$NON-NLS-1$ //$NON-NLS-2$
         assertEquals("patch_apply: Failed match.", "I am the very model of a modern major general.\tfalse\tfalse", resultStr); //$NON-NLS-1$ //$NON-NLS-2$
+
+        p = new Patch("", "test"); //$NON-NLS-1$ //$NON-NLS-2$
+        results = p.apply(""); //$NON-NLS-1$
+        boolArray = results.getResults();
+        resultStr = results.getText() + "\t" + boolArray[0]; //$NON-NLS-1$
+        assertEquals("patch_apply: Exact match against empty string.", "test\ttrue", resultStr); //$NON-NLS-1$ //$NON-NLS-2$
+
+        p = new Patch("test", ""); //$NON-NLS-1$ //$NON-NLS-2$
+        results = p.apply("test"); //$NON-NLS-1$
+        boolArray = results.getResults();
+        resultStr = results.getText() + "\t" + boolArray[0]; //$NON-NLS-1$
+        assertEquals("patch_apply: Exact match against empty string.", "\ttrue", resultStr); //$NON-NLS-1$ //$NON-NLS-2$
     }
   
 }




More information about the jsword-svn mailing list