constants and membership tests for ascii characters nul 0x00 soh 0x01 a stx 0x02 b etx 0x03 c eot 0x04 d enq 0x05 e ack 0x06 f bel 0x07 g bs 0x08 h tab 0x09 i ht 0x09 i lf 0x0a j nl 0x0a j vt 0x0b k ff 0x0c l cr 0x0d m so 0x0e n si 0x0f o dle 0x10 p dc1 0x11 q dc2 0x12 r dc3 0x13 s dc4 0x14 t nak 0x15 u syn 0x16 v etb 0x17 w can 0x18 x em 0x19 y sub 0x1a z esc 0x1b fs 0x1c gs 0x1d rs 0x1e us 0x1f sp 0x20 space del 0x7f delete controlnames nul soh stx etx eot enq ack bel bs ht lf vt ff cr so si dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us sp def ctoic if isinstancec str return ordc else return c def isalnumc return isalphac or isdigitc def isalphac return isupperc or islowerc def isasciic return 0 ctoic 127 def isblankc return ctoic in 9 32 def iscntrlc return 0 ctoic 31 or ctoic 127 def isdigitc return 48 ctoic 57 def isgraphc return 33 ctoic 126 def islowerc return 97 ctoic 122 def isprintc return 32 ctoic 126 def ispunctc return isgraphc and not isalnumc def isspacec return ctoic in 9 10 11 12 13 32 def isupperc return 65 ctoic 90 def isxdigitc return isdigitc or 65 ctoic 70 or 97 ctoic 102 def isctrlc return 0 ctoic 32 def ismetac return ctoic 127 def asciic if isinstancec str return chrctoic 0x7f else return ctoic 0x7f def ctrlc if isinstancec str return chrctoic 0x1f else return ctoic 0x1f def altc if isinstancec str return chrctoic 0x80 else return ctoic 0x80 def unctrlc bits ctoic if bits 0x7f rep elif isprintbits 0x7f rep chrbits 0x7f else rep chrbits 0x7f 0x20 0x20 if bits 0x80 return rep return rep a b c d e f g h i i j j k l m n o p q r s t u v w x y z _ space delete
NUL     = 0x00  
SOH     = 0x01  
STX     = 0x02  
ETX     = 0x03  
EOT     = 0x04  
ENQ     = 0x05  
ACK     = 0x06  
BEL     = 0x07  
BS      = 0x08  
TAB     = 0x09  
HT      = 0x09  
LF      = 0x0a  
NL      = 0x0a  
VT      = 0x0b  
FF      = 0x0c  
CR      = 0x0d  
SO      = 0x0e  
SI      = 0x0f  
DLE     = 0x10  
DC1     = 0x11  
DC2     = 0x12  
DC3     = 0x13  
DC4     = 0x14  
NAK     = 0x15  
SYN     = 0x16  
ETB     = 0x17  
CAN     = 0x18  
EM      = 0x19  
SUB     = 0x1a  
ESC     = 0x1b  
FS      = 0x1c  
GS      = 0x1d  
RS      = 0x1e  
US      = 0x1f  
SP      = 0x20  
DEL     = 0x7f  

controlnames = [
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS",  "HT",  "LF",  "VT",  "FF",  "CR",  "SO",  "SI",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM",  "SUB", "ESC", "FS",  "GS",  "RS",  "US",
"SP"
]

def _ctoi(c):
    if isinstance(c, str):
        return ord(c)
    else:
        return c

def isalnum(c): return isalpha(c) or isdigit(c)
def isalpha(c): return isupper(c) or islower(c)
def isascii(c): return 0 <= _ctoi(c) <= 127          
def isblank(c): return _ctoi(c) in (9, 32)
def iscntrl(c): return 0 <= _ctoi(c) <= 31 or _ctoi(c) == 127
def isdigit(c): return 48 <= _ctoi(c) <= 57
def isgraph(c): return 33 <= _ctoi(c) <= 126
def islower(c): return 97 <= _ctoi(c) <= 122
def isprint(c): return 32 <= _ctoi(c) <= 126
def ispunct(c): return isgraph(c) and not isalnum(c)
def isspace(c): return _ctoi(c) in (9, 10, 11, 12, 13, 32)
def isupper(c): return 65 <= _ctoi(c) <= 90
def isxdigit(c): return isdigit(c) or \
    (65 <= _ctoi(c) <= 70) or (97 <= _ctoi(c) <= 102)
def isctrl(c): return 0 <= _ctoi(c) < 32
def ismeta(c): return _ctoi(c) > 127

def ascii(c):
    if isinstance(c, str):
        return chr(_ctoi(c) & 0x7f)
    else:
        return _ctoi(c) & 0x7f

def ctrl(c):
    if isinstance(c, str):
        return chr(_ctoi(c) & 0x1f)
    else:
        return _ctoi(c) & 0x1f

def alt(c):
    if isinstance(c, str):
        return chr(_ctoi(c) | 0x80)
    else:
        return _ctoi(c) | 0x80

def unctrl(c):
    bits = _ctoi(c)
    if bits == 0x7f:
        rep = "^?"
    elif isprint(bits & 0x7f):
        rep = chr(bits & 0x7f)
    else:
        rep = "^" + chr(((bits & 0x7f) | 0x20) + 0x20)
    if bits & 0x80:
        return "!" + rep
    return rep