fast-myers-diff
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -1,1 +0,1 @@ | ||
"use strict";function*diff_rec(t,e,o,f,n,c){const i=[];let l=0,r=2*Math.min(o,c)+2;const s=new Uint32Array(2*r);let[a,y]=[0,r],[d,u,h,p,x]=[0,0,0,0,0];for(;;){t:{if(o>0&&c>0){const g=o+c,P=1&g,b=o-c,A=(g>>>1)+P;e:for(let i=0;i<=A;i++){const l=2*Math.max(0,i-c)-i,g=i-2*Math.max(0,i-o);for(let A=l;A<=g;A+=2){const l=A%r+r,[g,M]=[s[a+(l+1)%r],s[a+(l-1)%r]];for(p=A===-i||A<i&&M<g?g:M+1,x=p-A,[u,h]=[p,x];p<o&&x<c&&t[e+p]===f[n+x];)p++,x++;s[a+l%r]=p;const _=b-A;if(1===P&&_>-i&&_<i&&p+s[y+(r+_%r)%r]>=o){d=2*i-1;break e}}[a,y]=[y,a];for(let A=l;A<=g;A+=2){const l=A%r+r,[g,M]=[s[a+(l+1)%r],s[a+(l-1)%r]];u=A===-i||A<i&&M<g?g:M+1,h=u-A,[p,x]=[o-u,c-h];const _=e+o-1,m=n+c-1;for(;u<o&&h<c&&t[_-u]===f[m-h];)u++,h++;s[a+l%r]=u;const k=b-A;if(0===P&&k>=-i&&k<=i&&u+s[y+(r+k%r)%r]>=o){[d,u,h]=[2*i,o-u,c-h];break e}}[a,y]=[y,a]}if((d>1||u!==p&&h!==x)&&(i[l++]=[e+p,o-p,n+x,c-x],[o,c]=[u,h],o>0&&c>0))break t}if(c>o){const[t,f]=[e+o,n+o];yield[t,t,f,f+c]}else if(c<o){const[t,f]=[e+c,n+c];yield[t,t+o,f,f]}if(0===l)return;[e,o,n,c]=i[--l]}r=2*Math.min(o,c)+2,[a,y]=[0,r],d=s[1]=s[y+1]=0}}function*diff(t,e){let[o,f,n]=[0,t.length,e.length];for(;o<f&&o<n&&t[o]===e[o];)o++;if(o===f&&f===n)return;for(;f>o&&n>o&&t[--f]===e[--n];);const c=diff_rec(t,o,f+1-o,e,o,n+1-o);let i=c.next().value;for(const t of c){const[e,o,f,n]=t;if(e===o){if(f===i[3]){i[3]=n;continue}if(e>=i[0]&&e<=i[1]&&i[2]===i[3]){[i[2],i[3]]=[f,n];continue}}else if(e===i[1]){i[1]=o;continue}yield i,i=t}yield i}function*lcs(t,e){let[o,f,n]=[0,t.length,e.length];for(;o<f&&o<n&&t[o]===e[o];)o++;if(o>0&&(yield[0,0,o]),o===f&&f===n)return;let[c,i]=[f,n];for(;c>o&&i>o&&t[--c]===e[--i];);const l=diff_rec(t,o,c+1-o,e,o,i+1-o);let[r,s,a,y]=l.next().value,d=o+y-a;o+=s-r;for(const t of l)[r,s,,y]=t,o!==r&&(t.length--,[t[0],t[1],t[2]]=[o,d,r-o],yield t),[o,d]=[s,y];o<f&&(yield[o,d,f-o])}function*calcPatch(t,e){const o=ArrayBuffer.isView(e)?Uint8Array.prototype.subarray:e.slice;for(const[f,n,c,i]of diff(t,e))yield[f,n,o.call(e,c,i)]}function*applyPatch(t,e){let o=0;const f=ArrayBuffer.isView(t)?Uint8Array.prototype.subarray:t.slice;for(const[n,c,i]of e)o<n&&(yield f.call(t,o,n)),i.length>0&&(yield i),o=c;o<t.length&&(yield f.call(t,o))}Object.defineProperty(exports,"__esModule",{value:!0}),exports.diff=diff,exports.lcs=lcs,exports.calcPatch=calcPatch,exports.applyPatch=applyPatch; | ||
"use strict";function*diff_rec(t,e,f,l,o,n){const r=[];let i=0,c=2*Math.min(f,n)+2;const a=new Uint32Array(2*c);let[s,y,d,u]=[0,0,0,0,0];for(;;){t:{if(f>0&&n>0){const h=f+n,p=1&h,x=f-n,g=(h>>>1)+p,P=e+f-1,b=o+n-1;e:for(let h=0;h<=g;h++){const g=2*Math.max(0,h-n)-h,A=h-2*Math.max(0,h-f);for(let P=g;P<=A;P+=2){const g=P%c+c;let b,A=a[(g+1)%c];for(s=d=P===-h||(b=a[(g-1)%c],P<h&&b<A)?A:b+1,y=u=d-P;d<f&&u<n&&t[e+d]===l[o+u];)d++,u++;a[g%c]=d;const M=x-P;if(1===p&&M<h&&M>-h&&d+a[c+(c+M%c)%c]>=f){(2*h-1>1||s!==d)&&(r[i++]=[e+d,f-d,o+u,n-u],[f,n]=[s,y]);break e}}for(let M=g;M<=A;M+=2){const g=M%c+c;let A,_=a[c+(g+1)%c];for(s=d=M===-h||(A=a[c+(g-1)%c],M<h&&A<_)?_:A+1,y=u=d-M;s<f&&y<n&&t[P-s]===l[b-y];)s++,y++;a[c+g%c]=s;const m=x-M;if(0===p&&m<=h&&m>=-h&&s+a[(c+m%c)%c]>=f){(h>0||s!==d)&&(r[i++]=[e+f-d,d,o+n-u,u],[f,n]=[f-s,n-y]);break e}}}}if(0===f)yield[e,e,o,o+n];else{if(0!==n)break t;yield[e,e+f,o,o]}if(0===i)return;[e,f,o,n]=r[--i]}c=2*Math.min(f,n)+2,a[1]=a[c+1]=0}}function*diff(t,e){let[f,l,o]=[0,t.length,e.length];for(;f<l&&f<o&&t[f]===e[f];)f++;if(f===l&&l===o)return;for(;l>f&&o>f&&t[--l]===e[--o];);const n=diff_rec(t,f,l+1-f,e,f,o+1-f);let r=n.next().value;for(const t of n){const[e,f,l,o]=t;if(e===f){if(l===r[3]){r[3]=o;continue}if(e>=r[0]&&e<=r[1]&&r[2]===r[3]){[r[2],r[3]]=[l,o];continue}}else if(e===r[1]){r[1]=f;continue}yield r,r=t}yield r}function*lcs(t,e){let[f,l,o]=[0,t.length,e.length];for(;f<l&&f<o&&t[f]===e[f];)f++;if(f>0&&(yield[0,0,f]),f===l&&l===o)return;let[n,r]=[l,o];for(;n>f&&r>f&&t[--n]===e[--r];);const i=diff_rec(t,f,n+1-f,e,f,r+1-f);let[c,a,s,y]=i.next().value,d=f+y-s;f+=a-c;for(const t of i)[c,a,,y]=t,f!==c&&(t.length--,[t[0],t[1],t[2]]=[f,d,c-f],yield t),[f,d]=[a,y];f<l&&(yield[f,d,l-f])}function*calcPatch(t,e){const f=ArrayBuffer.isView(e)?Uint8Array.prototype.subarray:e.slice;for(const[l,o,n,r]of diff(t,e))yield[l,o,f.call(e,n,r)]}function*applyPatch(t,e){let f=0;const l=ArrayBuffer.isView(t)?Uint8Array.prototype.subarray:t.slice;for(const[o,n,r]of e)f<o&&(yield l.call(t,f,o)),r.length>0&&(yield r),f=n;f<t.length&&(yield l.call(t,f))}Object.defineProperty(exports,"__esModule",{value:!0}),exports.diff=diff,exports.lcs=lcs,exports.calcPatch=calcPatch,exports.applyPatch=applyPatch; |
{ | ||
"name": "fast-myers-diff", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A fast, minimal, memory-efficient diff algorithm on strings, arrays, and typed arrays.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -5,3 +5,3 @@ Fast-Myers-Diff | ||
This is a fast, compact, memory efficient implementation of the O(ND) Myers diff algorithm. | ||
The core diff algorithm, including blank lines and comment, is only 117 lines. With LCS and patch features added, the total library is still less than 200 lines. | ||
The core diff algorithm, including blank lines and comment, is only 110 lines. With LCS and patch features added, the total library is still less than 180 lines. | ||
Minified and including type definitions, the published library is less than 3KB. | ||
@@ -8,0 +8,0 @@ |
8950