@@ -50,7 +50,7 @@ function isWindowsDeviceRoot(code) {
5050}
5151
5252// Resolves . and .. elements in a path with directory names
53- function normalizeStringWin32 ( path , allowAboveRoot ) {
53+ function normalizeString ( path , allowAboveRoot , separator ) {
5454var res = '' ;
5555var lastSegmentLength = 0 ;
5656var lastSlash = - 1 ;
@@ -72,14 +72,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
7272res . charCodeAt ( res . length - 1 ) !== CHAR_DOT ||
7373res . charCodeAt ( res . length - 2 ) !== CHAR_DOT ) {
7474if ( res . length > 2 ) {
75- const lastSlashIndex = res . lastIndexOf ( '\\' ) ;
75+ const lastSlashIndex = res . lastIndexOf ( separator ) ;
7676if ( lastSlashIndex !== res . length - 1 ) {
7777if ( lastSlashIndex === - 1 ) {
7878res = '' ;
7979lastSegmentLength = 0 ;
8080} else {
8181res = res . slice ( 0 , lastSlashIndex ) ;
82- lastSegmentLength = res . length - 1 - res . lastIndexOf ( '\\' ) ;
82+ lastSegmentLength = res . length - 1 - res . lastIndexOf ( separator ) ;
8383}
8484lastSlash = i ;
8585dots = 0 ;
@@ -95,82 +95,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
9595}
9696if ( allowAboveRoot ) {
9797if ( res . length > 0 )
98- res += '\\..' ;
98+ res += ` ${ separator } ..` ;
9999else
100100res = '..' ;
101101lastSegmentLength = 2 ;
102102}
103103} else {
104104if ( res . length > 0 )
105- res += '\\' + path . slice ( lastSlash + 1 , i ) ;
106- else
107- res = path . slice ( lastSlash + 1 , i ) ;
108- lastSegmentLength = i - lastSlash - 1 ;
109- }
110- lastSlash = i ;
111- dots = 0 ;
112- } else if ( code === CHAR_DOT && dots !== - 1 ) {
113- ++ dots ;
114- } else {
115- dots = - 1 ;
116- }
117- }
118- return res ;
119- }
120-
121- // Resolves . and .. elements in a path with directory names
122- function normalizeStringPosix ( path , allowAboveRoot ) {
123- var res = '' ;
124- var lastSegmentLength = 0 ;
125- var lastSlash = - 1 ;
126- var dots = 0 ;
127- var code ;
128- for ( var i = 0 ; i <= path . length ; ++ i ) {
129- if ( i < path . length )
130- code = path . charCodeAt ( i ) ;
131- else if ( code === CHAR_FORWARD_SLASH )
132- break ;
133- else
134- code = CHAR_FORWARD_SLASH ;
135- if ( code === CHAR_FORWARD_SLASH ) {
136- if ( lastSlash === i - 1 || dots === 1 ) {
137- // NOOP
138- } else if ( lastSlash !== i - 1 && dots === 2 ) {
139- if ( res . length < 2 || lastSegmentLength !== 2 ||
140- res . charCodeAt ( res . length - 1 ) !== CHAR_DOT ||
141- res . charCodeAt ( res . length - 2 ) !== CHAR_DOT ) {
142- if ( res . length > 2 ) {
143- const lastSlashIndex = res . lastIndexOf ( '/' ) ;
144- if ( lastSlashIndex !== res . length - 1 ) {
145- if ( lastSlashIndex === - 1 ) {
146- res = '' ;
147- lastSegmentLength = 0 ;
148- } else {
149- res = res . slice ( 0 , lastSlashIndex ) ;
150- lastSegmentLength = res . length - 1 - res . lastIndexOf ( '/' ) ;
151- }
152- lastSlash = i ;
153- dots = 0 ;
154- continue ;
155- }
156- } else if ( res . length === 2 || res . length === 1 ) {
157- res = '' ;
158- lastSegmentLength = 0 ;
159- lastSlash = i ;
160- dots = 0 ;
161- continue ;
162- }
163- }
164- if ( allowAboveRoot ) {
165- if ( res . length > 0 )
166- res += '/..' ;
167- else
168- res = '..' ;
169- lastSegmentLength = 2 ;
170- }
171- } else {
172- if ( res . length > 0 )
173- res += '/' + path . slice ( lastSlash + 1 , i ) ;
105+ res += separator + path . slice ( lastSlash + 1 , i ) ;
174106else
175107res = path . slice ( lastSlash + 1 , i ) ;
176108lastSegmentLength = i - lastSlash - 1 ;
@@ -340,7 +272,7 @@ const win32 = {
340272// fails)
341273
342274// Normalize the tail path
343- resolvedTail = normalizeStringWin32 ( resolvedTail , ! resolvedAbsolute ) ;
275+ resolvedTail = normalizeString ( resolvedTail , ! resolvedAbsolute , '\\' ) ;
344276
345277return ( resolvedDevice + ( resolvedAbsolute ? '\\' : '' ) + resolvedTail ) ||
346278'.' ;
@@ -432,7 +364,7 @@ const win32 = {
432364
433365var tail ;
434366if ( rootEnd < len )
435- tail = normalizeStringWin32 ( path . slice ( rootEnd ) , ! isAbsolute ) ;
367+ tail = normalizeString ( path . slice ( rootEnd ) , ! isAbsolute , '\\' ) ;
436368else
437369tail = '' ;
438370if ( tail . length === 0 && ! isAbsolute )
@@ -1163,7 +1095,7 @@ const posix = {
11631095// handle relative paths to be safe (might happen when process.cwd() fails)
11641096
11651097// Normalize the path
1166- resolvedPath = normalizeStringPosix ( resolvedPath , ! resolvedAbsolute ) ;
1098+ resolvedPath = normalizeString ( resolvedPath , ! resolvedAbsolute , '/' ) ;
11671099
11681100if ( resolvedAbsolute ) {
11691101if ( resolvedPath . length > 0 )
@@ -1189,7 +1121,7 @@ const posix = {
11891121path . charCodeAt ( path . length - 1 ) === CHAR_FORWARD_SLASH ;
11901122
11911123// Normalize the path
1192- path = normalizeStringPosix ( path , ! isAbsolute ) ;
1124+ path = normalizeString ( path , ! isAbsolute , '/' ) ;
11931125
11941126if ( path . length === 0 && ! isAbsolute )
11951127path = '.' ;
0 commit comments