Output of the script:
ascii letters allowed in escape sequences: a, b, e, f, n, r, t, u, v, x, E, U
Non-escape letters: A, B, C, D, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, V, W,
X, Y, Z, c, d, g, h, i, j, k, l, m, o, p, q, s, w, y, z
NOTE: '\U'
, '\x'
, '\u'
by themselves do not form escape sequences. \
, '
, "
, ?
and digits are not considered due to they are not alphabetic. '\e'
is GCC only.
The sequences are produced by compiling C code that contains the string "\a\b...(for all ascii letters)...\z"
and parsing compiler warnings:
#!/usr/bin/env python
import re, string, subprocess, sys
def _find_non_escape_chars(compiler="cc -x c -".split(), verbose=False):
# prepare C code to compile
test_code = 'char *s = "%s";' % ''.join('\\'+c for c in string.ascii_letters)
# compile it
p = subprocess.Popen(compiler,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, _ = p.communicate(test_code)
if verbose:
print stdout
# find all non-escape characters
return set(re.findall(r"'\\(.)'", stdout))
def is_escape_char(c, non_escape=_find_non_escape_chars()):
"""Whether `c` letter may be present in an escape sequence in C.
>>> f = is_escape_char
>>> f("a")
True
>>> f("g")
False
"""
return c not in non_escape
def main():
escape_chars = filter(is_escape_char, string.ascii_letters)
print "ascii letters allowed in escape sequences:", ', '.join(escape_chars)
print "Non-escape letters:", ', '.join(
sorted(set(string.ascii_letters)-set(escape_chars)))
if __name__=="__main__":
import doctest; doctest.testmod()
main()