Created
June 14, 2012 13:01
-
-
Save knzm/2930139 to your computer and use it in GitHub Desktop.
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
| static void safe_print(const char *cp, int len) | |
| { | |
| unsigned char ch; | |
| if (len < 0) | |
| len = strlen(cp); | |
| while (len--) { | |
| ch = *cp++; | |
| if (ch > 128) { | |
| fputs("M-", stdout); | |
| ch -= 128; | |
| } | |
| if ((ch < 32) || (ch == 0x7f)) { | |
| fputc('^', stdout); | |
| ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */ | |
| } | |
| fputc(ch, stdout); | |
| } | |
| } |
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
| def unsafe_print(s): | |
| def repl(m): | |
| mode_m, mode_x, body = m.group(1, 2, 3) | |
| ch = ord(body) | |
| if mode_x: | |
| ch ^= 0x40 | |
| assert ch < 32 or ch == 0x7f | |
| if mode_m: | |
| ch += 128 | |
| assert ch > 128 | |
| return chr(ch) | |
| return re.sub(r"(M-)?(\^)?(.)", repl, s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment