3 var path = require('path');
4 var inspect = require('util').inspect;
6 function assertPath(path) {
7 if (typeof path !== 'string') {
8 throw new TypeError('Path must be a string. Received ' + inspect(path));
12 function posix(path) {
14 if (path.length === 0)
16 var code = path.charCodeAt(0);
17 var hasRoot = (code === 47/*/*/);
19 var matchedSlash = true;
20 for (var i = path.length - 1; i >= 1; --i) {
21 code = path.charCodeAt(i);
22 if (code === 47/*/*/) {
28 // We saw the first non-path separator
34 return hasRoot ? '/' : '.';
35 if (hasRoot && end === 1)
37 return path.slice(0, end);
40 function win32(path) {
42 var len = path.length;
47 var matchedSlash = true;
49 var code = path.charCodeAt(0);
51 // Try to match a root
53 if (code === 47/*/*/ || code === 92/*\*/) {
58 code = path.charCodeAt(1);
59 if (code === 47/*/*/ || code === 92/*\*/) {
60 // Matched double path separator at beginning
63 // Match 1 or more non-path separators
64 for (; j < len; ++j) {
65 code = path.charCodeAt(j);
66 if (code === 47/*/*/ || code === 92/*\*/)
69 if (j < len && j !== last) {
72 // Match 1 or more path separators
73 for (; j < len; ++j) {
74 code = path.charCodeAt(j);
75 if (code !== 47/*/*/ && code !== 92/*\*/)
78 if (j < len && j !== last) {
81 // Match 1 or more non-path separators
82 for (; j < len; ++j) {
83 code = path.charCodeAt(j);
84 if (code === 47/*/*/ || code === 92/*\*/)
88 // We matched a UNC root only
92 // We matched a UNC root with leftovers
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;
101 } else if ((code >= 65/*A*/ && code <= 90/*Z*/) ||
102 (code >= 97/*a*/ && code <= 122/*z*/)) {
103 // Possible device root
105 code = path.charCodeAt(1);
106 if (path.charCodeAt(1) === 58/*:*/) {
107 rootEnd = offset = 2;
109 code = path.charCodeAt(2);
110 if (code === 47/*/*/ || code === 92/*\*/)
111 rootEnd = offset = 3;
115 } else if (code === 47/*/*/ || code === 92/*\*/) {
119 for (var i = len - 1; i >= offset; --i) {
120 code = path.charCodeAt(i);
121 if (code === 47/*/*/ || code === 92/*\*/) {
127 // We saw the first non-path separator
128 matchedSlash = false;
138 return path.slice(0, end);
141 module.exports = process.platform === 'win32' ? win32 : posix;
142 module.exports.posix = posix;
143 module.exports.win32 = win32;