Created
June 13, 2020 08:01
-
-
Save olegchir/93691b4735eeff8310a39b70f6dc841b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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