Skip to content

Commit c03b04b

Browse files
author
Kanchalai Tanglertsampan
committed
Only use localeCompare in CompareStrings
1 parent d7e33c9 commit c03b04b

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

src/compiler/core.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ namespace ts {
2323
// More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times.
2424
export const collator: { compare(a: string, b: string): number } = typeof Intl === "object" && typeof Intl.Collator === "function" ? new Intl.Collator() : undefined;
2525

26+
// This means "compare in a case insensitive manner but consider accentsor other diacritic marks"
27+
// (e.g. a ≠ b, a ≠ á, a = A)
28+
const accentSensitivity: Intl.CollatorOptions = { usage: "sort", sensitivity: "accent" };
29+
2630
/**
2731
* Use this function instead of calling "String.prototype.localeCompre". This function will preform appropriate check to make sure that
2832
* "typeof Intl" is correct as there are reported issues #11110 nad #11339.
@@ -1029,13 +1033,13 @@ namespace ts {
10291033
return a < b ? Comparison.LessThan : Comparison.GreaterThan;
10301034
}
10311035

1032-
export function compareStrings(a: string, b: string, ignoreCase?: boolean): Comparison {
1036+
export function compareStrings(a: string, b: string, locales?: string | string[], options?: Intl.CollatorOptions): Comparison {
10331037
if (a === b) return Comparison.EqualTo;
10341038
if (a === undefined) return Comparison.LessThan;
10351039
if (b === undefined) return Comparison.GreaterThan;
1036-
if (ignoreCase) {
1037-
const result = localeCompare(a, b, /*locales*/ undefined, /*options*/ { usage: "sort", sensitivity: "accent" });
1038-
if (result) {
1040+
if (options) {
1041+
if (collator && String.prototype.localeCompare) {
1042+
const result = a.localeCompare(b, locales, options);
10391043
return result < 0 ? Comparison.LessThan : result > 0 ? Comparison.GreaterThan : Comparison.EqualTo;
10401044
}
10411045

@@ -1048,7 +1052,7 @@ namespace ts {
10481052
}
10491053

10501054
export function compareStringsCaseInsensitive(a: string, b: string) {
1051-
return compareStrings(a, b, /*ignoreCase*/ true);
1055+
return compareStrings(a, b, /*locales*/ undefined, accentSensitivity);
10521056
}
10531057

10541058
function getDiagnosticFileName(diagnostic: Diagnostic): string {
@@ -1418,7 +1422,8 @@ namespace ts {
14181422
const bComponents = getNormalizedPathComponents(b, currentDirectory);
14191423
const sharedLength = Math.min(aComponents.length, bComponents.length);
14201424
for (let i = 0; i < sharedLength; i++) {
1421-
const result = compareStrings(aComponents[i], bComponents[i], ignoreCase);
1425+
const result = compareStrings(aComponents[i], bComponents[i],
1426+
/*locales*/ undefined, /*options*/ ignoreCase ? accentSensitivity : undefined);
14221427
if (result !== Comparison.EqualTo) {
14231428
return result;
14241429
}
@@ -1440,7 +1445,8 @@ namespace ts {
14401445
}
14411446

14421447
for (let i = 0; i < parentComponents.length; i++) {
1443-
const result = compareStrings(parentComponents[i], childComponents[i], ignoreCase);
1448+
const result = compareStrings(parentComponents[i], childComponents[i],
1449+
/*locales*/ undefined, /*options*/ ignoreCase ? accentSensitivity : undefined);
14441450
if (result !== Comparison.EqualTo) {
14451451
return false;
14461452
}

src/harness/harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ namespace Harness {
16341634

16351635
export function collateOutputs(outputFiles: Harness.Compiler.GeneratedFile[]): string {
16361636
// Collect, test, and sort the fileNames
1637-
outputFiles.sort((a, b) => ts.localeCompare(cleanName(a.fileName), cleanName(b.fileName)));
1637+
outputFiles.sort((a, b) => ts.compareStrings(cleanName(a.fileName), cleanName(b.fileName)));
16381638

16391639
// Emit them
16401640
let result = "";

src/server/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ namespace ts.server {
959959
result.push({ name, kind, kindModifiers, sortText, replacementSpan: convertedSpan });
960960
}
961961
return result;
962-
}, []).sort((a, b) => localeCompare(a.name, b.name));
962+
}, []).sort((a, b) => ts.compareStrings(a.name, b.name));
963963
}
964964
else {
965965
return completions;

src/services/navigateTo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ namespace ts.NavigateTo {
188188
// We first sort case insensitively. So "Aaa" will come before "bar".
189189
// Then we sort case sensitively, so "aaa" will come before "Aaa".
190190
return i1.matchKind - i2.matchKind ||
191-
ts.localeCompare(i1.name, i2.name, /*locales*/ undefined, /*options*/ baseSensitivity) ||
192-
ts.localeCompare(i1.name, i2.name);
191+
ts.compareStrings(i1.name, i2.name, /*locales*/ undefined, /*options*/ baseSensitivity) ||
192+
ts.compareStrings(i1.name, i2.name);
193193
}
194194

195195
function createNavigateToItem(rawItem: RawNavigateToItem): NavigateToItem {

src/services/navigationBar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ namespace ts.NavigationBar {
335335
if (chA === "'" && chB === "\"") {
336336
return -1;
337337
}
338-
const cmp = ts.localeCompare(chA.toLocaleLowerCase(), chB.toLocaleLowerCase());
338+
const cmp = ts.compareStrings(chA.toLocaleLowerCase(), chB.toLocaleLowerCase());
339339
if (cmp !== 0) {
340340
return cmp;
341341
}

0 commit comments

Comments
 (0)