GeezerVison is showing github.com — path: fix relative on Windows · nodejs/node@8a770cf
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: benchmark/path/relative-win32.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,6 +9,7 @@ const bench = common.createBenchmark(main, { | 9 | 9 | ['C:\\foo\\bar\\baz', 'C:\\foo\\bar\\baz'].join('|'), | 10 | 10 | ['C:\\foo\\BAR\\BAZ', 'C:\\foo\\bar\\baz'].join('|'), | 11 | 11 | ['C:\\foo\\bar\\baz\\quux', 'C:\\'].join('|'), | | 12 | + ['c:\\İ\\a\\İ', 'c:\\İ\\b\\İ\\test.txt', '..\\..\\b\\İ\\test.txt'].join('|'), | 12 | 13 | ], | 13 | 14 | n: [1e5], | 14 | 15 | }); | |
Collapse file Copy file name to clipboardExpand all lines: lib/path.js +40Lines changed: 40 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | 'use strict'; | 23 | 23 |
| 24 | 24 | const { | | 25 | + ArrayPrototypeJoin, | | 26 | + ArrayPrototypeSlice, | 25 | 27 | FunctionPrototypeBind, | 26 | 28 | StringPrototypeCharCodeAt, | 27 | 29 | StringPrototypeIndexOf, | 28 | 30 | StringPrototypeLastIndexOf, | | 31 | + StringPrototypeRepeat, | 29 | 32 | StringPrototypeReplace, | 30 | 33 | StringPrototypeSlice, | | 34 | + StringPrototypeSplit, | 31 | 35 | StringPrototypeToLowerCase, | 32 | 36 | } = primordials; | 33 | 37 |
| @@ -539,6 +543,42 @@ const win32 = { | 539 | 543 | if (from === to) | 540 | 544 | return ''; | 541 | 545 |
| | 546 | + if (fromOrig.length !== from.length || toOrig.length !== to.length) { | | 547 | + const fromSplit = StringPrototypeSplit(fromOrig, '\\'); | | 548 | + const toSplit = StringPrototypeSplit(toOrig, '\\'); | | 549 | + if (fromSplit[fromSplit.length - 1] === '') { | | 550 | + fromSplit.pop(); | | 551 | + } | | 552 | + if (toSplit[toSplit.length - 1] === '') { | | 553 | + toSplit.pop(); | | 554 | + } | | 555 | + | | 556 | + const fromLen = fromSplit.length; | | 557 | + const toLen = toSplit.length; | | 558 | + const length = fromLen < toLen ? fromLen : toLen; | | 559 | + | | 560 | + let i; | | 561 | + for (i = 0; i < length; i++) { | | 562 | + if (StringPrototypeToLowerCase(fromSplit[i]) !== StringPrototypeToLowerCase(toSplit[i])) { | | 563 | + break; | | 564 | + } | | 565 | + } | | 566 | + | | 567 | + if (i === 0) { | | 568 | + return toOrig; | | 569 | + } else if (i === length) { | | 570 | + if (toLen > length) { | | 571 | + return ArrayPrototypeJoin(ArrayPrototypeSlice(toSplit, i), '\\'); | | 572 | + } | | 573 | + if (fromLen > length) { | | 574 | + return StringPrototypeRepeat('..\\', fromLen - 1 - i) + '..'; | | 575 | + } | | 576 | + return ''; | | 577 | + } | | 578 | + | | 579 | + return StringPrototypeRepeat('..\\', fromLen - i) + ArrayPrototypeJoin(ArrayPrototypeSlice(toSplit, i), '\\'); | | 580 | + } | | 581 | + | 542 | 582 | // Trim any leading backslashes | 543 | 583 | let fromStart = 0; | 544 | 584 | while (fromStart < from.length && | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-path-relative.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,11 @@ const relativeTests = [ | 32 | 32 | ['\\\\foo\\baz', '\\\\foo\\baz-quux', '..\\baz-quux'], | 33 | 33 | ['C:\\baz', '\\\\foo\\bar\\baz', '\\\\foo\\bar\\baz'], | 34 | 34 | ['\\\\foo\\bar\\baz', 'C:\\baz', 'C:\\baz'], | | 35 | + ['c:\\a\\İ', 'c:\\a\\İ\\test.txt', 'test.txt'], | | 36 | + ['c:\\İ\\a\\İ', 'c:\\İ\\b\\İ\\test.txt', '..\\..\\b\\İ\\test.txt'], | | 37 | + ['c:\\İ\\a\\i̇', 'c:\\İ\\b\\İ\\test.txt', '..\\..\\b\\İ\\test.txt'], | | 38 | + ['c:\\i̇\\a\\İ', 'c:\\İ\\b\\İ\\test.txt', '..\\..\\b\\İ\\test.txt'], | | 39 | + ['c:\\ß\\a\\ß', 'c:\\ß\\b\\ß\\test.txt', '..\\..\\b\\ß\\test.txt'], | 35 | 40 | ], | 36 | 41 | ], | 37 | 42 | [ path.posix.relative, | |
|