Skip to content

Commit dcd4589

Browse files
Simon-XYDTclaude
andcommitted
fix(shared): return "" from sliceUtf16Safe when end <= start, matching native .slice
sliceUtf16Safe silently swapped reversed bounds (to < from) instead of returning "" like String.prototype.slice, creating a subtle footgun for callers with dynamic start/end pairs. Caller scan across src/, extensions/, and packages/ confirmed no production code relies on the old swap behavior — all callers use (text, 0, N) or (text, -N) forms only. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent cbb920c commit dcd4589

2 files changed

Lines changed: 4 additions & 6 deletions

File tree

packages/normalization-core/src/utf16-slice.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ describe("sliceUtf16Safe", () => {
2323
expect(sliceUtf16Safe("hello", 0, 10)).toBe("hello");
2424
});
2525

26-
it("swaps start and end when start > end", () => {
27-
expect(sliceUtf16Safe("hello", 3, 1)).toBe("el");
26+
it("returns empty when start > end, matching String.prototype.slice", () => {
27+
expect(sliceUtf16Safe("hello", 3, 1)).toBe("");
2828
});
2929

3030
it("preserves emoji with surrogate pairs", () => {

packages/normalization-core/src/utf16-slice.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ export function sliceUtf16Safe(input: string, start: number, end?: number): stri
1919
let from = start < 0 ? Math.max(len + start, 0) : Math.min(start, len);
2020
let to = end === undefined ? len : end < 0 ? Math.max(len + end, 0) : Math.min(end, len);
2121

22-
if (to < from) {
23-
const tmp = from;
24-
from = to;
25-
to = tmp;
22+
if (to <= from) {
23+
return "";
2624
}
2725

2826
if (from > 0 && from < len) {

0 commit comments

Comments
 (0)