Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support spell checking utf16 files with BOM #4232

Merged
merged 4 commits into from
Feb 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: support reading utf16 dictionaries
Jason3S committed Feb 25, 2023
commit f31172e2c98e8c48efff36ec45fcb31279f152b7
Binary file added packages/Samples/dicts/cities.utf16be.txt
Binary file not shown.
Binary file added packages/Samples/dicts/cities.utf16le.txt
Binary file not shown.
3 changes: 2 additions & 1 deletion packages/cspell-io/src/handlers/node/file.ts
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import type { URL } from 'url';
import { fileURLToPath } from 'url';
import { gunzipSync, gzipSync } from 'zlib';

import { decode } from '../../common/encode-decode';
import { toError } from '../../errors';
import { decodeDataUrl } from '../../node/dataUrl';
import { fetchURL } from '../../node/file/fetch';
@@ -152,7 +153,7 @@ const handleRequestFsReadBinaryFileData = RequestFsReadBinaryFile.createRequestH
);

function bufferToText(buf: Buffer, encoding: BufferEncoding): string {
return buf[0] === 0x1f && buf[1] === 0x8b ? bufferToText(gunzipSync(buf), encoding) : buf.toString(encoding);
return buf[0] === 0x1f && buf[1] === 0x8b ? decode(gunzipSync(buf), encoding) : decode(buf, encoding);
}

/**
Binary file added packages/cspell-lib/samples/words.utf16be.txt
Binary file not shown.
Binary file added packages/cspell-lib/samples/words.utf16le.txt
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -139,7 +139,9 @@ describe('Validate DictionaryLoader', () => {
${dDef({ name: 'words', path: dict('cities.txt'), type: 'W' })} | ${'New York'} | ${false}
${dDef({ name: 'words', path: dict('cities.txt'), type: 'W' })} | ${'York'} | ${true}
${{ name: 'cities', words: ['Paris', 'New York'] }} | ${'New York'} | ${true}
`('sync load dict has word $def $word', async ({ def, word, hasWord }) => {
${dDef({ name: 'words', path: dict('cities.utf16le.txt') })} | ${'New York'} | ${true}
${dDef({ name: 'words', path: dict('cities.utf16be.txt') })} | ${'New York'} | ${true}
`('async load dict has word $def $word', async ({ def, word, hasWord }) => {
const d = await loadDictionary(def);
expect(d.has(word)).toBe(hasWord);
});
@@ -157,6 +159,8 @@ describe('Validate DictionaryLoader', () => {
${dDef({ name: 'words', path: dict('cities.txt'), type: 'W' })} | ${'New York'} | ${false}
${dDef({ name: 'words', path: dict('cities.txt'), type: 'W' })} | ${'York'} | ${true}
${{ name: 'cities', words: ['Paris', 'New York'] }} | ${'New York'} | ${true}
${dDef({ name: 'words', path: dict('cities.utf16le.txt') })} | ${'New York'} | ${true}
${dDef({ name: 'words', path: dict('cities.utf16be.txt') })} | ${'New York'} | ${true}
`('sync load dict has word $def $word', ({ def, word, hasWord }) => {
const d = loadDictionarySync(def);
expect(d.has(word)).toBe(hasWord);