blob: 3b49ce7759037e18daf077bf9c6c741ad0461673 [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
14/* Since splitting on whitespace is an important use case, and
15 whitespace in most situations is solely ASCII whitespace, we
16 optimize for the common case by using a quick look-up table
17 _Py_ascii_whitespace (see below) with an inlined check.
18
19 */
20#define Py_UNICODE_ISSPACE(ch) \
21 ((ch) < 128U ? _Py_ascii_whitespace[(ch)] : _PyUnicode_IsWhitespace(ch))
22
23#define Py_UNICODE_ISLOWER(ch) _PyUnicode_IsLowercase(ch)
24#define Py_UNICODE_ISUPPER(ch) _PyUnicode_IsUppercase(ch)
25#define Py_UNICODE_ISTITLE(ch) _PyUnicode_IsTitlecase(ch)
26#define Py_UNICODE_ISLINEBREAK(ch) _PyUnicode_IsLinebreak(ch)
27
28#define Py_UNICODE_TOLOWER(ch) _PyUnicode_ToLowercase(ch)
29#define Py_UNICODE_TOUPPER(ch) _PyUnicode_ToUppercase(ch)
30#define Py_UNICODE_TOTITLE(ch) _PyUnicode_ToTitlecase(ch)
31
32#define Py_UNICODE_ISDECIMAL(ch) _PyUnicode_IsDecimalDigit(ch)
33#define Py_UNICODE_ISDIGIT(ch) _PyUnicode_IsDigit(ch)
34#define Py_UNICODE_ISNUMERIC(ch) _PyUnicode_IsNumeric(ch)
35#define Py_UNICODE_ISPRINTABLE(ch) _PyUnicode_IsPrintable(ch)
36
37#define Py_UNICODE_TODECIMAL(ch) _PyUnicode_ToDecimalDigit(ch)
38#define Py_UNICODE_TODIGIT(ch) _PyUnicode_ToDigit(ch)
39#define Py_UNICODE_TONUMERIC(ch) _PyUnicode_ToNumeric(ch)
40
41#define Py_UNICODE_ISALPHA(ch) _PyUnicode_IsAlpha(ch)
42
43#define Py_UNICODE_ISALNUM(ch) \
44 (Py_UNICODE_ISALPHA(ch) || \
45 Py_UNICODE_ISDECIMAL(ch) || \
46 Py_UNICODE_ISDIGIT(ch) || \
47 Py_UNICODE_ISNUMERIC(ch))
48
49#define Py_UNICODE_COPY(target, source, length) \
50 memcpy((target), (source), (length)*sizeof(Py_UNICODE))
51
52#define Py_UNICODE_FILL(target, value, length) \
53 do {Py_ssize_t i_; Py_UNICODE *t_ = (target); Py_UNICODE v_ = (value);\
54 for (i_ = 0; i_ < (length); i_++) t_[i_] = v_;\
55 } while (0)
56
57/* macros to work with surrogates */
58#define Py_UNICODE_IS_SURROGATE(ch) (0xD800 <= (ch) && (ch) <= 0xDFFF)
59#define Py_UNICODE_IS_HIGH_SURROGATE(ch) (0xD800 <= (ch) && (ch) <= 0xDBFF)
60#define Py_UNICODE_IS_LOW_SURROGATE(ch) (0xDC00 <= (ch) && (ch) <= 0xDFFF)
61/* Join two surrogate characters and return a single Py_UCS4 value. */
62#define Py_UNICODE_JOIN_SURROGATES(high, low) \
63 (((((Py_UCS4)(high) & 0x03FF) << 10) | \
64 ((Py_UCS4)(low) & 0x03FF)) + 0x10000)
65/* high surrogate = top 10 bits added to D800 */
66#define Py_UNICODE_HIGH_SURROGATE(ch) (0xD800 - (0x10000 >> 10) + ((ch) >> 10))
67/* low surrogate = bottom 10 bits added to DC00 */
68#define Py_UNICODE_LOW_SURROGATE(ch) (0xDC00 + ((ch) & 0x3FF))
69
70/* Check if substring matches at given offset. The offset must be
71 valid, and the substring must not be empty. */
72
73#define Py_UNICODE_MATCH(string, offset, substring) \
74 ((*((string)->wstr + (offset)) == *((substring)->wstr)) && \
75 ((*((string)->wstr + (offset) + (substring)->wstr_length-1) == *((substring)->wstr + (substring)->wstr_length-1))) && \
76 !memcmp((string)->wstr + (offset), (substring)->wstr, (substring)->wstr_length*sizeof(Py_UNICODE)))
77
78/* --- Unicode Type ------------------------------------------------------- */
79
80/* ASCII-only strings created through PyUnicode_New use the PyASCIIObject
81 structure. state.ascii and state.compact are set, and the data
82 immediately follow the structure. utf8_length and wstr_length can be found
83 in the length field; the utf8 pointer is equal to the data pointer. */
84typedef struct {
85 /* There are 4 forms of Unicode strings:
86
87 - compact ascii:
88
89 * structure = PyASCIIObject
90 * test: PyUnicode_IS_COMPACT_ASCII(op)
91 * kind = PyUnicode_1BYTE_KIND
92 * compact = 1
93 * ascii = 1
94 * ready = 1
95 * (length is the length of the utf8 and wstr strings)
96 * (data starts just after the structure)
97 * (since ASCII is decoded from UTF-8, the utf8 string are the data)
98
99 - compact:
100
101 * structure = PyCompactUnicodeObject
102 * test: PyUnicode_IS_COMPACT(op) && !PyUnicode_IS_ASCII(op)
103 * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
104 PyUnicode_4BYTE_KIND
105 * compact = 1
106 * ready = 1
107 * ascii = 0
108 * utf8 is not shared with data
109 * utf8_length = 0 if utf8 is NULL
110 * wstr is shared with data and wstr_length=length
111 if kind=PyUnicode_2BYTE_KIND and sizeof(wchar_t)=2
112 or if kind=PyUnicode_4BYTE_KIND and sizeof(wchar_t)=4
113 * wstr_length = 0 if wstr is NULL
114 * (data starts just after the structure)
115
116 - legacy string, not ready:
117
118 * structure = PyUnicodeObject
119 * test: kind == PyUnicode_WCHAR_KIND
120 * length = 0 (use wstr_length)
121 * hash = -1
122 * kind = PyUnicode_WCHAR_KIND
123 * compact = 0
124 * ascii = 0
125 * ready = 0
126 * interned = SSTATE_NOT_INTERNED
127 * wstr is not NULL
128 * data.any is NULL
129 * utf8 is NULL
130 * utf8_length = 0
131
132 - legacy string, ready:
133
134 * structure = PyUnicodeObject structure
135 * test: !PyUnicode_IS_COMPACT(op) && kind != PyUnicode_WCHAR_KIND
136 * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
137 PyUnicode_4BYTE_KIND
138 * compact = 0
139 * ready = 1
140 * data.any is not NULL
141 * utf8 is shared and utf8_length = length with data.any if ascii = 1
142 * utf8_length = 0 if utf8 is NULL
143 * wstr is shared with data.any and wstr_length = length
144 if kind=PyUnicode_2BYTE_KIND and sizeof(wchar_t)=2
145 or if kind=PyUnicode_4BYTE_KIND and sizeof(wchar_4)=4
146 * wstr_length = 0 if wstr is NULL
147
148 Compact strings use only one memory block (structure + characters),
149 whereas legacy strings use one block for the structure and one block
150 for characters.
151
152 Legacy strings are created by PyUnicode_FromUnicode() and
153 PyUnicode_FromStringAndSize(NULL, size) functions. They become ready
154 when PyUnicode_READY() is called.
155
156 See also _PyUnicode_CheckConsistency().
157 */
158 PyObject_HEAD
159 Py_ssize_t length; /* Number of code points in the string */
160 Py_hash_t hash; /* Hash value; -1 if not set */
161 struct {
162 /*
163 SSTATE_NOT_INTERNED (0)
164 SSTATE_INTERNED_MORTAL (1)
165 SSTATE_INTERNED_IMMORTAL (2)
166
167 If interned != SSTATE_NOT_INTERNED, the two references from the
168 dictionary to this object are *not* counted in ob_refcnt.
169 */
170 unsigned int interned:2;
171 /* Character size:
172
173 - PyUnicode_WCHAR_KIND (0):
174
175 * character type = wchar_t (16 or 32 bits, depending on the
176 platform)
177
178 - PyUnicode_1BYTE_KIND (1):
179
180 * character type = Py_UCS1 (8 bits, unsigned)
181 * all characters are in the range U+0000-U+00FF (latin1)
182 * if ascii is set, all characters are in the range U+0000-U+007F
183 (ASCII), otherwise at least one character is in the range
184 U+0080-U+00FF
185
186 - PyUnicode_2BYTE_KIND (2):
187
188 * character type = Py_UCS2 (16 bits, unsigned)
189 * all characters are in the range U+0000-U+FFFF (BMP)
190 * at least one character is in the range U+0100-U+FFFF
191
192 - PyUnicode_4BYTE_KIND (4):
193
194 * character type = Py_UCS4 (32 bits, unsigned)
195 * all characters are in the range U+0000-U+10FFFF
196 * at least one character is in the range U+10000-U+10FFFF
197 */
198 unsigned int kind:3;
199 /* Compact is with respect to the allocation scheme. Compact unicode
200 objects only require one memory block while non-compact objects use
201 one block for the PyUnicodeObject struct and another for its data
202 buffer. */
203 unsigned int compact:1;
204 /* The string only contains characters in the range U+0000-U+007F (ASCII)
205 and the kind is PyUnicode_1BYTE_KIND. If ascii is set and compact is
206 set, use the PyASCIIObject structure. */
207 unsigned int ascii:1;
208 /* The ready flag indicates whether the object layout is initialized
209 completely. This means that this is either a compact object, or
210 the data pointer is filled out. The bit is redundant, and helps
211 to minimize the test in PyUnicode_IS_READY(). */
212 unsigned int ready:1;
213 /* Padding to ensure that PyUnicode_DATA() is always aligned to
214 4 bytes (see issue #19537 on m68k). */
215 unsigned int :24;
216 } state;
217 wchar_t *wstr; /* wchar_t representation (null-terminated) */
218} PyASCIIObject;
219
220/* Non-ASCII strings allocated through PyUnicode_New use the
221 PyCompactUnicodeObject structure. state.compact is set, and the data
222 immediately follow the structure. */
223typedef struct {
224 PyASCIIObject _base;
225 Py_ssize_t utf8_length; /* Number of bytes in utf8, excluding the
226 * terminating \0. */
227 char *utf8; /* UTF-8 representation (null-terminated) */
228 Py_ssize_t wstr_length; /* Number of code points in wstr, possible
229 * surrogates count as two code points. */
230} PyCompactUnicodeObject;
231
232/* Strings allocated through PyUnicode_FromUnicode(NULL, len) use the
233 PyUnicodeObject structure. The actual string data is initially in the wstr
234 block, and copied into the data block using _PyUnicode_Ready. */
235typedef struct {
236 PyCompactUnicodeObject _base;
237 union {
238 void *any;
239 Py_UCS1 *latin1;
240 Py_UCS2 *ucs2;
241 Py_UCS4 *ucs4;
242 } data; /* Canonical, smallest-form Unicode buffer */
243} PyUnicodeObject;
244
Victor Stinner68762572019-10-07 18:42:01 +0200245PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
246 PyObject *op,
247 int check_content);
248
Victor Stinner75e46992018-11-26 17:29:38 +0100249/* Fast access macros */
250#define PyUnicode_WSTR_LENGTH(op) \
251 (PyUnicode_IS_COMPACT_ASCII(op) ? \
252 ((PyASCIIObject*)op)->length : \
253 ((PyCompactUnicodeObject*)op)->wstr_length)
254
255/* Returns the deprecated Py_UNICODE representation's size in code units
256 (this includes surrogate pairs as 2 units).
257 If the Py_UNICODE representation is not available, it will be computed
258 on request. Use PyUnicode_GET_LENGTH() for the length in code points. */
259
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600260/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100261#define PyUnicode_GET_SIZE(op) \
262 (assert(PyUnicode_Check(op)), \
263 (((PyASCIIObject *)(op))->wstr) ? \
264 PyUnicode_WSTR_LENGTH(op) : \
265 ((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
266 assert(((PyASCIIObject *)(op))->wstr), \
267 PyUnicode_WSTR_LENGTH(op)))
Victor Stinner75e46992018-11-26 17:29:38 +0100268
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600269/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100270#define PyUnicode_GET_DATA_SIZE(op) \
271 (PyUnicode_GET_SIZE(op) * Py_UNICODE_SIZE)
Victor Stinner75e46992018-11-26 17:29:38 +0100272
273/* Alias for PyUnicode_AsUnicode(). This will create a wchar_t/Py_UNICODE
274 representation on demand. Using this macro is very inefficient now,
275 try to port your code to use the new PyUnicode_*BYTE_DATA() macros or
276 use PyUnicode_WRITE() and PyUnicode_READ(). */
277
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600278/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100279#define PyUnicode_AS_UNICODE(op) \
280 (assert(PyUnicode_Check(op)), \
281 (((PyASCIIObject *)(op))->wstr) ? (((PyASCIIObject *)(op))->wstr) : \
282 PyUnicode_AsUnicode(_PyObject_CAST(op)))
Victor Stinner75e46992018-11-26 17:29:38 +0100283
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600284/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100285#define PyUnicode_AS_DATA(op) \
286 ((const char *)(PyUnicode_AS_UNICODE(op)))
Victor Stinner75e46992018-11-26 17:29:38 +0100287
288
289/* --- Flexible String Representation Helper Macros (PEP 393) -------------- */
290
291/* Values for PyASCIIObject.state: */
292
293/* Interning state. */
294#define SSTATE_NOT_INTERNED 0
295#define SSTATE_INTERNED_MORTAL 1
296#define SSTATE_INTERNED_IMMORTAL 2
297
298/* Return true if the string contains only ASCII characters, or 0 if not. The
299 string may be compact (PyUnicode_IS_COMPACT_ASCII) or not, but must be
300 ready. */
301#define PyUnicode_IS_ASCII(op) \
302 (assert(PyUnicode_Check(op)), \
303 assert(PyUnicode_IS_READY(op)), \
304 ((PyASCIIObject*)op)->state.ascii)
305
306/* Return true if the string is compact or 0 if not.
307 No type checks or Ready calls are performed. */
308#define PyUnicode_IS_COMPACT(op) \
309 (((PyASCIIObject*)(op))->state.compact)
310
311/* Return true if the string is a compact ASCII string (use PyASCIIObject
312 structure), or 0 if not. No type checks or Ready calls are performed. */
313#define PyUnicode_IS_COMPACT_ASCII(op) \
314 (((PyASCIIObject*)op)->state.ascii && PyUnicode_IS_COMPACT(op))
315
316enum PyUnicode_Kind {
317/* String contains only wstr byte characters. This is only possible
318 when the string was created with a legacy API and _PyUnicode_Ready()
319 has not been called yet. */
320 PyUnicode_WCHAR_KIND = 0,
321/* Return values of the PyUnicode_KIND() macro: */
322 PyUnicode_1BYTE_KIND = 1,
323 PyUnicode_2BYTE_KIND = 2,
324 PyUnicode_4BYTE_KIND = 4
325};
326
327/* Return pointers to the canonical representation cast to unsigned char,
328 Py_UCS2, or Py_UCS4 for direct character access.
329 No checks are performed, use PyUnicode_KIND() before to ensure
330 these will work correctly. */
331
332#define PyUnicode_1BYTE_DATA(op) ((Py_UCS1*)PyUnicode_DATA(op))
333#define PyUnicode_2BYTE_DATA(op) ((Py_UCS2*)PyUnicode_DATA(op))
334#define PyUnicode_4BYTE_DATA(op) ((Py_UCS4*)PyUnicode_DATA(op))
335
336/* Return one of the PyUnicode_*_KIND values defined above. */
337#define PyUnicode_KIND(op) \
338 (assert(PyUnicode_Check(op)), \
339 assert(PyUnicode_IS_READY(op)), \
340 ((PyASCIIObject *)(op))->state.kind)
341
342/* Return a void pointer to the raw unicode buffer. */
343#define _PyUnicode_COMPACT_DATA(op) \
344 (PyUnicode_IS_ASCII(op) ? \
345 ((void*)((PyASCIIObject*)(op) + 1)) : \
346 ((void*)((PyCompactUnicodeObject*)(op) + 1)))
347
348#define _PyUnicode_NONCOMPACT_DATA(op) \
349 (assert(((PyUnicodeObject*)(op))->data.any), \
350 ((((PyUnicodeObject *)(op))->data.any)))
351
352#define PyUnicode_DATA(op) \
353 (assert(PyUnicode_Check(op)), \
354 PyUnicode_IS_COMPACT(op) ? _PyUnicode_COMPACT_DATA(op) : \
355 _PyUnicode_NONCOMPACT_DATA(op))
356
357/* In the access macros below, "kind" may be evaluated more than once.
358 All other macro parameters are evaluated exactly once, so it is safe
359 to put side effects into them (such as increasing the index). */
360
361/* Write into the canonical representation, this macro does not do any sanity
362 checks and is intended for usage in loops. The caller should cache the
363 kind and data pointers obtained from other macro calls.
364 index is the index in the string (starts at 0) and value is the new
365 code point value which should be written to that location. */
366#define PyUnicode_WRITE(kind, data, index, value) \
367 do { \
368 switch ((kind)) { \
369 case PyUnicode_1BYTE_KIND: { \
370 ((Py_UCS1 *)(data))[(index)] = (Py_UCS1)(value); \
371 break; \
372 } \
373 case PyUnicode_2BYTE_KIND: { \
374 ((Py_UCS2 *)(data))[(index)] = (Py_UCS2)(value); \
375 break; \
376 } \
377 default: { \
378 assert((kind) == PyUnicode_4BYTE_KIND); \
379 ((Py_UCS4 *)(data))[(index)] = (Py_UCS4)(value); \
380 } \
381 } \
382 } while (0)
383
384/* Read a code point from the string's canonical representation. No checks
385 or ready calls are performed. */
386#define PyUnicode_READ(kind, data, index) \
387 ((Py_UCS4) \
388 ((kind) == PyUnicode_1BYTE_KIND ? \
389 ((const Py_UCS1 *)(data))[(index)] : \
390 ((kind) == PyUnicode_2BYTE_KIND ? \
391 ((const Py_UCS2 *)(data))[(index)] : \
392 ((const Py_UCS4 *)(data))[(index)] \
393 ) \
394 ))
395
396/* PyUnicode_READ_CHAR() is less efficient than PyUnicode_READ() because it
397 calls PyUnicode_KIND() and might call it twice. For single reads, use
398 PyUnicode_READ_CHAR, for multiple consecutive reads callers should
399 cache kind and use PyUnicode_READ instead. */
400#define PyUnicode_READ_CHAR(unicode, index) \
401 (assert(PyUnicode_Check(unicode)), \
402 assert(PyUnicode_IS_READY(unicode)), \
403 (Py_UCS4) \
404 (PyUnicode_KIND((unicode)) == PyUnicode_1BYTE_KIND ? \
405 ((const Py_UCS1 *)(PyUnicode_DATA((unicode))))[(index)] : \
406 (PyUnicode_KIND((unicode)) == PyUnicode_2BYTE_KIND ? \
407 ((const Py_UCS2 *)(PyUnicode_DATA((unicode))))[(index)] : \
408 ((const Py_UCS4 *)(PyUnicode_DATA((unicode))))[(index)] \
409 ) \
410 ))
411
412/* Returns the length of the unicode string. The caller has to make sure that
413 the string has it's canonical representation set before calling
414 this macro. Call PyUnicode_(FAST_)Ready to ensure that. */
415#define PyUnicode_GET_LENGTH(op) \
416 (assert(PyUnicode_Check(op)), \
417 assert(PyUnicode_IS_READY(op)), \
418 ((PyASCIIObject *)(op))->length)
419
420
421/* Fast check to determine whether an object is ready. Equivalent to
422 PyUnicode_IS_COMPACT(op) || ((PyUnicodeObject*)(op))->data.any) */
423
424#define PyUnicode_IS_READY(op) (((PyASCIIObject*)op)->state.ready)
425
426/* PyUnicode_READY() does less work than _PyUnicode_Ready() in the best
427 case. If the canonical representation is not yet set, it will still call
428 _PyUnicode_Ready().
429 Returns 0 on success and -1 on errors. */
430#define PyUnicode_READY(op) \
431 (assert(PyUnicode_Check(op)), \
432 (PyUnicode_IS_READY(op) ? \
433 0 : _PyUnicode_Ready(_PyObject_CAST(op))))
434
435/* Return a maximum character value which is suitable for creating another
436 string based on op. This is always an approximation but more efficient
437 than iterating over the string. */
438#define PyUnicode_MAX_CHAR_VALUE(op) \
439 (assert(PyUnicode_IS_READY(op)), \
440 (PyUnicode_IS_ASCII(op) ? \
441 (0x7f) : \
442 (PyUnicode_KIND(op) == PyUnicode_1BYTE_KIND ? \
443 (0xffU) : \
444 (PyUnicode_KIND(op) == PyUnicode_2BYTE_KIND ? \
445 (0xffffU) : \
446 (0x10ffffU)))))
447
448/* === Public API ========================================================= */
449
450/* --- Plain Py_UNICODE --------------------------------------------------- */
451
452/* With PEP 393, this is the recommended way to allocate a new unicode object.
453 This function will allocate the object and its buffer in a single memory
454 block. Objects created using this function are not resizable. */
455PyAPI_FUNC(PyObject*) PyUnicode_New(
456 Py_ssize_t size, /* Number of code points in the new string */
457 Py_UCS4 maxchar /* maximum code point value in the string */
458 );
459
460/* Initializes the canonical string representation from the deprecated
461 wstr/Py_UNICODE representation. This function is used to convert Unicode
462 objects which were created using the old API to the new flexible format
463 introduced with PEP 393.
464
465 Don't call this function directly, use the public PyUnicode_READY() macro
466 instead. */
467PyAPI_FUNC(int) _PyUnicode_Ready(
468 PyObject *unicode /* Unicode object */
469 );
470
471/* Get a copy of a Unicode string. */
472PyAPI_FUNC(PyObject*) _PyUnicode_Copy(
473 PyObject *unicode
474 );
475
476/* Copy character from one unicode object into another, this function performs
477 character conversion when necessary and falls back to memcpy() if possible.
478
479 Fail if to is too small (smaller than *how_many* or smaller than
480 len(from)-from_start), or if kind(from[from_start:from_start+how_many]) >
481 kind(to), or if *to* has more than 1 reference.
482
483 Return the number of written character, or return -1 and raise an exception
484 on error.
485
486 Pseudo-code:
487
488 how_many = min(how_many, len(from) - from_start)
489 to[to_start:to_start+how_many] = from[from_start:from_start+how_many]
490 return how_many
491
492 Note: The function doesn't write a terminating null character.
493 */
494PyAPI_FUNC(Py_ssize_t) PyUnicode_CopyCharacters(
495 PyObject *to,
496 Py_ssize_t to_start,
497 PyObject *from,
498 Py_ssize_t from_start,
499 Py_ssize_t how_many
500 );
501
502/* Unsafe version of PyUnicode_CopyCharacters(): don't check arguments and so
503 may crash if parameters are invalid (e.g. if the output string
504 is too short). */
505PyAPI_FUNC(void) _PyUnicode_FastCopyCharacters(
506 PyObject *to,
507 Py_ssize_t to_start,
508 PyObject *from,
509 Py_ssize_t from_start,
510 Py_ssize_t how_many
511 );
512
513/* Fill a string with a character: write fill_char into
514 unicode[start:start+length].
515
516 Fail if fill_char is bigger than the string maximum character, or if the
517 string has more than 1 reference.
518
519 Return the number of written character, or return -1 and raise an exception
520 on error. */
521PyAPI_FUNC(Py_ssize_t) PyUnicode_Fill(
522 PyObject *unicode,
523 Py_ssize_t start,
524 Py_ssize_t length,
525 Py_UCS4 fill_char
526 );
527
528/* Unsafe version of PyUnicode_Fill(): don't check arguments and so may crash
529 if parameters are invalid (e.g. if length is longer than the string). */
530PyAPI_FUNC(void) _PyUnicode_FastFill(
531 PyObject *unicode,
532 Py_ssize_t start,
533 Py_ssize_t length,
534 Py_UCS4 fill_char
535 );
536
537/* Create a Unicode Object from the Py_UNICODE buffer u of the given
538 size.
539
540 u may be NULL which causes the contents to be undefined. It is the
541 user's responsibility to fill in the needed data afterwards. Note
542 that modifying the Unicode object contents after construction is
543 only allowed if u was set to NULL.
544
545 The buffer is copied into the new object. */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600546/* Py_DEPRECATED(3.3) */ PyAPI_FUNC(PyObject*) PyUnicode_FromUnicode(
Victor Stinner75e46992018-11-26 17:29:38 +0100547 const Py_UNICODE *u, /* Unicode buffer */
548 Py_ssize_t size /* size of buffer */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600549 );
Victor Stinner75e46992018-11-26 17:29:38 +0100550
551/* Create a new string from a buffer of Py_UCS1, Py_UCS2 or Py_UCS4 characters.
552 Scan the string to find the maximum character. */
553PyAPI_FUNC(PyObject*) PyUnicode_FromKindAndData(
554 int kind,
555 const void *buffer,
556 Py_ssize_t size);
557
558/* Create a new string from a buffer of ASCII characters.
559 WARNING: Don't check if the string contains any non-ASCII character. */
560PyAPI_FUNC(PyObject*) _PyUnicode_FromASCII(
561 const char *buffer,
562 Py_ssize_t size);
563
564/* Compute the maximum character of the substring unicode[start:end].
565 Return 127 for an empty string. */
566PyAPI_FUNC(Py_UCS4) _PyUnicode_FindMaxChar (
567 PyObject *unicode,
568 Py_ssize_t start,
569 Py_ssize_t end);
570
571/* Return a read-only pointer to the Unicode object's internal
572 Py_UNICODE buffer.
573 If the wchar_t/Py_UNICODE representation is not yet available, this
574 function will calculate it. */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600575/* Py_DEPRECATED(3.3) */ PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
Victor Stinner75e46992018-11-26 17:29:38 +0100576 PyObject *unicode /* Unicode object */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600577 );
Victor Stinner75e46992018-11-26 17:29:38 +0100578
579/* Similar to PyUnicode_AsUnicode(), but raises a ValueError if the string
580 contains null characters. */
581PyAPI_FUNC(const Py_UNICODE *) _PyUnicode_AsUnicode(
582 PyObject *unicode /* Unicode object */
583 );
584
585/* Return a read-only pointer to the Unicode object's internal
586 Py_UNICODE buffer and save the length at size.
587 If the wchar_t/Py_UNICODE representation is not yet available, this
588 function will calculate it. */
589
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600590/* Py_DEPRECATED(3.3) */ PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicodeAndSize(
Victor Stinner75e46992018-11-26 17:29:38 +0100591 PyObject *unicode, /* Unicode object */
592 Py_ssize_t *size /* location where to save the length */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600593 );
Victor Stinner75e46992018-11-26 17:29:38 +0100594
595/* Get the maximum ordinal for a Unicode character. */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600596Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE) PyUnicode_GetMax(void);
Victor Stinner75e46992018-11-26 17:29:38 +0100597
598
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
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001165Py_DEPRECATED(3.3) PyAPI_FUNC(size_t) Py_UNICODE_strlen(
Victor Stinner75e46992018-11-26 17:29:38 +01001166 const Py_UNICODE *u
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001167 );
Victor Stinner75e46992018-11-26 17:29:38 +01001168
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001169Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strcpy(
Victor Stinner75e46992018-11-26 17:29:38 +01001170 Py_UNICODE *s1,
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001171 const Py_UNICODE *s2);
Victor Stinner75e46992018-11-26 17:29:38 +01001172
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001173Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strcat(
1174 Py_UNICODE *s1, const Py_UNICODE *s2);
Victor Stinner75e46992018-11-26 17:29:38 +01001175
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001176Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strncpy(
Victor Stinner75e46992018-11-26 17:29:38 +01001177 Py_UNICODE *s1,
1178 const Py_UNICODE *s2,
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001179 size_t n);
Victor Stinner75e46992018-11-26 17:29:38 +01001180
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001181Py_DEPRECATED(3.3) PyAPI_FUNC(int) Py_UNICODE_strcmp(
Victor Stinner75e46992018-11-26 17:29:38 +01001182 const Py_UNICODE *s1,
1183 const Py_UNICODE *s2
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001184 );
Victor Stinner75e46992018-11-26 17:29:38 +01001185
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001186Py_DEPRECATED(3.3) PyAPI_FUNC(int) Py_UNICODE_strncmp(
Victor Stinner75e46992018-11-26 17:29:38 +01001187 const Py_UNICODE *s1,
1188 const Py_UNICODE *s2,
1189 size_t n
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001190 );
Victor Stinner75e46992018-11-26 17:29:38 +01001191
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001192Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strchr(
Victor Stinner75e46992018-11-26 17:29:38 +01001193 const Py_UNICODE *s,
1194 Py_UNICODE c
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001195 );
Victor Stinner75e46992018-11-26 17:29:38 +01001196
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001197Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strrchr(
Victor Stinner75e46992018-11-26 17:29:38 +01001198 const Py_UNICODE *s,
1199 Py_UNICODE c
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001200 );
Victor Stinner75e46992018-11-26 17:29:38 +01001201
1202PyAPI_FUNC(PyObject*) _PyUnicode_FormatLong(PyObject *, int, int, int);
1203
1204/* Create a copy of a unicode string ending with a nul character. Return NULL
1205 and raise a MemoryError exception on memory allocation failure, otherwise
1206 return a new allocated buffer (use PyMem_Free() to free the buffer). */
1207
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001208Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE*) PyUnicode_AsUnicodeCopy(
Victor Stinner75e46992018-11-26 17:29:38 +01001209 PyObject *unicode
Zackery Spytz3c8724f2019-05-28 09:16:33 -06001210 );
Victor Stinner75e46992018-11-26 17:29:38 +01001211
1212/* Return an interned Unicode object for an Identifier; may fail if there is no memory.*/
1213PyAPI_FUNC(PyObject*) _PyUnicode_FromId(_Py_Identifier*);
Victor Stinner75e46992018-11-26 17:29:38 +01001214
1215/* Fast equality check when the inputs are known to be exact unicode types
1216 and where the hash values are equal (i.e. a very probable match) */
1217PyAPI_FUNC(int) _PyUnicode_EQ(PyObject *, PyObject *);
1218
Serhiy Storchaka74ea6b52020-05-12 12:42:04 +03001219PyAPI_FUNC(Py_ssize_t) _PyUnicode_ScanIdentifier(PyObject *);