|
| 1 | +#include "config.h" |
| 2 | + |
| 3 | +#include "torrent/utils/string_manip.h" |
| 4 | + |
| 5 | +namespace torrent::utils { |
| 6 | + |
| 7 | +namespace { |
| 8 | + |
| 9 | +inline char |
| 10 | +to_hex_char(char val, bool pos) { |
| 11 | + if (pos) |
| 12 | + val = (val >> 4) & 0x0F; |
| 13 | + else |
| 14 | + val = val & 0x0F; |
| 15 | + |
| 16 | + if (val < 10) |
| 17 | + return '0' + val; |
| 18 | + |
| 19 | + return 'A' + (val - 10); |
| 20 | +} |
| 21 | + |
| 22 | +} // namespace |
| 23 | + |
| 24 | +std::string |
| 25 | +trim_string(const std::string& str) { |
| 26 | + std::string::size_type pos{}; |
| 27 | + std::string::size_type end{str.length()}; |
| 28 | + |
| 29 | + while (pos != end && str[pos] >= ' ' && str[pos] <= '~') |
| 30 | + pos++; |
| 31 | + |
| 32 | + while (end != pos && str[end - 1] >= ' ' && str[end - 1] <= '~') |
| 33 | + end--; |
| 34 | + |
| 35 | + return str.substr(pos, end - pos); |
| 36 | +} |
| 37 | + |
| 38 | +std::string |
| 39 | +string_with_escape_codes(const std::string& str) { |
| 40 | + std::string result; |
| 41 | + |
| 42 | + for (auto c : str) { |
| 43 | + if (c < ' ' || c > '~') { |
| 44 | + result += '%' + to_hex_char(c, true) + to_hex_char(c, false); |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + result += c; |
| 49 | + } |
| 50 | + |
| 51 | + return result; |
| 52 | +} |
| 53 | + |
| 54 | +std::string |
| 55 | +sanitize_string(const std::string& str) { |
| 56 | + std::string result; |
| 57 | + bool unprintable{}; |
| 58 | + bool space{}; |
| 59 | + |
| 60 | + for (auto c : str) { |
| 61 | + if (c < ' ' || c > '~') { |
| 62 | + if (c == '\n' || c == '\r' || c == '\t') { |
| 63 | + if (!space && !unprintable) |
| 64 | + result += ' '; |
| 65 | + |
| 66 | + space = true; |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + if (!unprintable) |
| 71 | + result += '*'; |
| 72 | + |
| 73 | + unprintable = true; |
| 74 | + space = false; |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + result += c; |
| 79 | + unprintable = false; |
| 80 | + space = false; |
| 81 | + } |
| 82 | + |
| 83 | + return trim_string(result); |
| 84 | +} |
| 85 | + |
| 86 | +std::string |
| 87 | +sanitize_string_with_escape_codes(const std::string& str) { |
| 88 | + std::string result; |
| 89 | + bool space{}; |
| 90 | + |
| 91 | + for (auto c : str) { |
| 92 | + if (c < ' ' || c > '~') { |
| 93 | + if (c == '\n' || c == '\r' || c == '\t') { |
| 94 | + if (!space) |
| 95 | + result += ' '; |
| 96 | + |
| 97 | + space = true; |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + result += '%' + to_hex_char(c, true) + to_hex_char(c, false); |
| 102 | + |
| 103 | + space = false; |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + result += c; |
| 108 | + space = false; |
| 109 | + } |
| 110 | + |
| 111 | + return trim_string(result); |
| 112 | +} |
| 113 | + |
| 114 | +std::string |
| 115 | +sanitize_string_with_tags(const std::string& str) { |
| 116 | + bool in_tag{}; |
| 117 | + std::string result; |
| 118 | + std::string sanitized = sanitize_string(str); |
| 119 | + |
| 120 | + for (auto c : sanitized) { |
| 121 | + if (c == '>') { |
| 122 | + in_tag = false; |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + if (in_tag || c == '<') { |
| 127 | + in_tag = true; |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + result += c; |
| 132 | + } |
| 133 | + |
| 134 | + result = trim_string(result); |
| 135 | + |
| 136 | + if (result.empty()) |
| 137 | + return trim_string(sanitized); |
| 138 | + |
| 139 | + return result; |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace torrent::utils |
0 commit comments