From 143d61d5db87c07c8a15e3aad0af2847a27b064d Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Tue, 30 Jan 2024 16:51:42 -0700 Subject: [PATCH 1/5] JavaScript: Stop breaking surrogate pairs in toDelta() Resolves Google/diff-match-patch#69 for JavaScript Sometimes we can find a common prefix that runs into the middle of a surrogate pair and we split that pair when building our diff groups. This is fine as long as we are operating on UTF-16 code units. It becomes problematic when we start trying to treat those substrings as valid Unicode (or UTF-8) sequences. When we pass these split groups into `toDelta()` we do just that and the library crashes. In this patch we're post-processing the diff groups before encoding them to make sure that we un-split the surrogate pairs. The post-processed diffs should produce the same output when applying the diffs. The diff string itself will be different but should change that much - only by a single character at surrogate boundaries. --- javascript/diff_match_patch.js | 109 ++++++------ javascript/diff_match_patch_uncompressed.js | 178 +++++++++++++++++++- javascript/tests/diff_match_patch_test.js | 130 ++++++++++++++ 3 files changed, 364 insertions(+), 53 deletions(-) diff --git a/javascript/diff_match_patch.js b/javascript/diff_match_patch.js index 2fe320a1..56b12cd8 100644 --- a/javascript/diff_match_patch.js +++ b/javascript/diff_match_patch.js @@ -1,55 +1,60 @@ var diff_match_patch=function(){this.Diff_Timeout=1;this.Diff_EditCost=4;this.Match_Threshold=.5;this.Match_Distance=1E3;this.Patch_DeleteThreshold=.5;this.Patch_Margin=4;this.Match_MaxBits=32},DIFF_DELETE=-1,DIFF_INSERT=1,DIFF_EQUAL=0;diff_match_patch.Diff=function(a,b){this[0]=a;this[1]=b};diff_match_patch.Diff.prototype.length=2;diff_match_patch.Diff.prototype.toString=function(){return this[0]+","+this[1]}; -diff_match_patch.prototype.diff_main=function(a,b,c,d){"undefined"==typeof d&&(d=0>=this.Diff_Timeout?Number.MAX_VALUE:(new Date).getTime()+1E3*this.Diff_Timeout);if(null==a||null==b)throw Error("Null input. (diff_main)");if(a==b)return a?[new diff_match_patch.Diff(DIFF_EQUAL,a)]:[];"undefined"==typeof c&&(c=!0);var e=c,f=this.diff_commonPrefix(a,b);c=a.substring(0,f);a=a.substring(f);b=b.substring(f);f=this.diff_commonSuffix(a,b);var g=a.substring(a.length-f);a=a.substring(0,a.length-f);b=b.substring(0, -b.length-f);a=this.diff_compute_(a,b,e,d);c&&a.unshift(new diff_match_patch.Diff(DIFF_EQUAL,c));g&&a.push(new diff_match_patch.Diff(DIFF_EQUAL,g));this.diff_cleanupMerge(a);return a}; -diff_match_patch.prototype.diff_compute_=function(a,b,c,d){if(!a)return[new diff_match_patch.Diff(DIFF_INSERT,b)];if(!b)return[new diff_match_patch.Diff(DIFF_DELETE,a)];var e=a.length>b.length?a:b,f=a.length>b.length?b:a,g=e.indexOf(f);return-1!=g?(c=[new diff_match_patch.Diff(DIFF_INSERT,e.substring(0,g)),new diff_match_patch.Diff(DIFF_EQUAL,f),new diff_match_patch.Diff(DIFF_INSERT,e.substring(g+f.length))],a.length>b.length&&(c[0][0]=c[2][0]=DIFF_DELETE),c):1==f.length?[new diff_match_patch.Diff(DIFF_DELETE, -a),new diff_match_patch.Diff(DIFF_INSERT,b)]:(e=this.diff_halfMatch_(a,b))?(b=e[1],f=e[3],a=e[4],e=this.diff_main(e[0],e[2],c,d),c=this.diff_main(b,f,c,d),e.concat([new diff_match_patch.Diff(DIFF_EQUAL,a)],c)):c&&100c);t++){for(var v=-t+p;v<=t-x;v+=2){var n=f+v;var r=v==-t||v!=t&&h[n-1]d)x+=2;else if(y>e)p+=2;else if(m&&(n=f+k-v,0<=n&&n= -u)return this.diff_bisectSplit_(a,b,r,y,c)}}for(v=-t+w;v<=t-q;v+=2){n=f+v;u=v==-t||v!=t&&l[n-1]d)q+=2;else if(r>e)w+=2;else if(!m&&(n=f+k-v,0<=n&&n=u)))return this.diff_bisectSplit_(a,b,r,y,c)}}return[new diff_match_patch.Diff(DIFF_DELETE,a),new diff_match_patch.Diff(DIFF_INSERT,b)]}; -diff_match_patch.prototype.diff_bisectSplit_=function(a,b,c,d,e){var f=a.substring(0,c),g=b.substring(0,d);a=a.substring(c);b=b.substring(d);f=this.diff_main(f,g,!1,e);e=this.diff_main(a,b,!1,e);return f.concat(e)}; -diff_match_patch.prototype.diff_linesToChars_=function(a,b){function c(a){for(var b="",c=0,g=-1,h=d.length;gd?a=a.substring(c-d):c=a.length?[h,k,l,m,g]:null}if(0>=this.Diff_Timeout)return null; -var d=a.length>b.length?a:b,e=a.length>b.length?b:a;if(4>d.length||2*e.lengthd[4].length?g:d:d:g;else return null;if(a.length>b.length){d=g[0];e=g[1];var h=g[2];var l=g[3]}else h=g[0],l=g[1],d=g[2],e=g[3];return[d,e,h,l,g[4]]}; -diff_match_patch.prototype.diff_cleanupSemantic=function(a){for(var b=!1,c=[],d=0,e=null,f=0,g=0,h=0,l=0,k=0;f=e){if(d>=b.length/2||d>=c.length/2)a.splice(f,0,new diff_match_patch.Diff(DIFF_EQUAL,c.substring(0,d))),a[f-1][1]=b.substring(0,b.length-d),a[f+1][1]=c.substring(d),f++}else if(e>=b.length/2||e>=c.length/2)a.splice(f,0,new diff_match_patch.Diff(DIFF_EQUAL,b.substring(0,e))),a[f-1][0]=DIFF_INSERT,a[f-1][1]=c.substring(0,c.length-e),a[f+1][0]=DIFF_DELETE, +diff_match_patch.prototype.diff_main=function(a,b,d,c){"undefined"==typeof c&&(c=0>=this.Diff_Timeout?Number.MAX_VALUE:(new Date).getTime()+1E3*this.Diff_Timeout);if(null==a||null==b)throw Error("Null input. (diff_main)");if(a==b)return a?[new diff_match_patch.Diff(DIFF_EQUAL,a)]:[];"undefined"==typeof d&&(d=!0);var e=d,f=this.diff_commonPrefix(a,b);d=a.substring(0,f);a=a.substring(f);b=b.substring(f);f=this.diff_commonSuffix(a,b);var g=a.substring(a.length-f);a=a.substring(0,a.length-f);b=b.substring(0, +b.length-f);a=this.diff_compute_(a,b,e,c);d&&a.unshift(new diff_match_patch.Diff(DIFF_EQUAL,d));g&&a.push(new diff_match_patch.Diff(DIFF_EQUAL,g));this.diff_cleanupMerge(a);return a}; +diff_match_patch.prototype.diff_compute_=function(a,b,d,c){if(!a)return[new diff_match_patch.Diff(DIFF_INSERT,b)];if(!b)return[new diff_match_patch.Diff(DIFF_DELETE,a)];var e=a.length>b.length?a:b,f=a.length>b.length?b:a,g=e.indexOf(f);return-1!=g?(d=[new diff_match_patch.Diff(DIFF_INSERT,e.substring(0,g)),new diff_match_patch.Diff(DIFF_EQUAL,f),new diff_match_patch.Diff(DIFF_INSERT,e.substring(g+f.length))],a.length>b.length&&(d[0][0]=d[2][0]=DIFF_DELETE),d):1==f.length?[new diff_match_patch.Diff(DIFF_DELETE, +a),new diff_match_patch.Diff(DIFF_INSERT,b)]:(e=this.diff_halfMatch_(a,b))?(b=e[1],f=e[3],a=e[4],e=this.diff_main(e[0],e[2],d,c),d=this.diff_main(b,f,d,c),e.concat([new diff_match_patch.Diff(DIFF_EQUAL,a)],d)):d&&100d);u++){for(var v=-u+n;v<=u-w;v+=2){var q=f+v;var t=v==-u||v!=u&&h[q-1]c)w+=2;else if(y>e)n+=2;else if(m&&(q=f+k-v,0<=q&&q= +x)return this.diff_bisectSplit_(a,b,t,y,d)}}for(v=-u+p;v<=u-r;v+=2){q=f+v;x=v==-u||v!=u&&l[q-1]c)r+=2;else if(t>e)p+=2;else if(!m&&(q=f+k-v,0<=q&&q=x)))return this.diff_bisectSplit_(a,b,t,y,d)}}return[new diff_match_patch.Diff(DIFF_DELETE,a),new diff_match_patch.Diff(DIFF_INSERT,b)]}; +diff_match_patch.prototype.diff_bisectSplit_=function(a,b,d,c,e){var f=a.substring(0,d),g=b.substring(0,c);a=a.substring(d);b=b.substring(c);f=this.diff_main(f,g,!1,e);e=this.diff_main(a,b,!1,e);return f.concat(e)}; +diff_match_patch.prototype.diff_linesToChars_=function(a,b){function d(l){for(var k="",m=0,n=-1,w=c.length;nc?a=a.substring(d-c):d=k.length?[u,v,q,t,r]:null}if(0>=this.Diff_Timeout)return null; +var c=a.length>b.length?a:b,e=a.length>b.length?b:a;if(4>c.length||2*e.lengthc[4].length?g:c:c:g;else return null;if(a.length>b.length){c=g[0];e=g[1];var h=g[2];var l=g[3]}else h=g[0],l=g[1],c=g[2],e=g[3];return[c,e,h,l,g[4]]}; +diff_match_patch.prototype.diff_cleanupSemantic=function(a){for(var b=!1,d=[],c=0,e=null,f=0,g=0,h=0,l=0,k=0;f=e){if(c>=b.length/2||c>=d.length/2)a.splice(f,0,new diff_match_patch.Diff(DIFF_EQUAL,d.substring(0,c))),a[f-1][1]=b.substring(0,b.length-c),a[f+1][1]=d.substring(c),f++}else if(e>=b.length/2||e>=d.length/2)a.splice(f,0,new diff_match_patch.Diff(DIFF_EQUAL,b.substring(0,e))),a[f-1][0]=DIFF_INSERT,a[f-1][1]=d.substring(0,d.length-e),a[f+1][0]=DIFF_DELETE, a[f+1][1]=b.substring(e),f++;f++}f++}}; -diff_match_patch.prototype.diff_cleanupSemanticLossless=function(a){function b(a,b){if(!a||!b)return 6;var c=a.charAt(a.length-1),d=b.charAt(0),e=c.match(diff_match_patch.nonAlphaNumericRegex_),f=d.match(diff_match_patch.nonAlphaNumericRegex_),g=e&&c.match(diff_match_patch.whitespaceRegex_),h=f&&d.match(diff_match_patch.whitespaceRegex_);c=g&&c.match(diff_match_patch.linebreakRegex_);d=h&&d.match(diff_match_patch.linebreakRegex_);var k=c&&a.match(diff_match_patch.blanklineEndRegex_),l=d&&b.match(diff_match_patch.blanklineStartRegex_); -return k||l?5:c||d?4:e&&!g&&h?3:g||h?2:e||f?1:0}for(var c=1;c=k&&(k=m,g=d,h=e,l=f)}a[c-1][1]!=g&&(g?a[c-1][1]=g:(a.splice(c- -1,1),c--),a[c][1]=h,l?a[c+1][1]=l:(a.splice(c+1,1),c--))}c++}};diff_match_patch.nonAlphaNumericRegex_=/[^a-zA-Z0-9]/;diff_match_patch.whitespaceRegex_=/\s/;diff_match_patch.linebreakRegex_=/[\r\n]/;diff_match_patch.blanklineEndRegex_=/\n\r?\n$/;diff_match_patch.blanklineStartRegex_=/^\r?\n\r?\n/; -diff_match_patch.prototype.diff_cleanupEfficiency=function(a){for(var b=!1,c=[],d=0,e=null,f=0,g=!1,h=!1,l=!1,k=!1;fb)break;e=c;f=d}return a.length!=g&&a[g][0]===DIFF_DELETE?f:f+(b-e)}; -diff_match_patch.prototype.diff_prettyHtml=function(a){for(var b=[],c=/&/g,d=//g,f=/\n/g,g=0;g");switch(h){case DIFF_INSERT:b[g]=''+l+"";break;case DIFF_DELETE:b[g]=''+l+"";break;case DIFF_EQUAL:b[g]=""+l+""}}return b.join("")}; -diff_match_patch.prototype.diff_text1=function(a){for(var b=[],c=0;cthis.Match_MaxBits)throw Error("Pattern too long for this browser.");var e=this.match_alphabet_(b),f=this,g=this.Match_Threshold,h=a.indexOf(b,c);-1!=h&&(g=Math.min(d(0,h),g),h=a.lastIndexOf(b,c+b.length),-1!=h&&(g=Math.min(d(0,h),g)));var l=1<=k;q--){var t=e[a.charAt(q-1)];m[q]=0===w?(m[q+1]<<1|1)&t:(m[q+1]<<1|1)&t|(x[q+1]|x[q])<<1|1|x[q+1];if(m[q]&l&&(t=d(w,q-1),t<=g))if(g=t,h=q-1,h>c)k=Math.max(1,2*c-h);else break}if(d(w+1,c)>g)break;x=m}return h}; -diff_match_patch.prototype.match_alphabet_=function(a){for(var b={},c=0;c=2*this.Patch_Margin&&e&&(this.patch_addContext_(a,h),c.push(a),a=new diff_match_patch.patch_obj,e=0,h=d,f=g)}k!==DIFF_INSERT&&(f+=m.length);k!==DIFF_DELETE&&(g+=m.length)}e&&(this.patch_addContext_(a,h),c.push(a));return c}; -diff_match_patch.prototype.patch_deepCopy=function(a){for(var b=[],c=0;cthis.Match_MaxBits){var k=this.match_main(b,h.substring(0,this.Match_MaxBits),g);-1!=k&&(l=this.match_main(b,h.substring(h.length-this.Match_MaxBits),g+h.length-this.Match_MaxBits),-1==l||k>=l)&&(k=-1)}else k=this.match_main(b,h, -g);if(-1==k)e[f]=!1,d-=a[f].length2-a[f].length1;else if(e[f]=!0,d=k-g,g=-1==l?b.substring(k,k+h.length):b.substring(k,l+this.Match_MaxBits),h==g)b=b.substring(0,k)+this.diff_text2(a[f].diffs)+b.substring(k+h.length);else if(g=this.diff_main(h,g,!1),h.length>this.Match_MaxBits&&this.diff_levenshtein(g)/h.length>this.Patch_DeleteThreshold)e[f]=!1;else{this.diff_cleanupSemanticLossless(g);h=0;var m;for(l=0;le[0][1].length){var f=b-e[0][1].length;e[0][1]=c.substring(e[0][1].length)+e[0][1];d.start1-=f;d.start2-=f;d.length1+=f;d.length2+=f}d=a[a.length-1];e=d.diffs; -0==e.length||e[e.length-1][0]!=DIFF_EQUAL?(e.push(new diff_match_patch.Diff(DIFF_EQUAL,c)),d.length1+=b,d.length2+=b):b>e[e.length-1][1].length&&(f=b-e[e.length-1][1].length,e[e.length-1][1]+=c.substring(0,f),d.length1+=f,d.length2+=f);return c}; -diff_match_patch.prototype.patch_splitMax=function(a){for(var b=this.Match_MaxBits,c=0;c2*b?(h.length1+=k.length,e+=k.length,l=!1,h.diffs.push(new diff_match_patch.Diff(g,k)),d.diffs.shift()):(k=k.substring(0,b-h.length1-this.Patch_Margin),h.length1+=k.length,e+=k.length,g===DIFF_EQUAL?(h.length2+=k.length,f+=k.length):l=!1,h.diffs.push(new diff_match_patch.Diff(g,k)),k==d.diffs[0][1]?d.diffs.shift():d.diffs[0][1]=d.diffs[0][1].substring(k.length))}g=this.diff_text2(h.diffs); -g=g.substring(g.length-this.Patch_Margin);k=this.diff_text1(d.diffs).substring(0,this.Patch_Margin);""!==k&&(h.length1+=k.length,h.length2+=k.length,0!==h.diffs.length&&h.diffs[h.diffs.length-1][0]===DIFF_EQUAL?h.diffs[h.diffs.length-1][1]+=k:h.diffs.push(new diff_match_patch.Diff(DIFF_EQUAL,k)));l||a.splice(++c,0,h)}}};diff_match_patch.prototype.patch_toText=function(a){for(var b=[],c=0;c=k&&(k=m,g=c,h=e,l=f)}a[d-1][1]!=g&&(g?a[d-1][1]=g:(a.splice(d- +1,1),d--),a[d][1]=h,l?a[d+1][1]=l:(a.splice(d+1,1),d--))}d++}};diff_match_patch.nonAlphaNumericRegex_=/[^a-zA-Z0-9]/;diff_match_patch.whitespaceRegex_=/\s/;diff_match_patch.linebreakRegex_=/[\r\n]/;diff_match_patch.blanklineEndRegex_=/\n\r?\n$/;diff_match_patch.blanklineStartRegex_=/^\r?\n\r?\n/; +diff_match_patch.prototype.diff_cleanupEfficiency=function(a){for(var b=!1,d=[],c=0,e=null,f=0,g=!1,h=!1,l=!1,k=!1;f=a}; +diff_match_patch.prototype.isLowSurrogate=function(a){a=a.charCodeAt(0);return 56320<=a&&57343>=a}; +diff_match_patch.prototype.digit16=function(a){switch(a){case "0":return 0;case "1":return 1;case "2":return 2;case "3":return 3;case "4":return 4;case "5":return 5;case "6":return 6;case "7":return 7;case "8":return 8;case "9":return 9;case "A":case "a":return 10;case "B":case "b":return 11;case "C":case "c":return 12;case "D":case "d":return 13;case "E":case "e":return 14;case "F":case "f":return 15;default:throw Error("Invalid hex-code");}}; +diff_match_patch.prototype.decodeURI=function(a){try{return decodeURI(a)}catch(h){for(var b=0,d="";b=c)){d+=String.fromCharCode((c&65535)>>>10&1023|55296);d+=String.fromCharCode(56320|c&1023); +b+=12;continue}throw new URIError("URI malformed");}}}}return d}};diff_match_patch.prototype.diff_xIndex=function(a,b){var d=0,c=0,e=0,f=0,g;for(g=0;gb)break;e=d;f=c}return a.length!=g&&a[g][0]===DIFF_DELETE?f:f+(b-e)}; +diff_match_patch.prototype.diff_prettyHtml=function(a){a=this.diff_cleanupSplitSurrogates(a);for(var b=[],d=/&/g,c=//g,f=/\n/g,g=0;g");switch(h){case DIFF_INSERT:b[g]=''+l+"";break;case DIFF_DELETE:b[g]=''+l+"";break;case DIFF_EQUAL:b[g]=""+l+""}}return b.join("")}; +diff_match_patch.prototype.diff_text1=function(a){for(var b=[],d=0;dthis.Match_MaxBits)throw Error("Pattern too long for this browser.");var e=this.match_alphabet_(b),f=this,g=this.Match_Threshold,h=a.indexOf(b,d);-1!=h&&(g=Math.min(c(0,h),g),h=a.lastIndexOf(b,d+b.length),-1!=h&&(g=Math.min(c(0,h),g)));var l=1<=k;r--){var u=e[a.charAt(r-1)];m[r]=0===p?(m[r+1]<<1|1)&u:(m[r+1]<<1|1)&u|(w[r+1]|w[r])<<1|1|w[r+1];if(m[r]&l&&(u=c(p,r-1),u<=g))if(g=u,h=r-1,h>d)k=Math.max(1,2*d-h);else break}if(c(p+1,d)>g)break;w=m}return h}; +diff_match_patch.prototype.match_alphabet_=function(a){for(var b={},d=0;d=2*this.Patch_Margin&&e&&(this.patch_addContext_(a,h),d.push(a),a=new diff_match_patch.patch_obj,e=0,h=c,f=g)}k!==DIFF_INSERT&&(f+=m.length);k!==DIFF_DELETE&&(g+=m.length)}e&&(this.patch_addContext_(a,h),d.push(a));return d}; +diff_match_patch.prototype.patch_deepCopy=function(a){for(var b=[],d=0;dthis.Match_MaxBits){var k=this.match_main(b,h.substring(0,this.Match_MaxBits),g);-1!=k&&(l=this.match_main(b,h.substring(h.length-this.Match_MaxBits),g+h.length-this.Match_MaxBits),-1==l||k>=l)&&(k=-1)}else k=this.match_main(b,h, +g);if(-1==k)e[f]=!1,c-=a[f].length2-a[f].length1;else if(e[f]=!0,c=k-g,g=-1==l?b.substring(k,k+h.length):b.substring(k,l+this.Match_MaxBits),h==g)b=b.substring(0,k)+this.diff_text2(a[f].diffs)+b.substring(k+h.length);else if(g=this.diff_main(h,g,!1),h.length>this.Match_MaxBits&&this.diff_levenshtein(g)/h.length>this.Patch_DeleteThreshold)e[f]=!1;else{this.diff_cleanupSemanticLossless(g);h=0;var m;for(l=0;le[0][1].length){var f=b-e[0][1].length;e[0][1]=d.substring(e[0][1].length)+e[0][1];c.start1-=f;c.start2-=f;c.length1+=f;c.length2+=f}c=a[a.length-1];e=c.diffs; +0==e.length||e[e.length-1][0]!=DIFF_EQUAL?(e.push(new diff_match_patch.Diff(DIFF_EQUAL,d)),c.length1+=b,c.length2+=b):b>e[e.length-1][1].length&&(f=b-e[e.length-1][1].length,e[e.length-1][1]+=d.substring(0,f),c.length1+=f,c.length2+=f);return d}; +diff_match_patch.prototype.patch_splitMax=function(a){for(var b=this.Match_MaxBits,d=0;d2*b?(h.length1+=k.length,e+=k.length,l=!1,h.diffs.push(new diff_match_patch.Diff(g,k)),c.diffs.shift()):(k=k.substring(0,b-h.length1-this.Patch_Margin),h.length1+=k.length,e+=k.length,g===DIFF_EQUAL?(h.length2+=k.length,f+=k.length):l=!1,h.diffs.push(new diff_match_patch.Diff(g,k)),k==c.diffs[0][1]?c.diffs.shift():c.diffs[0][1]=c.diffs[0][1].substring(k.length))}g=this.diff_text2(h.diffs); +g=g.substring(g.length-this.Patch_Margin);k=this.diff_text1(c.diffs).substring(0,this.Patch_Margin);""!==k&&(h.length1+=k.length,h.length2+=k.length,0!==h.diffs.length&&h.diffs[h.diffs.length-1][0]===DIFF_EQUAL?h.diffs[h.diffs.length-1][1]+=k:h.diffs.push(new diff_match_patch.Diff(DIFF_EQUAL,k)));l||a.splice(++d,0,h)}}};diff_match_patch.prototype.patch_toText=function(a){for(var b=[],d=0;d} diffs Array of diff tuples. + */ +diff_match_patch.prototype.diff_cleanupSplitSurrogates = function(diffs) { + var lastEnd; + for (var x = 0; x < diffs.length; x++) { + var thisDiff = diffs[x]; + var thisTop = thisDiff[1][0]; + var thisEnd = thisDiff[1][thisDiff[1].length - 1]; + + if (0 === thisDiff[1].length) { + diffs.splice(x--, 1); + continue; + } + + if (thisEnd && this.isHighSurrogate(thisEnd)) { + lastEnd = thisEnd; + thisDiff[1] = thisDiff[1].slice(0, -1); + } + + if (lastEnd && thisTop && this.isHighSurrogate(lastEnd) && this.isLowSurrogate(thisTop)) { + thisDiff[1] = lastEnd + thisDiff[1]; + } + + if (0 === thisDiff[1].length) { + diffs.splice(x--, 1); + continue; + } + } + + return diffs; +}; + +diff_match_patch.prototype.isHighSurrogate = function(c) { + var v = c.charCodeAt(0); + return v >= 0xD800 && v <= 0xDBFF; +}; + +diff_match_patch.prototype.isLowSurrogate = function(c) { + var v = c.charCodeAt(0); + return v >= 0xDC00 && v <= 0xDFFF; +}; + +diff_match_patch.prototype.digit16 = function(c) { + switch (c) { + case '0': return 0; + case '1': return 1; + case '2': return 2; + case '3': return 3; + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + case '8': return 8; + case '9': return 9; + case 'A': case 'a': return 10; + case 'B': case 'b': return 11; + case 'C': case 'c': return 12; + case 'D': case 'd': return 13; + case 'E': case 'e': return 14; + case 'F': case 'f': return 15; + default: throw new Error('Invalid hex-code'); + } +}; + +/** + * Decode URI-encoded string but allow for encoded surrogate halves + * + * diff_match_patch needs this relaxation of the requirements because + * not all libraries and versions produce valid URI strings in toDelta + * and we don't want to crash this code when the input is valid input + * but at the same time invalid utf-8 + * + * @example: decodeURI( 'abcd%3A %F0%9F%85%B0' ) = 'abcd: \ud83c\udd70' + * @example: decodeURI( 'abcd%3A %ED%A0%BC' ) = 'abcd: \ud83c' + * + * @cite: @mathiasbynens utf8.js at https://github.com/mathiasbynens/utf8.js + * + * @param {String} text input string encoded by encodeURI() or equivalent + * @return {String} + */ +diff_match_patch.prototype.decodeURI = function(text) { + try { + return decodeURI(text); + } catch ( e ) { + var i = 0; + var decoded = ''; + + while (i < text.length) { + if ( text[i] !== '%' ) { + decoded += text[i++]; + continue; + } + + // start a percent-sequence + var byte1 = (this.digit16(text[i + 1]) << 4) + this.digit16(text[i + 2]); + if ((byte1 & 0x80) === 0) { + decoded += String.fromCharCode(byte1); + i += 3; + continue; + } + + if ('%' !== text[i + 3]) { + throw new URIError('URI malformed'); + } + + var byte2 = (this.digit16(text[i + 4]) << 4) + this.digit16(text[i + 5]); + if ((byte2 & 0xC0) !== 0x80) { + throw new URIError('URI malformed'); + } + byte2 = byte2 & 0x3F; + if ((byte1 & 0xE0) === 0xC0) { + decoded += String.fromCharCode(((byte1 & 0x1F) << 6) | byte2); + i += 6; + continue; + } + + if ('%' !== text[i + 6]) { + throw new URIError('URI malformed'); + } + + var byte3 = (this.digit16(text[i + 7]) << 4) + this.digit16(text[i + 8]); + if ((byte3 & 0xC0) !== 0x80) { + throw new URIError('URI malformed'); + } + byte3 = byte3 & 0x3F; + if ((byte1 & 0xF0) === 0xE0) { + // unpaired surrogate are fine here + decoded += String.fromCharCode(((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3); + i += 9; + continue; + } + + if ('%' !== text[i + 9]) { + throw new URIError('URI malformed'); + } + + var byte4 = (this.digit16(text[i + 10]) << 4) + this.digit16(text[i + 11]); + if ((byte4 & 0xC0) !== 0x80) { + throw new URIError('URI malformed'); + } + byte4 = byte4 & 0x3F; + if ((byte1 & 0xF8) === 0xF0) { + var codePoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0C) | (byte3 << 0x06) | byte4; + if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) { + decoded += String.fromCharCode((codePoint & 0xFFFF) >>> 10 & 0x3FF | 0xD800); + decoded += String.fromCharCode(0xDC00 | (codePoint & 0xFFFF) & 0x3FF); + i += 12; + continue; + } + } + + throw new URIError('URI malformed'); + } + + return decoded; + } +}; /** * loc is a location in text1, compute and return the equivalent location in @@ -1249,6 +1409,7 @@ diff_match_patch.prototype.diff_xIndex = function(diffs, loc) { * @return {string} HTML representation. */ diff_match_patch.prototype.diff_prettyHtml = function(diffs) { + diffs = this.diff_cleanupSplitSurrogates(diffs); var html = []; var pattern_amp = /&/g; var pattern_lt = / 0 && + diff_match_patch.prototype.isLowSurrogate(text[patch.start2 - padding]) + ) { + padding++; + } var prefix = text.substring(patch.start2 - padding, patch.start2); if (prefix) { patch.diffs.unshift(new diff_match_patch.Diff(DIFF_EQUAL, prefix)); } // Add the suffix. + if ( + patch.start2 + patch.length1 + padding < text.length && + diff_match_patch.prototype.isHighSurrogate(text[patch.start2 + patch.length1 + padding]) + ) { + padding++; + } var suffix = text.substring(patch.start2 + patch.length1, patch.start2 + patch.length1 + padding); if (suffix) { @@ -1705,6 +1879,7 @@ diff_match_patch.prototype.patch_make = function(a, opt_b, opt_c) { if (diffs.length === 0) { return []; // Get rid of the null case. } + diffs = this.diff_cleanupSplitSurrogates(diffs); var patches = []; var patch = new diff_match_patch.patch_obj(); var patchDiffLength = 0; // Keeping our own length var is faster in JS. @@ -2202,6 +2377,7 @@ diff_match_patch.patch_obj.prototype.toString = function() { var text = ['@@ -' + coords1 + ' +' + coords2 + ' @@\n']; var op; // Escape the body of the patch with %xx notation. + diff_match_patch.prototype.diff_cleanupSplitSurrogates(this.diffs); for (var x = 0; x < this.diffs.length; x++) { switch (this.diffs[x][0]) { case DIFF_INSERT: diff --git a/javascript/tests/diff_match_patch_test.js b/javascript/tests/diff_match_patch_test.js index 109e56ad..99aaca13 100644 --- a/javascript/tests/diff_match_patch_test.js +++ b/javascript/tests/diff_match_patch_test.js @@ -492,6 +492,136 @@ function testDiffDelta() { // Convert delta string into a diff. assertEquivalent(diffs, dmp.diff_fromDelta(text1, delta)); + diffs = [[DIFF_EQUAL, '\ud83d\ude4b\ud83d'], [DIFF_INSERT, '\ude4c\ud83d'], [DIFF_EQUAL, '\ude4b']]; + try { + delta = dmp.diff_toDelta(diffs); + assertEquals('=2\t+%F0%9F%99%8C\t=2', delta); + } catch ( e ) { + assertEquals(false, true); + } + + (function(){ + const originalText = `U+1F17x πŸ…°οΈ πŸ…±οΈ πŸ…ΎοΈ πŸ…ΏοΈ safhawifhkw + U+1F18x πŸ†Ž + 0 1 2 3 4 5 6 7 8 9 A B C D E F + U+1F19x πŸ†‘ πŸ†’ πŸ†“ πŸ†” πŸ†• πŸ†– πŸ†— πŸ†˜ πŸ†™ πŸ†š + U+1F20x 🈁 πŸˆ‚οΈ sfss.,_||saavvvbbds + U+1F21x 🈚 + U+1F22x 🈯 + U+1F23x 🈲 🈳 🈴 🈡 🈢 🈷️ 🈸 🈹 🈺 + U+1F25x πŸ‰ πŸ‰‘ + U+1F30x πŸŒ€ 🌁 πŸŒ‚ πŸŒƒ πŸŒ„ πŸŒ… πŸŒ† πŸŒ‡ 🌈 πŸŒ‰ 🌊 πŸŒ‹ 🌌 🌍 🌎 🌏 + U+1F31x 🌐 πŸŒ‘ πŸŒ’ πŸŒ“ πŸŒ” πŸŒ• πŸŒ– πŸŒ— 🌘 πŸŒ™ 🌚 πŸŒ› 🌜 🌝 🌞 `; + + // applies some random edits to string and returns new, edited string + function applyRandomTextEdit(text) { + let textArr = [...text]; + let r = Math.random(); + if(r < 1/3) { // swap + let swapCount = Math.floor(Math.random()*5); + for(let i = 0; i < swapCount; i++) { + let swapPos1 = Math.floor(Math.random()*textArr.length); + let swapPos2 = Math.floor(Math.random()*textArr.length); + let char1 = textArr[swapPos1]; + let char2 = textArr[swapPos2]; + textArr[swapPos1] = char2; + textArr[swapPos2] = char1; + } + } else if(r < 2/3) { // remove + let removeCount = Math.floor(Math.random()*5); + for(let i = 0; i < removeCount; i++) { + let removePos = Math.floor(Math.random()*textArr.length); + textArr[removePos] = ""; + } + } else { // add + let addCount = Math.floor(Math.random()*5); + for(let i = 0; i < addCount; i++) { + let addPos = Math.floor(Math.random()*textArr.length); + let addFromPos = Math.floor(Math.random()*textArr.length); + textArr[addPos] = textArr[addPos] + textArr[addFromPos]; + } + } + return textArr.join(""); + } + + for(let i = 0; i < 1000; i++) { + newText = applyRandomTextEdit(originalText); + dmp.diff_toDelta(dmp.diff_main(originalText, newText)); + } + })(); + + // Unicode - splitting surrogates + try { + assertEquivalent( + dmp.diff_toDelta([[DIFF_INSERT,'\ud83c\udd71'], [DIFF_EQUAL, '\ud83c\udd70\ud83c\udd71']]), + dmp.diff_toDelta(dmp.diff_main('\ud83c\udd70\ud83c\udd71', '\ud83c\udd71\ud83c\udd70\ud83c\udd71')) + ); + } catch ( e ) { + assertEquals('Inserting similar surrogate pair at beginning', 'crashed'); + } + + try { + assertEquivalent( + dmp.diff_toDelta([[DIFF_EQUAL,'\ud83c\udd70'], [DIFF_INSERT, '\ud83c\udd70'], [DIFF_EQUAL, '\ud83c\udd71']]), + dmp.diff_toDelta(dmp.diff_main('\ud83c\udd70\ud83c\udd71', '\ud83c\udd70\ud83c\udd70\ud83c\udd71')) + ); + } catch ( e ) { + assertEquals('Inserting similar surrogate pair in the middle', 'crashed'); + } + + try { + assertEquivalent( + dmp.diff_toDelta([[DIFF_DELETE,'\ud83c\udd71'], [DIFF_EQUAL, '\ud83c\udd70\ud83c\udd71']]), + dmp.diff_toDelta(dmp.diff_main('\ud83c\udd71\ud83c\udd70\ud83c\udd71', '\ud83c\udd70\ud83c\udd71')) + ); + } catch ( e ) { + assertEquals('Deleting similar surrogate pair at the beginning', 'crashed'); + } + + try { + assertEquivalent( + dmp.diff_toDelta([[DIFF_EQUAL, '\ud83c\udd70'], [DIFF_DELETE,'\ud83c\udd72'], [DIFF_EQUAL, '\ud83c\udd71']]), + dmp.diff_toDelta(dmp.diff_main('\ud83c\udd70\ud83c\udd72\ud83c\udd71', '\ud83c\udd70\ud83c\udd71')) + ); + } catch ( e ) { + assertEquals('Deleting similar surrogate pair in the middle', 'crashed'); + } + + try { + assertEquivalent( + dmp.diff_toDelta([[DIFF_DELETE, '\ud83c\udd70'], [DIFF_INSERT, '\ud83c\udd71']]), + dmp.diff_toDelta([[DIFF_EQUAL, '\ud83c'], [DIFF_DELETE, '\udd70'], [DIFF_INSERT, '\udd71']]), + ); + } catch ( e ) { + assertEquals('Swap surrogate pair', 'crashed'); + } + + try { + assertEquivalent( + dmp.diff_toDelta([[DIFF_INSERT, '\ud83c\udd70'], [DIFF_DELETE, '\ud83c\udd71']]), + dmp.diff_toDelta([[DIFF_EQUAL, '\ud83c'], [DIFF_INSERT, '\udd70'], [DIFF_DELETE, '\udd71']]), + ); + } catch ( e ) { + assertEquals('Swap surrogate pair', 'crashed'); + } + + // Empty diff groups + assertEquivalent( + dmp.diff_toDelta([[DIFF_EQUAL, 'abcdef'], [DIFF_DELETE, ''], [DIFF_INSERT, 'ghijk']]), + dmp.diff_toDelta([[DIFF_EQUAL, 'abcdef'], [DIFF_INSERT, 'ghijk']]), + ); + + // Different versions of the library may have created deltas with + // half of a surrogate pair encoded as if it were valid UTF-8 + try { + assertEquivalent( + dmp.diff_toDelta(dmp.diff_fromDelta('\ud83c\udd70', '-2\t+%F0%9F%85%B1')), + dmp.diff_toDelta(dmp.diff_fromDelta('\ud83c\udd70', '=1\t-1\t+%ED%B5%B1')) + ); + } catch ( e ) { + assertEquals('Decode UTF8-encoded surrogate half', 'crashed'); + } + // Verify pool of unchanged characters. diffs = [[DIFF_INSERT, 'A-Z a-z 0-9 - _ . ! ~ * \' ( ) ; / ? : @ & = + $ , # ']]; var text2 = dmp.diff_text2(diffs); From 13309a1978e804d50a0c46caea97b6c3e82ddccf Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Tue, 30 Jan 2024 16:52:44 -0700 Subject: [PATCH 2/5] Java: Stop breaking surrogate pairs in toDelta() Resolves Google/diff-match-patch#69 for Java Sometimes we can find a common prefix that runs into the middle of a surrogate pair and we split that pair when building our diff groups. This is fine as long as we are operating on UTF-16 code units. It becomes problematic when we start trying to treat those substrings as valid Unicode (or UTF-8) sequences. When we pass these split groups into `toDelta()` we do just that and the library crashes. In this patch we're post-processing the diff groups before encoding them to make sure that we un-split the surrogate pairs. The post-processed diffs should produce the same output when applying the diffs. The diff string itself will be different but should change that much - only by a single character at surrogate boundaries. --- .../neil/plaintext/diff_match_patch.java | 149 +++++++++++++++++- .../neil/plaintext/diff_match_patch_test.java | 36 +++++ 2 files changed, 177 insertions(+), 8 deletions(-) diff --git a/java/src/name/fraser/neil/plaintext/diff_match_patch.java b/java/src/name/fraser/neil/plaintext/diff_match_patch.java index 9d07867d..d467f7fa 100644 --- a/java/src/name/fraser/neil/plaintext/diff_match_patch.java +++ b/java/src/name/fraser/neil/plaintext/diff_match_patch.java @@ -19,6 +19,7 @@ package name.fraser.neil.plaintext; import java.io.UnsupportedEncodingException; +import java.lang.Character; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.*; @@ -1293,6 +1294,46 @@ public void diff_cleanupMerge(LinkedList diffs) { } } + /** + * Rearrange diff boudnaries that split Unicode surrogate pairs. + * @param diffs Linked list of diff objects + */ + public void diff_cleanupSplitSurrogates(List diffs) { + char lastEnd = 0; + boolean isFirst = true; + HashSet toRemove = new HashSet(); + + for (Diff aDiff : diffs) { + if (aDiff.text.isEmpty()) { + toRemove.add(aDiff); + continue; + } + + char thisTop = aDiff.text.charAt(0); + char thisEnd = aDiff.text.charAt(aDiff.text.length() - 1); + + if (Character.isHighSurrogate(thisEnd)) { + lastEnd = thisEnd; + aDiff.text = aDiff.text.substring(0, aDiff.text.length() - 1); + } + + if (!isFirst && Character.isHighSurrogate(lastEnd) && Character.isLowSurrogate(thisTop)) { + aDiff.text = lastEnd + aDiff.text; + } + + isFirst = false; + + if ( aDiff.text.isEmpty() ) { + toRemove.add(aDiff); + continue; + } + } + + for (Diff aDiff : toRemove) { + diffs.remove(aDiff); + } + } + /** * loc is a location in text1, compute and return the equivalent location in * text2. @@ -1429,6 +1470,7 @@ public int diff_levenshtein(List diffs) { */ public String diff_toDelta(List diffs) { StringBuilder text = new StringBuilder(); + this.diff_cleanupSplitSurrogates(diffs); for (Diff aDiff : diffs) { switch (aDiff.operation) { case INSERT: @@ -1457,6 +1499,103 @@ public String diff_toDelta(List diffs) { return delta; } + private int digit16(char b) throws IllegalArgumentException { + switch (b) { + case '0': return 0; + case '1': return 1; + case '2': return 2; + case '3': return 3; + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + case '8': return 8; + case '9': return 9; + case 'A': case 'a': return 10; + case 'B': case 'b': return 11; + case 'C': case 'c': return 12; + case 'D': case 'd': return 13; + case 'E': case 'e': return 14; + case 'F': case 'f': return 15; + default: + throw new IllegalArgumentException(); + } + } + + private String decodeURI(String text) throws IllegalArgumentException { + int i = 0; + StringBuilder decoded = new StringBuilder(text.length()); + + while (i < text.length()) { + if (text.charAt(i) != '%') { + decoded.append(text.charAt(i++)); + continue; + } + + // start a percent-sequence + int byte1 = (digit16(text.charAt(i + 1)) << 4) + digit16(text.charAt(i + 2)); + if ((byte1 & 0x80) == 0) { + decoded.append(Character.toChars(byte1)); + i += 3; + continue; + } + + if ( text.charAt(i + 3) != '%') { + throw new IllegalArgumentException(); + } + + int byte2 = (digit16(text.charAt(i + 4)) << 4) + digit16(text.charAt(i + 5)); + if ((byte2 & 0xC0) != 0x80) { + throw new IllegalArgumentException(); + } + byte2 = byte2 & 0x3F; + if ((byte1 & 0xE0) == 0xC0) { + decoded.append(Character.toChars(((byte1 & 0x1F) << 6) | byte2)); + i += 6; + continue; + } + + if (text.charAt(i + 6) != '%') { + throw new IllegalArgumentException(); + } + + int byte3 = (digit16(text.charAt(i + 7)) << 4) + digit16(text.charAt(i + 8)); + if ((byte3 & 0xC0) != 0x80) { + throw new IllegalArgumentException(); + } + byte3 = byte3 & 0x3F; + if ((byte1 & 0xF0) == 0xE0) { + // unpaired surrogate are fine here + decoded.append(Character.toChars(((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3)); + i += 9; + continue; + } + + if (text.charAt(i + 9) != '%') { + throw new IllegalArgumentException(); + } + + int byte4 = (digit16(text.charAt(i + 10)) << 4) + digit16(text.charAt(i + 11)); + if ((byte4 & 0xC0) != 0x80) { + throw new IllegalArgumentException(); + } + byte4 = byte4 & 0x3F; + if ((byte1 & 0xF8) == 0xF0) { + int codePoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0C) | (byte3 << 0x06) | byte4; + if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) { + decoded.append(Character.toChars((codePoint & 0xFFFF) >>> 10 & 0x3FF | 0xD800)); + decoded.append(Character.toChars(0xDC00 | (codePoint & 0xFFFF) & 0x3FF)); + i += 12; + continue; + } + } + + throw new IllegalArgumentException(); + } + + return decoded.toString(); + } + /** * Given the original text1, and an encoded string which describes the * operations required to transform text1 into text2, compute the full diff. @@ -1483,10 +1622,7 @@ public LinkedList diff_fromDelta(String text1, String delta) // decode would change all "+" to " " param = param.replace("+", "%2B"); try { - param = URLDecoder.decode(param, "UTF-8"); - } catch (UnsupportedEncodingException e) { - // Not likely on modern system. - throw new Error("This system does not support UTF-8.", e); + param = this.decodeURI(param); } catch (IllegalArgumentException e) { // Malformed URI sequence. throw new IllegalArgumentException( @@ -2269,10 +2405,7 @@ public List patch_fromText(String textline) line = text.getFirst().substring(1); line = line.replace("+", "%2B"); // decode would change all "+" to " " try { - line = URLDecoder.decode(line, "UTF-8"); - } catch (UnsupportedEncodingException e) { - // Not likely on modern system. - throw new Error("This system does not support UTF-8.", e); + line = this.decodeURI(line); } catch (IllegalArgumentException e) { // Malformed URI sequence. throw new IllegalArgumentException( diff --git a/java/tests/name/fraser/neil/plaintext/diff_match_patch_test.java b/java/tests/name/fraser/neil/plaintext/diff_match_patch_test.java index 2f387933..5be10f13 100644 --- a/java/tests/name/fraser/neil/plaintext/diff_match_patch_test.java +++ b/java/tests/name/fraser/neil/plaintext/diff_match_patch_test.java @@ -424,6 +424,42 @@ public static void testDiffDelta() { assertEquals("diff_fromDelta: Unicode.", diffs, dmp.diff_fromDelta(text1, delta)); + diffs = diffList(new Diff(EQUAL, "\ud83d\ude4b\ud83d"), new Diff(INSERT, "\ude4c\ud83d"), new Diff(EQUAL, "\ude4b")); + delta = dmp.diff_toDelta(diffs); + assertEquals("diff_toDelta: Surrogate Pairs.", "=2\t+%F0%9F%99%8C\t=2", delta); + + assertEquals( + "diff_toDelta: insert surrogate pair between similar high surrogates", + dmp.diff_toDelta(diffList(new Diff(EQUAL, "\ud83c\udd70"), new Diff(INSERT, "\ud83c\udd70"), new Diff(EQUAL, "\ud83c\udd71"))), + dmp.diff_toDelta(diffList(new Diff(EQUAL, "\ud83c\udd70\ud83c"), new Diff(INSERT, "\udd70\ud83c"), new Diff(EQUAL, "\udd71"))) + ); + + assertEquals( + "diff_toDelta: swap surrogate pairs delete/insert", + dmp.diff_toDelta(diffList(new Diff(DELETE, "\ud83c\udd70"), new Diff(INSERT, "\ud83c\udd71"))), + dmp.diff_toDelta(diffList(new Diff(EQUAL, "\ud83c"), new Diff(DELETE, "\udd70"), new Diff(INSERT, "\udd71"))) + ); + + assertEquals( + "diff_toDelta: swap surrogate pairs insert/delete", + dmp.diff_toDelta(diffList(new Diff(INSERT, "\ud83c\udd70"), new Diff(DELETE, "\ud83c\udd71"))), + dmp.diff_toDelta(diffList(new Diff(EQUAL, "\ud83c"), new Diff(INSERT, "\udd70"), new Diff(DELETE, "\udd71"))) + ); + + assertEquals( + "diff_toDelta: empty diff groups", + dmp.diff_toDelta(diffList(new Diff(EQUAL, "abcdef"), new Diff(DELETE, ""), new Diff(INSERT, "ghijk"))), + dmp.diff_toDelta(diffList(new Diff(EQUAL, "abcdef"), new Diff(INSERT, "ghijk"))) + ); + + // Different versions of the library may have created deltas with + // half of a surrogate pair encoded as if it were valid UTF-8 + assertEquals( + "diff_toDelta: surrogate half encoded as UTF8", + dmp.diff_toDelta(dmp.diff_fromDelta("\ud83c\udd70", "-2\t+%F0%9F%85%B1")), + dmp.diff_toDelta(dmp.diff_fromDelta("\ud83c\udd70", "=1\t-1\t+%ED%B5%B1")) + ); + // Verify pool of unchanged characters. diffs = diffList(new Diff(INSERT, "A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # ")); String text2 = dmp.diff_text2(diffs); From dfadc9c073aa86a04860c82861391302a2ff7d4c Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Tue, 30 Jan 2024 16:53:15 -0700 Subject: [PATCH 3/5] Objective-C: Stop breaking surrogate pairs in toDelta() Resolves Google/diff-match-patch#69 for Objective-C Sometimes we can find a common prefix that runs into the middle of a surrogate pair and we split that pair when building our diff groups. This is fine as long as we are operating on UTF-16 code units. It becomes problematic when we start trying to treat those substrings as valid Unicode (or UTF-8) sequences. When we pass these split groups into `toDelta()` we do just that and the library crashes. In this patch we're post-processing the diff groups before encoding them to make sure that we un-split the surrogate pairs. The post-processed diffs should produce the same output when applying the diffs. The diff string itself will be different but should change that much - only by a single character at surrogate boundaries. --- objectivec/DiffMatchPatch.m | 193 +++++++++++++++++++++++++- objectivec/Tests/DiffMatchPatchTest.m | 67 +++++++++ 2 files changed, 259 insertions(+), 1 deletion(-) diff --git a/objectivec/DiffMatchPatch.m b/objectivec/DiffMatchPatch.m index 580f2656..0c56201d 100755 --- a/objectivec/DiffMatchPatch.m +++ b/objectivec/DiffMatchPatch.m @@ -1299,7 +1299,28 @@ - (NSString *)diff_text2:(NSMutableArray *)diffs; - (NSString *)diff_toDelta:(NSMutableArray *)diffs; { NSMutableString *delta = [NSMutableString string]; + UniChar lastEnd = 0; for (Diff *aDiff in diffs) { + if (0 == [aDiff.text length]) { + continue; + } + + UniChar thisTop = [aDiff.text characterAtIndex:0]; + UniChar thisEnd = [aDiff.text characterAtIndex:([aDiff.text length]-1)]; + + if (CFStringIsSurrogateHighCharacter(thisEnd)) { + lastEnd = thisEnd; + aDiff.text = [aDiff.text substringToIndex:([aDiff.text length] - 1)]; + } + + if (0 != lastEnd && CFStringIsSurrogateHighCharacter(lastEnd) && CFStringIsSurrogateLowCharacter(thisTop)) { + aDiff.text = [NSString stringWithFormat:@"%C%@", lastEnd, aDiff.text]; + } + + if (0 == [aDiff.text length]) { + continue; + } + switch (aDiff.operation) { case DIFF_INSERT: [delta appendFormat:@"+%@\t", [[aDiff.text diff_stringByAddingPercentEscapesForEncodeUriCompatibility] @@ -1321,6 +1342,176 @@ - (NSString *)diff_toDelta:(NSMutableArray *)diffs; return delta; } +- (NSUInteger)diff_digit16:(unichar)c +{ + switch (c) { + case '0': return 0; + case '1': return 1; + case '2': return 2; + case '3': return 3; + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + case '8': return 8; + case '9': return 9; + case 'A': case 'a': return 10; + case 'B': case 'b': return 11; + case 'C': case 'c': return 12; + case 'D': case 'd': return 13; + case 'E': case 'e': return 14; + case 'F': case 'f': return 15; + default: + [NSException raise:@"Invalid percent-encoded string" format:@"%c is not a hex digit", c]; + } +} + +/** +* Decode a percent-encoded UTF-8 string into a string of UTF-16 code units +* This is more permissive than `stringByRemovingPercentEncoding` because +* that fails if the input represents invalid Unicode characters. However, different +* diff-match-patch libraries may encode surrogate halves as if they were valid +* Unicode code points. Therefore, instead of failing or corrupting the output, which +* `stringByRemovingPercentEncoding` does when it inserts "(null)" in these places +* we can decode it anyway and then once the string is reconstructed from the diffs +* we'll end up with valid Unicode again, after the surrogate halves are re-joined +*/ +- (NSString *)diff_decodeURIWithText:(NSString *)percentEncoded +{ + unichar decoded[[percentEncoded length]]; + NSInteger input = 0; + NSInteger output = 0; + + @try { + while (input < [percentEncoded length]) { + unichar c = [percentEncoded characterAtIndex:input]; + + // not special, so just return it + if ('%' != c) { + decoded[output++] = c; + input += 1; + continue; + } + + NSUInteger byte1 = ([self diff_digit16:[percentEncoded characterAtIndex:(input+1)]] << 4) + + [self diff_digit16:[percentEncoded characterAtIndex:(input+2)]]; + + // single-byte UTF-8 first byte has bitmask 0xxx xxxx + if ((byte1 & 0x80) == 0) { + decoded[output++] = byte1; + input += 3; + continue; + } + + // at least one continuation byte + if ('%' != [percentEncoded characterAtIndex:(input + 3)]) { + return nil; + } + + NSUInteger byte2 = ([self diff_digit16:[percentEncoded characterAtIndex:(input+4)]] << 4) + + [self diff_digit16:[percentEncoded characterAtIndex:(input+5)]]; + + // continuation bytes have bitmask 10xx xxxx + if ((byte2 & 0xC0) != 0x80) { + return nil; + } + + // continuation bytes thus only contribute six bits each + // these data bits are found with the bit mask xx11 1111 + byte2 = byte2 & 0x3F; + + // in two-byte sequences the first byte has bitmask 110x xxxx + if ((byte1 & 0xE0) == 0xC0) { + // byte1 ___x xxxx << 6 + // byte2 __yy yyyy + // value x xxxxyy yyyy -> 11 bits + decoded[output++] = ((byte1 & 0x1F) << 6) | byte2; + input += 6; + continue; + } + + // at least two continuation bytes + if ('%' != [percentEncoded characterAtIndex:(input + 6)]) { + return nil; + } + + NSUInteger byte3 = ([self diff_digit16:[percentEncoded characterAtIndex:(input+7)]] << 4) + + [self diff_digit16:[percentEncoded characterAtIndex:(input+8)]]; + + if ((byte3 & 0xC0) != 0x80) { + return nil; + } + + byte3 = byte3 & 0x3F; + + // in three-byte sequences the first byte has bitmask 1110 xxxx + if ((byte1 & 0xF0) == 0xE0) { + // byte1 ____ xxxx << 12 + // byte2 __yy yyyy << 6 + // byte3 __zz zzzz + // value xxxxyy yyyyzz zzzz -> 16 bits + decoded[output++] = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3; + input += 9; + continue; + } + + // three continuation bytes + if ('%' != [percentEncoded characterAtIndex:(input + 9)]) { + return nil; + } + + NSUInteger byte4 = ([self diff_digit16:[percentEncoded characterAtIndex:(input+10)]] << 4) + + [self diff_digit16:[percentEncoded characterAtIndex:(input+11)]]; + + if ((byte4 & 0xC0) != 0x80) { + return nil; + } + + byte4 = byte4 & 0x3F; + + // in four-byte sequences the first byte has bitmask 1111 0xxx + if ((byte1 & 0xF8) == 0xF0) { + // byte1 ____ _xxx << 18 + // byte2 __yy yyyy << 12 + // byte3 __zz zzzz << 6 + // byte4 __tt tttt + // value xxxyy yyyyzz zzzztt tttt -> 21 bits + NSUInteger codePoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0C) | (byte3 << 0x06) | byte4; + if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) { + codePoint -= 0x010000; + decoded[output++] = ((codePoint >> 10) & 0x3FF) | 0xD800; + decoded[output++] = 0xDC00 | (codePoint & 0x3FF); + input += 12; + continue; + } + } + + return nil; + } + } + @catch (NSException *e) { + return nil; + } + + // some objective-c versions of the library produced patches with + // (null) in the place where surrogates were split across diff + // boundaries. if we leave those in we'll be stuck with a + // high-surrogate (null) low-surrogate pattern that will break + // deeper in the library or consuming application. we'll "fix" + // these by dropping the (null) and re-joining the surrogate halves + NSString *result = [NSString stringWithCharacters:decoded length:output]; + NSRegularExpression *replacer = [NSRegularExpression + regularExpressionWithPattern:@"([\\x{D800}-\\x{DBFF}])\\(null\\)([\\x{DC00}-\\x{DFFF}])" + options:0 + error:nil]; + + return [replacer + stringByReplacingMatchesInString:result + options:0 + range:NSMakeRange(0, [result length]) + withTemplate:@"$1$2"]; +} + /** * Given the original text1, and an encoded NSString which describes the * operations required to transform text1 into text2, compute the full diff. @@ -1348,7 +1539,7 @@ - (NSMutableArray *)diff_fromDeltaWithText:(NSString *)text1 NSString *param = [token substringFromIndex:1]; switch ([token characterAtIndex:0]) { case '+': - param = [param diff_stringByReplacingPercentEscapesForEncodeUriCompatibility]; + param = [self diff_decodeURIWithText:param]; if (param == nil) { if (error != NULL) { errorDetail = [NSDictionary dictionaryWithObjectsAndKeys: diff --git a/objectivec/Tests/DiffMatchPatchTest.m b/objectivec/Tests/DiffMatchPatchTest.m index 9697b04c..7e31508b 100755 --- a/objectivec/Tests/DiffMatchPatchTest.m +++ b/objectivec/Tests/DiffMatchPatchTest.m @@ -752,6 +752,68 @@ - (void)test_diff_deltaTest { XCTAssertEqualObjects(diffs, [dmp diff_fromDeltaWithText:text1 andDelta:delta error:NULL], @"diff_fromDelta: Unicode 2."); + diffs = [dmp diff_mainOfOldString:@"β˜ΊοΈπŸ––πŸΏ" andNewString:@"β˜ΊοΈπŸ˜ƒπŸ––πŸΏ"]; + delta = [dmp diff_toDelta:diffs]; + + XCTAssertEqualObjects(delta, @"=2\t+%F0%9F%98%83\t=4", @"Delta should match the expected string"); + + diffs = [dmp diff_mainOfOldString:@"β˜ΊοΈπŸ––πŸΏ" andNewString:@"β˜ΊοΈπŸ˜ƒπŸ––πŸΏ"]; + NSArray *patches = [dmp patch_makeFromDiffs:diffs]; + NSArray *patchResult = [dmp patch_apply:patches toString:@"β˜ΊοΈπŸ––πŸΏ"]; + + expectedString = [patchResult firstObject]; + XCTAssertEqualObjects(@"β˜ΊοΈπŸ˜ƒπŸ––πŸΏ", expectedString, @"Output String should match the Edited one!"); + + // Unicode - splitting surrogates + + // Inserting similar surrogate pair at beginning + diffs = [NSMutableArray arrayWithObjects: + [Diff diffWithOperation:DIFF_INSERT andText:@"πŸ…±"], + [Diff diffWithOperation:DIFF_EQUAL andText:@"πŸ…°πŸ…±"], + nil]; + XCTAssertEqualObjects( [dmp diff_toDelta:diffs], [dmp diff_toDelta:[dmp diff_mainOfOldString:@"πŸ…°πŸ…±" andNewString:@"πŸ…±πŸ…°πŸ…±"]]); + + // Inserting similar surrogate pair in the middle + diffs = [NSMutableArray arrayWithObjects: + [Diff diffWithOperation:DIFF_EQUAL andText:@"πŸ…°"], + [Diff diffWithOperation:DIFF_INSERT andText:@"πŸ…°"], + [Diff diffWithOperation:DIFF_EQUAL andText:@"πŸ…±"], + nil]; + XCTAssertEqualObjects( [dmp diff_toDelta:diffs], [dmp diff_toDelta:[dmp diff_mainOfOldString:@"πŸ…°πŸ…±" andNewString:@"πŸ…°πŸ…°πŸ…±"]]); + + // Deleting similar surrogate pair at the beginning + diffs = [NSMutableArray arrayWithObjects: + [Diff diffWithOperation:DIFF_DELETE andText:@"πŸ…±"], + [Diff diffWithOperation:DIFF_EQUAL andText:@"πŸ…°πŸ…±"], + nil]; + XCTAssertEqualObjects( [dmp diff_toDelta:diffs], [dmp diff_toDelta:[dmp diff_mainOfOldString:@"πŸ…±πŸ…°πŸ…±" andNewString:@"πŸ…°πŸ…±"]]); + + // Deleting similar surrogate pair in the middle + diffs = [NSMutableArray arrayWithObjects: + [Diff diffWithOperation:DIFF_EQUAL andText:@"πŸ…°"], + [Diff diffWithOperation:DIFF_DELETE andText:@"πŸ…²"], + [Diff diffWithOperation:DIFF_EQUAL andText:@"πŸ…±"], + nil]; + XCTAssertEqualObjects( [dmp diff_toDelta:diffs], [dmp diff_toDelta:[dmp diff_mainOfOldString:@"πŸ…°πŸ…²πŸ…±" andNewString:@"πŸ…°πŸ…±"]]); + + // Swapping surrogate pairs + diffs = [NSMutableArray arrayWithObjects: + [Diff diffWithOperation:DIFF_DELETE andText:@"πŸ…°"], + [Diff diffWithOperation:DIFF_INSERT andText:@"πŸ…±"], + nil]; + XCTAssertEqualObjects( [dmp diff_toDelta:diffs], [dmp diff_toDelta:[dmp diff_mainOfOldString:@"πŸ…°" andNewString:@"πŸ…±"]]); + + // Swapping surrogate pairs + XCTAssertEqualObjects( [dmp diff_toDelta:([NSMutableArray arrayWithObjects: + [Diff diffWithOperation:DIFF_DELETE andText:@"πŸ…°"], + [Diff diffWithOperation:DIFF_INSERT andText:@"πŸ…±"], + nil])], + [dmp diff_toDelta:([NSMutableArray arrayWithObjects: + [Diff diffWithOperation:DIFF_EQUAL andText:[NSString stringWithFormat:@"%C", 0xd83c]], + [Diff diffWithOperation:DIFF_DELETE andText:[NSString stringWithFormat:@"%C", 0xdd70]], + [Diff diffWithOperation:DIFF_INSERT andText:[NSString stringWithFormat:@"%C", 0xdd71]], + nil])]); + // Verify pool of unchanged characters. diffs = [NSMutableArray arrayWithObject: [Diff diffWithOperation:DIFF_INSERT andText:@"A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # "]]; @@ -781,6 +843,11 @@ - (void)test_diff_deltaTest { expectedResult = [dmp diff_fromDeltaWithText:@"" andDelta:delta error:NULL]; XCTAssertEqualObjects(diffs, expectedResult, @"diff_fromDelta: 160kb string. Convert delta string into a diff."); + // Different versions of the library may have created deltas with + // half of a surrogate pair encoded as if it were valid UTF-8 + XCTAssertEqualObjects([dmp diff_toDelta:([dmp diff_fromDeltaWithText:@"πŸ…°" andDelta:@"-2\t+%F0%9F%85%B1" error:NULL])], + [dmp diff_toDelta:([dmp diff_fromDeltaWithText:@"πŸ…°" andDelta:@"=1\t-1\t+%ED%B5%B1" error:NULL])]); + [dmp release]; } From db1cbba2800fd0b30c71adb39a2066d46c37cfc3 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Tue, 30 Jan 2024 16:53:42 -0700 Subject: [PATCH 4/5] Python2: Stop breaking surrogate pairs in toDelta() Resolves Google/diff-match-patch#69 for Python2 Sometimes we can find a common prefix that runs into the middle of a surrogate pair and we split that pair when building our diff groups. This is fine as long as we are operating on UTF-16 code units. It becomes problematic when we start trying to treat those substrings as valid Unicode (or UTF-8) sequences. When we pass these split groups into `toDelta()` we do just that and the library crashes. In this patch we're post-processing the diff groups before encoding them to make sure that we un-split the surrogate pairs. The post-processed diffs should produce the same output when applying the diffs. The diff string itself will be different but should change that much - only by a single character at surrogate boundaries. --- python2/diff_match_patch.py | 34 +++++++++-- python2/tests/diff_match_patch_test.py | 80 ++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 4 deletions(-) diff --git a/python2/diff_match_patch.py b/python2/diff_match_patch.py index 806fe1e6..8b26125c 100644 --- a/python2/diff_match_patch.py +++ b/python2/diff_match_patch.py @@ -28,6 +28,7 @@ __author__ = 'fraser@google.com (Neil Fraser)' import re +import struct import sys import time import urllib @@ -1135,6 +1136,14 @@ def diff_levenshtein(self, diffs): levenshtein += max(insertions, deletions) return levenshtein + @classmethod + def is_high_surrogate(cls, c): + return 0xd800 <= struct.unpack('>H', c)[0] <= 0xdbff + + @classmethod + def is_low_surrogate(cls, c): + return 0xdc00 <= struct.unpack('>H', c)[0] <= 0xdfff + def diff_toDelta(self, diffs): """Crush the diff into an encoded string which describes the operations required to transform text1 into text2. @@ -1148,15 +1157,32 @@ def diff_toDelta(self, diffs): Delta text. """ text = [] + last_end = None for (op, data) in diffs: + if 0 == len(data): + continue + + encoded = data.encode('utf-16be') + this_top = encoded[0:2] + this_end = encoded[-2:] + + if self.is_high_surrogate(this_end): + last_end = this_end + encoded = encoded[0:-2] + + if last_end and self.is_high_surrogate(last_end) and self.is_low_surrogate(this_top): + encoded = last_end + encoded + + if 0 == len(encoded): + continue + if op == self.DIFF_INSERT: # High ascii will raise UnicodeDecodeError. Use Unicode instead. - data = data.encode("utf-8") - text.append("+" + urllib.quote(data, "!~*'();/?:@&=+$,# ")) + text.append("+" + urllib.quote(encoded.decode('utf-16be').encode('utf-8'), "!~*'();/?:@&=+$,# ")) elif op == self.DIFF_DELETE: - text.append("-%d" % len(data)) + text.append("-%d" % (len(encoded) // 2)) elif op == self.DIFF_EQUAL: - text.append("=%d" % len(data)) + text.append("=%d" % (len(encoded) // 2)) return "\t".join(text) def diff_fromDelta(self, text1, delta): diff --git a/python2/tests/diff_match_patch_test.py b/python2/tests/diff_match_patch_test.py index 661a6b67..94f5fd35 100644 --- a/python2/tests/diff_match_patch_test.py +++ b/python2/tests/diff_match_patch_test.py @@ -441,6 +441,86 @@ def testDiffDelta(self): # Convert delta string into a diff. self.assertEquals(diffs, self.dmp.diff_fromDelta(text1, delta)) + diffs = [(self.dmp.DIFF_EQUAL, u"\ud83d\ude4b\ud83d"), (self.dmp.DIFF_INSERT, u"\ude4c\ud83d"), (self.dmp.DIFF_EQUAL, u"\ude4b")] + delta = self.dmp.diff_toDelta(diffs) + self.assertEquals("=2\t+%F0%9F%99%8C\t=2", delta) + + # Unicode: split surrogates + # Inserting similar surrogate pair at beginning + self.assertEquals( + self.dmp.diff_toDelta([ + (self.dmp.DIFF_INSERT, u'\U0001F171'), + (self.dmp.DIFF_EQUAL, u'\U0001F170\U0001F171') + ]), + self.dmp.diff_toDelta(self.dmp.diff_main( + u'\U0001F170\U0001F171', + u'\U0001F171\U0001F170\U0001F171' + )) + ) + + # Inserting similar surrogate pair in the middle + self.assertEquals( + self.dmp.diff_toDelta([ + (self.dmp.DIFF_EQUAL, u'\U0001F170'), + (self.dmp.DIFF_INSERT, u'\U0001F172'), + (self.dmp.DIFF_EQUAL, u'\U0001F171') + ]), + self.dmp.diff_toDelta(self.dmp.diff_main( + u'\U0001F170\U0001F171', + u'\U0001F170\U0001F172\U0001F171' + )) + ) + + # Deleting similar surogate pair at the beginning + self.assertEquals( + self.dmp.diff_toDelta([ + (self.dmp.DIFF_DELETE, u'\U0001F171'), + (self.dmp.DIFF_EQUAL, u'\U0001F170\U0001F171') + ]), + self.dmp.diff_toDelta(self.dmp.diff_main( + u'\U0001F171\U0001F170\U0001F171', + u'\U0001F170\U0001F171' + )) + ) + + # Deleting similar surogate pair in the middle + self.assertEquals( + self.dmp.diff_toDelta([ + (self.dmp.DIFF_EQUAL, u'\U0001F170'), + (self.dmp.DIFF_DELETE, u'\U0001F172'), + (self.dmp.DIFF_EQUAL, u'\U0001F171') + ]), + self.dmp.diff_toDelta(self.dmp.diff_main( + u'\U0001F170\U0001F172\U0001F171', + u'\U0001F170\U0001F171' + )) + ) + + # Swap surrogate pair + self.assertEquals( + self.dmp.diff_toDelta([ + (self.dmp.DIFF_DELETE, u'\U0001F170'), + (self.dmp.DIFF_INSERT, u'\U0001F171') + ]), + self.dmp.diff_toDelta(self.dmp.diff_main( + u'\U0001F170', + u'\U0001F171' + )) + ) + + # Swap surrogate pair, force the invalid diff groups + self.assertEquals( + self.dmp.diff_toDelta([ + (self.dmp.DIFF_INSERT, u'\U0001F170'), + (self.dmp.DIFF_DELETE, u'\U0001F171') + ]), + self.dmp.diff_toDelta([ + (self.dmp.DIFF_EQUAL, u'\ud83c'), + (self.dmp.DIFF_INSERT, u'\udd70'), + (self.dmp.DIFF_DELETE, u'\udd71') + ]) + ) + # Verify pool of unchanged characters. diffs = [(self.dmp.DIFF_INSERT, "A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # ")] text2 = self.dmp.diff_text2(diffs) From 50f15423d886660436d57144535afe2ef6c15987 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Tue, 30 Jan 2024 16:54:05 -0700 Subject: [PATCH 5/5] Python3: Stop breaking surrogate pairs in toDelta() Resolves Google/diff-match-patch#69 for Python3 Sometimes we can find a common prefix that runs into the middle of a surrogate pair and we split that pair when building our diff groups. This is fine as long as we are operating on UTF-16 code units. It becomes problematic when we start trying to treat those substrings as valid Unicode (or UTF-8) sequences. When we pass these split groups into `toDelta()` we do just that and the library crashes. In this patch we're post-processing the diff groups before encoding them to make sure that we un-split the surrogate pairs. The post-processed diffs should produce the same output when applying the diffs. The diff string itself will be different but should change that much - only by a single character at surrogate boundaries. --- python3/diff_match_patch.py | 19 ++++--- python3/tests/diff_match_patch_test.py | 70 ++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/python3/diff_match_patch.py b/python3/diff_match_patch.py index cc7f5907..3bf825c5 100644 --- a/python3/diff_match_patch.py +++ b/python3/diff_match_patch.py @@ -26,6 +26,7 @@ __author__ = 'fraser@google.com (Neil Fraser)' import re +import struct import sys import time import urllib.parse @@ -1147,14 +1148,17 @@ def diff_toDelta(self, diffs): """ text = [] for (op, data) in diffs: + if 0 == len(data): + continue + if op == self.DIFF_INSERT: # High ascii will raise UnicodeDecodeError. Use Unicode instead. data = data.encode("utf-8") text.append("+" + urllib.parse.quote(data, "!~*'();/?:@&=+$,# ")) elif op == self.DIFF_DELETE: - text.append("-%d" % len(data)) + text.append("-%d" % (len(data.encode('utf-16-be')) // 2)) elif op == self.DIFF_EQUAL: - text.append("=%d" % len(data)) + text.append("=%d" % (len(data.encode('utf-16-be')) // 2)) return "\t".join(text) def diff_fromDelta(self, text1, delta): @@ -1172,7 +1176,8 @@ def diff_fromDelta(self, text1, delta): ValueError: If invalid input. """ diffs = [] - pointer = 0 # Cursor in text1 + as_utf16 = text1.encode('utf-16-be') + pointer = 0 # Cursor in as_utf16 tokens = delta.split("\t") for token in tokens: if token == "": @@ -1191,8 +1196,8 @@ def diff_fromDelta(self, text1, delta): raise ValueError("Invalid number in diff_fromDelta: " + param) if n < 0: raise ValueError("Negative number in diff_fromDelta: " + param) - text = text1[pointer : pointer + n] - pointer += n + text = as_utf16[pointer : pointer + n * 2].decode('utf-16-be') + pointer += n * 2 if token[0] == "=": diffs.append((self.DIFF_EQUAL, text)) else: @@ -1201,10 +1206,10 @@ def diff_fromDelta(self, text1, delta): # Anything else is an error. raise ValueError("Invalid diff operation in diff_fromDelta: " + token[0]) - if pointer != len(text1): + if pointer != len(as_utf16): raise ValueError( "Delta length (%d) does not equal source text length (%d)." % - (pointer, len(text1))) + (pointer, len(as_utf16))) return diffs # MATCH FUNCTIONS diff --git a/python3/tests/diff_match_patch_test.py b/python3/tests/diff_match_patch_test.py index 3659d3e7..4ff16abd 100644 --- a/python3/tests/diff_match_patch_test.py +++ b/python3/tests/diff_match_patch_test.py @@ -18,6 +18,7 @@ """ import imp +import json import os import sys import time @@ -444,6 +445,12 @@ def testDiffDelta(self): # Convert delta string into a diff. self.assertEqual(diffs, self.dmp.diff_fromDelta(text1, delta)) + diffs = self.dmp.diff_main("\U0001F64B\U0001F64B", "\U0001F64B\U0001F64C\U0001F64B") + delta = self.dmp.diff_toDelta(diffs) + self.assertEqual("=2\t+%F0%9F%99%8C\t=2", delta) + + self.assertEqual(diffs, self.dmp.diff_fromDelta("\U0001F64B\U0001F64B", "=2\t+%F0%9F%99%8C\t=2")) + # Verify pool of unchanged characters. diffs = [(self.dmp.DIFF_INSERT, "A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # ")] text2 = self.dmp.diff_text2(diffs) @@ -455,6 +462,69 @@ def testDiffDelta(self): # Convert delta string into a diff. self.assertEqual(diffs, self.dmp.diff_fromDelta("", delta)) + # Unicode: split surrogates + self.assertEqual( + self.dmp.diff_toDelta([ + (self.dmp.DIFF_INSERT, '\U0001F171'), + (self.dmp.DIFF_EQUAL, '\U0001F170\U0001F171') + ]), + self.dmp.diff_toDelta(self.dmp.diff_main( + '\U0001F170\U0001F171', + '\U0001F171\U0001F170\U0001F171' + )), + 'Inserting similar surrogate pair at beginning' + ) + + self.assertEqual( + self.dmp.diff_toDelta([ + (self.dmp.DIFF_EQUAL, '\U0001F170'), + (self.dmp.DIFF_INSERT, '\U0001F172'), + (self.dmp.DIFF_EQUAL, '\U0001F171') + ]), + self.dmp.diff_toDelta(self.dmp.diff_main( + '\U0001F170\U0001F171', + '\U0001F170\U0001F172\U0001F171' + )), + 'Inserting similar surrogate pair in the middle' + ) + + self.assertEqual( + self.dmp.diff_toDelta([ + (self.dmp.DIFF_DELETE, '\U0001F171'), + (self.dmp.DIFF_EQUAL, '\U0001F170\U0001F171') + ]), + self.dmp.diff_toDelta(self.dmp.diff_main( + '\U0001F171\U0001F170\U0001F171', + '\U0001F170\U0001F171' + )), + 'Deleting similar surogate pair at the beginning' + ) + + self.assertEqual( + self.dmp.diff_toDelta([ + (self.dmp.DIFF_EQUAL, '\U0001F170'), + (self.dmp.DIFF_DELETE, '\U0001F172'), + (self.dmp.DIFF_EQUAL, '\U0001F171') + ]), + self.dmp.diff_toDelta(self.dmp.diff_main( + '\U0001F170\U0001F172\U0001F171', + '\U0001F170\U0001F171' + )), + 'Deleting similar surogate pair in the middle' + ) + + self.assertEqual( + self.dmp.diff_toDelta([ + (self.dmp.DIFF_DELETE, '\U0001F170'), + (self.dmp.DIFF_INSERT, '\U0001F171') + ]), + self.dmp.diff_toDelta(self.dmp.diff_main( + '\U0001F170', + '\U0001F171' + )), + 'Swap surrogate pair' + ) + # 160 kb string. a = "abcdefghij" for i in range(14):