Skip to content

Commit

Permalink
fix: added experimental encoding priority list (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnstonCode committed Feb 21, 2020
1 parent 609d44d commit 706dbc1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,14 @@
"description": "Try the experimental encoding detection",
"default": false
},
"svn.experimental.encoding_priority": {
"type":"array",
"description": "Priority of encoding",
"default": [],
"examples": [
["UTF-8", "GB18030", "windows-1251"]
]
},
"svn.gravatar.icon_url": {
"type": "string",
"description": "Url for the gravitar icon using the <AUTHOR>, <AUTHOR_MD5> and <SIZE> placeholders",
Expand Down
27 changes: 21 additions & 6 deletions src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const JSCHARDET_TO_ICONV_ENCODINGS: { [name: string]: string } = {
big5: "cp950"
};

function normaliseEncodingName(name: string): string {
return name.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
}

export function detectEncoding(buffer: Buffer): string | null {
const result = detectEncodingByBOM(buffer);

Expand All @@ -58,9 +62,22 @@ export function detectEncoding(buffer: Buffer): string | null {
false
);
if (experimental) {
const detected = chardet.detect(buffer);
if (detected) {
return detected.replace(/[^a-zA-Z0-9]/g, "").toLocaleLowerCase();
const detected = chardet.detectAll(buffer);
const encodingPriorities = configuration.get<string[]>(
"experimental.encoding_priority",
[]
);

if (!detected) {
return null;
}

for (const pri of encodingPriorities) {
for (const det of detected) {
if (normaliseEncodingName(pri) === normaliseEncodingName(det.name)) {
return normaliseEncodingName(det.name);
}
}
}

return null;
Expand All @@ -80,9 +97,7 @@ export function detectEncoding(buffer: Buffer): string | null {
return null;
}

const normalizedEncodingName = encoding
.replace(/[^a-zA-Z0-9]/g, "")
.toLowerCase();
const normalizedEncodingName = normaliseEncodingName(encoding);
const mapped = JSCHARDET_TO_ICONV_ENCODINGS[normalizedEncodingName];

return mapped || normalizedEncodingName;
Expand Down

0 comments on commit 706dbc1

Please sign in to comment.