From 0c1354aacc166e7cd9db2cb7c1912f10bb03e3a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 16 Nov 2017 22:31:24 -0800 Subject: [PATCH] Fixed FilePath behavior with trailing slash. --- src/filepath.cpp | 10 ++++++++++ tests/filepath_test.cpp | 22 ++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/filepath.cpp b/src/filepath.cpp index 9b05b11..e22b56a 100644 --- a/src/filepath.cpp +++ b/src/filepath.cpp @@ -61,6 +61,8 @@ namespace bx dotdot = size; } + bool trailingSlash = false; + while (idx < num && err.isOk() ) { switch (_src[idx]) @@ -68,6 +70,7 @@ namespace bx case '/': case '\\': ++idx; + trailingSlash = idx == num; break; case '.': @@ -129,6 +132,11 @@ namespace bx size += write(&writer, '.', &err); } + if (trailingSlash) + { + size += write(&writer, '/', &err); + } + write(&writer, '\0', &err); return size; @@ -326,6 +334,8 @@ namespace bx { return StringView(fileName.getPtr(), ext); } + + return fileName; } return StringView(); diff --git a/tests/filepath_test.cpp b/tests/filepath_test.cpp index 61e9b2a..ff04261 100644 --- a/tests/filepath_test.cpp +++ b/tests/filepath_test.cpp @@ -26,21 +26,21 @@ FilePathTest s_filePathTest[] = {"/abc", "/abc"}, {"/", "/"}, - // Remove trailing slash - {"abc/", "abc"}, - {"abc/def/", "abc/def"}, - {"a/b/c/", "a/b/c"}, - {"./", "."}, - {"../", ".."}, - {"../../", "../.."}, - {"/abc/", "/abc"}, + // Do not remove trailing slash + {"abc/", "abc/"}, + {"abc/def/", "abc/def/"}, + {"a/b/c/", "a/b/c/"}, + {"./", "./"}, + {"../", "../"}, + {"../../", "../../"}, + {"/abc/", "/abc/"}, // Remove doubled slash {"abc//def//ghi", "abc/def/ghi"}, {"//abc", "/abc"}, {"///abc", "/abc"}, - {"//abc//", "/abc"}, - {"abc//", "abc"}, + {"//abc//", "/abc/"}, + {"abc//", "abc/"}, // Remove . elements {"abc/./def", "abc/def"}, @@ -84,6 +84,8 @@ static const FilePathSplit s_filePathSplit[] = { "tmp/archive.tar.gz", false, "tmp/", "archive.tar.gz", "archive", ".tar.gz" }, { "/tmp/archive.tar.gz", true, "/tmp/", "archive.tar.gz", "archive", ".tar.gz" }, { "d:/tmp/archive.tar.gz", true, "D:/tmp/", "archive.tar.gz", "archive", ".tar.gz" }, + { "/tmp/abv/gd", true, "/tmp/abv/", "gd", "gd", "" }, + { "/tmp/abv/gd/", true, "/tmp/abv/gd/", "", "", "" }, }; TEST_CASE("FilePath", "")