| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- import { normalize, posix, sep } from './path.js';
- import { isWindows } from './platform.js';
- import { startsWithIgnoreCase } from './strings.js';
- /**
- * Takes a Windows OS path and changes backward slashes to forward slashes.
- * This should only be done for OS paths from Windows (or user provided paths potentially from Windows).
- * Using it on a Linux or MaxOS path might change it.
- */
- export function toSlashes(osPath) {
- return osPath.replace(/[\\/]/g, posix.sep);
- }
- /**
- * Takes a Windows OS path (using backward or forward slashes) and turns it into a posix path:
- * - turns backward slashes into forward slashes
- * - makes it absolute if it starts with a drive letter
- * This should only be done for OS paths from Windows (or user provided paths potentially from Windows).
- * Using it on a Linux or MaxOS path might change it.
- */
- export function toPosixPath(osPath) {
- if (osPath.indexOf('/') === -1) {
- osPath = toSlashes(osPath);
- }
- if (/^[a-zA-Z]:(\/|$)/.test(osPath)) { // starts with a drive letter
- osPath = '/' + osPath;
- }
- return osPath;
- }
- export function isEqualOrParent(base, parentCandidate, ignoreCase, separator = sep) {
- if (base === parentCandidate) {
- return true;
- }
- if (!base || !parentCandidate) {
- return false;
- }
- if (parentCandidate.length > base.length) {
- return false;
- }
- if (ignoreCase) {
- const beginsWith = startsWithIgnoreCase(base, parentCandidate);
- if (!beginsWith) {
- return false;
- }
- if (parentCandidate.length === base.length) {
- return true; // same path, different casing
- }
- let sepOffset = parentCandidate.length;
- if (parentCandidate.charAt(parentCandidate.length - 1) === separator) {
- sepOffset--; // adjust the expected sep offset in case our candidate already ends in separator character
- }
- return base.charAt(sepOffset) === separator;
- }
- if (parentCandidate.charAt(parentCandidate.length - 1) !== separator) {
- parentCandidate += separator;
- }
- return base.indexOf(parentCandidate) === 0;
- }
- export function isWindowsDriveLetter(char0) {
- return char0 >= 65 /* A */ && char0 <= 90 /* Z */ || char0 >= 97 /* a */ && char0 <= 122 /* z */;
- }
- export function isRootOrDriveLetter(path) {
- const pathNormalized = normalize(path);
- if (isWindows) {
- if (path.length > 3) {
- return false;
- }
- return hasDriveLetter(pathNormalized) &&
- (path.length === 2 || pathNormalized.charCodeAt(2) === 92 /* Backslash */);
- }
- return pathNormalized === posix.sep;
- }
- export function hasDriveLetter(path) {
- if (isWindows) {
- return isWindowsDriveLetter(path.charCodeAt(0)) && path.charCodeAt(1) === 58 /* Colon */;
- }
- return false;
- }
|