Skip to content

Commit af7757f

Browse files
committed
debt - don't let strings-util depend on map-util
1 parent f2d3761 commit af7757f

9 files changed

Lines changed: 75 additions & 65 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
'use strict';
6+
7+
import { LRUCache } from 'vs/base/common/map';
8+
9+
/**
10+
* The normalize() method returns the Unicode Normalization Form of a given string. The form will be
11+
* the Normalization Form Canonical Composition.
12+
*
13+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize}
14+
*/
15+
export const canNormalize = typeof ((<any>'').normalize) === 'function';
16+
17+
const nfcCache = new LRUCache<string, string>(10000); // bounded to 10000 elements
18+
export function normalizeNFC(str: string): string {
19+
return normalize(str, 'NFC', nfcCache);
20+
}
21+
22+
const nfdCache = new LRUCache<string, string>(10000); // bounded to 10000 elements
23+
export function normalizeNFD(str: string): string {
24+
return normalize(str, 'NFD', nfdCache);
25+
}
26+
27+
const nonAsciiCharactersPattern = /[^\u0000-\u0080]/;
28+
function normalize(str: string, form: string, normalizedCache: LRUCache<string, string>): string {
29+
if (!canNormalize || !str) {
30+
return str;
31+
}
32+
33+
const cached = normalizedCache.get(str);
34+
if (cached) {
35+
return cached;
36+
}
37+
38+
let res: string;
39+
if (nonAsciiCharactersPattern.test(str)) {
40+
res = (<any>str).normalize(form);
41+
} else {
42+
res = str;
43+
}
44+
45+
// Use the cache for fast lookup
46+
normalizedCache.set(str, res);
47+
48+
return res;
49+
}

src/vs/base/common/strings.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
import { LRUCache } from 'vs/base/common/map';
87
import { CharCode } from 'vs/base/common/charCode';
98

109
/**
@@ -239,48 +238,6 @@ export function regExpContainsBackreference(regexpValue: string): boolean {
239238
return !!regexpValue.match(/([^\\]|^)(\\\\)*\\\d+/);
240239
}
241240

242-
/**
243-
* The normalize() method returns the Unicode Normalization Form of a given string. The form will be
244-
* the Normalization Form Canonical Composition.
245-
*
246-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize}
247-
*/
248-
export const canNormalize = typeof ((<any>'').normalize) === 'function';
249-
250-
const nfcCache = new LRUCache<string, string>(10000); // bounded to 10000 elements
251-
export function normalizeNFC(str: string): string {
252-
return normalize(str, 'NFC', nfcCache);
253-
}
254-
255-
const nfdCache = new LRUCache<string, string>(10000); // bounded to 10000 elements
256-
export function normalizeNFD(str: string): string {
257-
return normalize(str, 'NFD', nfdCache);
258-
}
259-
260-
const nonAsciiCharactersPattern = /[^\u0000-\u0080]/;
261-
function normalize(str: string, form: string, normalizedCache: LRUCache<string, string>): string {
262-
if (!canNormalize || !str) {
263-
return str;
264-
}
265-
266-
const cached = normalizedCache.get(str);
267-
if (cached) {
268-
return cached;
269-
}
270-
271-
let res: string;
272-
if (nonAsciiCharactersPattern.test(str)) {
273-
res = (<any>str).normalize(form);
274-
} else {
275-
res = str;
276-
}
277-
278-
// Use the cache for fast lookup
279-
normalizedCache.set(str, res);
280-
281-
return res;
282-
}
283-
284241
/**
285242
* Returns first index of the string that is not whitespace.
286243
* If string is empty or contains only whitespaces, returns -1

src/vs/base/node/extfs.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@
55

66
'use strict';
77

8-
import * as uuid from 'vs/base/common/uuid';
9-
import * as strings from 'vs/base/common/strings';
10-
import * as platform from 'vs/base/common/platform';
11-
import * as flow from 'vs/base/node/flow';
128
import * as fs from 'fs';
139
import * as paths from 'path';
14-
import { TPromise } from 'vs/base/common/winjs.base';
1510
import { nfcall } from 'vs/base/common/async';
11+
import { normalizeNFC } from 'vs/base/common/normalization';
12+
import * as platform from 'vs/base/common/platform';
13+
import * as strings from 'vs/base/common/strings';
14+
import * as uuid from 'vs/base/common/uuid';
15+
import { TPromise } from 'vs/base/common/winjs.base';
1616
import { encode, encodeStream } from 'vs/base/node/encoding';
17+
import * as flow from 'vs/base/node/flow';
1718

1819
const loop = flow.loop;
1920

2021
export function readdirSync(path: string): string[] {
2122
// Mac: uses NFD unicode form on disk, but we want NFC
2223
// See also https://github.com/nodejs/node/issues/2165
2324
if (platform.isMacintosh) {
24-
return fs.readdirSync(path).map(c => strings.normalizeNFC(c));
25+
return fs.readdirSync(path).map(c => normalizeNFC(c));
2526
}
2627

2728
return fs.readdirSync(path);
@@ -36,7 +37,7 @@ export function readdir(path: string, callback: (error: Error, files: string[])
3637
return callback(error, null);
3738
}
3839

39-
return callback(null, children.map(c => strings.normalizeNFC(c)));
40+
return callback(null, children.map(c => normalizeNFC(c)));
4041
});
4142
}
4243

@@ -630,7 +631,7 @@ export function watch(path: string, onChange: (type: string, path: string) => vo
630631
if (platform.isMacintosh) {
631632
// Mac: uses NFD unicode form on disk, but we want NFC
632633
// See also https://github.com/nodejs/node/issues/2165
633-
file = strings.normalizeNFC(file);
634+
file = normalizeNFC(file);
634635
}
635636
}
636637

src/vs/base/test/node/extfs/extfs.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
'use strict';
77

88
import * as assert from 'assert';
9+
import * as fs from 'fs';
910
import * as os from 'os';
10-
1111
import * as path from 'path';
12-
import * as fs from 'fs';
13-
14-
import * as uuid from 'vs/base/common/uuid';
15-
import * as strings from 'vs/base/common/strings';
16-
import * as extfs from 'vs/base/node/extfs';
1712
import { Readable } from 'stream';
13+
import { canNormalize } from 'vs/base/common/normalization';
1814
import { isLinux, isWindows } from 'vs/base/common/platform';
15+
import * as uuid from 'vs/base/common/uuid';
16+
import * as extfs from 'vs/base/node/extfs';
17+
18+
1919

2020
const ignore = () => { };
2121

@@ -224,7 +224,7 @@ suite('Extfs', () => {
224224
});
225225

226226
test('readdir', function (done) {
227-
if (strings.canNormalize && typeof process.versions['electron'] !== 'undefined' /* needs electron */) {
227+
if (canNormalize && typeof process.versions['electron'] !== 'undefined' /* needs electron */) {
228228
const id = uuid.generateUuid();
229229
const parentDir = path.join(os.tmpdir(), 'vsctests', id);
230230
const newDir = path.join(parentDir, 'extfs', id, 'öäü');

src/vs/code/electron-main/windows.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { IWorkspacesMainService, IWorkspaceIdentifier, ISingleFolderWorkspaceIde
3333
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
3434
import { mnemonicButtonLabel } from 'vs/base/common/labels';
3535
import { Schemas } from 'vs/base/common/network';
36-
import { normalizeNFC } from 'vs/base/common/strings';
36+
import { normalizeNFC } from 'vs/base/common/normalization';
3737
import URI from 'vs/base/common/uri';
3838
import { Queue } from 'vs/base/common/async';
3939
import { exists } from 'vs/base/node/pfs';

src/vs/workbench/services/files/node/watcher/nsfw/nsfwWatcherService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { IWatcherService, IWatcherRequest } from 'vs/workbench/services/files/no
1313
import { TPromise, ProgressCallback, TValueCallback, ErrorCallback } from 'vs/base/common/winjs.base';
1414
import { ThrottledDelayer } from 'vs/base/common/async';
1515
import { FileChangeType } from 'vs/platform/files/common/files';
16-
import { normalizeNFC } from 'vs/base/common/strings';
16+
import { normalizeNFC } from 'vs/base/common/normalization';
1717

1818
const nsfwActionToRawChangeType: { [key: number]: number } = [];
1919
nsfwActionToRawChangeType[nsfw.actions.CREATED] = FileChangeType.ADDED;

src/vs/workbench/services/files/node/watcher/unix/chokidarWatcherService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { TPromise } from 'vs/base/common/winjs.base';
1515
import { FileChangeType } from 'vs/platform/files/common/files';
1616
import { ThrottledDelayer } from 'vs/base/common/async';
1717
import * as strings from 'vs/base/common/strings';
18+
import { normalizeNFC } from 'vs/base/common/normalization';
1819
import { realcaseSync } from 'vs/base/node/extfs';
1920
import { isMacintosh } from 'vs/base/common/platform';
2021
import * as watcher from 'vs/workbench/services/files/node/watcher/common';
@@ -74,7 +75,7 @@ export class ChokidarWatcherService implements IWatcherService {
7475
if (isMacintosh) {
7576
// Mac: uses NFD unicode form on disk, but we want NFC
7677
// See also https://github.com/nodejs/node/issues/2165
77-
path = strings.normalizeNFC(path);
78+
path = normalizeNFC(path);
7879
}
7980

8081
if (path.indexOf(realBasePath) < 0) {

src/vs/workbench/services/search/node/fileSearch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as objects from 'vs/base/common/objects';
1818
import * as arrays from 'vs/base/common/arrays';
1919
import * as platform from 'vs/base/common/platform';
2020
import * as strings from 'vs/base/common/strings';
21+
import * as normalization from 'vs/base/common/normalization';
2122
import * as types from 'vs/base/common/types';
2223
import * as glob from 'vs/base/common/glob';
2324
import { IProgress, IUncachedSearchStats } from 'vs/platform/search/common/search';
@@ -255,7 +256,7 @@ export class FileWalker {
255256
}
256257

257258
// Mac: uses NFD unicode form on disk, but we want NFC
258-
const normalized = leftover + (isMac ? strings.normalizeNFC(stdout) : stdout);
259+
const normalized = leftover + (isMac ? normalization.normalizeNFC(stdout) : stdout);
259260
const relativeFiles = normalized.split(useRipgrep ? '\n' : '\n./');
260261
if (!useRipgrep && first && normalized.length >= 2) {
261262
first = false;
@@ -820,4 +821,4 @@ class AbsoluteAndRelativeParsedExpression {
820821

821822
return pathTerms;
822823
}
823-
}
824+
}

src/vs/workbench/services/search/node/ripgrepFileSearch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { rgPath } from 'vscode-ripgrep';
88

99
import { isMacintosh as isMac } from 'vs/base/common/platform';
1010
import * as glob from 'vs/base/common/glob';
11-
import { normalizeNFD, startsWith } from 'vs/base/common/strings';
11+
import { startsWith } from 'vs/base/common/strings';
12+
import { normalizeNFD } from 'vs/base/common/normalization';
1213

1314
import { IFolderSearch, IRawSearch } from './search';
1415
import { foldersToIncludeGlobs, foldersToRgExcludeGlobs } from './ripgrepTextSearch';
@@ -83,4 +84,4 @@ function getRgArgs(config: IRawSearch, folderQuery: IFolderSearch, includePatter
8384

8485
function anchor(glob: string) {
8586
return startsWith(glob, '**') || startsWith(glob, '/') ? glob : `/${glob}`;
86-
}
87+
}

0 commit comments

Comments
 (0)