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

Commit 71646fb

Browse files
committed
Add fixes for Java client
1 parent 461b008 commit 71646fb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

+18
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,24 @@ 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;
1434+
boolean isFirst = true;
14321435
for (Diff aDiff : diffs) {
1436+
1437+
char thisTop = aDiff.text.charAt(0);
1438+
char thisEnd = aDiff.text.charAt(aDiff.text.length() - 1);
1439+
1440+
if (Character.isHighSurrogate(thisEnd)) {
1441+
aDiff.text = aDiff.text.substring(0, aDiff.text.length() - 1);
1442+
}
1443+
1444+
if (! isFirst && Character.isHighSurrogate(lastEnd) && Character.isLowSurrogate(thisTop)) {
1445+
aDiff.text = lastEnd + aDiff.text;
1446+
}
1447+
1448+
isFirst = false;
1449+
lastEnd = thisEnd;
1450+
14331451
switch (aDiff.operation) {
14341452
case INSERT:
14351453
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)