performance improvement by handling edge .. directories during path join
This commit is contained in:
parent
9bad3bd55b
commit
6f20d21328
@ -10,7 +10,8 @@
|
||||
|
||||
int path_join(lua_State* L)
|
||||
{
|
||||
int i, len;
|
||||
int i;
|
||||
size_t len;
|
||||
const char* part;
|
||||
char buffer[0x4000];
|
||||
char* ptr = buffer;
|
||||
@ -47,6 +48,31 @@ int path_join(lua_State* L)
|
||||
*(ptr++) = '/';
|
||||
}
|
||||
|
||||
/* if source has a .. prefix then take off last dest path part
|
||||
note that this doesn't guarantee a normalized result as this
|
||||
code doesn't check for .. in the mid path, however .. occurring
|
||||
mid path are much more likely to occur during path joins
|
||||
and its faster if we handle here as we don't have to remove
|
||||
substrings from the middle of the string. */
|
||||
|
||||
while (ptr != buffer && len >= 2 && part[0] == '.' && part[1] == '.') {
|
||||
*(--ptr) = '\0';
|
||||
ptr = strrchr(buffer, '/');
|
||||
if (!ptr) {
|
||||
ptr = buffer;
|
||||
} else {
|
||||
++ptr;
|
||||
*ptr = '\0';
|
||||
}
|
||||
part += 2;
|
||||
len -= 2;
|
||||
if (len > 0 && part[0] == '/') {
|
||||
++part;
|
||||
--len;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* append new part */
|
||||
strcpy(ptr, part);
|
||||
ptr += len;
|
||||
|
@ -259,6 +259,26 @@
|
||||
test.isequal("p2", path.join(".", "p2"))
|
||||
end
|
||||
|
||||
function suite.join_OnBackToBasePath()
|
||||
test.isequal("", path.join("p1/p2/", "../../"))
|
||||
end
|
||||
|
||||
function suite.join_OnBackToBasePathWithoutFinalSlash()
|
||||
test.isequal("", path.join("p1/p2/", "../.."))
|
||||
end
|
||||
|
||||
function suite.join_OnUptwoFolders()
|
||||
test.isequal("p1/foo", path.join("p1/p2/p3", "../../foo"))
|
||||
end
|
||||
|
||||
function suite.join_OnUptoBase()
|
||||
test.isequal("foo", path.join("p1/p2/p3", "../../../foo"))
|
||||
end
|
||||
|
||||
function suite.join_OnUptoParentOfBase()
|
||||
test.isequal("../../p1", path.join("p1/p2/p3/p4/p5/p6/p7/", "../../../../../../../../../p1"))
|
||||
end
|
||||
|
||||
function suite.join_OnNilSecondPart()
|
||||
test.isequal("p1", path.join("p1", nil))
|
||||
end
|
||||
|
Reference in New Issue
Block a user