Skip to content

Instantly share code, notes, and snippets.

@olegchir
Created June 13, 2020 08:01
Show Gist options
  • Save olegchir/93691b4735eeff8310a39b70f6dc841b to your computer and use it in GitHub Desktop.
Save olegchir/93691b4735eeff8310a39b70f6dc841b to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
std::string unfuck_windows_path(std::string path);
int main() {
auto in = "C:\\Users\\Мистер Йож\\Мои Документы\\Мои Игры";
auto out = unfuck_windows_path(in);
std::cout << out << std::endl;
return 0;
}
std::string unfuck_windows_path(std::string path) {
const int backSlashChar = '\\';
int firstChar = path.find(backSlashChar);
std::string result = path.substr(0, firstChar);
result.reserve(path.size());
for (size_t i = firstChar, end = path.size(); i < end; ++i) {
switch (auto symbol = path[i]) {
case 'a' ... 'z': [[fallthrough]];
case 'A' ... 'Z': [[fallthrough]];
case '0' ... '9': [[fallthrough]];
case '-': [[fallthrough]];
case '_': [[fallthrough]];
case '/': [[fallthrough]];
case '.':
result.push_back(symbol);
break;
case backSlashChar:
result.push_back('/');
break;
default:
result.push_back('_');
break;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment