fictioneer/js/diff-match-patch.js

1 line
21 KiB
JavaScript
Raw Permalink Normal View History

2024-05-10 00:05:05 +02:00
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(t,i){this[0]=t,this[1]=i},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(t,i,h,f){if(void 0===f&&(f=0>=this.Diff_Timeout?Number.MAX_VALUE:(new Date).getTime()+1e3*this.Diff_Timeout),null==t||null==i)throw Error("Null input. (diff_main)");if(t==i)return t?[new diff_match_patch.Diff(DIFF_EQUAL,t)]:[];void 0===h&&(h=!0);var e=h,n=this.diff_commonPrefix(t,i);h=t.substring(0,n),t=t.substring(n),i=i.substring(n),n=this.diff_commonSuffix(t,i);var a=t.substring(t.length-n);return t=t.substring(0,t.length-n),i=i.substring(0,i.length-n),t=this.diff_compute_(t,i,e,f),h&&t.unshift(new diff_match_patch.Diff(DIFF_EQUAL,h)),a&&t.push(new diff_match_patch.Diff(DIFF_EQUAL,a)),this.diff_cleanupMerge(t),t},diff_match_patch.prototype.diff_compute_=function(t,i,h,f){if(!t)return[new diff_match_patch.Diff(DIFF_INSERT,i)];if(!i)return[new diff_match_patch.Diff(DIFF_DELETE,t)];var e=t.length>i.length?t:i,n=t.length>i.length?i:t,a=e.indexOf(n);return-1!=a?(h=[new diff_match_patch.Diff(DIFF_INSERT,e.substring(0,a)),new diff_match_patch.Diff(DIFF_EQUAL,n),new diff_match_patch.Diff(DIFF_INSERT,e.substring(a+n.length))],t.length>i.length&&(h[0][0]=h[2][0]=DIFF_DELETE),h):1==n.length?[new diff_match_patch.Diff(DIFF_DELETE,t),new diff_match_patch.Diff(DIFF_INSERT,i)]:(e=this.diff_halfMatch_(t,i))?(i=e[1],n=e[3],t=e[4],e=this.diff_main(e[0],e[2],h,f),h=this.diff_main(i,n,h,f),e.concat([new diff_match_patch.Diff(DIFF_EQUAL,t)],h)):h&&100<t.length&&100<i.length?this.diff_lineMode_(t,i,f):this.diff_bisect_(t,i,f)},diff_match_patch.prototype.diff_lineMode_=function(t,i,h){var f=this.diff_linesToChars_(t,i);t=f.chars1,i=f.chars2,f=f.lineArray,t=this.diff_main(t,i,!1,h),this.diff_charsToLines_(t,f),this.diff_cleanupSemantic(t),t.push(new diff_match_patch.Diff(DIFF_EQUAL,""));for(var e=f=i=0,n="",a="";i<t.length;){switch(t[i][0]){case DIFF_INSERT:e++,a+=t[i][1];break;case DIFF_DELETE:f++,n+=t[i][1];break;case DIFF_EQUAL:if(1<=f&&1<=e){for(t.splice(i-f-e,f+e),i=i-f-e,e=(f=this.diff_main(n,a,!1,h)).length-1;0<=e;e--)t.splice(i,0,f[e]);i+=f.length}f=e=0,a=n=""}i++}return t.pop(),t},diff_match_patch.prototype.diff_bisect_=function(t,i,h){for(var f=t.length,e=i.length,n=Math.ceil((f+e)/2),a=2*n,r=Array(a),s=Array(a),_=0;_<a;_++)r[_]=-1,s[_]=-1;r[n+1]=0,s[n+1]=0;for(var c=0!=(_=f-e)%2,l=0,g=0,o=0,p=0,d=0;d<n&&!((new Date).getTime()>h);d++){for(var u=-d+l;u<=d-g;u+=2){for(var m=n+u,F=u==-d||u!=d&&r[m-1]<r[m+1]?r[m+1]:r[m-1]+1,D=F-u;F<f&&D<e&&t.charAt(F)==i.charAt(D);)F++,D++;if(r[m]=F,F>f)g+=2;else if(D>e)l+=2;else if(c&&(0<=(m=n+_-u)&&m<a&&-1!=s[m])){var E=f-s[m];if(F>=E)return this.diff_bisectSplit_(t,i,F,D,h)}}for(u=-d+o;u<=d-p;u+=2){for(m=n+u,F=(E=u==-d||u!=d&&s[m-1]<s[m+1]?s[m+1]:s[m-1]+1)-u;E<f&&F<e&&t.charAt(f-E-1)==i.charAt(e-F-1);)E++,F++;if(s[m]=E,E>f)p+=2;else if(F>e)o+=2;else if(!c&&(0<=(m=n+_-u)&&m<a&&-1!=r[m]&&(D=n+(F=r[m])-m,F>=(E=f-E))))return this.diff_bisectSplit_(t,i,F,D,h)}}return[new diff_match_patch.Diff(DIFF_DELETE,t),new diff_match_patch.Diff(DIFF_INSERT,i)]},diff_match_patch.prototype.diff_bisectSplit_=function(t,i,h,f,e){var n=t.substring(0,h),a=i.substring(0,f);return t=t.substring(h),i=i.substring(f),n=this.diff_main(n,a,!1,e),e=this.diff_main(t,i,!1,e),n.concat(e)},diff_match_patch.prototype.diff_linesToChars_=function(t,i){function h(t){for(var i="",h=0,a=-1,r=f.length;a<t.length-1;){-1==(a=t.indexOf("\n",h))&&(a=t.length-1);var s=t.substring(h,a+1);void 0!==e[s]?i+=String.fromCharCode(e[s]):(r==n&&(s=t.substring(h),a=t.length),i+=String.fromCharCode(r),e[s]=r,f[r++]=s),h=a+1}return i}var f=[],e={};f[0]="";var n=4e4,a=h(t);return n=65535,{chars1:a,chars2:h(i),lineArray:f}},diff_match_patch.prototype.diff_charsToLines_=function(t,i){for(var h=0;h<t.length;h