MCPcopy Create free account
hub / github.com/stenciljs/core / getEncodedRootLength

Function getEncodedRootLength

src/utils/path.ts:73–131  ·  view source on GitHub ↗
(path: string)

Source from the content-addressed store, hash-verified

71};
72
73const getEncodedRootLength = (path: string): number => {
74 if (!path) return 0;
75 const ch0 = path.charCodeAt(0);
76
77 // POSIX or UNC
78 if (ch0 === CharacterCodes.slash || ch0 === CharacterCodes.backslash) {
79 if (path.charCodeAt(1) !== ch0) return 1; // POSIX: "/" (or non-normalized "\")
80
81 const p1 = path.indexOf(ch0 === CharacterCodes.slash ? '/' : altDirectorySeparator, 2);
82 if (p1 < 0) return path.length; // UNC: "//server" or "\\server"
83
84 return p1 + 1; // UNC: "//server/" or "\\server\"
85 }
86
87 // DOS
88 if (isVolumeCharacter(ch0) && path.charCodeAt(1) === CharacterCodes.colon) {
89 const ch2 = path.charCodeAt(2);
90 if (ch2 === CharacterCodes.slash || ch2 === CharacterCodes.backslash) return 3; // DOS: "c:/" or "c:\"
91 if (path.length === 2) return 2; // DOS: "c:" (but not "c:d")
92 }
93
94 // URL
95 const schemeEnd = path.indexOf(urlSchemeSeparator);
96 if (schemeEnd !== -1) {
97 const authorityStart = schemeEnd + urlSchemeSeparator.length;
98 const authorityEnd = path.indexOf('/', authorityStart);
99 if (authorityEnd !== -1) {
100 // URL: "file:///", "file://server/", "file://server/path"
101 // For local "file" URLs, include the leading DOS volume (if present).
102 // Per https://www.ietf.org/rfc/rfc1738.txt, a host of "" or "localhost" is a
103 // special case interpreted as "the machine from which the URL is being interpreted".
104 const scheme = path.slice(0, schemeEnd);
105 const authority = path.slice(authorityStart, authorityEnd);
106 if (
107 scheme === 'file' &&
108 (authority === '' || authority === 'localhost') &&
109 isVolumeCharacter(path.charCodeAt(authorityEnd + 1))
110 ) {
111 const volumeSeparatorEnd = getFileUrlVolumeSeparatorEnd(path, authorityEnd + 2);
112 if (volumeSeparatorEnd !== -1) {
113 if (path.charCodeAt(volumeSeparatorEnd) === CharacterCodes.slash) {
114 // URL: "file:///c:/", "file://localhost/c:/", "file:///c%3a/", "file://localhost/c%3a/"
115 return ~(volumeSeparatorEnd + 1);
116 }
117 if (volumeSeparatorEnd === path.length) {
118 // URL: "file:///c:", "file://localhost/c:", "file:///c$3a", "file://localhost/c%3a"
119 // but not "file:///c:d" or "file:///c%3ad"
120 return ~volumeSeparatorEnd;
121 }
122 }
123 }
124 return ~(authorityEnd + 1); // URL: "file://server/", "http://server/"
125 }
126 return ~path.length; // URL: "file://server", "http://server"
127 }
128
129 // relative
130 return 0;

Callers 1

getRootLengthFunction · 0.85

Calls 2

isVolumeCharacterFunction · 0.85

Tested by

no test coverage detected