Updated readme + sanitize subtitle file names before matching, ref #699

This commit is contained in:
emeric
2025-08-04 22:35:26 +02:00
parent 9f13742a5a
commit 9ae425a800
6 changed files with 67 additions and 7 deletions
+20
View File
@@ -86,4 +86,24 @@ namespace lms::core::pathUtils
return longestCommonPath;
}
std::string sanitizeFileStem(const std::string_view fileStem)
{
// Keep UTF8-encoded characters, but skip illegal ASCII characters
constexpr std::array<unsigned char, 9> illegalChars{ '/', '\\', ':', '*', '?', '"', '<', '>', '|' };
static_assert(std::all_of(std::begin(illegalChars), std::end(illegalChars), [](unsigned char c) { return c < 128; }), "Illegal characters must be ASCII");
std::string sanitized;
sanitized.reserve(fileStem.size());
for (const char c : fileStem)
{
if (std::any_of(std::begin(illegalChars), std::end(illegalChars), [c](char illegalChar) { return c == illegalChar; }))
continue;
sanitized.push_back(c);
}
return sanitized;
}
} // namespace lms::core::pathUtils
+5
View File
@@ -21,6 +21,8 @@
#include <filesystem>
#include <span>
#include <string>
#include <string_view>
#include <Wt/WDateTime.h>
@@ -54,4 +56,7 @@ namespace lms::core::pathUtils
return longestCommonPath;
}
// A method that sanitize a file stem, removing any illegal chars
std::string sanitizeFileStem(std::string_view fileStem);
} // namespace lms::core::pathUtils
+30
View File
@@ -103,4 +103,34 @@ namespace lms::core::pathUtils::tests
EXPECT_EQ(core::pathUtils::isPathInRootPath(test.path, test.rootPath), test.expectedResult) << "Failed: path = " << test.path << ", rootPath = " << test.rootPath;
}
}
TEST(Path, sanitizeFileStem)
{
struct TestCase
{
std::string input;
std::string_view expectedOutput;
};
TestCase tests[]{
{ "", "" }, // empty input
{ "valid_file_name", "valid_file_name" },
{ "invalid:file*name?", "invalidfilename" },
{ "another|invalid<name>", "anotherinvalidname" },
{ "/leading/slash", "leadingslash" },
{ "\\backslash\\file", "backslashfile" },
{ "file_with_äöüß", "file_with_äöüß" }, // keep German umlauts
{ "file_with_éèêë", "file_with_éèêë" }, // keep French accents
{ "héllo 漢字", "héllo 漢字" }, // keep UTF8 characters
{ "file_with_üñîçødë", "file_with_üñîçødë" }, // keep special characters
{ "file_with_!@#$%^&*()_+", "file_with_!@#$%^&()_+" }, // remove special characters
{ "file_with_", "file_with_" }, // handle double dots
{ "file.with.extension", "file.with.extension" }, // keep extensions
};
for (const TestCase& test : tests)
{
EXPECT_EQ(core::pathUtils::sanitizeFileStem(test.input), test.expectedOutput) << "Failed: input = " << test.input;
}
}
} // namespace lms::core::pathUtils::tests