Skip to content

Commit 2cda78a

Browse files
A few improvements to SimplifyPath
1 parent 1622b44 commit 2cda78a

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

Platforms/Basic/src/BasicFileSystem.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ std::string BasicFileSystem::SimplifyPath(const Char* Path, Char Slash)
234234
Slash = IsSlash(Slash) ? Slash : SlashSymbol;
235235

236236
std::string SimplifiedPath;
237+
SimplifiedPath.reserve(std::strlen(Path));
237238
const char* c = Path;
238239

239240
if (Slash == WinSlash)
@@ -292,15 +293,12 @@ std::string BasicFileSystem::SimplifyPath(const Char* Path, Char Slash)
292293
// Handle /..
293294
c += (c[2] != '\0') ? 3 : 2;
294295
// Pop previous subdirectory unless it is a root
295-
if (SimplifiedPath.size() > RootLen)
296+
if (SimplifiedPath.length() > RootLen)
296297
{
297-
while (SimplifiedPath.size() > RootLen)
298-
{
299-
bool WasSlash = IsSlash(SimplifiedPath.back());
300-
SimplifiedPath.pop_back();
301-
if (WasSlash)
302-
break;
303-
}
298+
size_t PrevSlashPos = SimplifiedPath.length() - 1;
299+
while (PrevSlashPos > RootLen && !IsSlash(SimplifiedPath[PrevSlashPos]))
300+
--PrevSlashPos;
301+
SimplifiedPath.resize(PrevSlashPos);
304302
}
305303
else if (RootLen == 0)
306304
{
@@ -320,10 +318,12 @@ std::string BasicFileSystem::SimplifyPath(const Char* Path, Char Slash)
320318
}
321319

322320
// Copy regular path component
323-
while (*c != '\0' && !IsSlash(*c))
324-
{
325-
SimplifiedPath.push_back(*(c++));
326-
}
321+
const char* CmpEnd = c;
322+
while (*CmpEnd != '\0' && !IsSlash(*CmpEnd))
323+
++CmpEnd;
324+
325+
SimplifiedPath.append(c, CmpEnd);
326+
c = CmpEnd;
327327
}
328328

329329
if (NumLeadingDirUps > 0)

Tests/DiligentCoreTest/src/Platforms/FileSystemTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ TEST(Platforms_FileSystem, SimplifyPath)
331331
EXPECT_STREQ(FileSystem::SimplifyPath("//server/../..", '\\').c_str(), "\\\\server");
332332
EXPECT_STREQ(FileSystem::SimplifyPath("\\\\server\\..\\..", '\\').c_str(), "\\\\server");
333333

334+
EXPECT_STREQ(FileSystem::SimplifyPath("a/b/./..", '/').c_str(), "a");
335+
EXPECT_STREQ(FileSystem::SimplifyPath("a\\b\\.\\..", '\\').c_str(), "a");
336+
334337
EXPECT_STREQ(FileSystem::SimplifyPath("/a/..", '/').c_str(), "/");
335338
EXPECT_STREQ(FileSystem::SimplifyPath("\\a\\..", '/').c_str(), "/");
336339
EXPECT_STREQ(FileSystem::SimplifyPath("/a/..", '\\').c_str(), "");

0 commit comments

Comments
 (0)