-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
73f0ec6
commit 22e40f9
Showing
6 changed files
with
182 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { jschardet } from "./vscodeModules"; | ||
|
||
jschardet.Constants.MINIMUM_THRESHOLD = 0.2; | ||
jschardet.MacCyrillicModel.mTypicalPositiveRatio += 0.001; | ||
|
||
function detectEncodingByBOM(buffer: Buffer): string | null { | ||
if (!buffer || buffer.length < 2) { | ||
return null; | ||
} | ||
|
||
const b0 = buffer.readUInt8(0); | ||
const b1 = buffer.readUInt8(1); | ||
|
||
// UTF-16 BE | ||
if (b0 === 0xfe && b1 === 0xff) { | ||
return "utf16be"; | ||
} | ||
|
||
// UTF-16 LE | ||
if (b0 === 0xff && b1 === 0xfe) { | ||
return "utf16le"; | ||
} | ||
|
||
if (buffer.length < 3) { | ||
return null; | ||
} | ||
|
||
const b2 = buffer.readUInt8(2); | ||
|
||
// UTF-8 | ||
if (b0 === 0xef && b1 === 0xbb && b2 === 0xbf) { | ||
return "utf8"; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
const IGNORE_ENCODINGS = ["ascii", "utf-8", "utf-16", "utf-32"]; | ||
|
||
const JSCHARDET_TO_ICONV_ENCODINGS: { [name: string]: string } = { | ||
ibm866: "cp866", | ||
big5: "cp950" | ||
}; | ||
|
||
export function detectEncoding(buffer: Buffer): string | null { | ||
const result = detectEncodingByBOM(buffer); | ||
|
||
if (result) { | ||
return result; | ||
} | ||
|
||
const detected = jschardet.detect(buffer); | ||
|
||
if (!detected || !detected.encoding || detected.confidence < 0.8) { | ||
return null; | ||
} | ||
|
||
const encoding = detected.encoding; | ||
|
||
// Ignore encodings that cannot guess correctly | ||
// (http://chardet.readthedocs.io/en/latest/supported-encodings.html) | ||
if (0 <= IGNORE_ENCODINGS.indexOf(encoding.toLowerCase())) { | ||
return null; | ||
} | ||
|
||
const normalizedEncodingName = encoding | ||
.replace(/[^a-zA-Z0-9]/g, "") | ||
.toLowerCase(); | ||
const mapped = JSCHARDET_TO_ICONV_ENCODINGS[normalizedEncodingName]; | ||
|
||
return mapped || normalizedEncodingName; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.