Common/StringUtil: Add convenience function for converting paths to use forward slashes on Windows.

This commit is contained in:
Admiral H. Curtiss 2022-04-16 02:01:24 +02:00
parent 2081e2f2a1
commit b1d1f2aa06
No known key found for this signature in database
GPG Key ID: F051B4C4044F33FB
2 changed files with 25 additions and 0 deletions

View File

@ -335,6 +335,23 @@ bool SplitPath(std::string_view full_path, std::string* path, std::string* filen
return true;
}
void UnifyPathSeparators(std::string& path)
{
#ifdef _WIN32
for (char& c : path)
{
if (c == '\\')
c = '/';
}
#endif
}
std::string WithUnifiedPathSeparators(std::string path)
{
UnifyPathSeparators(path);
return path;
}
std::string PathToFileName(std::string_view path)
{
std::string file_name, extension;

View File

@ -157,9 +157,17 @@ std::vector<std::string> SplitString(const std::string& str, char delim);
std::string JoinStrings(const std::vector<std::string>& strings, const std::string& delimiter);
// "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe"
// This requires forward slashes to be used for the path separators, even on Windows.
bool SplitPath(std::string_view full_path, std::string* path, std::string* filename,
std::string* extension);
// Converts the path separators of a path into forward slashes on Windows, which is assumed to be
// true for paths at various places in the codebase.
void UnifyPathSeparators(std::string& path);
std::string WithUnifiedPathSeparators(std::string path);
// Extracts just the filename (including extension) from a full path.
// This requires forward slashes to be used for the path separators, even on Windows.
std::string PathToFileName(std::string_view path);
bool StringBeginsWith(std::string_view str, std::string_view begin);