blob: 569bdb1e2a94badf9d3d112403daa165ad161a06 [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
Inada Naoki2c4928d2020-06-17 20:09:44 +090049Py_DEPRECATED(3.3) static inline void
50Py_UNICODE_COPY(Py_UNICODE *target, const Py_UNICODE *source, Py_ssize_t length) {
51 memcpy(target, source, length * sizeof(Py_UNICODE));
52}
Victor Stinner75e46992018-11-26 17:29:38 +010053
Inada Naoki2c4928d2020-06-17 20:09:44 +090054Py_DEPRECATED(3.3) static inline void
55Py_UNICODE_FILL(Py_UNICODE *target, Py_UNICODE value, Py_ssize_t length) {
56 for (Py_ssize_t i = 0; i < length; i++) {
57 target[i] = value;
58 }
59}
Victor Stinner75e46992018-11-26 17:29:38 +010060
61/* macros to work with surrogates */
62#define Py_UNICODE_IS_SURROGATE(ch) (0xD800 <= (ch) && (ch) <= 0xDFFF)
63#define Py_UNICODE_IS_HIGH_SURROGATE(ch) (0xD800 <= (ch) && (ch) <= 0xDBFF)
64#define Py_UNICODE_IS_LOW_SURROGATE(ch) (0xDC00 <= (ch) && (ch) <= 0xDFFF)
65/* Join two surrogate characters and return a single Py_UCS4 value. */
66#define Py_UNICODE_JOIN_SURROGATES(high, low) \
67 (((((Py_UCS4)(high) & 0x03FF) << 10) | \
68 ((Py_UCS4)(low) & 0x03FF)) + 0x10000)
69/* high surrogate = top 10 bits added to D800 */
70#define Py_UNICODE_HIGH_SURROGATE(ch) (0xD800 - (0x10000 >> 10) + ((ch) >> 10))
71/* low surrogate = bottom 10 bits added to DC00 */
72#define Py_UNICODE_LOW_SURROGATE(ch) (0xDC00 + ((ch) & 0x3FF))
73
Victor Stinner75e46992018-11-26 17:29:38 +010074/* --- Unicode Type ------------------------------------------------------- */
75
76/* ASCII-only strings created through PyUnicode_New use the PyASCIIObject
77 structure. state.ascii and state.compact are set, and the data
78 immediately follow the structure. utf8_length and wstr_length can be found
79 in the length field; the utf8 pointer is equal to the data pointer. */
80typedef struct {
81 /* There are 4 forms of Unicode strings:
82
83 - compact ascii:
84
85 * structure = PyASCIIObject
86 * test: PyUnicode_IS_COMPACT_ASCII(op)
87 * kind = PyUnicode_1BYTE_KIND
88 * compact = 1
89 * ascii = 1
90 * ready = 1
91 * (length is the length of the utf8 and wstr strings)
92 * (data starts just after the structure)
93 * (since ASCII is decoded from UTF-8, the utf8 string are the data)
94
95 - compact:
96
97 * structure = PyCompactUnicodeObject
98 * test: PyUnicode_IS_COMPACT(op) && !PyUnicode_IS_ASCII(op)
99 * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
100 PyUnicode_4BYTE_KIND
101 * compact = 1
102 * ready = 1
103 * ascii = 0
104 * utf8 is not shared with data
105 * utf8_length = 0 if utf8 is NULL
106 * wstr is shared with data and wstr_length=length
107 if kind=PyUnicode_2BYTE_KIND and sizeof(wchar_t)=2
108 or if kind=PyUnicode_4BYTE_KIND and sizeof(wchar_t)=4
109 * wstr_length = 0 if wstr is NULL
110 * (data starts just after the structure)
111
112 - legacy string, not ready:
113
114 * structure = PyUnicodeObject
115 * test: kind == PyUnicode_WCHAR_KIND
116 * length = 0 (use wstr_length)
117 * hash = -1
118 * kind = PyUnicode_WCHAR_KIND
119 * compact = 0
120 * ascii = 0
121 * ready = 0
122 * interned = SSTATE_NOT_INTERNED
123 * wstr is not NULL
124 * data.any is NULL
125 * utf8 is NULL
126 * utf8_length = 0
127
128 - legacy string, ready:
129
130 * structure = PyUnicodeObject structure
131 * test: !PyUnicode_IS_COMPACT(op) && kind != PyUnicode_WCHAR_KIND
132 * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
133 PyUnicode_4BYTE_KIND
134 * compact = 0
135 * ready = 1
136 * data.any is not NULL
137 * utf8 is shared and utf8_length = length with data.any if ascii = 1
138 * utf8_length = 0 if utf8 is NULL
139 * wstr is shared with data.any and wstr_length = length
140 if kind=PyUnicode_2BYTE_KIND and sizeof(wchar_t)=2
141 or if kind=PyUnicode_4BYTE_KIND and sizeof(wchar_4)=4
142 * wstr_length = 0 if wstr is NULL
143
144 Compact strings use only one memory block (structure + characters),
145 whereas legacy strings use one block for the structure and one block
146 for characters.
147
148 Legacy strings are created by PyUnicode_FromUnicode() and
149 PyUnicode_FromStringAndSize(NULL, size) functions. They become ready
150 when PyUnicode_READY() is called.
151
152 See also _PyUnicode_CheckConsistency().
153 */
154 PyObject_HEAD
155 Py_ssize_t length; /* Number of code points in the string */
156 Py_hash_t hash; /* Hash value; -1 if not set */
157 struct {
158 /*
159 SSTATE_NOT_INTERNED (0)
160 SSTATE_INTERNED_MORTAL (1)
161 SSTATE_INTERNED_IMMORTAL (2)
162
163 If interned != SSTATE_NOT_INTERNED, the two references from the
164 dictionary to this object are *not* counted in ob_refcnt.
165 */
166 unsigned int interned:2;
167 /* Character size:
168
169 - PyUnicode_WCHAR_KIND (0):
170
171 * character type = wchar_t (16 or 32 bits, depending on the
172 platform)
173
174 - PyUnicode_1BYTE_KIND (1):
175
176 * character type = Py_UCS1 (8 bits, unsigned)
177 * all characters are in the range U+0000-U+00FF (latin1)
178 * if ascii is set, all characters are in the range U+0000-U+007F
179 (ASCII), otherwise at least one character is in the range
180 U+0080-U+00FF
181
182 - PyUnicode_2BYTE_KIND (2):
183
184 * character type = Py_UCS2 (16 bits, unsigned)
185 * all characters are in the range U+0000-U+FFFF (BMP)
186 * at least one character is in the range U+0100-U+FFFF
187
188 - PyUnicode_4BYTE_KIND (4):
189
190 * character type = Py_UCS4 (32 bits, unsigned)
191 * all characters are in the range U+0000-U+10FFFF
192 * at least one character is in the range U+10000-U+10FFFF
193 */
194 unsigned int kind:3;
195 /* Compact is with respect to the allocation scheme. Compact unicode
196 objects only require one memory block while non-compact objects use
197 one block for the PyUnicodeObject struct and another for its data
198 buffer. */
199 unsigned int compact:1;
200 /* The string only contains characters in the range U+0000-U+007F (ASCII)
201 and the kind is PyUnicode_1BYTE_KIND. If ascii is set and compact is
202 set, use the PyASCIIObject structure. */
203 unsigned int ascii:1;
204 /* The ready flag indicates whether the object layout is initialized
205 completely. This means that this is either a compact object, or
206 the data pointer is filled out. The bit is redundant, and helps
207 to minimize the test in PyUnicode_IS_READY(). */
208 unsigned int ready:1;
209 /* Padding to ensure that PyUnicode_DATA() is always aligned to
210 4 bytes (see issue #19537 on m68k). */
211 unsigned int :24;
212 } state;
213 wchar_t *wstr; /* wchar_t representation (null-terminated) */
214} PyASCIIObject;
215
216/* Non-ASCII strings allocated through PyUnicode_New use the
217 PyCompactUnicodeObject structure. state.compact is set, and the data
218 immediately follow the structure. */
219typedef struct {
220 PyASCIIObject _base;
221 Py_ssize_t utf8_length; /* Number of bytes in utf8, excluding the
222 * terminating \0. */
223 char *utf8; /* UTF-8 representation (null-terminated) */
224 Py_ssize_t wstr_length; /* Number of code points in wstr, possible
225 * surrogates count as two code points. */
226} PyCompactUnicodeObject;
227
228/* Strings allocated through PyUnicode_FromUnicode(NULL, len) use the
229 PyUnicodeObject structure. The actual string data is initially in the wstr
230 block, and copied into the data block using _PyUnicode_Ready. */
231typedef struct {
232 PyCompactUnicodeObject _base;
233 union {
234 void *any;
235 Py_UCS1 *latin1;
236 Py_UCS2 *ucs2;
237 Py_UCS4 *ucs4;
238 } data; /* Canonical, smallest-form Unicode buffer */
239} PyUnicodeObject;
240
Victor Stinner68762572019-10-07 18:42:01 +0200241PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
242 PyObject *op,
243 int check_content);
244
Victor Stinner75e46992018-11-26 17:29:38 +0100245/* Fast access macros */
Victor Stinner75e46992018-11-26 17:29:38 +0100246
247/* Returns the deprecated Py_UNICODE representation's size in code units
248 (this includes surrogate pairs as 2 units).
249 If the Py_UNICODE representation is not available, it will be computed
250 on request. Use PyUnicode_GET_LENGTH() for the length in code points. */
251
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600252/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100253#define PyUnicode_GET_SIZE(op) \
254 (assert(PyUnicode_Check(op)), \
255 (((PyASCIIObject *)(op))->wstr) ? \
256 PyUnicode_WSTR_LENGTH(op) : \
257 ((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
258 assert(((PyASCIIObject *)(op))->wstr), \
259 PyUnicode_WSTR_LENGTH(op)))
Victor Stinner75e46992018-11-26 17:29:38 +0100260
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600261/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100262#define PyUnicode_GET_DATA_SIZE(op) \
263 (PyUnicode_GET_SIZE(op) * Py_UNICODE_SIZE)
Victor Stinner75e46992018-11-26 17:29:38 +0100264
265/* Alias for PyUnicode_AsUnicode(). This will create a wchar_t/Py_UNICODE
266 representation on demand. Using this macro is very inefficient now,
267 try to port your code to use the new PyUnicode_*BYTE_DATA() macros or
268 use PyUnicode_WRITE() and PyUnicode_READ(). */
269
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600270/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100271#define PyUnicode_AS_UNICODE(op) \
272 (assert(PyUnicode_Check(op)), \
273 (((PyASCIIObject *)(op))->wstr) ? (((PyASCIIObject *)(op))->wstr) : \
274 PyUnicode_AsUnicode(_PyObject_CAST(op)))
Victor Stinner75e46992018-11-26 17:29:38 +0100275
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600276/* Py_DEPRECATED(3.3) */
Victor Stinner75e46992018-11-26 17:29:38 +0100277#define PyUnicode_AS_DATA(op) \
278 ((const char *)(PyUnicode_AS_UNICODE(op)))
Victor Stinner75e46992018-11-26 17:29:38 +0100279
280
281/* --- Flexible String Representation Helper Macros (PEP 393) -------------- */
282
283/* Values for PyASCIIObject.state: */
284
285/* Interning state. */
286#define SSTATE_NOT_INTERNED 0
287#define SSTATE_INTERNED_MORTAL 1
288#define SSTATE_INTERNED_IMMORTAL 2
289
290/* Return true if the string contains only ASCII characters, or 0 if not. The
291 string may be compact (PyUnicode_IS_COMPACT_ASCII) or not, but must be
292 ready. */
293#define PyUnicode_IS_ASCII(op) \
294 (assert(PyUnicode_Check(op)), \
295 assert(PyUnicode_IS_READY(op)), \
296 ((PyASCIIObject*)op)->state.ascii)
297
298/* Return true if the string is compact or 0 if not.
299 No type checks or Ready calls are performed. */
300#define PyUnicode_IS_COMPACT(op) \
301 (((PyASCIIObject*)(op))->state.compact)
302
303/* Return true if the string is a compact ASCII string (use PyASCIIObject
304 structure), or 0 if not. No type checks or Ready calls are performed. */
305#define PyUnicode_IS_COMPACT_ASCII(op) \
306 (((PyASCIIObject*)op)->state.ascii && PyUnicode_IS_COMPACT(op))
307
308enum PyUnicode_Kind {
309/* String contains only wstr byte characters. This is only possible
310 when the string was created with a legacy API and _PyUnicode_Ready()
311 has not been called yet. */
312 PyUnicode_WCHAR_KIND = 0,
313/* Return values of the PyUnicode_KIND() macro: */
314 PyUnicode_1BYTE_KIND = 1,
315 PyUnicode_2BYTE_KIND = 2,
316 PyUnicode_4BYTE_KIND = 4
317};
318
319/* Return pointers to the canonical representation cast to unsigned char,
320 Py_UCS2, or Py_UCS4 for direct character access.
321 No checks are performed, use PyUnicode_KIND() before to ensure
322 these will work correctly. */
323
324#define PyUnicode_1BYTE_DATA(op) ((Py_UCS1*)PyUnicode_DATA(op))
325#define PyUnicode_2BYTE_DATA(op) ((Py_UCS2*)PyUnicode_DATA(op))
326#define PyUnicode_4BYTE_DATA(op) ((Py_UCS4*)PyUnicode_DATA(op))
327
328/* Return one of the PyUnicode_*_KIND values defined above. */
329#define PyUnicode_KIND(op) \
330 (assert(PyUnicode_Check(op)), \
331 assert(PyUnicode_IS_READY(op)), \
332 ((PyASCIIObject *)(op))->state.kind)
333
334/* Return a void pointer to the raw unicode buffer. */
335#define _PyUnicode_COMPACT_DATA(op) \
336 (PyUnicode_IS_ASCII(op) ? \
337 ((void*)((PyASCIIObject*)(op) + 1)) : \
338 ((void*)((PyCompactUnicodeObject*)(op) + 1)))
339
340#define _PyUnicode_NONCOMPACT_DATA(op) \
341 (assert(((PyUnicodeObject*)(op))->data.any), \
342 ((((PyUnicodeObject *)(op))->data.any)))
343
344#define PyUnicode_DATA(op) \
345 (assert(PyUnicode_Check(op)), \
346 PyUnicode_IS_COMPACT(op) ? _PyUnicode_COMPACT_DATA(op) : \
347 _PyUnicode_NONCOMPACT_DATA(op))
348
349/* In the access macros below, "kind" may be evaluated more than once.
350 All other macro parameters are evaluated exactly once, so it is safe
351 to put side effects into them (such as increasing the index). */
352
353/* Write into the canonical representation, this macro does not do any sanity
354 checks and is intended for usage in loops. The caller should cache the
355 kind and data pointers obtained from other macro calls.
356 index is the index in the string (starts at 0) and value is the new
357 code point value which should be written to that location. */
358#define PyUnicode_WRITE(kind, data, index, value) \
359 do { \
360 switch ((kind)) { \
361 case PyUnicode_1BYTE_KIND: { \
362 ((Py_UCS1 *)(data))[(index)] = (Py_UCS1)(value); \
363 break; \
364 } \
365 case PyUnicode_2BYTE_KIND: { \
366 ((Py_UCS2 *)(data))[(index)] = (Py_UCS2)(value); \
367 break; \
368 } \
369 default: { \
370 assert((kind) == PyUnicode_4BYTE_KIND); \
371 ((Py_UCS4 *)(data))[(index)] = (Py_UCS4)(value); \
372 } \
373 } \
374 } while (0)
375
376/* Read a code point from the string's canonical representation. No checks
377 or ready calls are performed. */
378#define PyUnicode_READ(kind, data, index) \
379 ((Py_UCS4) \
380 ((kind) == PyUnicode_1BYTE_KIND ? \
381 ((const Py_UCS1 *)(data))[(index)] : \
382 ((kind) == PyUnicode_2BYTE_KIND ? \
383 ((const Py_UCS2 *)(data))[(index)] : \
384 ((const Py_UCS4 *)(data))[(index)] \
385 ) \
386 ))
387
388/* PyUnicode_READ_CHAR() is less efficient than PyUnicode_READ() because it
389 calls PyUnicode_KIND() and might call it twice. For single reads, use
390 PyUnicode_READ_CHAR, for multiple consecutive reads callers should
391 cache kind and use PyUnicode_READ instead. */
392#define PyUnicode_READ_CHAR(unicode, index) \
393 (assert(PyUnicode_Check(unicode)), \
394 assert(PyUnicode_IS_READY(unicode)), \
395 (Py_UCS4) \
396 (PyUnicode_KIND((unicode)) == PyUnicode_1BYTE_KIND ? \
397 ((const Py_UCS1 *)(PyUnicode_DATA((unicode))))[(index)] : \
398 (PyUnicode_KIND((unicode)) == PyUnicode_2BYTE_KIND ? \
399 ((const Py_UCS2 *)(PyUnicode_DATA((unicode))))[(index)] : \
400 ((const Py_UCS4 *)(PyUnicode_DATA((unicode))))[(index)] \
401 ) \
402 ))
403
404/* Returns the length of the unicode string. The caller has to make sure that
405 the string has it's canonical representation set before calling
406 this macro. Call PyUnicode_(FAST_)Ready to ensure that. */
407#define PyUnicode_GET_LENGTH(op) \
408 (assert(PyUnicode_Check(op)), \
409 assert(PyUnicode_IS_READY(op)), \
410 ((PyASCIIObject *)(op))->length)
411
412
413/* Fast check to determine whether an object is ready. Equivalent to
414 PyUnicode_IS_COMPACT(op) || ((PyUnicodeObject*)(op))->data.any) */
415
416#define PyUnicode_IS_READY(op) (((PyASCIIObject*)op)->state.ready)
417
418/* PyUnicode_READY() does less work than _PyUnicode_Ready() in the best
419 case. If the canonical representation is not yet set, it will still call
420 _PyUnicode_Ready().
421 Returns 0 on success and -1 on errors. */
422#define PyUnicode_READY(op) \
423 (assert(PyUnicode_Check(op)), \
424 (PyUnicode_IS_READY(op) ? \
425 0 : _PyUnicode_Ready(_PyObject_CAST(op))))
426
427/* Return a maximum character value which is suitable for creating another
428 string based on op. This is always an approximation but more efficient
429 than iterating over the string. */
430#define PyUnicode_MAX_CHAR_VALUE(op) \
431 (assert(PyUnicode_IS_READY(op)), \
432 (PyUnicode_IS_ASCII(op) ? \
433 (0x7f) : \
434 (PyUnicode_KIND(op) == PyUnicode_1BYTE_KIND ? \
435 (0xffU) : \
436 (PyUnicode_KIND(op) == PyUnicode_2BYTE_KIND ? \
437 (0xffffU) : \
438 (0x10ffffU)))))
439
Inada Naoki2c4928d2020-06-17 20:09:44 +0900440Py_DEPRECATED(3.3)
441static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
442 return PyUnicode_IS_COMPACT_ASCII(op) ?
443 ((PyASCIIObject*)op)->length :
444 ((PyCompactUnicodeObject*)op)->wstr_length;
445}
446#define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
447
Victor Stinner75e46992018-11-26 17:29:38 +0100448/* === 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. */
Inada Naoki2c4928d2020-06-17 20:09:44 +0900546Py_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. */
Inada Naoki2c4928d2020-06-17 20:09:44 +0900575Py_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. */
Inada Naoki2c4928d2020-06-17 20:09:44 +0900581Py_DEPRECATED(3.3) PyAPI_FUNC(const Py_UNICODE *) _PyUnicode_AsUnicode(
Victor Stinner75e46992018-11-26 17:29:38 +0100582 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
Inada Naoki2c4928d2020-06-17 20:09:44 +0900590Py_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 *);