fast-myers-diff
Advanced tools
Comparing version 3.1.0 to 3.2.0
@@ -18,2 +18,3 @@ export declare type GenericIndexable<T> = { | ||
export declare function applyPatch<T, S extends Sliceable<T>>(xs: S, patch: Iterable<[number, number, S]>): Generator<S>; | ||
export declare function calcSlices<T, S extends Sliceable<T>>(xs: S, ys: S, eq?: Comparator): Generator<[-1 | 0 | 1, S]>; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.applyPatch=exports.calcPatch=exports.lcs=exports.diff=exports.diff_core=void 0;function diff_internal(state,c){const{b,eq,stack_base}=state;let{i,N,j,M,Z,stack_top}=state;for(;;){switch(c){case 0:{Z_block:while(N>0&&M>0){b.fill(0,0,2*Z);const W=N-M;const L=N+M;const parity=L&1;const offsetx=i+N-1;const offsety=j+M-1;const hmax=(L+parity)/2;let z;h_loop:for(let h=0;h<=hmax;h++){const kmin=2*Math.max(0,h-M)-h;const kmax=h-2*Math.max(0,h-N);for(let k=kmin;k<=kmax;k+=2){const gkm=b[k-1-Z*Math.floor((k-1)/Z)];const gkp=b[k+1-Z*Math.floor((k+1)/Z)];const u=(k===-h||(k!==h&&gkm<gkp))?gkp:gkm+1;const v=u-k;let x=u;let y=v;while(x<N&&y<M&&eq(i+x,j+y)) | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.calcSlices=exports.applyPatch=exports.calcPatch=exports.lcs=exports.diff=exports.diff_core=void 0;function diff_internal(state,c){const{b,eq,stack_base}=state;let{i,N,j,M,Z,stack_top}=state;for(;;){switch(c){case 0:{Z_block:while(N>0&&M>0){b.fill(0,0,2*Z);const W=N-M;const L=N+M;const parity=L&1;const offsetx=i+N-1;const offsety=j+M-1;const hmax=(L+parity)/2;let z;h_loop:for(let h=0;h<=hmax;h++){const kmin=2*Math.max(0,h-M)-h;const kmax=h-2*Math.max(0,h-N);for(let k=kmin;k<=kmax;k+=2){const gkm=b[k-1-Z*Math.floor((k-1)/Z)];const gkp=b[k+1-Z*Math.floor((k+1)/Z)];const u=(k===-h||(k!==h&&gkm<gkp))?gkp:gkm+1;const v=u-k;let x=u;let y=v;while(x<N&&y<M&&eq(i+x,j+y)) | ||
x++,y++;b[k-Z*Math.floor(k/Z)]=x;if(parity===1&&(z=W-k)>=1-h&&z<h&&x+b[Z+z-Z*Math.floor(z/Z)]>=N){if(h>1||x!==u){stack_base[stack_top++]=i+x;stack_base[stack_top++]=N-x;stack_base[stack_top++]=j+y;stack_base[stack_top++]=M-y;N=u;M=v;Z=2*(Math.min(N,M)+1);continue Z_block;} | ||
@@ -44,2 +44,8 @@ else | ||
yield slice.call(xs,i);} | ||
exports.applyPatch=applyPatch; | ||
exports.applyPatch=applyPatch;function*calcSlices(xs,ys,eq){let i=0;const slice=ArrayBuffer.isView(xs)?Uint8Array.prototype.subarray:xs.slice;for(const[dels,dele,inss,inse]of diff(xs,ys,eq)){if(i<dels) | ||
yield[0,slice.call(xs,i,dels)];if(dels<dele) | ||
yield[-1,slice.call(xs,dels,dele)];if(inss<inse) | ||
yield[1,slice.call(ys,inss,inse)];i=dele;} | ||
if(i<xs.length) | ||
yield[0,xs.slice(i)];} | ||
exports.calcSlices=calcSlices; |
{ | ||
"name": "fast-myers-diff", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "A fast, minimal, memory-efficient diff algorithm on strings, arrays, and typed arrays.", | ||
@@ -5,0 +5,0 @@ "main": "bin/index.js", |
@@ -48,2 +48,4 @@ Fast-Myers-Diff | ||
declare function applyPatch<T extends Sliceable>(xs: T, patch: Iterable<[number, number, T]>): Generator<T>; | ||
declare function calcSlices<T, S extends Sliceable<T>>(xs: S, ys: S, eq?: Comparator): Generator<[-1 | 0 | 1, S]>; | ||
``` | ||
@@ -65,4 +67,6 @@ | ||
`diff` and `lcs` will work with custom container types, as long as your container objects have a numeric `length` property. `calcPatch` and `applyPatch` will work with custom types provided that they also implement a suitable `slice(start[, end])` method. | ||
`calcSlices(xs, ys)` is a thin wrapper over `diff` which uses the calculated indices to return the complete list of segments of `xs` and `ys` coded by whether they are unique to `xs` (deletions from `xs` to `ys`), components of the longest common subsequence, or unique to `ys` (insertions from `xs` to `ys`). Replacements at the same location result in yeilding the slice of `xs` first, followed by the slice of `ys`. The output elements are pairs of `[type, slice]`, where a type of -1 indicates the slice comes from `xs`, a type of 0 indicates that the slice is common, and a type of 1 indicates that the slice comes from `ys`. This is useful for displaying diffs in a UI, where you want all components shown with deletions and insertions highlighted. | ||
`diff` and `lcs` will work with custom container types, as long as your container objects have a numeric `length` property. `calcPatch`, `applyPatch`, and `calcSlices` will work with custom types provided that they also implement a suitable `slice(start[, end])` method. | ||
### Empirical results | ||
@@ -69,0 +73,0 @@ |
21238
72
112