Skip to content

Instantly share code, notes, and snippets.

@knzm
Created June 14, 2012 13:01
Show Gist options
  • Select an option

  • Save knzm/2930139 to your computer and use it in GitHub Desktop.

Select an option

Save knzm/2930139 to your computer and use it in GitHub Desktop.
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);
}
}
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