@@ -1478,12 +1478,6 @@ fs.unwatchFile = function(filename, listener) {
1478
1478
} ;
1479
1479
1480
1480
1481
- // Regexp that finds the next portion of a (partial) path
1482
- // result is [base_with_slash, base], e.g. ['somedir/', 'somedir']
1483
- const nextPartRe = isWindows ?
1484
- / ( .* ?) (?: [ / \\ ] + | $ ) / g :
1485
- / ( .* ?) (?: [ / ] + | $ ) / g;
1486
-
1487
1481
// Regex to find the device root, including trailing slash. E.g. 'c:\\'.
1488
1482
const splitRootRe = isWindows ?
1489
1483
/ ^ (?: [ a - z A - Z ] : | [ \\ / ] { 2 } [ ^ \\ / ] + [ \\ / ] [ ^ \\ / ] + ) ? [ \\ / ] * / :
@@ -1500,6 +1494,21 @@ function encodeRealpathResult(result, options) {
1500
1494
}
1501
1495
}
1502
1496
1497
+ // Finds the next portion of a (partial) path, up to the next path delimiter
1498
+ var nextPart ;
1499
+ if ( isWindows ) {
1500
+ nextPart = function nextPart ( p , i ) {
1501
+ for ( ; i < p . length ; ++ i ) {
1502
+ const ch = p . charCodeAt ( i ) ;
1503
+ if ( ch === 92 /*'\'*/ || ch === 47 /*'/'*/ )
1504
+ return i ;
1505
+ }
1506
+ return - 1 ;
1507
+ } ;
1508
+ } else {
1509
+ nextPart = function nextPart ( p , i ) { return p . indexOf ( '/' , i ) ; } ;
1510
+ }
1511
+
1503
1512
fs . realpathSync = function realpathSync ( p , options ) {
1504
1513
options = getOptions ( options , { } ) ;
1505
1514
handleError ( ( p = getPathFromURL ( p ) ) ) ;
@@ -1544,12 +1553,18 @@ fs.realpathSync = function realpathSync(p, options) {
1544
1553
// NB: p.length changes.
1545
1554
while ( pos < p . length ) {
1546
1555
// find the next part
1547
- nextPartRe . lastIndex = pos ;
1548
- var result = nextPartRe . exec ( p ) ;
1556
+ var result = nextPart ( p , pos ) ;
1549
1557
previous = current ;
1550
- current += result [ 0 ] ;
1551
- base = previous + result [ 1 ] ;
1552
- pos = nextPartRe . lastIndex ;
1558
+ if ( result === - 1 ) {
1559
+ var last = p . slice ( pos ) ;
1560
+ current += last ;
1561
+ base = previous + last ;
1562
+ pos = p . length ;
1563
+ } else {
1564
+ current += p . slice ( pos , result + 1 ) ;
1565
+ base = previous + p . slice ( pos , result ) ;
1566
+ pos = result + 1 ;
1567
+ }
1553
1568
1554
1569
// continue if not a symlink
1555
1570
if ( knownHard [ base ] || ( cache && cache . get ( base ) === base ) ) {
@@ -1658,12 +1673,18 @@ fs.realpath = function realpath(p, options, callback) {
1658
1673
}
1659
1674
1660
1675
// find the next part
1661
- nextPartRe . lastIndex = pos ;
1662
- var result = nextPartRe . exec ( p ) ;
1676
+ var result = nextPart ( p , pos ) ;
1663
1677
previous = current ;
1664
- current += result [ 0 ] ;
1665
- base = previous + result [ 1 ] ;
1666
- pos = nextPartRe . lastIndex ;
1678
+ if ( result === - 1 ) {
1679
+ var last = p . slice ( pos ) ;
1680
+ current += last ;
1681
+ base = previous + last ;
1682
+ pos = p . length ;
1683
+ } else {
1684
+ current += p . slice ( pos , result + 1 ) ;
1685
+ base = previous + p . slice ( pos , result ) ;
1686
+ pos = result + 1 ;
1687
+ }
1667
1688
1668
1689
// continue if not a symlink
1669
1690
if ( knownHard [ base ] ) {
0 commit comments