Files
bun.sh/test/js/node/buffer-utf16.test.ts
pfg c69ed120e9 Rename some instances of latin1 to cp1252 (#22059)
in JS, `new TextDecoder("latin1").decode(...)` uses cp1252. In python,
latin1 is half-width utf-16. In our code, latin1 typically refers to
half-width utf-16 because JavaScriptCore uses that for most strings, but
sometimes it refers to cp1252. Rename the cp1252 functions to be called
cp1252

Also fixes an issue where Buffer.from with utf-16le would sometimes
output the wrong value:

```js
$> bun -p "Buffer.from('\x80', 'utf-16le')"
<Buffer ac 20>
$> node -p "Buffer.from('\x80', 'utf-16le')"
<Buffer 80 00>
$> bun-debug -p "Buffer.from('\x80', 'utf-16le')"
<Buffer 80 00>
```
2025-08-28 17:28:38 -07:00

35 lines
1.4 KiB
TypeScript

import { expect, test } from "bun:test";
test("utf16-le buffer", () => {
const twoByteString = new Array(16)
.fill(0)
.map((_, i) =>
Buffer.from(
new Array(16)
.fill(0)
.map((_, j) => String.fromCharCode(i * 16 + j))
.join(""),
"utf-16le",
).toString("hex"),
)
.join("\n");
expect(twoByteString.toString("hex")).toEqual(
`00000100020003000400050006000700080009000a000b000c000d000e000f00
10001100120013001400150016001700180019001a001b001c001d001e001f00
20002100220023002400250026002700280029002a002b002c002d002e002f00
30003100320033003400350036003700380039003a003b003c003d003e003f00
40004100420043004400450046004700480049004a004b004c004d004e004f00
50005100520053005400550056005700580059005a005b005c005d005e005f00
60006100620063006400650066006700680069006a006b006c006d006e006f00
70007100720073007400750076007700780079007a007b007c007d007e007f00
80008100820083008400850086008700880089008a008b008c008d008e008f00
90009100920093009400950096009700980099009a009b009c009d009e009f00
a000a100a200a300a400a500a600a700a800a900aa00ab00ac00ad00ae00af00
b000b100b200b300b400b500b600b700b800b900ba00bb00bc00bd00be00bf00
c000c100c200c300c400c500c600c700c800c900ca00cb00cc00cd00ce00cf00
d000d100d200d300d400d500d600d700d800d900da00db00dc00dd00de00df00
e000e100e200e300e400e500e600e700e800e900ea00eb00ec00ed00ee00ef00
f000f100f200f300f400f500f600f700f800f900fa00fb00fc00fd00fe00ff00`,
);
});