diff --git a/benchmark/path/relative-win32.js b/benchmark/path/relative-win32.js index e513b828533..219c6a31e6a 100644 --- a/benchmark/path/relative-win32.js +++ b/benchmark/path/relative-win32.js @@ -9,6 +9,7 @@ const bench = common.createBenchmark(main, { ['C:\\foo\\bar\\baz', 'C:\\foo\\bar\\baz'].join('|'), ['C:\\foo\\BAR\\BAZ', 'C:\\foo\\bar\\baz'].join('|'), ['C:\\foo\\bar\\baz\\quux', 'C:\\'].join('|'), + ['c:\\İ\\a\\İ', 'c:\\İ\\b\\İ\\test.txt', '..\\..\\b\\İ\\test.txt'].join('|'), ], n: [1e5], }); diff --git a/lib/path.js b/lib/path.js index f72f5835e9b..eba07f376ad 100644 --- a/lib/path.js +++ b/lib/path.js @@ -22,12 +22,16 @@ 'use strict'; const { + ArrayPrototypeJoin, + ArrayPrototypeSlice, FunctionPrototypeBind, StringPrototypeCharCodeAt, StringPrototypeIndexOf, StringPrototypeLastIndexOf, + StringPrototypeRepeat, StringPrototypeReplace, StringPrototypeSlice, + StringPrototypeSplit, StringPrototypeToLowerCase, } = primordials; @@ -539,6 +543,42 @@ const win32 = { if (from === to) return ''; + if (fromOrig.length !== from.length || toOrig.length !== to.length) { + const fromSplit = StringPrototypeSplit(fromOrig, '\\'); + const toSplit = StringPrototypeSplit(toOrig, '\\'); + if (fromSplit[fromSplit.length - 1] === '') { + fromSplit.pop(); + } + if (toSplit[toSplit.length - 1] === '') { + toSplit.pop(); + } + + const fromLen = fromSplit.length; + const toLen = toSplit.length; + const length = fromLen < toLen ? fromLen : toLen; + + let i; + for (i = 0; i < length; i++) { + if (StringPrototypeToLowerCase(fromSplit[i]) !== StringPrototypeToLowerCase(toSplit[i])) { + break; + } + } + + if (i === 0) { + return toOrig; + } else if (i === length) { + if (toLen > length) { + return ArrayPrototypeJoin(ArrayPrototypeSlice(toSplit, i), '\\'); + } + if (fromLen > length) { + return StringPrototypeRepeat('..\\', fromLen - 1 - i) + '..'; + } + return ''; + } + + return StringPrototypeRepeat('..\\', fromLen - i) + ArrayPrototypeJoin(ArrayPrototypeSlice(toSplit, i), '\\'); + } + // Trim any leading backslashes let fromStart = 0; while (fromStart < from.length && diff --git a/test/parallel/test-path-relative.js b/test/parallel/test-path-relative.js index f6a9f5662a6..999ef93784b 100644 --- a/test/parallel/test-path-relative.js +++ b/test/parallel/test-path-relative.js @@ -32,6 +32,11 @@ const relativeTests = [ ['\\\\foo\\baz', '\\\\foo\\baz-quux', '..\\baz-quux'], ['C:\\baz', '\\\\foo\\bar\\baz', '\\\\foo\\bar\\baz'], ['\\\\foo\\bar\\baz', 'C:\\baz', 'C:\\baz'], + ['c:\\a\\İ', 'c:\\a\\İ\\test.txt', 'test.txt'], + ['c:\\İ\\a\\İ', 'c:\\İ\\b\\İ\\test.txt', '..\\..\\b\\İ\\test.txt'], + ['c:\\İ\\a\\i̇', 'c:\\İ\\b\\İ\\test.txt', '..\\..\\b\\İ\\test.txt'], + ['c:\\i̇\\a\\İ', 'c:\\İ\\b\\İ\\test.txt', '..\\..\\b\\İ\\test.txt'], + ['c:\\ß\\a\\ß', 'c:\\ß\\b\\ß\\test.txt', '..\\..\\b\\ß\\test.txt'], ], ], [ path.posix.relative,