| 1 | #include <Common/hex.h> |
|---|---|
| 2 | #include <Common/StringUtils/StringUtils.h> |
| 3 | #include <Common/escapeForFileName.h> |
| 4 | |
| 5 | namespace DB |
| 6 | { |
| 7 | |
| 8 | std::string escapeForFileName(const std::string & s) |
| 9 | { |
| 10 | std::string res; |
| 11 | const char * pos = s.data(); |
| 12 | const char * end = pos + s.size(); |
| 13 | |
| 14 | while (pos != end) |
| 15 | { |
| 16 | unsigned char c = *pos; |
| 17 | |
| 18 | if (isWordCharASCII(c)) |
| 19 | res += c; |
| 20 | else |
| 21 | { |
| 22 | res += '%'; |
| 23 | res += hexDigitUppercase(c / 16); |
| 24 | res += hexDigitUppercase(c % 16); |
| 25 | } |
| 26 | |
| 27 | ++pos; |
| 28 | } |
| 29 | |
| 30 | return res; |
| 31 | } |
| 32 | |
| 33 | std::string unescapeForFileName(const std::string & s) |
| 34 | { |
| 35 | std::string res; |
| 36 | const char * pos = s.data(); |
| 37 | const char * end = pos + s.size(); |
| 38 | |
| 39 | while (pos != end) |
| 40 | { |
| 41 | if (!(*pos == '%' && pos + 2 < end)) |
| 42 | { |
| 43 | res += *pos; |
| 44 | ++pos; |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | ++pos; |
| 49 | res += unhex2(pos); |
| 50 | pos += 2; |
| 51 | } |
| 52 | } |
| 53 | return res; |
| 54 | } |
| 55 | |
| 56 | } |
| 57 |