Skip to content

Commit

Permalink
fix: Use swap16 to convert Unicode 16be to 16le (#4468)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed May 7, 2023
1 parent a1b7c5d commit 5f9f6ae
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 9 deletions.
4 changes: 1 addition & 3 deletions packages/cspell-io/src/common/encode-decode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ describe('encode-decode', () => {

test.each`
encoding
${undefined}
${'utf8'}
${'utf16le'}
`('swapBytesInPlace $encoding', ({ encoding }) => {
const src = Buffer.from('The sun is shining.', encoding);
Expand All @@ -35,7 +33,7 @@ describe('encode-decode', () => {
${'123'}
${'1234'}
`('swapBytes $encoding', ({ text }) => {
const src = Buffer.from(text);
const src = Buffer.from(text, 'utf16le');
const buf = Buffer.from(src);
expect(buf).toEqual(src);
const buf2 = swapBytes(buf);
Expand Down
7 changes: 1 addition & 6 deletions packages/cspell-io/src/common/encode-decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ export function decode(buf: Buffer, encoding?: BufferEncodingExt): string {
}

export function swapBytesInPlace(buf: Buffer): Buffer {
for (let i = 0; i < buf.length - 1; i += 2) {
const v = buf[i];
buf[i] = buf[i + 1];
buf[i + 1] = v;
}
return buf;
return buf.swap16();
}

export function swapBytes(buf: Buffer): Buffer {
Expand Down

0 comments on commit 5f9f6ae

Please sign in to comment.