@@ -11,10 +11,10 @@ function assertPath(path) {
11
11
// Resolves . and .. elements in a path with directory names
12
12
function normalizeStringWin32 ( path , allowAboveRoot ) {
13
13
var res = '' ;
14
+ var lastSegmentLength = 0 ;
14
15
var lastSlash = - 1 ;
15
16
var dots = 0 ;
16
17
var code ;
17
- var isAboveRoot = false ;
18
18
for ( var i = 0 ; i <= path . length ; ++ i ) {
19
19
if ( i < path . length )
20
20
code = path . charCodeAt ( i ) ;
@@ -26,7 +26,7 @@ function normalizeStringWin32(path, allowAboveRoot) {
26
26
if ( lastSlash === i - 1 || dots === 1 ) {
27
27
// NOOP
28
28
} else if ( lastSlash !== i - 1 && dots === 2 ) {
29
- if ( res . length < 2 || ! isAboveRoot ||
29
+ if ( res . length < 2 || lastSegmentLength !== 2 ||
30
30
res . charCodeAt ( res . length - 1 ) !== 46 /*.*/ ||
31
31
res . charCodeAt ( res . length - 2 ) !== 46 /*.*/ ) {
32
32
if ( res . length > 2 ) {
@@ -37,20 +37,22 @@ function normalizeStringWin32(path, allowAboveRoot) {
37
37
break ;
38
38
}
39
39
if ( j !== start ) {
40
- if ( j === - 1 )
40
+ if ( j === - 1 ) {
41
41
res = '' ;
42
- else
42
+ lastSegmentLength = 0 ;
43
+ } else {
43
44
res = res . slice ( 0 , j ) ;
45
+ lastSegmentLength = j ;
46
+ }
44
47
lastSlash = i ;
45
48
dots = 0 ;
46
- isAboveRoot = false ;
47
49
continue ;
48
50
}
49
51
} else if ( res . length === 2 || res . length === 1 ) {
50
52
res = '' ;
53
+ lastSegmentLength = 0 ;
51
54
lastSlash = i ;
52
55
dots = 0 ;
53
- isAboveRoot = false ;
54
56
continue ;
55
57
}
56
58
}
@@ -59,14 +61,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
59
61
res += '\\..' ;
60
62
else
61
63
res = '..' ;
62
- isAboveRoot = true ;
64
+ lastSegmentLength = 2 ;
63
65
}
64
66
} else {
65
67
if ( res . length > 0 )
66
68
res += '\\' + path . slice ( lastSlash + 1 , i ) ;
67
69
else
68
70
res = path . slice ( lastSlash + 1 , i ) ;
69
- isAboveRoot = false ;
71
+ lastSegmentLength = i - lastSlash - 1 ;
70
72
}
71
73
lastSlash = i ;
72
74
dots = 0 ;
@@ -82,10 +84,10 @@ function normalizeStringWin32(path, allowAboveRoot) {
82
84
// Resolves . and .. elements in a path with directory names
83
85
function normalizeStringPosix ( path , allowAboveRoot ) {
84
86
var res = '' ;
87
+ var lastSegmentLength = 0 ;
85
88
var lastSlash = - 1 ;
86
89
var dots = 0 ;
87
90
var code ;
88
- var isAboveRoot = false ;
89
91
for ( var i = 0 ; i <= path . length ; ++ i ) {
90
92
if ( i < path . length )
91
93
code = path . charCodeAt ( i ) ;
@@ -97,7 +99,7 @@ function normalizeStringPosix(path, allowAboveRoot) {
97
99
if ( lastSlash === i - 1 || dots === 1 ) {
98
100
// NOOP
99
101
} else if ( lastSlash !== i - 1 && dots === 2 ) {
100
- if ( res . length < 2 || ! isAboveRoot ||
102
+ if ( res . length < 2 || lastSegmentLength !== 2 ||
101
103
res . charCodeAt ( res . length - 1 ) !== 46 /*.*/ ||
102
104
res . charCodeAt ( res . length - 2 ) !== 46 /*.*/ ) {
103
105
if ( res . length > 2 ) {
@@ -108,20 +110,22 @@ function normalizeStringPosix(path, allowAboveRoot) {
108
110
break ;
109
111
}
110
112
if ( j !== start ) {
111
- if ( j === - 1 )
113
+ if ( j === - 1 ) {
112
114
res = '' ;
113
- else
115
+ lastSegmentLength = 0 ;
116
+ } else {
114
117
res = res . slice ( 0 , j ) ;
118
+ lastSegmentLength = j ;
119
+ }
115
120
lastSlash = i ;
116
121
dots = 0 ;
117
- isAboveRoot = false ;
118
122
continue ;
119
123
}
120
124
} else if ( res . length === 2 || res . length === 1 ) {
121
125
res = '' ;
126
+ lastSegmentLength = 0 ;
122
127
lastSlash = i ;
123
128
dots = 0 ;
124
- isAboveRoot = false ;
125
129
continue ;
126
130
}
127
131
}
@@ -130,14 +134,14 @@ function normalizeStringPosix(path, allowAboveRoot) {
130
134
res += '/..' ;
131
135
else
132
136
res = '..' ;
133
- isAboveRoot = true ;
137
+ lastSegmentLength = 2 ;
134
138
}
135
139
} else {
136
140
if ( res . length > 0 )
137
141
res += '/' + path . slice ( lastSlash + 1 , i ) ;
138
142
else
139
143
res = path . slice ( lastSlash + 1 , i ) ;
140
- isAboveRoot = false ;
144
+ lastSegmentLength = i - lastSlash - 1 ;
141
145
}
142
146
lastSlash = i ;
143
147
dots = 0 ;
0 commit comments