From 17a61a531c1b57aaf64aa3bf52a21fdbde9f15e7 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Tue, 31 May 2016 10:25:13 +0200 Subject: [PATCH] make isUNC more spec compliant --- src/vs/base/common/paths.ts | 42 ++++++++++++++++++++++++--- src/vs/base/test/common/paths.test.ts | 16 +++++----- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/vs/base/common/paths.ts b/src/vs/base/common/paths.ts index 57757422e74..ae76ac45628 100644 --- a/src/vs/base/common/paths.ts +++ b/src/vs/base/common/paths.ts @@ -233,14 +233,48 @@ export const join: (...parts: string[]) => string = function () { }; +/** + * Check if the path follows this pattern: `\\hostname\sharename`. + * + * @see https://msdn.microsoft.com/en-us/library/gg465305.aspx + * @return A boolean indication if the path is a UNC path, on none-windows + * always false. + */ export function isUNC(path: string): boolean { - if (!isWindows || !path) { - return false; // UNC is a windows concept + if (!isWindows) { + // UNC is a windows concept + return false; } - path = this.normalize(path, true); + if (!path || path.length < 5) { + // at least \\a\b + return false; + } - return path[0] === nativeSep && path[1] === nativeSep; + let code = path.charCodeAt(0); + if (code !== _backslash) { + return false; + } + code = path.charCodeAt(1); + if (code !== _backslash) { + return false; + } + let pos = 2; + let start = pos; + for (; pos < path.length; pos++) { + code = path.charCodeAt(pos); + if (code === _backslash) { + break; + } + } + if (start === pos) { + return false; + } + code = path.charCodeAt(pos + 1); + if (isNaN(code) || code === _backslash) { + return false; + } + return true; } function isPosixAbsolute(path: string): boolean { diff --git a/src/vs/base/test/common/paths.test.ts b/src/vs/base/test/common/paths.test.ts index d509bb9c88b..f43a6d8dffc 100644 --- a/src/vs/base/test/common/paths.test.ts +++ b/src/vs/base/test/common/paths.test.ts @@ -174,14 +174,16 @@ suite('Paths', () => { }); test('isUNC', () => { - assert(!paths.isUNC('foo')); - assert(!paths.isUNC('/foo')); - assert(!paths.isUNC('\\foo')); - if (platform.isWindows) { - assert(paths.isUNC('\\\\foo')); - } else { - assert(!paths.isUNC('\\\\foo')); + assert.ok(!paths.isUNC('foo')); + assert.ok(!paths.isUNC('/foo')); + assert.ok(!paths.isUNC('\\foo')); + assert.ok(!paths.isUNC('\\\\foo')); + assert.ok(paths.isUNC('\\\\a\\b')); + assert.ok(!paths.isUNC('//a/b')); + assert.ok(paths.isUNC('\\\\server\\share')); + assert.ok(paths.isUNC('\\\\server\\share\\')); + assert.ok(paths.isUNC('\\\\server\\share\\path')); } }); -- GitLab