Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.

Commit 01426e2

Browse files
committed
Add fixes for Java client
1 parent 461b008 commit 01426e2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

java/src/name/fraser/neil/plaintext/diff_match_patch.java

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package name.fraser.neil.plaintext;
2020

2121
import java.io.UnsupportedEncodingException;
22+
import java.lang.Character;
2223
import java.net.URLDecoder;
2324
import java.net.URLEncoder;
2425
import java.util.*;
@@ -1429,7 +1430,22 @@ public int diff_levenshtein(List<Diff> diffs) {
14291430
*/
14301431
public String diff_toDelta(List<Diff> diffs) {
14311432
StringBuilder text = new StringBuilder();
1433+
char lastEnd = 0;
14321434
for (Diff aDiff : diffs) {
1435+
1436+
char thisTop = aDiff.text.charAt(0);
1437+
char thisEnd = aDiff.text.charAt(aDiff.text.length() - 1);
1438+
1439+
if (Character.isHighSurrogate(thisEnd)) {
1440+
aDiff.text = aDiff.text.substring(0, aDiff.text.length() - 1);
1441+
}
1442+
1443+
if (0 != lastEnd && Character.isHighSurrogate(lastEnd) && Character.isLowSurrogate(thisTop)) {
1444+
aDiff.text = lastEnd + aDiff.text;
1445+
}
1446+
1447+
lastEnd = thisEnd;
1448+
14331449
switch (aDiff.operation) {
14341450
case INSERT:
14351451
try {

java/tests/name/fraser/neil/plaintext/diff_match_patch_test.java

+4
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,10 @@ public static void testDiffDelta() {
424424

425425
assertEquals("diff_fromDelta: Unicode.", diffs, dmp.diff_fromDelta(text1, delta));
426426

427+
diffs = diffList(new Diff(EQUAL, "\ud83d\ude4b\ud83d"), new Diff(INSERT, "\ude4c\ud83d"), new Diff(EQUAL, "\ude4b"));
428+
delta = dmp.diff_toDelta(diffs);
429+
assertEquals("diff_toDelta: Surrogate Pairs.", "=2\t+%F0%9F%99%8C\t=2", delta);
430+
427431
// Verify pool of unchanged characters.
428432
diffs = diffList(new Diff(INSERT, "A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # "));
429433
String text2 = dmp.diff_text2(diffs);

0 commit comments

Comments
 (0)