Add helper: rdcstr standardise_directory_separator(const rdcstr &path);

Replaces all directory separators combinations with '/'
i.e.
'\' -> '/'
'//' -> '/'
This commit is contained in:
Jake Turner
2024-09-04 10:40:12 +01:00
parent e7e3f00508
commit f730eb6659
2 changed files with 38 additions and 0 deletions
+34
View File
@@ -141,6 +141,29 @@ rdcstr strip_extension(const rdcstr &path)
return path.substr(0, offs);
}
rdcstr standardise_directory_separator(const rdcstr &path)
{
// Replace '\' -> '/'
// Replace '//' -> '/'
rdcstr ret;
ret.reserve(path.size());
int slashCount = 0;
for(size_t i = 0; i < path.size(); ++i)
{
char c = path[i];
if(c == '\\')
c = '/';
if(c == '/')
slashCount++;
else
slashCount = 0;
if(slashCount < 2)
ret.push_back(c);
}
return ret;
}
void strip_nonbasic(rdcstr &str)
{
for(char &c : str)
@@ -339,6 +362,17 @@ TEST_CASE("String manipulation", "[string]")
CHECK(strip_extension("bar/foo.exe") == "bar/foo");
};
SECTION("standardise_directory_separator")
{
CHECK(standardise_directory_separator("a/exe.ext") == "a/exe.ext");
CHECK(standardise_directory_separator("a\\exe.ext") == "a/exe.ext");
CHECK(standardise_directory_separator("a//exe.ext") == "a/exe.ext");
CHECK(standardise_directory_separator("a\\\\exe.ext") == "a/exe.ext");
CHECK(standardise_directory_separator("a\\b/exe.ext") == "a/b/exe.ext");
CHECK(standardise_directory_separator("a\\/b/\\exe.ext") == "a/b/exe.ext");
CHECK(standardise_directory_separator("a\\\\/b//exe.ext") == "a/b/exe.ext");
};
SECTION("strupper")
{
CHECK(strupper("foobar") == "FOOBAR");
+4
View File
@@ -37,6 +37,10 @@ rdcstr get_basename(const rdcstr &path);
rdcstr get_dirname(const rdcstr &path);
rdcstr strip_extension(const rdcstr &path);
// Replace all directory separators combinations with '/'
// i.e. '\' -> '/' and '//' -> '/'
rdcstr standardise_directory_separator(const rdcstr &path);
// remove everything but alphanumeric ' ' and '.'
// It replaces everything else with _
// for logging strings where they might contain garbage characters