File tree 2 files changed +34
-20
lines changed
2 files changed +34
-20
lines changed Original file line number Diff line number Diff line change @@ -51,19 +51,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
51
51
res . charCodeAt ( res . length - 1 ) !== 46 /*.*/ ||
52
52
res . charCodeAt ( res . length - 2 ) !== 46 /*.*/ ) {
53
53
if ( res . length > 2 ) {
54
- const start = res . length - 1 ;
55
- var j = start ;
56
- for ( ; j >= 0 ; -- j ) {
57
- if ( res . charCodeAt ( j ) === 92 /*\*/ )
58
- break ;
59
- }
60
- if ( j !== start ) {
61
- if ( j === - 1 ) {
54
+ const lastSlashIndex = res . lastIndexOf ( '\\' ) ;
55
+ if ( lastSlashIndex !== res . length - 1 ) {
56
+ if ( lastSlashIndex === - 1 ) {
62
57
res = '' ;
63
58
lastSegmentLength = 0 ;
64
59
} else {
65
- res = res . slice ( 0 , j ) ;
66
- lastSegmentLength = j ;
60
+ res = res . slice ( 0 , lastSlashIndex ) ;
61
+ lastSegmentLength = res . length - 1 - res . lastIndexOf ( '\\' ) ;
67
62
}
68
63
lastSlash = i ;
69
64
dots = 0 ;
@@ -124,19 +119,14 @@ function normalizeStringPosix(path, allowAboveRoot) {
124
119
res . charCodeAt ( res . length - 1 ) !== 46 /*.*/ ||
125
120
res . charCodeAt ( res . length - 2 ) !== 46 /*.*/ ) {
126
121
if ( res . length > 2 ) {
127
- const start = res . length - 1 ;
128
- var j = start ;
129
- for ( ; j >= 0 ; -- j ) {
130
- if ( res . charCodeAt ( j ) === 47 /*/*/ )
131
- break ;
132
- }
133
- if ( j !== start ) {
134
- if ( j === - 1 ) {
122
+ const lastSlashIndex = res . lastIndexOf ( '/' ) ;
123
+ if ( lastSlashIndex !== res . length - 1 ) {
124
+ if ( lastSlashIndex === - 1 ) {
135
125
res = '' ;
136
126
lastSegmentLength = 0 ;
137
127
} else {
138
- res = res . slice ( 0 , j ) ;
139
- lastSegmentLength = j ;
128
+ res = res . slice ( 0 , lastSlashIndex ) ;
129
+ lastSegmentLength = res . length - 1 - res . lastIndexOf ( '/' ) ;
140
130
}
141
131
lastSlash = i ;
142
132
dots = 0 ;
Original file line number Diff line number Diff line change @@ -27,6 +27,18 @@ assert.strictEqual(path.win32.normalize('..\\foo..\\..\\..\\bar'),
27
27
'..\\..\\bar' ) ;
28
28
assert . strictEqual ( path . win32 . normalize ( '..\\...\\..\\.\\...\\..\\..\\bar' ) ,
29
29
'..\\..\\bar' ) ;
30
+ assert . strictEqual ( path . win32 . normalize ( '../../../foo/../../../bar' ) ,
31
+ '..\\..\\..\\..\\..\\bar' ) ;
32
+ assert . strictEqual ( path . win32 . normalize ( '../../../foo/../../../bar/../../' ) ,
33
+ '..\\..\\..\\..\\..\\..\\' ) ;
34
+ assert . strictEqual (
35
+ path . win32 . normalize ( '../foobar/barfoo/foo/../../../bar/../../' ) ,
36
+ '..\\..\\'
37
+ ) ;
38
+ assert . strictEqual (
39
+ path . win32 . normalize ( '../.../../foobar/../../../bar/../../baz' ) ,
40
+ '..\\..\\..\\..\\baz'
41
+ ) ;
30
42
31
43
assert . strictEqual ( path . posix . normalize ( './fixtures///b/../b/c.js' ) ,
32
44
'fixtures/b/c.js' ) ;
@@ -44,3 +56,15 @@ assert.strictEqual(path.posix.normalize('bar/foo..'), 'bar/foo..');
44
56
assert . strictEqual ( path . posix . normalize ( '../foo../../../bar' ) , '../../bar' ) ;
45
57
assert . strictEqual ( path . posix . normalize ( '../.../.././.../../../bar' ) ,
46
58
'../../bar' ) ;
59
+ assert . strictEqual ( path . posix . normalize ( '../../../foo/../../../bar' ) ,
60
+ '../../../../../bar' ) ;
61
+ assert . strictEqual ( path . posix . normalize ( '../../../foo/../../../bar/../../' ) ,
62
+ '../../../../../../' ) ;
63
+ assert . strictEqual (
64
+ path . posix . normalize ( '../foobar/barfoo/foo/../../../bar/../../' ) ,
65
+ '../../'
66
+ ) ;
67
+ assert . strictEqual (
68
+ path . posix . normalize ( '../.../../foobar/../../../bar/../../baz' ) ,
69
+ '../../../../baz'
70
+ ) ;
You can’t perform that action at this time.
0 commit comments