This commit is contained in:
Jason Perkins 2015-04-04 11:17:24 -04:00
commit 0557e38efd
2 changed files with 44 additions and 0 deletions

View File

@ -13,6 +13,7 @@ int path_normalize(lua_State* L)
char buffer[0x4000];
char* src;
char* dst;
char* ptr;
char last;
const char* path = luaL_checkstring(L, 1);
@ -30,6 +31,24 @@ int path_normalize(lua_State* L)
ch = '/';
}
/* filter out .. */
if (ch == '.' && last == '.') {
ptr = dst - 3;
while (ptr >= buffer) {
if (*ptr != '/') {
--ptr;
}
else {
dst = ptr;
break;
}
}
if (ptr >= buffer) {
++src;
continue;
}
}
/* add to the result, filtering out duplicate slashes */
if (ch != '/' || last != '/') {
*(dst++) = ch;

View File

@ -364,3 +364,28 @@
function suite.wildcards_escapeStarStar()
test.isequal("Images/.*%.bmp", path.wildcards("Images/**.bmp"))
end
--
-- path.normalize tests
--
function suite.normalize_Test1()
local p = path.normalize("d:/game/../test")
test.isequal("d:/test", p)
end
function suite.normalize_Test2()
local p = path.normalize("d:/game/../../test")
test.isequal("d:/../test", p)
end
function suite.normalize_Test3()
local p = path.normalize("../../test")
test.isequal("../../test", 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