10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""Constants and membership tests for ASCII characters"""
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30a8c90248264a8b26970b4473770bcc3df8515fJosh GaoNUL     = 0x00  # ^@
40a8c90248264a8b26970b4473770bcc3df8515fJosh GaoSOH     = 0x01  # ^A
50a8c90248264a8b26970b4473770bcc3df8515fJosh GaoSTX     = 0x02  # ^B
60a8c90248264a8b26970b4473770bcc3df8515fJosh GaoETX     = 0x03  # ^C
70a8c90248264a8b26970b4473770bcc3df8515fJosh GaoEOT     = 0x04  # ^D
80a8c90248264a8b26970b4473770bcc3df8515fJosh GaoENQ     = 0x05  # ^E
90a8c90248264a8b26970b4473770bcc3df8515fJosh GaoACK     = 0x06  # ^F
100a8c90248264a8b26970b4473770bcc3df8515fJosh GaoBEL     = 0x07  # ^G
110a8c90248264a8b26970b4473770bcc3df8515fJosh GaoBS      = 0x08  # ^H
120a8c90248264a8b26970b4473770bcc3df8515fJosh GaoTAB     = 0x09  # ^I
130a8c90248264a8b26970b4473770bcc3df8515fJosh GaoHT      = 0x09  # ^I
140a8c90248264a8b26970b4473770bcc3df8515fJosh GaoLF      = 0x0a  # ^J
150a8c90248264a8b26970b4473770bcc3df8515fJosh GaoNL      = 0x0a  # ^J
160a8c90248264a8b26970b4473770bcc3df8515fJosh GaoVT      = 0x0b  # ^K
170a8c90248264a8b26970b4473770bcc3df8515fJosh GaoFF      = 0x0c  # ^L
180a8c90248264a8b26970b4473770bcc3df8515fJosh GaoCR      = 0x0d  # ^M
190a8c90248264a8b26970b4473770bcc3df8515fJosh GaoSO      = 0x0e  # ^N
200a8c90248264a8b26970b4473770bcc3df8515fJosh GaoSI      = 0x0f  # ^O
210a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDLE     = 0x10  # ^P
220a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDC1     = 0x11  # ^Q
230a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDC2     = 0x12  # ^R
240a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDC3     = 0x13  # ^S
250a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDC4     = 0x14  # ^T
260a8c90248264a8b26970b4473770bcc3df8515fJosh GaoNAK     = 0x15  # ^U
270a8c90248264a8b26970b4473770bcc3df8515fJosh GaoSYN     = 0x16  # ^V
280a8c90248264a8b26970b4473770bcc3df8515fJosh GaoETB     = 0x17  # ^W
290a8c90248264a8b26970b4473770bcc3df8515fJosh GaoCAN     = 0x18  # ^X
300a8c90248264a8b26970b4473770bcc3df8515fJosh GaoEM      = 0x19  # ^Y
310a8c90248264a8b26970b4473770bcc3df8515fJosh GaoSUB     = 0x1a  # ^Z
320a8c90248264a8b26970b4473770bcc3df8515fJosh GaoESC     = 0x1b  # ^[
330a8c90248264a8b26970b4473770bcc3df8515fJosh GaoFS      = 0x1c  # ^\
340a8c90248264a8b26970b4473770bcc3df8515fJosh GaoGS      = 0x1d  # ^]
350a8c90248264a8b26970b4473770bcc3df8515fJosh GaoRS      = 0x1e  # ^^
360a8c90248264a8b26970b4473770bcc3df8515fJosh GaoUS      = 0x1f  # ^_
370a8c90248264a8b26970b4473770bcc3df8515fJosh GaoSP      = 0x20  # space
380a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDEL     = 0x7f  # delete
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gaocontrolnames = [
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"BS",  "HT",  "LF",  "VT",  "FF",  "CR",  "SO",  "SI",
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"CAN", "EM",  "SUB", "ESC", "FS",  "GS",  "RS",  "US",
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"SP"
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao]
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _ctoi(c):
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if type(c) == type(""):
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return ord(c)
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    else:
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return c
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef isalnum(c): return isalpha(c) or isdigit(c)
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef isalpha(c): return isupper(c) or islower(c)
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef isascii(c): return _ctoi(c) <= 127          # ?
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef isblank(c): return _ctoi(c) in (8,32)
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef iscntrl(c): return _ctoi(c) <= 31
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef isdigit(c): return _ctoi(c) >= 48 and _ctoi(c) <= 57
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef isgraph(c): return _ctoi(c) >= 33 and _ctoi(c) <= 126
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef islower(c): return _ctoi(c) >= 97 and _ctoi(c) <= 122
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef isprint(c): return _ctoi(c) >= 32 and _ctoi(c) <= 126
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef ispunct(c): return _ctoi(c) != 32 and not isalnum(c)
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef isspace(c): return _ctoi(c) in (9, 10, 11, 12, 13, 32)
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef isupper(c): return _ctoi(c) >= 65 and _ctoi(c) <= 90
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef isxdigit(c): return isdigit(c) or \
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    (_ctoi(c) >= 65 and _ctoi(c) <= 70) or (_ctoi(c) >= 97 and _ctoi(c) <= 102)
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef isctrl(c): return _ctoi(c) < 32
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef ismeta(c): return _ctoi(c) > 127
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef ascii(c):
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if type(c) == type(""):
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return chr(_ctoi(c) & 0x7f)
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    else:
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return _ctoi(c) & 0x7f
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef ctrl(c):
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if type(c) == type(""):
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return chr(_ctoi(c) & 0x1f)
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    else:
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return _ctoi(c) & 0x1f
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef alt(c):
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if type(c) == type(""):
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return chr(_ctoi(c) | 0x80)
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    else:
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return _ctoi(c) | 0x80
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef unctrl(c):
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    bits = _ctoi(c)
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if bits == 0x7f:
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        rep = "^?"
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    elif isprint(bits & 0x7f):
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        rep = chr(bits & 0x7f)
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    else:
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        rep = "^" + chr(((bits & 0x7f) | 0x20) + 0x20)
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if bits & 0x80:
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return "!" + rep
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return rep
100