Debian Codegraph @

pypy/module/cpyext/parse/cpyext_unicodeobject.h

raw symbols SCIP
1 /* --- Internal Unicode Format -------------------------------------------- */
2
3
4 /* Py_UNICODE was the native Unicode storage format (code unit) used by
5 Python and represents a single Unicode element in the Unicode type.
6 With PEP 393, Py_UNICODE is deprecated and replaced with a
7 typedef to wchar_t. */
8
9 #define PY_UNICODE_TYPE wchar_t
10 typedef wchar_t Py_UNICODE;
11
12 /* Py_UCS4 and Py_UCS2 are typedefs for the respective
13 unicode representations. */
14 /* PyPy: the cpyext parser does not understand uint32_t, uint16_t, uint8_t */
15 typedef unsigned int Py_UCS4;
16 typedef unsigned short Py_UCS2;
17 typedef unsigned char Py_UCS1;
18
19
20 /* --- Unicode Type ------------------------------------------------------- */
21
22 typedef struct {
23 /*
24 SSTATE_NOT_INTERNED (0)
25 SSTATE_INTERNED_MORTAL (1)
26 SSTATE_INTERNED_IMMORTAL (2)
27
28 If interned != SSTATE_NOT_INTERNED, the two references from the
29 dictionary to this object are *not* counted in ob_refcnt.
30 */
31 /* no bit fields on PyPy
32 unsigned int interned:2; */
33 unsigned char interned;
34 /* Character size:
35
36 - PyUnicode_WCHAR_KIND (0):
37
38 * character type = wchar_t (16 or 32 bits, depending on the
39 platform)
40
41 - PyUnicode_1BYTE_KIND (1):
42
43 * character type = Py_UCS1 (8 bits, unsigned)
44 * all characters are in the range U+0000-U+00FF (latin1)
45 * if ascii is set, all characters are in the range U+0000-U+007F
46 (ASCII), otherwise at least one character is in the range
47 U+0080-U+00FF
48
49 - PyUnicode_2BYTE_KIND (2):
50
51 * character type = Py_UCS2 (16 bits, unsigned)
52 * all characters are in the range U+0000-U+FFFF (BMP)
53 * at least one character is in the range U+0100-U+FFFF
54
55 - PyUnicode_4BYTE_KIND (4):
56
57 * character type = Py_UCS4 (32 bits, unsigned)
58 * all characters are in the range U+0000-U+10FFFF
59 * at least one character is in the range U+10000-U+10FFFF
60 */
61 /* no bit fields on PyPy
62 unsigned int kind:3; */
63 unsigned char kind;
64 /* Compact is with respect to the allocation scheme. Compact unicode
65 objects only require one memory block while non-compact objects use
66 one block for the PyUnicodeObject struct and another for its data
67 buffer. */
68 /* no bit fields on PyPy
69 unsigned int compact:1; */
70 unsigned char compact;
71 /* The string only contains characters in the range U+0000-U+007F (ASCII)
72 and the kind is PyUnicode_1BYTE_KIND. If ascii is set and compact is
73 set, use the PyASCIIObject structure. */
74 /* no bit fields on PyPy
75 unsigned int ascii:1; */
76 unsigned char ascii;
77 /* The ready flag indicates whether the object layout is initialized
78 completely. This means that this is either a compact object, or
79 the data pointer is filled out. The bit is redundant, and helps
80 to minimize the test in PyUnicode_IS_READY(). */
81 /* no bit fields on PyPy
82 unsigned int ready:1; */
83 unsigned char ready;
84 /* Padding to ensure that PyUnicode_DATA() is always aligned to
85 4 bytes (see issue #19537 on m68k). */
86 /* Not on PyPy, since not using bit fields */
87 /*unsigned int padding:24; */
88 } _PyASCIIObject_state_t;
89
90 /* ASCII-only strings created through PyUnicode_New use the PyASCIIObject
91 structure. state.ascii and state.compact are set, and the data
92 immediately follow the structure. utf8_length and wstr_length can be found
93 in the length field; the utf8 pointer is equal to the data pointer. */
94 typedef struct {
95 /* There are 4 forms of Unicode strings:
96
97 - compact ascii:
98
99 * structure = PyASCIIObject
100 * test: PyUnicode_IS_COMPACT_ASCII(op)
101 * kind = PyUnicode_1BYTE_KIND
102 * compact = 1
103 * ascii = 1
104 * ready = 1
105 * (length is the length of the utf8 and wstr strings)
106 * (data starts just after the structure)
107 * (since ASCII is decoded from UTF-8, the utf8 string are the data)
108
109 - compact:
110
111 * structure = PyCompactUnicodeObject
112 * test: PyUnicode_IS_COMPACT(op) && !PyUnicode_IS_ASCII(op)
113 * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
114 PyUnicode_4BYTE_KIND
115 * compact = 1
116 * ready = 1
117 * ascii = 0
118 * utf8 is not shared with data
119 * utf8_length = 0 if utf8 is NULL
120 * wstr is shared with data and wstr_length=length
121 if kind=PyUnicode_2BYTE_KIND and sizeof(wchar_t)=2
122 or if kind=PyUnicode_4BYTE_KIND and sizeof(wchar_t)=4
123 * wstr_length = 0 if wstr is NULL
124 * (data starts just after the structure)
125
126 - legacy string, not ready:
127
128 * structure = PyUnicodeObject
129 * test: kind == PyUnicode_WCHAR_KIND
130 * length = 0 (use wstr_length)
131 * hash = -1
132 * kind = PyUnicode_WCHAR_KIND
133 * compact = 0
134 * ascii = 0
135 * ready = 0
136 * interned = SSTATE_NOT_INTERNED
137 * wstr is not NULL
138 * data.any is NULL
139 * utf8 is NULL
140 * utf8_length = 0
141
142 - legacy string, ready:
143
144 * structure = PyUnicodeObject structure
145 * test: !PyUnicode_IS_COMPACT(op) && kind != PyUnicode_WCHAR_KIND
146 * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
147 PyUnicode_4BYTE_KIND
148 * compact = 0
149 * ready = 1
150 * data.any is not NULL
151 * utf8 is shared and utf8_length = length with data.any if ascii = 1
152 * utf8_length = 0 if utf8 is NULL
153 * wstr is shared with data.any and wstr_length = length
154 if kind=PyUnicode_2BYTE_KIND and sizeof(wchar_t)=2
155 or if kind=PyUnicode_4BYTE_KIND and sizeof(wchar_4)=4
156 * wstr_length = 0 if wstr is NULL
157
158 Compact strings use only one memory block (structure + characters),
159 whereas legacy strings use one block for the structure and one block
160 for characters.
161
162 Legacy strings are created by PyUnicode_FromUnicode() and
163 PyUnicode_FromStringAndSize(NULL, size) functions. They become ready
164 when PyUnicode_READY() is called.
165
166 See also _PyUnicode_CheckConsistency().
167 */
168 PyObject_HEAD
169 Py_ssize_t length; /* Number of code points in the string */
170 Py_hash_t hash; /* Hash value; -1 if not set */
171 _PyASCIIObject_state_t state;
172 wchar_t *wstr; /* wchar_t representation (null-terminated) */
173 } PyASCIIObject;
174
175 /* Non-ASCII strings allocated through PyUnicode_New use the
176 PyCompactUnicodeObject structure. state.compact is set, and the data
177 immediately follow the structure. */
178 typedef struct {
179 PyASCIIObject _base;
180 Py_ssize_t utf8_length; /* Number of bytes in utf8, excluding the
181 * terminating \0. */
182 char *utf8; /* UTF-8 representation (null-terminated) */
183 Py_ssize_t wstr_length; /* Number of code points in wstr, possible
184 * surrogates count as two code points. */
185 } PyCompactUnicodeObject;
186
187 /* Strings allocated through PyUnicode_FromUnicode(NULL, len) use the
188 PyUnicodeObject structure. The actual string data is initially in the wstr
189 block, and copied into the data block using _PyUnicode_Ready. */
190 typedef struct {
191 PyCompactUnicodeObject _base;
192 /*
193 union {
194 void *any;
195 Py_UCS1 *latin1;
196 Py_UCS2 *ucs2;
197 Py_UCS4 *ucs4;
198 } */ void * data; /* Canonical, smallest-form Unicode buffer */
199 } PyUnicodeObject;
200
201
202 /* --- Flexible String Representation Helper Macros (PEP 393) -------------- */
203
204 /* Values for PyASCIIObject.state: */
205
206 /* Interning state. */
207 #define SSTATE_NOT_INTERNED 0
208 #define SSTATE_INTERNED_MORTAL 1
209 #define SSTATE_INTERNED_IMMORTAL 2
210
211 /* --- Constants ---------------------------------------------------------- */
212
213 /* This Unicode character will be used as replacement character during
214 decoding if the errors argument is set to "replace". Note: the
215 Unicode character U+FFFD is the official REPLACEMENT CHARACTER in
216 Unicode 3.0. */
217
218 #define Py_UNICODE_REPLACEMENT_CHARACTER ((Py_UCS4) 0xFFFD)