Created
April 22, 2022 05:27
-
-
Save JaHIY/3c91bbf7bea5661e6abfbd1349ee81a2 to your computer and use it in GitHub Desktop.
decode M notation from cat -v
This file contains hidden or 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 std::cin; | |
using std::cout; | |
using std::getline; | |
using std::endl; | |
#include <string> | |
using std::string; | |
#include <string_view> | |
using std::string_view; | |
string decode_m_notation(string_view in) { | |
string_view::size_type in_len = in.size(); | |
string result; | |
string prev; | |
for (string::size_type i = 0; i < in_len; i += 1) { | |
if (prev == "M-^") { | |
if (in[i] > 63 && in[i] <= 95) { | |
result.append(1, in[i] + 64); | |
} else if (in[i] == 63) { | |
result.append(1, 255); | |
} else { | |
result.append("M-^"); | |
result.append(1, in[i]); | |
} | |
prev = ""; | |
} else if (prev == "M-") { | |
if (in[i] == '^') { | |
prev.append("^"); | |
} else if (in[i] >= 32 && in[i] <= 126) { | |
result.append(1, in[i] + 128); | |
prev = ""; | |
} else { | |
result.append("M-"); | |
result.append(1, in[i]); | |
prev = ""; | |
} | |
} else if (prev == "M") { | |
if (in[i] == '-') { | |
prev.append("-"); | |
} else { | |
result.append("M"); | |
result.append(1, in[i]); | |
prev = ""; | |
} | |
} else if (prev == "^") { | |
if (in[i] > 63 && in[i] <= 126) { | |
result.append(1, in[i] - 64); | |
} else if (in[i] == 63) { | |
result.append(1, 127); | |
} else { | |
result.append("^"); | |
result.append(1, in[i]); | |
} | |
prev = ""; | |
} else { | |
if (in[i] == 'M') { | |
prev.append("M"); | |
} else if (in[i] == '^') { | |
prev.append("^"); | |
} else { | |
result.append(1, in[i]); | |
} | |
} | |
} | |
return result; | |
} | |
int main(void) { | |
for (string line; getline(cin, line);) { | |
cout << decode_m_notation(line) << endl; | |
} | |
return 0; | |
} | |
/* | |
> g++ -std=c++20 decode_m_notation.cpp -o decode_m_notation | |
> printf '你好,世界' | cat -v | ./decode_m_notation | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment