Merge pull request #104 from Blizzard/normalize-fix

filter out /./ in path.join and path.normalize
This commit is contained in:
starkos 2015-06-21 19:30:45 -04:00
commit 0280d44efd
3 changed files with 25 additions and 1 deletions

View File

@ -28,6 +28,12 @@ int path_join(lua_State* L)
part = luaL_checkstring(L, i);
len = strlen(part);
/* remove leading "./" */
while (strncmp(part, "./", 2) == 0) {
part += 2;
len -= 2;
}
/* remove trailing slashes */
while (len > 1 && part[len - 1] == '/') {
--len;

View File

@ -47,6 +47,15 @@ int path_normalize(lua_State* L)
}
}
/* filter out /./ */
if (ch == '/' && last == '.') {
ptr = dst - 2;
if (ptr >= buffer && ptr[0] == '/') {
dst = ptr;
continue;
}
}
/* add to the result, filtering out duplicate slashes */
if (ch != '/' || last != '/') {
*(dst++) = ch;

View File

@ -275,6 +275,10 @@
test.isequal("foo", path.join("p1/p2/p3", "../../../foo"))
end
function suite.join_ignoreLeadingDots()
test.isequal("p1/p2/foo", path.join("p1/p2", "././foo"))
end
function suite.join_OnUptoParentOfBase()
test.isequal("../../p1", path.join("p1/p2/p3/p4/p5/p6/p7/", "../../../../../../../../../p1"))
end
@ -429,7 +433,12 @@
test.isequal("..", p)
end
function suite.normalize_singleDot()
local p = path.normalize("../../p1/p2/p3/p4/./a.pb.cc")
test.isequal("../../p1/p2/p3/p4/a.pb.cc", p)
end
function suite.normalize()
test.isequal("d:/ProjectB/bin", path.normalize("d:/ProjectA/../ProjectB/bin"))
test.isequal("/ProjectB/bin", path.normalize("/ProjectA/../ProjectB/bin"))
end
end