2021-12-09 15:59:26 -04:00
|
|
|
#ifndef Py_INTERNAL_UNICODEOBJECT_H
|
|
|
|
#define Py_INTERNAL_UNICODEOBJECT_H
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef Py_BUILD_CORE
|
|
|
|
# error "this header requires Py_BUILD_CORE define"
|
|
|
|
#endif
|
|
|
|
|
2023-12-07 15:33:40 -04:00
|
|
|
#include "pycore_lock.h" // PyMutex
|
2022-01-11 06:56:16 -04:00
|
|
|
#include "pycore_fileutils.h" // _Py_error_handler
|
2023-08-28 21:29:46 -03:00
|
|
|
#include "pycore_identifier.h" // _Py_Identifier
|
2022-12-07 18:56:31 -04:00
|
|
|
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
|
2022-01-11 06:56:16 -04:00
|
|
|
|
2023-07-24 15:26:29 -03:00
|
|
|
/* --- Characters Type APIs ----------------------------------------------- */
|
|
|
|
|
|
|
|
extern int _PyUnicode_IsXidStart(Py_UCS4 ch);
|
|
|
|
extern int _PyUnicode_IsXidContinue(Py_UCS4 ch);
|
|
|
|
extern int _PyUnicode_ToLowerFull(Py_UCS4 ch, Py_UCS4 *res);
|
|
|
|
extern int _PyUnicode_ToTitleFull(Py_UCS4 ch, Py_UCS4 *res);
|
|
|
|
extern int _PyUnicode_ToUpperFull(Py_UCS4 ch, Py_UCS4 *res);
|
|
|
|
extern int _PyUnicode_ToFoldedFull(Py_UCS4 ch, Py_UCS4 *res);
|
|
|
|
extern int _PyUnicode_IsCaseIgnorable(Py_UCS4 ch);
|
|
|
|
extern int _PyUnicode_IsCased(Py_UCS4 ch);
|
|
|
|
|
|
|
|
/* --- Unicode API -------------------------------------------------------- */
|
|
|
|
|
2023-08-24 12:40:56 -03:00
|
|
|
// Export for '_json' shared extension
|
2023-07-04 05:59:09 -03:00
|
|
|
PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
|
|
|
|
PyObject *op,
|
|
|
|
int check_content);
|
|
|
|
|
2024-02-29 12:11:28 -04:00
|
|
|
PyAPI_FUNC(void) _PyUnicode_ExactDealloc(PyObject *op);
|
2023-07-04 05:59:09 -03:00
|
|
|
extern Py_ssize_t _PyUnicode_InternedSize(void);
|
2021-12-09 15:59:26 -04:00
|
|
|
|
2023-08-24 12:40:56 -03:00
|
|
|
// Get a copy of a Unicode string.
|
|
|
|
// Export for '_datetime' shared extension.
|
2023-07-03 19:35:46 -03:00
|
|
|
PyAPI_FUNC(PyObject*) _PyUnicode_Copy(
|
2023-08-24 12:40:56 -03:00
|
|
|
PyObject *unicode);
|
2023-07-03 19:35:46 -03:00
|
|
|
|
|
|
|
/* Unsafe version of PyUnicode_Fill(): don't check arguments and so may crash
|
|
|
|
if parameters are invalid (e.g. if length is longer than the string). */
|
|
|
|
extern void _PyUnicode_FastFill(
|
|
|
|
PyObject *unicode,
|
|
|
|
Py_ssize_t start,
|
|
|
|
Py_ssize_t length,
|
|
|
|
Py_UCS4 fill_char
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Unsafe version of PyUnicode_CopyCharacters(): don't check arguments and so
|
|
|
|
may crash if parameters are invalid (e.g. if the output string
|
|
|
|
is too short). */
|
|
|
|
extern void _PyUnicode_FastCopyCharacters(
|
|
|
|
PyObject *to,
|
|
|
|
Py_ssize_t to_start,
|
|
|
|
PyObject *from,
|
|
|
|
Py_ssize_t from_start,
|
|
|
|
Py_ssize_t how_many
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Create a new string from a buffer of ASCII characters.
|
|
|
|
WARNING: Don't check if the string contains any non-ASCII character. */
|
|
|
|
extern PyObject* _PyUnicode_FromASCII(
|
|
|
|
const char *buffer,
|
|
|
|
Py_ssize_t size);
|
|
|
|
|
|
|
|
/* Compute the maximum character of the substring unicode[start:end].
|
|
|
|
Return 127 for an empty string. */
|
|
|
|
extern Py_UCS4 _PyUnicode_FindMaxChar (
|
|
|
|
PyObject *unicode,
|
|
|
|
Py_ssize_t start,
|
|
|
|
Py_ssize_t end);
|
|
|
|
|
2023-07-03 05:23:43 -03:00
|
|
|
/* --- _PyUnicodeWriter API ----------------------------------------------- */
|
|
|
|
|
|
|
|
/* Format the object based on the format_spec, as defined in PEP 3101
|
|
|
|
(Advanced String Formatting). */
|
2023-07-24 23:25:45 -03:00
|
|
|
extern int _PyUnicode_FormatAdvancedWriter(
|
2023-07-03 05:23:43 -03:00
|
|
|
_PyUnicodeWriter *writer,
|
|
|
|
PyObject *obj,
|
|
|
|
PyObject *format_spec,
|
|
|
|
Py_ssize_t start,
|
|
|
|
Py_ssize_t end);
|
|
|
|
|
2023-07-04 04:29:52 -03:00
|
|
|
/* --- UTF-7 Codecs ------------------------------------------------------- */
|
|
|
|
|
|
|
|
extern PyObject* _PyUnicode_EncodeUTF7(
|
|
|
|
PyObject *unicode, /* Unicode object */
|
|
|
|
int base64SetO, /* Encode RFC2152 Set O characters in base64 */
|
|
|
|
int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
|
|
|
|
const char *errors); /* error handling */
|
|
|
|
|
|
|
|
/* --- UTF-8 Codecs ------------------------------------------------------- */
|
|
|
|
|
2023-08-24 12:40:56 -03:00
|
|
|
// Export for '_tkinter' shared extension.
|
2023-07-04 04:29:52 -03:00
|
|
|
PyAPI_FUNC(PyObject*) _PyUnicode_AsUTF8String(
|
|
|
|
PyObject *unicode,
|
|
|
|
const char *errors);
|
|
|
|
|
|
|
|
/* --- UTF-32 Codecs ------------------------------------------------------ */
|
|
|
|
|
2023-08-24 12:40:56 -03:00
|
|
|
// Export for '_tkinter' shared extension
|
2023-07-04 04:29:52 -03:00
|
|
|
PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF32(
|
|
|
|
PyObject *object, /* Unicode object */
|
|
|
|
const char *errors, /* error handling */
|
|
|
|
int byteorder); /* byteorder to use 0=BOM+native;-1=LE,1=BE */
|
|
|
|
|
|
|
|
/* --- UTF-16 Codecs ------------------------------------------------------ */
|
|
|
|
|
2023-08-24 12:40:56 -03:00
|
|
|
// Returns a Python string object holding the UTF-16 encoded value of
|
|
|
|
// the Unicode data.
|
|
|
|
//
|
|
|
|
// If byteorder is not 0, output is written according to the following
|
|
|
|
// byte order:
|
|
|
|
//
|
|
|
|
// byteorder == -1: little endian
|
|
|
|
// byteorder == 0: native byte order (writes a BOM mark)
|
|
|
|
// byteorder == 1: big endian
|
|
|
|
//
|
|
|
|
// If byteorder is 0, the output string will always start with the
|
|
|
|
// Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
|
|
|
|
// prepended.
|
|
|
|
//
|
|
|
|
// Export for '_tkinter' shared extension
|
2023-07-04 04:29:52 -03:00
|
|
|
PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF16(
|
|
|
|
PyObject* unicode, /* Unicode object */
|
|
|
|
const char *errors, /* error handling */
|
|
|
|
int byteorder); /* byteorder to use 0=BOM+native;-1=LE,1=BE */
|
|
|
|
|
|
|
|
/* --- Unicode-Escape Codecs ---------------------------------------------- */
|
|
|
|
|
|
|
|
/* Variant of PyUnicode_DecodeUnicodeEscape that supports partial decoding. */
|
|
|
|
extern PyObject* _PyUnicode_DecodeUnicodeEscapeStateful(
|
|
|
|
const char *string, /* Unicode-Escape encoded string */
|
|
|
|
Py_ssize_t length, /* size of string */
|
|
|
|
const char *errors, /* error handling */
|
|
|
|
Py_ssize_t *consumed); /* bytes consumed */
|
|
|
|
|
2023-07-24 23:25:45 -03:00
|
|
|
// Helper for PyUnicode_DecodeUnicodeEscape that detects invalid escape
|
|
|
|
// chars.
|
|
|
|
// Export for test_peg_generator.
|
2023-07-04 04:29:52 -03:00
|
|
|
PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscapeInternal(
|
|
|
|
const char *string, /* Unicode-Escape encoded string */
|
|
|
|
Py_ssize_t length, /* size of string */
|
|
|
|
const char *errors, /* error handling */
|
|
|
|
Py_ssize_t *consumed, /* bytes consumed */
|
|
|
|
const char **first_invalid_escape); /* on return, points to first
|
|
|
|
invalid escaped char in
|
|
|
|
string. */
|
|
|
|
|
|
|
|
/* --- Raw-Unicode-Escape Codecs ---------------------------------------------- */
|
|
|
|
|
|
|
|
/* Variant of PyUnicode_DecodeRawUnicodeEscape that supports partial decoding. */
|
|
|
|
extern PyObject* _PyUnicode_DecodeRawUnicodeEscapeStateful(
|
|
|
|
const char *string, /* Unicode-Escape encoded string */
|
|
|
|
Py_ssize_t length, /* size of string */
|
|
|
|
const char *errors, /* error handling */
|
|
|
|
Py_ssize_t *consumed); /* bytes consumed */
|
|
|
|
|
|
|
|
/* --- Latin-1 Codecs ----------------------------------------------------- */
|
|
|
|
|
|
|
|
extern PyObject* _PyUnicode_AsLatin1String(
|
|
|
|
PyObject* unicode,
|
|
|
|
const char* errors);
|
|
|
|
|
|
|
|
/* --- ASCII Codecs ------------------------------------------------------- */
|
|
|
|
|
|
|
|
extern PyObject* _PyUnicode_AsASCIIString(
|
|
|
|
PyObject* unicode,
|
|
|
|
const char* errors);
|
|
|
|
|
|
|
|
/* --- Character Map Codecs ----------------------------------------------- */
|
|
|
|
|
|
|
|
/* Translate an Unicode object by applying a character mapping table to
|
|
|
|
it and return the resulting Unicode object.
|
|
|
|
|
|
|
|
The mapping table must map Unicode ordinal integers to Unicode strings,
|
|
|
|
Unicode ordinal integers or None (causing deletion of the character).
|
|
|
|
|
|
|
|
Mapping tables may be dictionaries or sequences. Unmapped character
|
|
|
|
ordinals (ones which cause a LookupError) are left untouched and
|
|
|
|
are copied as-is.
|
|
|
|
*/
|
|
|
|
extern PyObject* _PyUnicode_EncodeCharmap(
|
|
|
|
PyObject *unicode, /* Unicode object */
|
|
|
|
PyObject *mapping, /* encoding mapping */
|
|
|
|
const char *errors); /* error handling */
|
|
|
|
|
2023-07-04 05:59:09 -03:00
|
|
|
/* --- Decimal Encoder ---------------------------------------------------- */
|
|
|
|
|
2023-08-24 12:40:56 -03:00
|
|
|
// Coverts a Unicode object holding a decimal value to an ASCII string
|
|
|
|
// for using in int, float and complex parsers.
|
|
|
|
// Transforms code points that have decimal digit property to the
|
|
|
|
// corresponding ASCII digit code points. Transforms spaces to ASCII.
|
|
|
|
// Transforms code points starting from the first non-ASCII code point that
|
|
|
|
// is neither a decimal digit nor a space to the end into '?'.
|
|
|
|
//
|
|
|
|
// Export for '_testinternalcapi' shared extension.
|
2023-07-04 05:59:09 -03:00
|
|
|
PyAPI_FUNC(PyObject*) _PyUnicode_TransformDecimalAndSpaceToASCII(
|
|
|
|
PyObject *unicode); /* Unicode object */
|
|
|
|
|
2023-07-03 05:23:43 -03:00
|
|
|
/* --- Methods & Slots ---------------------------------------------------- */
|
|
|
|
|
2024-02-29 12:11:28 -04:00
|
|
|
PyAPI_FUNC(PyObject*) _PyUnicode_JoinArray(
|
2023-07-03 19:35:46 -03:00
|
|
|
PyObject *separator,
|
|
|
|
PyObject *const *items,
|
|
|
|
Py_ssize_t seqlen
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Test whether a unicode is equal to ASCII identifier. Return 1 if true,
|
|
|
|
0 otherwise. The right argument must be ASCII identifier.
|
|
|
|
Any error occurs inside will be cleared before return. */
|
|
|
|
extern int _PyUnicode_EqualToASCIIId(
|
|
|
|
PyObject *left, /* Left string */
|
|
|
|
_Py_Identifier *right /* Right identifier */
|
|
|
|
);
|
|
|
|
|
2023-08-24 12:40:56 -03:00
|
|
|
// Test whether a unicode is equal to ASCII string. Return 1 if true,
|
|
|
|
// 0 otherwise. The right argument must be ASCII-encoded string.
|
|
|
|
// Any error occurs inside will be cleared before return.
|
|
|
|
// Export for '_ctypes' shared extension
|
2023-07-03 19:35:46 -03:00
|
|
|
PyAPI_FUNC(int) _PyUnicode_EqualToASCIIString(
|
|
|
|
PyObject *left,
|
|
|
|
const char *right /* ASCII-encoded string */
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Externally visible for str.strip(unicode) */
|
|
|
|
extern PyObject* _PyUnicode_XStrip(
|
|
|
|
PyObject *self,
|
|
|
|
int striptype,
|
|
|
|
PyObject *sepobj
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2023-07-03 05:23:43 -03:00
|
|
|
/* Using explicit passed-in values, insert the thousands grouping
|
|
|
|
into the string pointed to by buffer. For the argument descriptions,
|
|
|
|
see Objects/stringlib/localeutil.h */
|
2023-07-03 19:35:46 -03:00
|
|
|
extern Py_ssize_t _PyUnicode_InsertThousandsGrouping(
|
2023-07-03 05:23:43 -03:00
|
|
|
_PyUnicodeWriter *writer,
|
|
|
|
Py_ssize_t n_buffer,
|
|
|
|
PyObject *digits,
|
|
|
|
Py_ssize_t d_pos,
|
|
|
|
Py_ssize_t n_digits,
|
|
|
|
Py_ssize_t min_width,
|
|
|
|
const char *grouping,
|
|
|
|
PyObject *thousands_sep,
|
|
|
|
Py_UCS4 *maxchar);
|
|
|
|
|
2023-07-04 05:59:09 -03:00
|
|
|
/* --- Misc functions ----------------------------------------------------- */
|
|
|
|
|
|
|
|
extern PyObject* _PyUnicode_FormatLong(PyObject *, int, int, int);
|
|
|
|
|
|
|
|
/* Fast equality check when the inputs are known to be exact unicode types
|
|
|
|
and where the hash values are equal (i.e. a very probable match) */
|
|
|
|
extern int _PyUnicode_EQ(PyObject *, PyObject *);
|
|
|
|
|
2023-08-24 12:40:56 -03:00
|
|
|
// Equality check.
|
|
|
|
// Export for '_pickle' shared extension.
|
2023-07-04 05:59:09 -03:00
|
|
|
PyAPI_FUNC(int) _PyUnicode_Equal(PyObject *, PyObject *);
|
|
|
|
|
|
|
|
extern int _PyUnicode_WideCharString_Converter(PyObject *, void *);
|
|
|
|
extern int _PyUnicode_WideCharString_Opt_Converter(PyObject *, void *);
|
|
|
|
|
2023-07-24 23:25:45 -03:00
|
|
|
// Export for test_peg_generator
|
2023-07-04 05:59:09 -03:00
|
|
|
PyAPI_FUNC(Py_ssize_t) _PyUnicode_ScanIdentifier(PyObject *);
|
|
|
|
|
2023-07-03 05:23:43 -03:00
|
|
|
/* --- Runtime lifecycle -------------------------------------------------- */
|
2021-12-09 15:59:26 -04:00
|
|
|
|
|
|
|
extern void _PyUnicode_InitState(PyInterpreterState *);
|
|
|
|
extern PyStatus _PyUnicode_InitGlobalObjects(PyInterpreterState *);
|
|
|
|
extern PyStatus _PyUnicode_InitTypes(PyInterpreterState *);
|
|
|
|
extern void _PyUnicode_Fini(PyInterpreterState *);
|
2022-01-22 17:55:39 -04:00
|
|
|
extern void _PyUnicode_FiniTypes(PyInterpreterState *);
|
2021-12-09 15:59:26 -04:00
|
|
|
|
2022-04-18 11:18:27 -03:00
|
|
|
extern PyTypeObject _PyUnicodeASCIIIter_Type;
|
2021-12-09 15:59:26 -04:00
|
|
|
|
2023-07-03 05:23:43 -03:00
|
|
|
/* --- Other API ---------------------------------------------------------- */
|
2021-12-09 15:59:26 -04:00
|
|
|
|
|
|
|
struct _Py_unicode_runtime_ids {
|
2023-12-07 15:33:40 -04:00
|
|
|
PyMutex mutex;
|
2021-12-09 15:59:26 -04:00
|
|
|
// next_index value must be preserved when Py_Initialize()/Py_Finalize()
|
|
|
|
// is called multiple times: see _PyUnicode_FromId() implementation.
|
|
|
|
Py_ssize_t next_index;
|
|
|
|
};
|
|
|
|
|
2022-11-16 12:37:14 -04:00
|
|
|
struct _Py_unicode_runtime_state {
|
|
|
|
struct _Py_unicode_runtime_ids ids;
|
|
|
|
};
|
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
/* fs_codec.encoding is initialized to NULL.
|
|
|
|
Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */
|
|
|
|
struct _Py_unicode_fs_codec {
|
|
|
|
char *encoding; // Filesystem encoding (encoded to UTF-8)
|
|
|
|
int utf8; // encoding=="utf-8"?
|
|
|
|
char *errors; // Filesystem errors (encoded to UTF-8)
|
|
|
|
_Py_error_handler error_handler;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _Py_unicode_ids {
|
|
|
|
Py_ssize_t size;
|
|
|
|
PyObject **array;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _Py_unicode_state {
|
|
|
|
struct _Py_unicode_fs_codec fs_codec;
|
|
|
|
|
2022-12-07 18:56:31 -04:00
|
|
|
_PyUnicode_Name_CAPI *ucnhash_capi;
|
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
// Unicode identifiers (_Py_Identifier): see _PyUnicode_FromId()
|
|
|
|
struct _Py_unicode_ids ids;
|
|
|
|
};
|
|
|
|
|
2023-03-28 15:52:28 -03:00
|
|
|
extern void _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p);
|
2022-01-06 03:53:44 -04:00
|
|
|
extern void _PyUnicode_ClearInterned(PyInterpreterState *interp);
|
2021-12-09 15:59:26 -04:00
|
|
|
|
2023-11-10 15:31:36 -04:00
|
|
|
// Like PyUnicode_AsUTF8(), but check for embedded null characters.
|
|
|
|
// Export for '_sqlite3' shared extension.
|
|
|
|
PyAPI_FUNC(const char *) _PyUnicode_AsUTF8NoNUL(PyObject *);
|
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* !Py_INTERNAL_UNICODEOBJECT_H */
|