* Returns length of the root part of a path or URL (i.e. length of "/", "x:/", "//server/share/, file:///user/files"). * If the root is part of a URL, the twos-complement of the root length is returned.
(path)
| 7655 | * If the root is part of a URL, the twos-complement of the root length is returned. |
| 7656 | */ |
| 7657 | function getEncodedRootLength(path) { |
| 7658 | if (!path) |
| 7659 | return 0; |
| 7660 | var ch0 = path.charCodeAt(0); |
| 7661 | // POSIX or UNC |
| 7662 | if (ch0 === 47 /* CharacterCodes.slash */ || ch0 === 92 /* CharacterCodes.backslash */) { |
| 7663 | if (path.charCodeAt(1) !== ch0) |
| 7664 | return 1; // POSIX: "/" (or non-normalized "\") |
| 7665 | var p1 = path.indexOf(ch0 === 47 /* CharacterCodes.slash */ ? ts.directorySeparator : ts.altDirectorySeparator, 2); |
| 7666 | if (p1 < 0) |
| 7667 | return path.length; // UNC: "//server" or "\\server" |
| 7668 | return p1 + 1; // UNC: "//server/" or "\\server\" |
| 7669 | } |
| 7670 | // DOS |
| 7671 | if (isVolumeCharacter(ch0) && path.charCodeAt(1) === 58 /* CharacterCodes.colon */) { |
| 7672 | var ch2 = path.charCodeAt(2); |
| 7673 | if (ch2 === 47 /* CharacterCodes.slash */ || ch2 === 92 /* CharacterCodes.backslash */) |
| 7674 | return 3; // DOS: "c:/" or "c:\" |
| 7675 | if (path.length === 2) |
| 7676 | return 2; // DOS: "c:" (but not "c:d") |
| 7677 | } |
| 7678 | // URL |
| 7679 | var schemeEnd = path.indexOf(urlSchemeSeparator); |
| 7680 | if (schemeEnd !== -1) { |
| 7681 | var authorityStart = schemeEnd + urlSchemeSeparator.length; |
| 7682 | var authorityEnd = path.indexOf(ts.directorySeparator, authorityStart); |
| 7683 | if (authorityEnd !== -1) { // URL: "file:///", "file://server/", "file://server/path" |
| 7684 | // For local "file" URLs, include the leading DOS volume (if present). |
| 7685 | // Per https://www.ietf.org/rfc/rfc1738.txt, a host of "" or "localhost" is a |
| 7686 | // special case interpreted as "the machine from which the URL is being interpreted". |
| 7687 | var scheme = path.slice(0, schemeEnd); |
| 7688 | var authority = path.slice(authorityStart, authorityEnd); |
| 7689 | if (scheme === "file" && (authority === "" || authority === "localhost") && |
| 7690 | isVolumeCharacter(path.charCodeAt(authorityEnd + 1))) { |
| 7691 | var volumeSeparatorEnd = getFileUrlVolumeSeparatorEnd(path, authorityEnd + 2); |
| 7692 | if (volumeSeparatorEnd !== -1) { |
| 7693 | if (path.charCodeAt(volumeSeparatorEnd) === 47 /* CharacterCodes.slash */) { |
| 7694 | // URL: "file:///c:/", "file://localhost/c:/", "file:///c%3a/", "file://localhost/c%3a/" |
| 7695 | return ~(volumeSeparatorEnd + 1); |
| 7696 | } |
| 7697 | if (volumeSeparatorEnd === path.length) { |
| 7698 | // URL: "file:///c:", "file://localhost/c:", "file:///c$3a", "file://localhost/c%3a" |
| 7699 | // but not "file:///c:d" or "file:///c%3ad" |
| 7700 | return ~volumeSeparatorEnd; |
| 7701 | } |
| 7702 | } |
| 7703 | } |
| 7704 | return ~(authorityEnd + 1); // URL: "file://server/", "http://server/" |
| 7705 | } |
| 7706 | return ~path.length; // URL: "file://server", "http://server" |
| 7707 | } |
| 7708 | // relative |
| 7709 | return 0; |
| 7710 | } |
| 7711 | /** |
| 7712 | * Returns length of the root part of a path or URL (i.e. length of "/", "x:/", "//server/share/, file:///user/files"). |
| 7713 | * |
no test coverage detected
searching dependent graphs…