提交 17a61a53 编写于 作者: J Johannes Rieken

make isUNC more spec compliant

上级 874fb60b
......@@ -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 {
......
......@@ -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'));
}
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册