blob: 0f19b2a14bcd0a619ab18b8eb487bf030203e5c4 [file] [log] [blame]
Victor Stinner75e46992018-11-26 17:29:38 +01001#ifndef Py_CPYTHON_UNICODEOBJECT_H
2# error "this header file must not be included directly"
3#endif
4
Victor Stinner75e46992018-11-26 17:29:38 +01005/* Py_UNICODE was the native Unicode storage format (code unit) used by
6 Python and represents a single Unicode element in the Unicode type.
7 With PEP 393, Py_UNICODE is deprecated and replaced with a
8 typedef to wchar_t. */
9#define PY_UNICODE_TYPE wchar_t
Zackery Spytz3c8724f2019-05-28 09:16:33 -060010/* Py_DEPRECATED(3.3) */ typedef wchar_t Py_UNICODE;
Victor Stinner75e46992018-11-26 17:29:38 +010011
12/* --- Internal Unicode Operations ---------------------------------------- */
13
Serhiy Storchaka349f76c2020-06-30 09:03:15 +030014#define USE_UNICODE_WCHAR_CACHE 1
15
Victor Stinner75e46992018-11-26 17:29:38 +010016/* Since splitting on whitespace is an important use case, and
17 whitespace in most situations is solely ASCII whitespace, we
18 optimize for the common case by using a quick look-up table
19 _Py_ascii_whitespace (see below) with an inlined check.
20
21 */
22#define Py_UNICODE_ISSPACE(ch) \
23 ((ch) < 128U ? _Py_ascii_whitespace[(ch)] : _PyUnicode_IsWhitespace(ch))
24
25#define Py_UNICODE_ISLOWER(ch) _PyUnicode_IsLowercase(ch)
26#define Py_UNICODE_ISUPPER(ch) _PyUnicode_IsUppercase(ch)
27#define Py_UNICODE_ISTITLE(ch) _PyUnicode_IsTitlecase(ch)
28#define Py_UNICODE_ISLINEBREAK(ch) _PyUnicode_IsLinebreak(ch)
29
30#define Py_UNICODE_TOLOWER(ch) _PyUnicode_ToLowercase(ch)
31#define Py_UNICODE_TOUPPER(ch) _PyUnicode_ToUppercase(ch)
32#define Py_UNICODE_TOTITLE(ch) _PyUnicode_ToTitlecase(ch)
33
34#define Py_UNICODE_ISDECIMAL(ch) _PyUnicode_IsDecimalDigit(ch)
35#define Py_UNICODE_ISDIGIT(ch) _PyUnicode_IsDigit(ch)
36#define Py_UNICODE_ISNUMERIC(ch) _PyUnicode_IsNumeric(ch)
37#define Py_UNICODE_ISPRINTABLE(ch) _PyUnicode_IsPrintable(ch)
38
39#define Py_UNICODE_TODECIMAL(ch) _PyUnicode_ToDecimalDigit(ch)
40#define Py_UNICODE_TODIGIT(ch) _PyUnicode_ToDigit(ch)
41#define Py_UNICODE_TONUMERIC(ch) _PyUnicode_ToNumeric(ch)
42
43#define Py_UNICODE_ISALPHA(ch) _PyUnicode_IsAlpha(ch)
44
45#define Py_UNICODE_ISALNUM(ch) \
46 (Py_UNICODE_ISALPHA(ch) || \
47 Py_UNICODE_ISDECIMAL(ch) || \
48 Py_UNICODE_ISDIGIT(ch) || \
49 Py_UNICODE_ISNUMERIC(ch))
50
Inada Naoki2c4928d2020-06-17 20:09:44 +090051Py_DEPRECATED(3.3) static inline void
52Py_UNICODE_COPY(Py_UNICODE *target, const Py_UNICODE *source, Py_ssize_t length) {
53 memcpy(target, source, length * sizeof(Py_UNICODE));
54}
Victor Stinner75e46992018-11-26 17:29:38 +010055
Inada Naoki2c4928d2020-06-17 20:09:44 +090056Py_DEPRECATED(3.3) static inline void
57Py_UNICODE_FILL(Py_UNICODE *target, Py_UNICODE value, Py_ssize_t length) {
Inada Naoki8e34e922020-06-17 23:43:01 +090058 Py_ssize_t i;
59 for (i = 0; i < length; i++) {
Inada Naoki2c4928d2020-06-17 20:09:44 +090060 target[i] = value;
61 }
62}
Victor Stinner75e46992018-11-26 17:29:38 +010063
64/* macros to work with surrogates */
65#define Py_UNICODE_IS_SURROGATE(ch) (0xD800 <= (ch) && (ch) <= 0xDFFF)
66#define Py_UNICODE_IS_HIGH_SURROGATE(ch) (0xD800 <= (ch) && (ch) <= 0xDBFF)
67#define Py_UNICODE_IS_LOW_SURROGATE(ch) (0xDC00 <= (ch) && (ch) <= 0xDFFF)
68/* Join two surrogate characters and return a single Py_UCS4 value. */
69#define Py_UNICODE_JOIN_SURROGATES(high, low) \
70 (((((Py_UCS4)(high) & 0x03FF) << 10) | \
71 ((Py_UCS4)(low) & 0x03FF)) + 0x10000)
72/* high surrogate = top 10 bits added to D800 */
73#define Py_UNICODE_HIGH_SURROGATE(ch) (0xD800 - (0x10000 >> 10) + ((ch) >> 10))
74/* low surrogate = bottom 10 bits added to DC00 */
75#define Py_UNICODE_LOW_SURROGATE(ch) (0xDC00 + ((ch) & 0x3FF))
76
Victor Stinner75e46992018-11-26 17:29:38 +010077/* --- Unicode Type ------------------------------------------------------- */
78
79/* ASCII-only strings created through PyUnicode_New use the PyASCIIObject
80 structure. state.ascii and state.compact are set, and the data
81 immediately follow the structure. utf8_length and wstr_length can be found
82 in the length field; the utf8 pointer is equal to the data pointer. */
83typedef struct {
84 /* There are 4 forms of Unicode strings:
85
86 - compact ascii:
87
88 * structure = PyASCIIObject
89 * test: PyUnicode_IS_COMPACT_ASCII(op)
90 * kind = PyUnicode_1BYTE_KIND
91 * compact = 1
92 * ascii = 1
93 * ready = 1
94 * (length is the length of the utf8 and wstr strings)
95 * (data starts just after the structure)
96 * (since ASCII is decoded from UTF-8, the utf8 string are the data)
97
98 - compact:
99
100 * structure = PyCompactUnicodeObject
101 * test: PyUnicode_IS_COMPACT(op) && !PyUnicode_IS_ASCII(op)
102 * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
103 PyUnicode_4BYTE_KIND
104 * compact = 1
105 * ready = 1
106 * ascii = 0
107 * utf8 is not shared with data
108 * utf8_length = 0 if utf8 is NULL
109 * wstr is shared with data and wstr_length=length
110 if kind=PyUnicode_2BYTE_KIND and sizeof(wchar_t)=2
111 or if kind=PyUnicode_4BYTE_KIND and sizeof(wchar_t)=4
112 * wstr_length = 0 if wstr is NULL
113 * (data starts just after the structure)
114
115 - legacy string, not ready:
116
117 * structure = PyUnicodeObject
118 * test: kind == PyUnicode_WCHAR_KIND
119 * length = 0 (use wstr_length)
120 * hash = -1
121 * kind = PyUnicode_WCHAR_KIND
122 * compact = 0
123 * ascii = 0
124 * ready = 0
125 * interned = SSTATE_NOT_INTERNED
126 * wstr is not NULL
127 * data.any is NULL
128 * utf8 is NULL
129 * utf8_length = 0
130
131 - legacy string, ready:
132
133 * structure = PyUnicodeObject structure
134 * test: !PyUnicode_IS_COMPACT(op) && kind != PyUnicode_WCHAR_KIND
135 * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
136 PyUnicode_4BYTE_KIND
137 * compact = 0
138 * ready = 1
139 * data.any is not NULL
140 * utf8 is shared and utf8_length = length with data.any if ascii = 1
141 * utf8_length = 0 if utf8 is NULL
142 * wstr is shared with data.any and wstr_length = length
143 if kind=PyUnicode_2BYTE_KIND and sizeof(wchar_t)=2
144 or if kind=PyUnicode_4BYTE_KIND and sizeof(wchar_4)=4
145 * wstr_length = 0 if wstr is NULL
146
147 Compact strings use only one memory block (structure + characters),
148 whereas legacy strings use one block for the structure and one block
149 for characters.
150
151 Legacy strings are created by PyUnicode_FromUnicode() and
152 PyUnicode_FromStringAndSize(NULL, size) functions. They become ready
153 when PyUnicode_READY() is called.
154
155 See also _PyUnicode_CheckConsistency().
156 */
157 PyObject_HEAD
158 Py_ssize_t length; /* Number of code points in the string */
159 Py_hash_t hash; /* Hash value; -1 if not set */
160 struct {
161 /*
162 SSTATE_NOT_INTERNED (0)
163 SSTATE_INTERNED_MORTAL (1)
164 SSTATE_INTERNED_IMMORTAL (2)
165
166 If interned != SSTATE_NOT_INTERNED, the two references from the
167 dictionary to this object are *not* counted in ob_refcnt.
168 */
169 unsigned int interned:2;
170 /* Character size:
171
172 - PyUnicode_WCHAR_KIND (0):
173
174 * character type = wchar_t (16 or 32 bits, depending on the
175 platform)
176
177 - PyUnicode_1BYTE_KIND (1):
178
179 * character type = Py_UCS1 (8 bits, unsigned)
180 * all characters are in the range U+0000-U+00FF (latin1)
181 * if ascii is set, all characters are in the range U+0000-U+007F
182 (ASCII), otherwise at least one character is in the range
183 U+0080-U+00FF
184
185 - PyUnicode_2BYTE_KIND (2):
186
187 * character type = Py_UCS2 (16 bits, unsigned)
188 * all characters are in the range U+0000-U+FFFF (BMP)
189 * at least one character is in the range U+0100-U+FFFF
190
191 - PyUnicode_4BYTE_KIND (4):
192
193 * character type = Py_UCS4 (32 bits, unsigned)
194 * all characters are in the range U+0000-U+10FFFF
195 * at least one character is in the range U+10000-U+10FFFF
196 */
197 unsigned int kind:3;
198 /* Compact is with respect to the allocation scheme. Compact unicode
199 objects only require one memory block while non-compact objects use
200 one block for the PyUnicodeObject struct and another for its data
201 buffer. */
202 unsigned int compact:1;
203 /* The string only contains characters in the range U+0000-U+007F (ASCII)
204 and the kind is PyUnicode_1BYTE_KIND. If ascii is set and compact is
205 set, use the PyASCIIObject structure. */
206 unsigned int ascii:1;
207 /* The ready flag indicates whether the object layout is initialized
208 completely. This means that this is either a compact object, or
209 the data pointer is filled out. The bit is redundant, and helps
210 to minimize the test in PyUnicode_IS_READY(). */
211 unsigned int ready:1;
212 /* Padding to ensure that PyUnicode_DATA() is always aligned to
213 4 bytes (see issue #19537 on m68k). */
214 unsigned int :24;
215 } state;
216 wchar_t *wstr; /* wchar_t representation (null-terminated) */
217} PyASCIIObject;
218
219/* Non-ASCII strings allocated through PyUnicode_New use the
220 PyCompactUnicodeObject structure. state.compact is set, and the data
221 immediately follow the structure. */
222typedef struct {
223 PyASCIIObject _base;
224 Py_ssize_t utf8_length; /* Number of bytes in utf8, excluding the
225 * terminating \0. */
226 char *utf8; /* UTF-8 representation (null-terminated) */
227 Py_ssize_t wstr_length; /* Number of code points in wstr, possible
228 * surrogates count as two code points. */
229} PyCompactUnicodeObject;
230
231/* Strings allocated through PyUnicode_FromUnicode(NULL, len) use the
232 PyUnicodeObject structure. The actual string data is initially in the wstr
233 block, and copied into the data block using _PyUnicode_Ready. */
234typedef struct {
235 PyCompactUnicodeObject _base;
236 union {
237 void *any;
238 Py_UCS1 *latin1;
239 Py_UCS2 *ucs2;
240 Py_UCS4 *ucs4;
241 } data; /* Canonical, smallest-form Unicode buffer */
242} PyUnicodeObject;
243
Victor Stinner68762572019-10-07 18:42:01 +0200244PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
245 PyObject *op,
246 int check_content);
247
Victor Stinner75e46992018-11-26 17:29:38 +0100248/* Fast access macros */
Victor Stinner75e46992018-11-26 17:29:38 +0100249
250/* Returns the deprecated Py_UNICODE representation's size in code units
251 (this includes surrogate pairs as 2 units).
252 If the Py_UNICODE representation is not available, it will be computed
253 on request. Use PyUnicode_GET_LENGTH() for the length in code points. */
254
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600255/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100256#define PyUnicode_GET_SIZE(op) \
257 (assert(PyUnicode_Check(op)), \
258 (((PyASCIIObject *)(op))->wstr) ? \
259 PyUnicode_WSTR_LENGTH(op) : \
260 ((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
261 assert(((PyASCIIObject *)(op))->wstr), \
262 PyUnicode_WSTR_LENGTH(op)))
Victor Stinner75e46992018-11-26 17:29:38 +0100263
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600264/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100265#define PyUnicode_GET_DATA_SIZE(op) \
266 (PyUnicode_GET_SIZE(op) * Py_UNICODE_SIZE)
Victor Stinner75e46992018-11-26 17:29:38 +0100267
268/* Alias for PyUnicode_AsUnicode(). This will create a wchar_t/Py_UNICODE
269 representation on demand. Using this macro is very inefficient now,
270 try to port your code to use the new PyUnicode_*BYTE_DATA() macros or
271 use PyUnicode_WRITE() and PyUnicode_READ(). */
272
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600273/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100274#define PyUnicode_AS_UNICODE(op) \
275 (assert(PyUnicode_Check(op)), \
276 (((PyASCIIObject *)(op))->wstr) ? (((PyASCIIObject *)(op))->wstr) : \
277 PyUnicode_AsUnicode(_PyObject_CAST(op)))
Victor Stinner75e46992018-11-26 17:29:38 +0100278
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600279/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100280#define PyUnicode_AS_DATA(op) \
281 ((const char *)(PyUnicode_AS_UNICODE(op)))
Victor Stinner75e46992018-11-26 17:29:38 +0100282
283
284/* --- Flexible String Representation Helper Macros (PEP 393) -------------- */
285
286/* Values for PyASCIIObject.state: */
287
288/* Interning state. */
289#define SSTATE_NOT_INTERNED 0
290#define SSTATE_INTERNED_MORTAL 1
291#define SSTATE_INTERNED_IMMORTAL 2
292
293/* Return true if the string contains only ASCII characters, or 0 if not. The
294 string may be compact (PyUnicode_IS_COMPACT_ASCII) or not, but must be
295 ready. */
296#define PyUnicode_IS_ASCII(op) \
297 (assert(PyUnicode_Check(op)), \
298 assert(PyUnicode_IS_READY(op)), \
299 ((PyASCIIObject*)op)->state.ascii)
300
301/* Return true if the string is compact or 0 if not.
302 No type checks or Ready calls are performed. */
303#define PyUnicode_IS_COMPACT(op) \
304 (((PyASCIIObject*)(op))->state.compact)
305
306/* Return true if the string is a compact ASCII string (use PyASCIIObject
307 structure), or 0 if not. No type checks or Ready calls are performed. */
308#define PyUnicode_IS_COMPACT_ASCII(op) \
309 (((PyASCIIObject*)op)->state.ascii && PyUnicode_IS_COMPACT(op))
310
311enum PyUnicode_Kind {
312/* String contains only wstr byte characters. This is only possible
313 when the string was created with a legacy API and _PyUnicode_Ready()
314 has not been called yet. */
315 PyUnicode_WCHAR_KIND = 0,
316/* Return values of the PyUnicode_KIND() macro: */
317 PyUnicode_1BYTE_KIND = 1,
318 PyUnicode_2BYTE_KIND = 2,
319 PyUnicode_4BYTE_KIND = 4
320};
321
322/* Return pointers to the canonical representation cast to unsigned char,
323 Py_UCS2, or Py_UCS4 for direct character access.
324 No checks are performed, use PyUnicode_KIND() before to ensure
325 these will work correctly. */
326
327#define PyUnicode_1BYTE_DATA(op) ((Py_UCS1*)PyUnicode_DATA(op))
328#define PyUnicode_2BYTE_DATA(op) ((Py_UCS2*)PyUnicode_DATA(op))
329#define PyUnicode_4BYTE_DATA(op) ((Py_UCS4*)PyUnicode_DATA(op))
330
331/* Return one of the PyUnicode_*_KIND values defined above. */
332#define PyUnicode_KIND(op) \
333 (assert(PyUnicode_Check(op)), \
334 assert(PyUnicode_IS_READY(op)), \
335 ((PyASCIIObject *)(op))->state.kind)
336
337/* Return a void pointer to the raw unicode buffer. */
338#define _PyUnicode_COMPACT_DATA(op) \
339 (PyUnicode_IS_ASCII(op) ? \
340 ((void*)((PyASCIIObject*)(op) + 1)) : \
341 ((void*)((PyCompactUnicodeObject*)(op) + 1)))
342
343#define _PyUnicode_NONCOMPACT_DATA(op) \
344 (assert(((PyUnicodeObject*)(op))->data.any), \
345 ((((PyUnicodeObject *)(op))->data.any)))
346
347#define PyUnicode_DATA(op) \
348 (assert(PyUnicode_Check(op)), \
349 PyUnicode_IS_COMPACT(op) ? _PyUnicode_COMPACT_DATA(op) : \
350 _PyUnicode_NONCOMPACT_DATA(op))
351
352/* In the access macros below, "kind" may be evaluated more than once.
353 All other macro parameters are evaluated exactly once, so it is safe
354 to put side effects into them (such as increasing the index). */
355
356/* Write into the canonical representation, this macro does not do any sanity
357 checks and is intended for usage in loops. The caller should cache the
358 kind and data pointers obtained from other macro calls.
359 index is the index in the string (starts at 0) and value is the new
360 code point value which should be written to that location. */
361#define PyUnicode_WRITE(kind, data, index, value) \
362 do { \
363 switch ((kind)) { \
364 case PyUnicode_1BYTE_KIND: { \
365 ((Py_UCS1 *)(data))[(index)] = (Py_UCS1)(value); \
366 break; \
367 } \
368 case PyUnicode_2BYTE_KIND: { \
369 ((Py_UCS2 *)(data))[(index)] = (Py_UCS2)(value); \
370 break; \
371 } \
372 default: { \
373 assert((kind) == PyUnicode_4BYTE_KIND); \
374 ((Py_UCS4 *)(data))[(index)] = (Py_UCS4)(value); \
375 } \
376 } \
377 } while (0)
378
379/* Read a code point from the string's canonical representation. No checks
380 or ready calls are performed. */
381#define PyUnicode_READ(kind, data, index) \
382 ((Py_UCS4) \
383 ((kind) == PyUnicode_1BYTE_KIND ? \
384 ((const Py_UCS1 *)(data))[(index)] : \
385 ((kind) == PyUnicode_2BYTE_KIND ? \
386 ((const Py_UCS2 *)(data))[(index)] : \
387 ((const Py_UCS4 *)(data))[(index)] \
388 ) \
389 ))
390
391/* PyUnicode_READ_CHAR() is less efficient than PyUnicode_READ() because it
392 calls PyUnicode_KIND() and might call it twice. For single reads, use
393 PyUnicode_READ_CHAR, for multiple consecutive reads callers should
394 cache kind and use PyUnicode_READ instead. */
395#define PyUnicode_READ_CHAR(unicode, index) \
396 (assert(PyUnicode_Check(unicode)), \
397 assert(PyUnicode_IS_READY(unicode)), \
398 (Py_UCS4) \
399 (PyUnicode_KIND((unicode)) == PyUnicode_1BYTE_KIND ? \
400 ((const Py_UCS1 *)(PyUnicode_DATA((unicode))))[(index)] : \
401 (PyUnicode_KIND((unicode)) == PyUnicode_2BYTE_KIND ? \
402 ((const Py_UCS2 *)(PyUnicode_DATA((unicode))))[(index)] : \
403 ((const Py_UCS4 *)(PyUnicode_DATA((unicode))))[(index)] \
404 ) \
405 ))
406
407/* Returns the length of the unicode string. The caller has to make sure that
408 the string has it's canonical representation set before calling
409 this macro. Call PyUnicode_(FAST_)Ready to ensure that. */
410#define PyUnicode_GET_LENGTH(op) \
411 (assert(PyUnicode_Check(op)), \
412 assert(PyUnicode_IS_READY(op)), \
413 ((PyASCIIObject *)(op))->length)
414
415
416/* Fast check to determine whether an object is ready. Equivalent to
417 PyUnicode_IS_COMPACT(op) || ((PyUnicodeObject*)(op))->data.any) */
418
419#define PyUnicode_IS_READY(op) (((PyASCIIObject*)op)->state.ready)
420
421/* PyUnicode_READY() does less work than _PyUnicode_Ready() in the best
422 case. If the canonical representation is not yet set, it will still call
423 _PyUnicode_Ready().
424 Returns 0 on success and -1 on errors. */
425#define PyUnicode_READY(op) \
426 (assert(PyUnicode_Check(op)), \
427 (PyUnicode_IS_READY(op) ? \
428 0 : _PyUnicode_Ready(_PyObject_CAST(op))))
429
430/* Return a maximum character value which is suitable for creating another
431 string based on op. This is always an approximation but more efficient
432 than iterating over the string. */
433#define PyUnicode_MAX_CHAR_VALUE(op) \
434 (assert(PyUnicode_IS_READY(op)), \
435 (PyUnicode_IS_ASCII(op) ? \
436 (0x7f) : \
437 (PyUnicode_KIND(op) == PyUnicode_1BYTE_KIND ? \
438 (0xffU) : \
439 (PyUnicode_KIND(op) == PyUnicode_2BYTE_KIND ? \
440 (0xffffU) : \
441 (0x10ffffU)))))
442
Inada Naoki2c4928d2020-06-17 20:09:44 +0900443Py_DEPRECATED(3.3)
444static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
445 return PyUnicode_IS_COMPACT_ASCII(op) ?
446 ((PyASCIIObject*)op)->length :
447 ((PyCompactUnicodeObject*)op)->wstr_length;
448}
449#define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
450
Victor Stinner75e46992018-11-26 17:29:38 +0100451/* === Public API ========================================================= */
452
453/* --- Plain Py_UNICODE --------------------------------------------------- */
454
455/* With PEP 393, this is the recommended way to allocate a new unicode object.
456 This function will allocate the object and its buffer in a single memory
457 block. Objects created using this function are not resizable. */
458PyAPI_FUNC(PyObject*) PyUnicode_New(
459 Py_ssize_t size, /* Number of code points in the new string */
460 Py_UCS4 maxchar /* maximum code point value in the string */
461 );
462
463/* Initializes the canonical string representation from the deprecated
464 wstr/Py_UNICODE representation. This function is used to convert Unicode
465 objects which were created using the old API to the new flexible format
466 introduced with PEP 393.
467
468 Don't call this function directly, use the public PyUnicode_READY() macro
469 instead. */
470PyAPI_FUNC(int) _PyUnicode_Ready(
471 PyObject *unicode /* Unicode object */
472 );
473
474/* Get a copy of a Unicode string. */
475PyAPI_FUNC(PyObject*) _PyUnicode_Copy(
476 PyObject *unicode
477 );
478
479/* Copy character from one unicode object into another, this function performs
480 character conversion when necessary and falls back to memcpy() if possible.
481
482 Fail if to is too small (smaller than *how_many* or smaller than
483 len(from)-from_start), or if kind(from[from_start:from_start+how_many]) >
484 kind(to), or if *to* has more than 1 reference.
485
486 Return the number of written character, or return -1 and raise an exception
487 on error.
488
489 Pseudo-code:
490
491 how_many = min(how_many, len(from) - from_start)
492 to[to_start:to_start+how_many] = from[from_start:from_start+how_many]
493 return how_many
494
495 Note: The function doesn't write a terminating null character.
496 */
497PyAPI_FUNC(Py_ssize_t) PyUnicode_CopyCharacters(
498 PyObject *to,
499 Py_ssize_t to_start,
500 PyObject *from,
501 Py_ssize_t from_start,
502 Py_ssize_t how_many
503 );
504
505/* Unsafe version of PyUnicode_CopyCharacters(): don't check arguments and so
506 may crash if parameters are invalid (e.g. if the output string
507 is too short). */
508PyAPI_FUNC(void) _PyUnicode_FastCopyCharacters(
509 PyObject *to,
510 Py_ssize_t to_start,
511 PyObject *from,
512 Py_ssize_t from_start,
513 Py_ssize_t how_many
514 );
515
516/* Fill a string with a character: write fill_char into
517 unicode[start:start+length].
518
519 Fail if fill_char is bigger than the string maximum character, or if the
520 string has more than 1 reference.
521
522 Return the number of written character, or return -1 and raise an exception
523 on error. */
524PyAPI_FUNC(Py_ssize_t) PyUnicode_Fill(
525 PyObject *unicode,
526 Py_ssize_t start,
527 Py_ssize_t length,
528 Py_UCS4 fill_char
529 );
530
531/* Unsafe version of PyUnicode_Fill(): don't check arguments and so may crash
532 if parameters are invalid (e.g. if length is longer than the string). */
533PyAPI_FUNC(void) _PyUnicode_FastFill(
534 PyObject *unicode,
535 Py_ssize_t start,
536 Py_ssize_t length,
537 Py_UCS4 fill_char
538 );
539
540/* Create a Unicode Object from the Py_UNICODE buffer u of the given
541 size.
542
543 u may be NULL which causes the contents to be undefined. It is the
544 user's responsibility to fill in the needed data afterwards. Note
545 that modifying the Unicode object contents after construction is
546 only allowed if u was set to NULL.
547
548 The buffer is copied into the new object. */
Inada Naoki2c4928d2020-06-17 20:09:44 +0900549Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_FromUnicode(
Victor Stinner75e46992018-11-26 17:29:38 +0100550 const Py_UNICODE *u, /* Unicode buffer */
551 Py_ssize_t size /* size of buffer */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600552 );
Victor Stinner75e46992018-11-26 17:29:38 +0100553
554/* Create a new string from a buffer of Py_UCS1, Py_UCS2 or Py_UCS4 characters.
555 Scan the string to find the maximum character. */
556PyAPI_FUNC(PyObject*) PyUnicode_FromKindAndData(
557 int kind,
558 const void *buffer,
559 Py_ssize_t size);
560
561/* Create a new string from a buffer of ASCII characters.
562 WARNING: Don't check if the string contains any non-ASCII character. */
563PyAPI_FUNC(PyObject*) _PyUnicode_FromASCII(
564 const char *buffer,
565 Py_ssize_t size);
566
567/* Compute the maximum character of the substring unicode[start:end].
568 Return 127 for an empty string. */
569PyAPI_FUNC(Py_UCS4) _PyUnicode_FindMaxChar (
570 PyObject *unicode,
571 Py_ssize_t start,
572 Py_ssize_t end);
573
574/* Return a read-only pointer to the Unicode object's internal
575 Py_UNICODE buffer.
576 If the wchar_t/Py_UNICODE representation is not yet available, this
577 function will calculate it. */
Inada Naoki2c4928d2020-06-17 20:09:44 +0900578Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
Victor Stinner75e46992018-11-26 17:29:38 +0100579 PyObject *unicode /* Unicode object */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600580 );
Victor Stinner75e46992018-11-26 17:29:38 +0100581
582/* Similar to PyUnicode_AsUnicode(), but raises a ValueError if the string
583 contains null characters. */
Inada Naoki2c4928d2020-06-17 20:09:44 +0900584Py_DEPRECATED(3.3) PyAPI_FUNC(const Py_UNICODE *) _PyUnicode_AsUnicode(
Victor Stinner75e46992018-11-26 17:29:38 +0100585 PyObject *unicode /* Unicode object */
586 );
587
588/* Return a read-only pointer to the Unicode object's internal
589 Py_UNICODE buffer and save the length at size.
590 If the wchar_t/Py_UNICODE representation is not yet available, this
591 function will calculate it. */
592
Inada Naoki2c4928d2020-06-17 20:09:44 +0900593Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicodeAndSize(
Victor Stinner75e46992018-11-26 17:29:38 +0100594 PyObject *unicode, /* Unicode object */
595 Py_ssize_t *size /* location where to save the length */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600596 );
Victor Stinner75e46992018-11-26 17:29:38 +0100597
Victor Stinner75e46992018-11-26 17:29:38 +0100598
599/* --- _PyUnicodeWriter API ----------------------------------------------- */
600
601typedef struct {
602 PyObject *buffer;
603 void *data;
604 enum PyUnicode_Kind kind;
605 Py_UCS4 maxchar;
606 Py_ssize_t size;
607 Py_ssize_t pos;
608
609 /* minimum number of allocated characters (default: 0) */
610 Py_ssize_t min_length;
611
612 /* minimum character (default: 127, ASCII) */
613 Py_UCS4 min_char;
614
615 /* If non-zero, overallocate the buffer (default: 0). */
616 unsigned char overallocate;
617
618 /* If readonly is 1, buffer is a shared string (cannot be modified)
619 and size is set to 0. */
620 unsigned char readonly;
621} _PyUnicodeWriter ;
622
623/* Initialize a Unicode writer.
624 *
625 * By default, the minimum buffer size is 0 character and overallocation is
626 * disabled. Set min_length, min_char and overallocate attributes to control
627 * the allocation of the buffer. */
628PyAPI_FUNC(void)
629_PyUnicodeWriter_Init(_PyUnicodeWriter *writer);
630
631/* Prepare the buffer to write 'length' characters
632 with the specified maximum character.
633
634 Return 0 on success, raise an exception and return -1 on error. */
635#define _PyUnicodeWriter_Prepare(WRITER, LENGTH, MAXCHAR) \
636 (((MAXCHAR) <= (WRITER)->maxchar \
637 && (LENGTH) <= (WRITER)->size - (WRITER)->pos) \
638 ? 0 \
639 : (((LENGTH) == 0) \
640 ? 0 \
641 : _PyUnicodeWriter_PrepareInternal((WRITER), (LENGTH), (MAXCHAR))))
642
643/* Don't call this function directly, use the _PyUnicodeWriter_Prepare() macro
644 instead. */
645PyAPI_FUNC(int)
646_PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
647 Py_ssize_t length, Py_UCS4 maxchar);
648
649/* Prepare the buffer to have at least the kind KIND.
650 For example, kind=PyUnicode_2BYTE_KIND ensures that the writer will
651 support characters in range U+000-U+FFFF.
652
653 Return 0 on success, raise an exception and return -1 on error. */
654#define _PyUnicodeWriter_PrepareKind(WRITER, KIND) \
655 (assert((KIND) != PyUnicode_WCHAR_KIND), \
656 (KIND) <= (WRITER)->kind \
657 ? 0 \
658 : _PyUnicodeWriter_PrepareKindInternal((WRITER), (KIND)))
659
660/* Don't call this function directly, use the _PyUnicodeWriter_PrepareKind()
661 macro instead. */
662PyAPI_FUNC(int)
663_PyUnicodeWriter_PrepareKindInternal(_PyUnicodeWriter *writer,
664 enum PyUnicode_Kind kind);
665
666/* Append a Unicode character.
667 Return 0 on success, raise an exception and return -1 on error. */
668PyAPI_FUNC(int)
669_PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer,
670 Py_UCS4 ch
671 );
672
673/* Append a Unicode string.
674 Return 0 on success, raise an exception and return -1 on error. */
675PyAPI_FUNC(int)
676_PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer,
677 PyObject *str /* Unicode string */
678 );
679
680/* Append a substring of a Unicode string.
681 Return 0 on success, raise an exception and return -1 on error. */
682PyAPI_FUNC(int)
683_PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer,
684 PyObject *str, /* Unicode string */
685 Py_ssize_t start,
686 Py_ssize_t end
687 );
688
689/* Append an ASCII-encoded byte string.
690 Return 0 on success, raise an exception and return -1 on error. */
691PyAPI_FUNC(int)
692_PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer,
693 const char *str, /* ASCII-encoded byte string */
694 Py_ssize_t len /* number of bytes, or -1 if unknown */
695 );
696
697/* Append a latin1-encoded byte string.
698 Return 0 on success, raise an exception and return -1 on error. */
699PyAPI_FUNC(int)
700_PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer,
701 const char *str, /* latin1-encoded byte string */
702 Py_ssize_t len /* length in bytes */
703 );
704
705/* Get the value of the writer as a Unicode string. Clear the
706 buffer of the writer. Raise an exception and return NULL
707 on error. */
708PyAPI_FUNC(PyObject *)
709_PyUnicodeWriter_Finish(_PyUnicodeWriter *writer);
710
711/* Deallocate memory of a writer (clear its internal buffer). */
712PyAPI_FUNC(void)
713_PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer);
714
715
716/* Format the object based on the format_spec, as defined in PEP 3101
717 (Advanced String Formatting). */
718PyAPI_FUNC(int) _PyUnicode_FormatAdvancedWriter(
719 _PyUnicodeWriter *writer,
720 PyObject *obj,
721 PyObject *format_spec,
722 Py_ssize_t start,
723 Py_ssize_t end);
724
Victor Stinner75e46992018-11-26 17:29:38 +0100725/* --- Manage the default encoding ---------------------------------------- */
726
727/* Returns a pointer to the default encoding (UTF-8) of the
728 Unicode object unicode and the size of the encoded representation
729 in bytes stored in *size.
730
731 In case of an error, no *size is set.
732
733 This function caches the UTF-8 encoded string in the unicodeobject
734 and subsequent calls will return the same string. The memory is released
735 when the unicodeobject is deallocated.
736
737 _PyUnicode_AsStringAndSize is a #define for PyUnicode_AsUTF8AndSize to
738 support the previous internal function with the same behaviour.
Victor Stinner75e46992018-11-26 17:29:38 +0100739*/
740
741PyAPI_FUNC(const char *) PyUnicode_AsUTF8AndSize(
742 PyObject *unicode,
743 Py_ssize_t *size);
744
745#define _PyUnicode_AsStringAndSize PyUnicode_AsUTF8AndSize
746
747/* Returns a pointer to the default encoding (UTF-8) of the
748 Unicode object unicode.
749
750 Like PyUnicode_AsUTF8AndSize(), this also caches the UTF-8 representation
751 in the unicodeobject.
752
753 _PyUnicode_AsString is a #define for PyUnicode_AsUTF8 to
754 support the previous internal function with the same behaviour.
755
756 Use of this API is DEPRECATED since no size information can be
757 extracted from the returned data.
758
759 *** This API is for interpreter INTERNAL USE ONLY and will likely
760 *** be removed or changed for Python 3.1.
761
762 *** If you need to access the Unicode object as UTF-8 bytes string,
763 *** please use PyUnicode_AsUTF8String() instead.
764
765*/
766
767PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode);
768
769#define _PyUnicode_AsString PyUnicode_AsUTF8
770
771/* --- Generic Codecs ----------------------------------------------------- */
772
773/* Encodes a Py_UNICODE buffer of the given size and returns a
774 Python string object. */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600775Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_Encode(
Victor Stinner75e46992018-11-26 17:29:38 +0100776 const Py_UNICODE *s, /* Unicode char buffer */
777 Py_ssize_t size, /* number of Py_UNICODE chars to encode */
778 const char *encoding, /* encoding */
779 const char *errors /* error handling */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600780 );
Victor Stinner75e46992018-11-26 17:29:38 +0100781
782/* --- UTF-7 Codecs ------------------------------------------------------- */
783
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600784Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF7(
Victor Stinner75e46992018-11-26 17:29:38 +0100785 const Py_UNICODE *data, /* Unicode char buffer */
786 Py_ssize_t length, /* number of Py_UNICODE chars to encode */
787 int base64SetO, /* Encode RFC2152 Set O characters in base64 */
788 int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
789 const char *errors /* error handling */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600790 );
Victor Stinner75e46992018-11-26 17:29:38 +0100791
792PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF7(
793 PyObject *unicode, /* Unicode object */
794 int base64SetO, /* Encode RFC2152 Set O characters in base64 */
795 int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
796 const char *errors /* error handling */
797 );
798
799/* --- UTF-8 Codecs ------------------------------------------------------- */
800
801PyAPI_FUNC(PyObject*) _PyUnicode_AsUTF8String(
802 PyObject *unicode,
803 const char *errors);
804
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600805Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF8(
Victor Stinner75e46992018-11-26 17:29:38 +0100806 const Py_UNICODE *data, /* Unicode char buffer */
807 Py_ssize_t length, /* number of Py_UNICODE chars to encode */
808 const char *errors /* error handling */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600809 );
Victor Stinner75e46992018-11-26 17:29:38 +0100810
811/* --- UTF-32 Codecs ------------------------------------------------------ */
812
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600813Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF32(
Victor Stinner75e46992018-11-26 17:29:38 +0100814 const Py_UNICODE *data, /* Unicode char buffer */
815 Py_ssize_t length, /* number of Py_UNICODE chars to encode */
816 const char *errors, /* error handling */
817 int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600818 );
Victor Stinner75e46992018-11-26 17:29:38 +0100819
820PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF32(
821 PyObject *object, /* Unicode object */
822 const char *errors, /* error handling */
823 int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
824 );
825
826/* --- UTF-16 Codecs ------------------------------------------------------ */
827
828/* Returns a Python string object holding the UTF-16 encoded value of
829 the Unicode data.
830
831 If byteorder is not 0, output is written according to the following
832 byte order:
833
834 byteorder == -1: little endian
835 byteorder == 0: native byte order (writes a BOM mark)
836 byteorder == 1: big endian
837
838 If byteorder is 0, the output string will always start with the
839 Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
840 prepended.
841
842 Note that Py_UNICODE data is being interpreted as UTF-16 reduced to
843 UCS-2. This trick makes it possible to add full UTF-16 capabilities
844 at a later point without compromising the APIs.
845
846*/
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600847Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF16(
Victor Stinner75e46992018-11-26 17:29:38 +0100848 const Py_UNICODE *data, /* Unicode char buffer */
849 Py_ssize_t length, /* number of Py_UNICODE chars to encode */
850 const char *errors, /* error handling */
851 int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600852 );
Victor Stinner75e46992018-11-26 17:29:38 +0100853
854PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF16(
855 PyObject* unicode, /* Unicode object */
856 const char *errors, /* error handling */
857 int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
858 );
859
860/* --- Unicode-Escape Codecs ---------------------------------------------- */
861
862/* Helper for PyUnicode_DecodeUnicodeEscape that detects invalid escape
863 chars. */
864PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscape(
865 const char *string, /* Unicode-Escape encoded string */
866 Py_ssize_t length, /* size of string */
867 const char *errors, /* error handling */
868 const char **first_invalid_escape /* on return, points to first
869 invalid escaped char in
870 string. */
871);
872
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600873Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeUnicodeEscape(
Victor Stinner75e46992018-11-26 17:29:38 +0100874 const Py_UNICODE *data, /* Unicode char buffer */
875 Py_ssize_t length /* Number of Py_UNICODE chars to encode */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600876 );
Victor Stinner75e46992018-11-26 17:29:38 +0100877
878/* --- Raw-Unicode-Escape Codecs ------------------------------------------ */
879
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600880Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeRawUnicodeEscape(
Victor Stinner75e46992018-11-26 17:29:38 +0100881 const Py_UNICODE *data, /* Unicode char buffer */
882 Py_ssize_t length /* Number of Py_UNICODE chars to encode */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600883 );
Victor Stinner75e46992018-11-26 17:29:38 +0100884
Victor Stinner75e46992018-11-26 17:29:38 +0100885/* --- Latin-1 Codecs ----------------------------------------------------- */
886
887PyAPI_FUNC(PyObject*) _PyUnicode_AsLatin1String(
888 PyObject* unicode,
889 const char* errors);
890
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600891Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeLatin1(
Victor Stinner75e46992018-11-26 17:29:38 +0100892 const Py_UNICODE *data, /* Unicode char buffer */
893 Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
894 const char *errors /* error handling */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600895 );
Victor Stinner75e46992018-11-26 17:29:38 +0100896
897/* --- ASCII Codecs ------------------------------------------------------- */
898
899PyAPI_FUNC(PyObject*) _PyUnicode_AsASCIIString(
900 PyObject* unicode,
901 const char* errors);
902
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600903Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeASCII(
Victor Stinner75e46992018-11-26 17:29:38 +0100904 const Py_UNICODE *data, /* Unicode char buffer */
905 Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
906 const char *errors /* error handling */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600907 );
Victor Stinner75e46992018-11-26 17:29:38 +0100908
909/* --- Character Map Codecs ----------------------------------------------- */
910
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600911Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeCharmap(
Victor Stinner75e46992018-11-26 17:29:38 +0100912 const Py_UNICODE *data, /* Unicode char buffer */
913 Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
914 PyObject *mapping, /* encoding mapping */
915 const char *errors /* error handling */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600916 );
Victor Stinner75e46992018-11-26 17:29:38 +0100917
918PyAPI_FUNC(PyObject*) _PyUnicode_EncodeCharmap(
919 PyObject *unicode, /* Unicode object */
920 PyObject *mapping, /* encoding mapping */
921 const char *errors /* error handling */
922 );
923
924/* Translate a Py_UNICODE buffer of the given length by applying a
925 character mapping table to it and return the resulting Unicode
926 object.
927
928 The mapping table must map Unicode ordinal integers to Unicode strings,
929 Unicode ordinal integers or None (causing deletion of the character).
930
931 Mapping tables may be dictionaries or sequences. Unmapped character
932 ordinals (ones which cause a LookupError) are left untouched and
933 are copied as-is.
934
935*/
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600936Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject *) PyUnicode_TranslateCharmap(
Victor Stinner75e46992018-11-26 17:29:38 +0100937 const Py_UNICODE *data, /* Unicode char buffer */
938 Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
939 PyObject *table, /* Translate table */
940 const char *errors /* error handling */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600941 );
Victor Stinner75e46992018-11-26 17:29:38 +0100942
943/* --- MBCS codecs for Windows -------------------------------------------- */
944
945#ifdef MS_WINDOWS
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600946Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeMBCS(
Victor Stinner75e46992018-11-26 17:29:38 +0100947 const Py_UNICODE *data, /* Unicode char buffer */
948 Py_ssize_t length, /* number of Py_UNICODE chars to encode */
949 const char *errors /* error handling */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600950 );
Victor Stinner75e46992018-11-26 17:29:38 +0100951#endif
952
953/* --- Decimal Encoder ---------------------------------------------------- */
954
955/* Takes a Unicode string holding a decimal value and writes it into
956 an output buffer using standard ASCII digit codes.
957
958 The output buffer has to provide at least length+1 bytes of storage
959 area. The output string is 0-terminated.
960
961 The encoder converts whitespace to ' ', decimal characters to their
962 corresponding ASCII digit and all other Latin-1 characters except
963 \0 as-is. Characters outside this range (Unicode ordinals 1-256)
964 are treated as errors. This includes embedded NULL bytes.
965
966 Error handling is defined by the errors argument:
967
968 NULL or "strict": raise a ValueError
969 "ignore": ignore the wrong characters (these are not copied to the
970 output buffer)
971 "replace": replaces illegal characters with '?'
972
973 Returns 0 on success, -1 on failure.
974
975*/
976
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600977/* Py_DEPRECATED(3.3) */ PyAPI_FUNC(int) PyUnicode_EncodeDecimal(
Victor Stinner75e46992018-11-26 17:29:38 +0100978 Py_UNICODE *s, /* Unicode buffer */
979 Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
980 char *output, /* Output buffer; must have size >= length */
981 const char *errors /* error handling */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600982 );
Victor Stinner75e46992018-11-26 17:29:38 +0100983
984/* Transforms code points that have decimal digit property to the
985 corresponding ASCII digit code points.
986
987 Returns a new Unicode string on success, NULL on failure.
988*/
989
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600990/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100991PyAPI_FUNC(PyObject*) PyUnicode_TransformDecimalToASCII(
992 Py_UNICODE *s, /* Unicode buffer */
993 Py_ssize_t length /* Number of Py_UNICODE chars to transform */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600994 );
Victor Stinner75e46992018-11-26 17:29:38 +0100995
996/* Coverts a Unicode object holding a decimal value to an ASCII string
997 for using in int, float and complex parsers.
998 Transforms code points that have decimal digit property to the
999 corresponding ASCII digit code points. Transforms spaces to ASCII.
1000 Transforms code points starting from the first non-ASCII code point that
1001 is neither a decimal digit nor a space to the end into '?'. */
1002
1003PyAPI_FUNC(PyObject*) _PyUnicode_TransformDecimalAndSpaceToASCII(
1004 PyObject *unicode /* Unicode object */
1005 );
1006
1007/* --- Methods & Slots ---------------------------------------------------- */
1008
1009PyAPI_FUNC(PyObject *) _PyUnicode_JoinArray(
1010 PyObject *separator,
1011 PyObject *const *items,
1012 Py_ssize_t seqlen
1013 );
1014
1015/* Test whether a unicode is equal to ASCII identifier. Return 1 if true,
1016 0 otherwise. The right argument must be ASCII identifier.
1017 Any error occurs inside will be cleared before return. */
1018PyAPI_FUNC(int) _PyUnicode_EqualToASCIIId(
1019 PyObject *left, /* Left string */
1020 _Py_Identifier *right /* Right identifier */
1021 );
1022
1023/* Test whether a unicode is equal to ASCII string. Return 1 if true,
1024 0 otherwise. The right argument must be ASCII-encoded string.
1025 Any error occurs inside will be cleared before return. */
1026PyAPI_FUNC(int) _PyUnicode_EqualToASCIIString(
1027 PyObject *left,
1028 const char *right /* ASCII-encoded string */
1029 );
1030
1031/* Externally visible for str.strip(unicode) */
1032PyAPI_FUNC(PyObject *) _PyUnicode_XStrip(
1033 PyObject *self,
1034 int striptype,
1035 PyObject *sepobj
1036 );
1037
1038/* Using explicit passed-in values, insert the thousands grouping
1039 into the string pointed to by buffer. For the argument descriptions,
1040 see Objects/stringlib/localeutil.h */
1041PyAPI_FUNC(Py_ssize_t) _PyUnicode_InsertThousandsGrouping(
1042 _PyUnicodeWriter *writer,
1043 Py_ssize_t n_buffer,
1044 PyObject *digits,
1045 Py_ssize_t d_pos,
1046 Py_ssize_t n_digits,
1047 Py_ssize_t min_width,
1048 const char *grouping,
1049 PyObject *thousands_sep,
1050 Py_UCS4 *maxchar);
1051
1052/* === Characters Type APIs =============================================== */
1053
1054/* Helper array used by Py_UNICODE_ISSPACE(). */
1055
1056PyAPI_DATA(const unsigned char) _Py_ascii_whitespace[];
1057
1058/* These should not be used directly. Use the Py_UNICODE_IS* and
1059 Py_UNICODE_TO* macros instead.
1060
1061 These APIs are implemented in Objects/unicodectype.c.
1062
1063*/
1064
1065PyAPI_FUNC(int) _PyUnicode_IsLowercase(
1066 Py_UCS4 ch /* Unicode character */
1067 );
1068
1069PyAPI_FUNC(int) _PyUnicode_IsUppercase(
1070 Py_UCS4 ch /* Unicode character */
1071 );
1072
1073PyAPI_FUNC(int) _PyUnicode_IsTitlecase(
1074 Py_UCS4 ch /* Unicode character */
1075 );
1076
1077PyAPI_FUNC(int) _PyUnicode_IsXidStart(
1078 Py_UCS4 ch /* Unicode character */
1079 );
1080
1081PyAPI_FUNC(int) _PyUnicode_IsXidContinue(
1082 Py_UCS4 ch /* Unicode character */
1083 );
1084
1085PyAPI_FUNC(int) _PyUnicode_IsWhitespace(
1086 const Py_UCS4 ch /* Unicode character */
1087 );
1088
1089PyAPI_FUNC(int) _PyUnicode_IsLinebreak(
1090 const Py_UCS4 ch /* Unicode character */
1091 );
1092
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001093/* Py_DEPRECATED(3.3) */ PyAPI_FUNC(Py_UCS4) _PyUnicode_ToLowercase(
Victor Stinner75e46992018-11-26 17:29:38 +01001094 Py_UCS4 ch /* Unicode character */
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001095 );
Victor Stinner75e46992018-11-26 17:29:38 +01001096
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001097/* Py_DEPRECATED(3.3) */ PyAPI_FUNC(Py_UCS4) _PyUnicode_ToUppercase(
Victor Stinner75e46992018-11-26 17:29:38 +01001098 Py_UCS4 ch /* Unicode character */
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001099 );
Victor Stinner75e46992018-11-26 17:29:38 +01001100
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001101Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UCS4) _PyUnicode_ToTitlecase(
Victor Stinner75e46992018-11-26 17:29:38 +01001102 Py_UCS4 ch /* Unicode character */
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001103 );
Victor Stinner75e46992018-11-26 17:29:38 +01001104
1105PyAPI_FUNC(int) _PyUnicode_ToLowerFull(
1106 Py_UCS4 ch, /* Unicode character */
1107 Py_UCS4 *res
1108 );
1109
1110PyAPI_FUNC(int) _PyUnicode_ToTitleFull(
1111 Py_UCS4 ch, /* Unicode character */
1112 Py_UCS4 *res
1113 );
1114
1115PyAPI_FUNC(int) _PyUnicode_ToUpperFull(
1116 Py_UCS4 ch, /* Unicode character */
1117 Py_UCS4 *res
1118 );
1119
1120PyAPI_FUNC(int) _PyUnicode_ToFoldedFull(
1121 Py_UCS4 ch, /* Unicode character */
1122 Py_UCS4 *res
1123 );
1124
1125PyAPI_FUNC(int) _PyUnicode_IsCaseIgnorable(
1126 Py_UCS4 ch /* Unicode character */
1127 );
1128
1129PyAPI_FUNC(int) _PyUnicode_IsCased(
1130 Py_UCS4 ch /* Unicode character */
1131 );
1132
1133PyAPI_FUNC(int) _PyUnicode_ToDecimalDigit(
1134 Py_UCS4 ch /* Unicode character */
1135 );
1136
1137PyAPI_FUNC(int) _PyUnicode_ToDigit(
1138 Py_UCS4 ch /* Unicode character */
1139 );
1140
1141PyAPI_FUNC(double) _PyUnicode_ToNumeric(
1142 Py_UCS4 ch /* Unicode character */
1143 );
1144
1145PyAPI_FUNC(int) _PyUnicode_IsDecimalDigit(
1146 Py_UCS4 ch /* Unicode character */
1147 );
1148
1149PyAPI_FUNC(int) _PyUnicode_IsDigit(
1150 Py_UCS4 ch /* Unicode character */
1151 );
1152
1153PyAPI_FUNC(int) _PyUnicode_IsNumeric(
1154 Py_UCS4 ch /* Unicode character */
1155 );
1156
1157PyAPI_FUNC(int) _PyUnicode_IsPrintable(
1158 Py_UCS4 ch /* Unicode character */
1159 );
1160
1161PyAPI_FUNC(int) _PyUnicode_IsAlpha(
1162 Py_UCS4 ch /* Unicode character */
1163 );
1164
Victor Stinner75e46992018-11-26 17:29:38 +01001165PyAPI_FUNC(PyObject*) _PyUnicode_FormatLong(PyObject *, int, int, int);
1166
Victor Stinner75e46992018-11-26 17:29:38 +01001167/* Return an interned Unicode object for an Identifier; may fail if there is no memory.*/
1168PyAPI_FUNC(PyObject*) _PyUnicode_FromId(_Py_Identifier*);
Victor Stinner75e46992018-11-26 17:29:38 +01001169
1170/* Fast equality check when the inputs are known to be exact unicode types
1171 and where the hash values are equal (i.e. a very probable match) */
1172PyAPI_FUNC(int) _PyUnicode_EQ(PyObject *, PyObject *);
1173
Serhiy Storchaka349f76c2020-06-30 09:03:15 +03001174PyAPI_FUNC(int) _PyUnicode_WideCharString_Converter(PyObject *, void *);
1175PyAPI_FUNC(int) _PyUnicode_WideCharString_Opt_Converter(PyObject *, void *);
1176
Serhiy Storchaka74ea6b52020-05-12 12:42:04 +03001177PyAPI_FUNC(Py_ssize_t) _PyUnicode_ScanIdentifier(PyObject *);