| 32 | namespace Utility |
| 33 | { |
| 34 | std::string escapeString(const std::string& string_raw) |
| 35 | { |
| 36 | std::string result; |
| 37 | const std::locale c_locale("C"); |
| 38 | |
| 39 | for (auto it = string_raw.cbegin(); it != string_raw.cend(); ++it) { |
| 40 | const char c = *it; |
| 41 | |
| 42 | /* |
| 43 | * Escape any double-quote and backslash characters. |
| 44 | */ |
| 45 | if (c == '"') { |
| 46 | result.append("\\"); |
| 47 | result.append("\""); |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | if (c == '\\') { |
| 52 | result.append("\\"); |
| 53 | result.append("\\"); |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * If the current character is printable in the "C" locale, |
| 59 | * append it. Otherwise convert it to \xHH form, where HH is |
| 60 | * the hexadecimal representation of the character value. |
| 61 | */ |
| 62 | if (std::isprint(c, c_locale)) { |
| 63 | result.push_back((char)c); |
| 64 | } |
| 65 | else { |
| 66 | const std::string hexbyte = numberToString((uint8_t)c, "\\x", 16, 2, '0'); |
| 67 | result.append(hexbyte); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | std::string unescapeString(const std::string& string_escaped) |
| 75 | { |
no test coverage detected