Permalink
Browse files

path, refactor: rewrite path.resolve, more compatible with node.js

  • Loading branch information...
xicilion committed Nov 6, 2017
1 parent 4afd290 commit ddd6eaa1dd767f9fa248ecb2525eba9bd6a1328f
Showing with 39 additions and 31 deletions.
  1. +15 −8 fibjs/include/path.h
  2. +24 −23 test/path_test.js
View
@@ -36,7 +36,8 @@ class Path {
const char* c_str = other.c_str();
if (isWin32PathSlash(c_str[0]) && isWin32PathSlash(c_str[1])) {
if (isWin32PathSlash(c_str[0]) && isWin32PathSlash(c_str[1])
&& !isWin32PathSlash(c_str[2])) {
m_buf = other;
return;
}
@@ -239,7 +240,7 @@ inline const char* split_path_win32(const char* p)
return p2;
}
inline void _normalize_array(std::vector<exlib::string>& a)
inline void _normalize_array(std::vector<exlib::string>& a, bool removeSlash)
{
int32_t i;
@@ -288,9 +289,15 @@ inline void _normalize_array(std::vector<exlib::string>& a)
} else
i++;
}
if (removeSlash && a.size() > 1) {
i = (int32_t)a.size();
if (i > 1 && a[i - 1].empty() && !a[i - 2].empty())
a.erase(a.begin() + i - 1);
}
}
inline result_t _normalize(exlib::string path, exlib::string& retVal)
inline result_t _normalize(exlib::string path, exlib::string& retVal, bool removeSlash = false)
{
const char* p = path.c_str();
int32_t len = (int32_t)path.length();
@@ -306,7 +313,7 @@ inline result_t _normalize(exlib::string path, exlib::string& retVal)
}
a.push_back(exlib::string(p + p1, i - p1));
_normalize_array(a);
_normalize_array(a, removeSlash);
retVal.clear();
for (i = 0; i < (int32_t)a.size(); i++) {
@@ -318,7 +325,7 @@ inline result_t _normalize(exlib::string path, exlib::string& retVal)
return 0;
}
inline result_t _normalize_win32(exlib::string path, exlib::string& retVal)
inline result_t _normalize_win32(exlib::string path, exlib::string& retVal, bool removeSlash = false)
{
const char* p = path.c_str();
int32_t len = (int32_t)path.length();
@@ -357,7 +364,7 @@ inline result_t _normalize_win32(exlib::string path, exlib::string& retVal)
}
}
_normalize_array(a);
_normalize_array(a, removeSlash);
retVal.clear();
@@ -546,7 +553,7 @@ inline result_t _resolve(OptArgs ps, exlib::string& retVal)
p.resolvePosix(s);
}
return _normalize(p.str(), retVal);
return _normalize(p.str(), retVal, true);
}
inline result_t _resolve_win32(OptArgs ps, exlib::string& retVal)
@@ -566,7 +573,7 @@ inline result_t _resolve_win32(OptArgs ps, exlib::string& retVal)
p.resolveWin32(s);
}
return _normalize_win32(p.str(), retVal);
return _normalize_win32(p.str(), retVal, true);
}
inline result_t _sep(exlib::string& retVal)
View
@@ -561,56 +561,54 @@ describe('path', () => {
assert.equal(path.win32.join('c:/path1', 'd:path2'), 'c:\\path1\\d:path2');
});
it("resolve", function () {
it('resolve', () => {
var resolveTestsWin32 =
// arguments result
// arguments result
[
[
['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'
],
[
['c:blah\\blah', '/path'], 'c:\\path'
],
[
['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'
],
[
['c:/ignore', 'c:/some/file'], 'c:\\some\\file'
],
[
['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir\\'
],
[
['.'], process.cwd().replace(/\//g, '\\')
['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'
],
[
['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative\\'
['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'
],
// [[‘c:/‘, '//'], 'c:\\'],
// [[‘c:/‘, '//dir'], 'c:\\dir'],
// [
// ['c:/', '//'], 'c:\\'
// ],
// [
// ['c:/', '//dir'], 'c:\\dir'
// ],
[
['c:/', '//server/share'], '\\\\server\\share\\'
['', '//server/share'], '\\\\server\\share\\'
],
[
['c:/', '//server/share/file'], '\\\\server\\share\\file'
['c:/', '//server//share'], '\\\\server\\share\\'
],
[
['c:/', '//server//share'], '\\\\server\\share\\'
['c:/', '///some//dir'], 'c:\\some\\dir'
],
[
['c:/', '/some//dir'], 'c:\\some\\dir'
['C:\\foo\\tmp.3\\', '..\\tmp.3\\cycles\\root.js'],
'C:\\foo\\tmp.3\\cycles\\root.js'
]
];
// Posix
var resolveTestsPosix =
// arguments result
[
[
['/var/lib', '../', 'file/'], '/var/file/'
['/var/lib', '../', 'file/'], '/var/file'
],
[
['/var/lib', '/../', 'file/'], '/file/'
['/var/lib', '/../', 'file/'], '/file'
],
[
['a/b/c/', '../../..'], process.cwd()
@@ -619,7 +617,10 @@ describe('path', () => {
['.'], process.cwd()
],
[
['/some/dir', '.', '/absolute/'], '/absolute/'
['/some/dir', '.', '/absolute/'], '/absolute'
],
[
['/foo/tmp.3/', '../tmp.3/cycles/root.js'], '/foo/tmp.3/cycles/root.js'
]
];
@@ -628,7 +629,7 @@ describe('path', () => {
var resolveTests = isWindows ? resolveTestsWin32 : resolveTestsPosix;
// path.resolve
resolveTests.forEach(function (test) {
resolveTests.forEach(test => {
var actual = path.resolve.apply(path, test[0]);
var expected = test[1];
var message = 'path.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
@@ -646,7 +647,7 @@ describe('path', () => {
assert.equal(failures.length, 0, failures.join(''));
// path.posix.resolve
resolveTestsPosix.forEach(function (test) {
resolveTestsPosix.forEach(test => {
var actual = path.posix.resolve.apply(path.posix, test[0]);
var expected = test[1];
var message = 'path.posix.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
@@ -659,7 +660,7 @@ describe('path', () => {
assert.equal(failures.length, 0, failures.join(''));
// path.win32.resolve
resolveTestsWin32.forEach(function (test) {
resolveTestsWin32.forEach(test => {
var actual = path.win32.resolve.apply(path.win32, test[0]);
var expected = test[1];
var message = 'path.win32.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +

0 comments on commit ddd6eaa

Please sign in to comment.