Skip to content

Commit c4200f2

Browse files
committed
refactor(webpack-cli): clarify myersX params and cover long-vs-short case
Address review feedback: rename the swapped myersX parameters to longer/shorter so they match their internal usage, and add a regression test for a long string against a much shorter one (40 vs 5). https://claude.ai/code/session_013zogbY9uURTCwhdRrifVBt
1 parent 74caa91 commit c4200f2

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

packages/webpack-cli/src/levenshtein.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ function myers32(a: string, b: string): number {
4646
return sc;
4747
}
4848

49-
function myersX(b: string, a: string): number {
50-
const n = a.length;
51-
const m = b.length;
49+
function myersX(longer: string, shorter: string): number {
50+
const n = shorter.length;
51+
const m = longer.length;
5252
const mhc: number[] = [];
5353
const phc: number[] = [];
5454
const horizontalSize = Math.ceil(n / 32);
@@ -68,11 +68,11 @@ function myersX(b: string, a: string): number {
6868
const verticalLen = Math.min(32, m) + start;
6969

7070
for (let k = start; k < verticalLen; k++) {
71-
peq[b.charCodeAt(k)] |= 1 << k;
71+
peq[longer.charCodeAt(k)] |= 1 << k;
7272
}
7373

7474
for (let i = 0; i < n; i++) {
75-
const eq = peq[a.charCodeAt(i)];
75+
const eq = peq[shorter.charCodeAt(i)];
7676
const pb = (phc[(i / 32) | 0] >>> i) & 1;
7777
const mb = (mhc[(i / 32) | 0] >>> i) & 1;
7878
const xv = eq | mv;
@@ -95,7 +95,7 @@ function myersX(b: string, a: string): number {
9595
}
9696

9797
for (let k = start; k < verticalLen; k++) {
98-
peq[b.charCodeAt(k)] = 0;
98+
peq[longer.charCodeAt(k)] = 0;
9999
}
100100
}
101101

@@ -105,13 +105,13 @@ function myersX(b: string, a: string): number {
105105
const verticalLen = Math.min(32, m - start) + start;
106106

107107
for (let k = start; k < verticalLen; k++) {
108-
peq[b.charCodeAt(k)] |= 1 << k;
108+
peq[longer.charCodeAt(k)] |= 1 << k;
109109
}
110110

111111
let score = m;
112112

113113
for (let i = 0; i < n; i++) {
114-
const eq = peq[a.charCodeAt(i)];
114+
const eq = peq[shorter.charCodeAt(i)];
115115
const pb = (phc[(i / 32) | 0] >>> i) & 1;
116116
const mb = (mhc[(i / 32) | 0] >>> i) & 1;
117117
const xv = eq | mv;
@@ -137,7 +137,7 @@ function myersX(b: string, a: string): number {
137137
}
138138

139139
for (let k = start; k < verticalLen; k++) {
140-
peq[b.charCodeAt(k)] = 0;
140+
peq[longer.charCodeAt(k)] = 0;
141141
}
142142

143143
return score;

test/api/levenshtein.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,12 @@ describe("distance", () => {
3333
expect(distance(a, b)).toBe(1);
3434
expect(distance("a".repeat(40), "b".repeat(40))).toBe(40);
3535
});
36+
37+
it("should handle a long string against a much shorter one", () => {
38+
const long = "abcdefghijklmnopqrstuvwxyz0123456789ABCD";
39+
40+
expect(long).toHaveLength(40);
41+
expect(distance(long, "abcde")).toBe(35);
42+
expect(distance("abcde", long)).toBe(35);
43+
});
3644
});

0 commit comments

Comments
 (0)