1 |
3a515b92
|
cagy
|
'use strict';
|
2 |
|
|
|
3 |
|
|
var path = require('path');
|
4 |
|
|
var inspect = require('util').inspect;
|
5 |
|
|
|
6 |
|
|
function assertPath(path) {
|
7 |
|
|
if (typeof path !== 'string') {
|
8 |
|
|
throw new TypeError('Path must be a string. Received ' + inspect(path));
|
9 |
|
|
}
|
10 |
|
|
}
|
11 |
|
|
|
12 |
|
|
function posix(path) {
|
13 |
|
|
assertPath(path);
|
14 |
|
|
if (path.length === 0)
|
15 |
|
|
return '.';
|
16 |
|
|
var code = path.charCodeAt(0);
|
17 |
|
|
var hasRoot = (code === 47/*/*/);
|
18 |
|
|
var end = -1;
|
19 |
|
|
var matchedSlash = true;
|
20 |
|
|
for (var i = path.length - 1; i >= 1; --i) {
|
21 |
|
|
code = path.charCodeAt(i);
|
22 |
|
|
if (code === 47/*/*/) {
|
23 |
|
|
if (!matchedSlash) {
|
24 |
|
|
end = i;
|
25 |
|
|
break;
|
26 |
|
|
}
|
27 |
|
|
} else {
|
28 |
|
|
// We saw the first non-path separator
|
29 |
|
|
matchedSlash = false;
|
30 |
|
|
}
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
if (end === -1)
|
34 |
|
|
return hasRoot ? '/' : '.';
|
35 |
|
|
if (hasRoot && end === 1)
|
36 |
|
|
return '//';
|
37 |
|
|
return path.slice(0, end);
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
function win32(path) {
|
41 |
|
|
assertPath(path);
|
42 |
|
|
var len = path.length;
|
43 |
|
|
if (len === 0)
|
44 |
|
|
return '.';
|
45 |
|
|
var rootEnd = -1;
|
46 |
|
|
var end = -1;
|
47 |
|
|
var matchedSlash = true;
|
48 |
|
|
var offset = 0;
|
49 |
|
|
var code = path.charCodeAt(0);
|
50 |
|
|
|
51 |
|
|
// Try to match a root
|
52 |
|
|
if (len > 1) {
|
53 |
|
|
if (code === 47/*/*/ || code === 92/*\*/) {
|
54 |
|
|
// Possible UNC root
|
55 |
|
|
|
56 |
|
|
rootEnd = offset = 1;
|
57 |
|
|
|
58 |
|
|
code = path.charCodeAt(1);
|
59 |
|
|
if (code === 47/*/*/ || code === 92/*\*/) {
|
60 |
|
|
// Matched double path separator at beginning
|
61 |
|
|
var j = 2;
|
62 |
|
|
var last = j;
|
63 |
|
|
// Match 1 or more non-path separators
|
64 |
|
|
for (; j < len; ++j) {
|
65 |
|
|
code = path.charCodeAt(j);
|
66 |
|
|
if (code === 47/*/*/ || code === 92/*\*/)
|
67 |
|
|
break;
|
68 |
|
|
}
|
69 |
|
|
if (j < len && j !== last) {
|
70 |
|
|
// Matched!
|
71 |
|
|
last = j;
|
72 |
|
|
// Match 1 or more path separators
|
73 |
|
|
for (; j < len; ++j) {
|
74 |
|
|
code = path.charCodeAt(j);
|
75 |
|
|
if (code !== 47/*/*/ && code !== 92/*\*/)
|
76 |
|
|
break;
|
77 |
|
|
}
|
78 |
|
|
if (j < len && j !== last) {
|
79 |
|
|
// Matched!
|
80 |
|
|
last = j;
|
81 |
|
|
// Match 1 or more non-path separators
|
82 |
|
|
for (; j < len; ++j) {
|
83 |
|
|
code = path.charCodeAt(j);
|
84 |
|
|
if (code === 47/*/*/ || code === 92/*\*/)
|
85 |
|
|
break;
|
86 |
|
|
}
|
87 |
|
|
if (j === len) {
|
88 |
|
|
// We matched a UNC root only
|
89 |
|
|
return path;
|
90 |
|
|
}
|
91 |
|
|
if (j !== last) {
|
92 |
|
|
// We matched a UNC root with leftovers
|
93 |
|
|
|
94 |
|
|
// Offset by 1 to include the separator after the UNC root to
|
95 |
|
|
// treat it as a "normal root" on top of a (UNC) root
|
96 |
|
|
rootEnd = offset = j + 1;
|
97 |
|
|
}
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
}
|
101 |
|
|
} else if ((code >= 65/*A*/ && code <= 90/*Z*/) ||
|
102 |
|
|
(code >= 97/*a*/ && code <= 122/*z*/)) {
|
103 |
|
|
// Possible device root
|
104 |
|
|
|
105 |
|
|
code = path.charCodeAt(1);
|
106 |
|
|
if (path.charCodeAt(1) === 58/*:*/) {
|
107 |
|
|
rootEnd = offset = 2;
|
108 |
|
|
if (len > 2) {
|
109 |
|
|
code = path.charCodeAt(2);
|
110 |
|
|
if (code === 47/*/*/ || code === 92/*\*/)
|
111 |
|
|
rootEnd = offset = 3;
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
|
|
}
|
115 |
|
|
} else if (code === 47/*/*/ || code === 92/*\*/) {
|
116 |
|
|
return path[0];
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
for (var i = len - 1; i >= offset; --i) {
|
120 |
|
|
code = path.charCodeAt(i);
|
121 |
|
|
if (code === 47/*/*/ || code === 92/*\*/) {
|
122 |
|
|
if (!matchedSlash) {
|
123 |
|
|
end = i;
|
124 |
|
|
break;
|
125 |
|
|
}
|
126 |
|
|
} else {
|
127 |
|
|
// We saw the first non-path separator
|
128 |
|
|
matchedSlash = false;
|
129 |
|
|
}
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
if (end === -1) {
|
133 |
|
|
if (rootEnd === -1)
|
134 |
|
|
return '.';
|
135 |
|
|
else
|
136 |
|
|
end = rootEnd;
|
137 |
|
|
}
|
138 |
|
|
return path.slice(0, end);
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
module.exports = process.platform === 'win32' ? win32 : posix;
|
142 |
|
|
module.exports.posix = posix;
|
143 |
|
|
module.exports.win32 = win32;
|