blob: 6bc1fb9ae3db3c46843b350e11009c52e583e1a0 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`curses.ascii` --- Utilities for ASCII characters
2======================================================
3
4.. module:: curses.ascii
5 :synopsis: Constants and set-membership functions for ASCII characters.
6.. moduleauthor:: Eric S. Raymond <esr@thyrsus.com>
7.. sectionauthor:: Eric S. Raymond <esr@thyrsus.com>
8
9
Georg Brandl116aa622007-08-15 14:28:22 +000010The :mod:`curses.ascii` module supplies name constants for ASCII characters and
11functions to test membership in various ASCII character classes. The constants
12supplied are names for control characters as follows:
13
14+--------------+----------------------------------------------+
15| Name | Meaning |
16+==============+==============================================+
17| :const:`NUL` | |
18+--------------+----------------------------------------------+
19| :const:`SOH` | Start of heading, console interrupt |
20+--------------+----------------------------------------------+
21| :const:`STX` | Start of text |
22+--------------+----------------------------------------------+
23| :const:`ETX` | End of text |
24+--------------+----------------------------------------------+
25| :const:`EOT` | End of transmission |
26+--------------+----------------------------------------------+
27| :const:`ENQ` | Enquiry, goes with :const:`ACK` flow control |
28+--------------+----------------------------------------------+
29| :const:`ACK` | Acknowledgement |
30+--------------+----------------------------------------------+
31| :const:`BEL` | Bell |
32+--------------+----------------------------------------------+
33| :const:`BS` | Backspace |
34+--------------+----------------------------------------------+
35| :const:`TAB` | Tab |
36+--------------+----------------------------------------------+
37| :const:`HT` | Alias for :const:`TAB`: "Horizontal tab" |
38+--------------+----------------------------------------------+
39| :const:`LF` | Line feed |
40+--------------+----------------------------------------------+
41| :const:`NL` | Alias for :const:`LF`: "New line" |
42+--------------+----------------------------------------------+
43| :const:`VT` | Vertical tab |
44+--------------+----------------------------------------------+
45| :const:`FF` | Form feed |
46+--------------+----------------------------------------------+
47| :const:`CR` | Carriage return |
48+--------------+----------------------------------------------+
49| :const:`SO` | Shift-out, begin alternate character set |
50+--------------+----------------------------------------------+
51| :const:`SI` | Shift-in, resume default character set |
52+--------------+----------------------------------------------+
53| :const:`DLE` | Data-link escape |
54+--------------+----------------------------------------------+
55| :const:`DC1` | XON, for flow control |
56+--------------+----------------------------------------------+
57| :const:`DC2` | Device control 2, block-mode flow control |
58+--------------+----------------------------------------------+
59| :const:`DC3` | XOFF, for flow control |
60+--------------+----------------------------------------------+
61| :const:`DC4` | Device control 4 |
62+--------------+----------------------------------------------+
63| :const:`NAK` | Negative acknowledgement |
64+--------------+----------------------------------------------+
65| :const:`SYN` | Synchronous idle |
66+--------------+----------------------------------------------+
67| :const:`ETB` | End transmission block |
68+--------------+----------------------------------------------+
69| :const:`CAN` | Cancel |
70+--------------+----------------------------------------------+
71| :const:`EM` | End of medium |
72+--------------+----------------------------------------------+
73| :const:`SUB` | Substitute |
74+--------------+----------------------------------------------+
75| :const:`ESC` | Escape |
76+--------------+----------------------------------------------+
77| :const:`FS` | File separator |
78+--------------+----------------------------------------------+
79| :const:`GS` | Group separator |
80+--------------+----------------------------------------------+
81| :const:`RS` | Record separator, block-mode terminator |
82+--------------+----------------------------------------------+
83| :const:`US` | Unit separator |
84+--------------+----------------------------------------------+
85| :const:`SP` | Space |
86+--------------+----------------------------------------------+
87| :const:`DEL` | Delete |
88+--------------+----------------------------------------------+
89
90Note that many of these have little practical significance in modern usage. The
91mnemonics derive from teleprinter conventions that predate digital computers.
92
93The module supplies the following functions, patterned on those in the standard
94C library:
95
96
97.. function:: isalnum(c)
98
99 Checks for an ASCII alphanumeric character; it is equivalent to ``isalpha(c) or
100 isdigit(c)``.
101
102
103.. function:: isalpha(c)
104
105 Checks for an ASCII alphabetic character; it is equivalent to ``isupper(c) or
106 islower(c)``.
107
108
109.. function:: isascii(c)
110
111 Checks for a character value that fits in the 7-bit ASCII set.
112
113
114.. function:: isblank(c)
115
116 Checks for an ASCII whitespace character.
117
118
119.. function:: iscntrl(c)
120
121 Checks for an ASCII control character (in the range 0x00 to 0x1f).
122
123
124.. function:: isdigit(c)
125
126 Checks for an ASCII decimal digit, ``'0'`` through ``'9'``. This is equivalent
127 to ``c in string.digits``.
128
129
130.. function:: isgraph(c)
131
132 Checks for ASCII any printable character except space.
133
134
135.. function:: islower(c)
136
137 Checks for an ASCII lower-case character.
138
139
140.. function:: isprint(c)
141
142 Checks for any ASCII printable character including space.
143
144
145.. function:: ispunct(c)
146
147 Checks for any printable ASCII character which is not a space or an alphanumeric
148 character.
149
150
151.. function:: isspace(c)
152
153 Checks for ASCII white-space characters; space, line feed, carriage return, form
154 feed, horizontal tab, vertical tab.
155
156
157.. function:: isupper(c)
158
159 Checks for an ASCII uppercase letter.
160
161
162.. function:: isxdigit(c)
163
164 Checks for an ASCII hexadecimal digit. This is equivalent to ``c in
165 string.hexdigits``.
166
167
168.. function:: isctrl(c)
169
170 Checks for an ASCII control character (ordinal values 0 to 31).
171
172
173.. function:: ismeta(c)
174
175 Checks for a non-ASCII character (ordinal values 0x80 and above).
176
177These functions accept either integers or strings; when the argument is a
178string, it is first converted using the built-in function :func:`ord`.
179
180Note that all these functions check ordinal bit values derived from the first
181character of the string you pass in; they do not actually know anything about
182the host machine's character encoding. For functions that know about the
183character encoding (and handle internationalization properly) see the
184:mod:`string` module.
185
186The following two functions take either a single-character string or integer
187byte value; they return a value of the same type.
188
189
190.. function:: ascii(c)
191
192 Return the ASCII value corresponding to the low 7 bits of *c*.
193
194
195.. function:: ctrl(c)
196
197 Return the control character corresponding to the given character (the character
198 bit value is bitwise-anded with 0x1f).
199
200
201.. function:: alt(c)
202
203 Return the 8-bit character corresponding to the given ASCII character (the
204 character bit value is bitwise-ored with 0x80).
205
206The following function takes either a single-character string or integer value;
207it returns a string.
208
209
210.. function:: unctrl(c)
211
212 Return a string representation of the ASCII character *c*. If *c* is printable,
213 this string is the character itself. If the character is a control character
214 (0x00-0x1f) the string consists of a caret (``'^'``) followed by the
215 corresponding uppercase letter. If the character is an ASCII delete (0x7f) the
216 string is ``'^?'``. If the character has its meta bit (0x80) set, the meta bit
217 is stripped, the preceding rules applied, and ``'!'`` prepended to the result.
218
219
220.. data:: controlnames
221
222 A 33-element string array that contains the ASCII mnemonics for the thirty-two
223 ASCII control characters from 0 (NUL) to 0x1f (US), in order, plus the mnemonic
224 ``SP`` for the space character.
225