| 1 |
#ifndef Py_INTERNAL_UNICODEOBJECT_H |
| 2 |
#define Py_INTERNAL_UNICODEOBJECT_H |
| 3 |
#ifdef __cplusplus |
| 4 |
extern "C" { |
| 5 |
#endif |
| 6 |
|
| 7 |
#ifndef Py_BUILD_CORE |
| 8 |
# error "this header requires Py_BUILD_CORE define" |
| 9 |
#endif |
| 10 |
|
| 11 |
#include "pycore_lock.h" // PyMutex |
| 12 |
#include "pycore_fileutils.h" // _Py_error_handler |
| 13 |
#include "pycore_identifier.h" // _Py_Identifier |
| 14 |
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI |
| 15 |
#include "pycore_global_objects.h" // _Py_SINGLETON |
| 16 |
|
| 17 |
/* --- Characters Type APIs ----------------------------------------------- */ |
| 18 |
|
| 19 |
extern int _PyUnicode_IsXidStart(Py_UCS4 ch); |
| 20 |
extern int _PyUnicode_IsXidContinue(Py_UCS4 ch); |
| 21 |
extern int _PyUnicode_ToLowerFull(Py_UCS4 ch, Py_UCS4 *res); |
| 22 |
extern int _PyUnicode_ToTitleFull(Py_UCS4 ch, Py_UCS4 *res); |
| 23 |
extern int _PyUnicode_ToUpperFull(Py_UCS4 ch, Py_UCS4 *res); |
| 24 |
extern int _PyUnicode_ToFoldedFull(Py_UCS4 ch, Py_UCS4 *res); |
| 25 |
extern int _PyUnicode_IsCaseIgnorable(Py_UCS4 ch); |
| 26 |
extern int _PyUnicode_IsCased(Py_UCS4 ch); |
| 27 |
|
| 28 |
/* --- Unicode API -------------------------------------------------------- */ |
| 29 |
|
| 30 |
// Export for '_json' shared extension |
| 31 |
PyAPI_FUNC(int) _PyUnicode_CheckConsistency( |
| 32 |
PyObject *op, |
| 33 |
int check_content); |
| 34 |
|
| 35 |
PyAPI_FUNC(void) _PyUnicode_ExactDealloc(PyObject *op); |
| 36 |
extern Py_ssize_t _PyUnicode_InternedSize(void); |
| 37 |
extern Py_ssize_t _PyUnicode_InternedSize_Immortal(void); |
| 38 |
|
| 39 |
// Get a copy of a Unicode string. |
| 40 |
// Export for '_datetime' shared extension. |
| 41 |
PyAPI_FUNC(PyObject*) _PyUnicode_Copy( |
| 42 |
PyObject *unicode); |
| 43 |
|
| 44 |
/* Unsafe version of PyUnicode_Fill(): don't check arguments and so may crash |
| 45 |
if parameters are invalid (e.g. if length is longer than the string). */ |
| 46 |
extern void _PyUnicode_FastFill( |
| 47 |
PyObject *unicode, |
| 48 |
Py_ssize_t start, |
| 49 |
Py_ssize_t length, |
| 50 |
Py_UCS4 fill_char |
| 51 |
); |
| 52 |
|
| 53 |
/* Unsafe version of PyUnicode_CopyCharacters(): don't check arguments and so |
| 54 |
may crash if parameters are invalid (e.g. if the output string |
| 55 |
is too short). */ |
| 56 |
extern void _PyUnicode_FastCopyCharacters( |
| 57 |
PyObject *to, |
| 58 |
Py_ssize_t to_start, |
| 59 |
PyObject *from, |
| 60 |
Py_ssize_t from_start, |
| 61 |
Py_ssize_t how_many |
| 62 |
); |
| 63 |
|
| 64 |
/* Create a new string from a buffer of ASCII characters. |
| 65 |
WARNING: Don't check if the string contains any non-ASCII character. */ |
| 66 |
extern PyObject* _PyUnicode_FromASCII( |
| 67 |
const char *buffer, |
| 68 |
Py_ssize_t size); |
| 69 |
|
| 70 |
/* Compute the maximum character of the substring unicode[start:end]. |
| 71 |
Return 127 for an empty string. */ |
| 72 |
extern Py_UCS4 _PyUnicode_FindMaxChar ( |
| 73 |
PyObject *unicode, |
| 74 |
Py_ssize_t start, |
| 75 |
Py_ssize_t end); |
| 76 |
|
| 77 |
/* --- _PyUnicodeWriter API ----------------------------------------------- */ |
| 78 |
|
| 79 |
/* Format the object based on the format_spec, as defined in PEP 3101 |
| 80 |
(Advanced String Formatting). */ |
| 81 |
extern int _PyUnicode_FormatAdvancedWriter( |
| 82 |
_PyUnicodeWriter *writer, |
| 83 |
PyObject *obj, |
| 84 |
PyObject *format_spec, |
| 85 |
Py_ssize_t start, |
| 86 |
Py_ssize_t end); |
| 87 |
|
| 88 |
/* --- UTF-7 Codecs ------------------------------------------------------- */ |
| 89 |
|
| 90 |
extern PyObject* _PyUnicode_EncodeUTF7( |
| 91 |
PyObject *unicode, /* Unicode object */ |
| 92 |
int base64SetO, /* Encode RFC2152 Set O characters in base64 */ |
| 93 |
int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */ |
| 94 |
const char *errors); /* error handling */ |
| 95 |
|
| 96 |
/* --- UTF-8 Codecs ------------------------------------------------------- */ |
| 97 |
|
| 98 |
// Export for '_tkinter' shared extension. |
| 99 |
PyAPI_FUNC(PyObject*) _PyUnicode_AsUTF8String( |
| 100 |
PyObject *unicode, |
| 101 |
const char *errors); |
| 102 |
|
| 103 |
/* --- UTF-32 Codecs ------------------------------------------------------ */ |
| 104 |
|
| 105 |
// Export for '_tkinter' shared extension |
| 106 |
PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF32( |
| 107 |
PyObject *object, /* Unicode object */ |
| 108 |
const char *errors, /* error handling */ |
| 109 |
int byteorder); /* byteorder to use 0=BOM+native;-1=LE,1=BE */ |
| 110 |
|
| 111 |
/* --- UTF-16 Codecs ------------------------------------------------------ */ |
| 112 |
|
| 113 |
// Returns a Python string object holding the UTF-16 encoded value of |
| 114 |
// the Unicode data. |
| 115 |
// |
| 116 |
// If byteorder is not 0, output is written according to the following |
| 117 |
// byte order: |
| 118 |
// |
| 119 |
// byteorder == -1: little endian |
| 120 |
// byteorder == 0: native byte order (writes a BOM mark) |
| 121 |
// byteorder == 1: big endian |
| 122 |
// |
| 123 |
// If byteorder is 0, the output string will always start with the |
| 124 |
// Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is |
| 125 |
// prepended. |
| 126 |
// |
| 127 |
// Export for '_tkinter' shared extension |
| 128 |
PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF16( |
| 129 |
PyObject* unicode, /* Unicode object */ |
| 130 |
const char *errors, /* error handling */ |
| 131 |
int byteorder); /* byteorder to use 0=BOM+native;-1=LE,1=BE */ |
| 132 |
|
| 133 |
/* --- Unicode-Escape Codecs ---------------------------------------------- */ |
| 134 |
|
| 135 |
/* Variant of PyUnicode_DecodeUnicodeEscape that supports partial decoding. */ |
| 136 |
extern PyObject* _PyUnicode_DecodeUnicodeEscapeStateful( |
| 137 |
const char *string, /* Unicode-Escape encoded string */ |
| 138 |
Py_ssize_t length, /* size of string */ |
| 139 |
const char *errors, /* error handling */ |
| 140 |
Py_ssize_t *consumed); /* bytes consumed */ |
| 141 |
|
| 142 |
// Helper for PyUnicode_DecodeUnicodeEscape that detects invalid escape |
| 143 |
// chars. |
| 144 |
// Export for test_peg_generator. |
| 145 |
PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscapeInternal2( |
| 146 |
const char *string, /* Unicode-Escape encoded string */ |
| 147 |
Py_ssize_t length, /* size of string */ |
| 148 |
const char *errors, /* error handling */ |
| 149 |
Py_ssize_t *consumed, /* bytes consumed */ |
| 150 |
int *first_invalid_escape_char, /* on return, if not -1, contain the first |
| 151 |
invalid escaped char (<= 0xff) or invalid |
| 152 |
octal escape (> 0xff) in string. */ |
| 153 |
const char **first_invalid_escape_ptr); /* on return, if not NULL, may |
| 154 |
point to the first invalid escaped |
| 155 |
char in string. |
| 156 |
May be NULL if errors is not NULL. */ |
| 157 |
// Export for binary compatibility. |
| 158 |
PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscapeInternal( |
| 159 |
const char *string, /* Unicode-Escape encoded string */ |
| 160 |
Py_ssize_t length, /* size of string */ |
| 161 |
const char *errors, /* error handling */ |
| 162 |
Py_ssize_t *consumed, /* bytes consumed */ |
| 163 |
const char **first_invalid_escape); /* on return, points to first |
| 164 |
invalid escaped char in |
| 165 |
string. */ |
| 166 |
|
| 167 |
/* --- Raw-Unicode-Escape Codecs ---------------------------------------------- */ |
| 168 |
|
| 169 |
/* Variant of PyUnicode_DecodeRawUnicodeEscape that supports partial decoding. */ |
| 170 |
extern PyObject* _PyUnicode_DecodeRawUnicodeEscapeStateful( |
| 171 |
const char *string, /* Unicode-Escape encoded string */ |
| 172 |
Py_ssize_t length, /* size of string */ |
| 173 |
const char *errors, /* error handling */ |
| 174 |
Py_ssize_t *consumed); /* bytes consumed */ |
| 175 |
|
| 176 |
/* --- Latin-1 Codecs ----------------------------------------------------- */ |
| 177 |
|
| 178 |
extern PyObject* _PyUnicode_AsLatin1String( |
| 179 |
PyObject* unicode, |
| 180 |
const char* errors); |
| 181 |
|
| 182 |
/* --- ASCII Codecs ------------------------------------------------------- */ |
| 183 |
|
| 184 |
extern PyObject* _PyUnicode_AsASCIIString( |
| 185 |
PyObject* unicode, |
| 186 |
const char* errors); |
| 187 |
|
| 188 |
/* --- Character Map Codecs ----------------------------------------------- */ |
| 189 |
|
| 190 |
/* Translate an Unicode object by applying a character mapping table to |
| 191 |
it and return the resulting Unicode object. |
| 192 |
|
| 193 |
The mapping table must map Unicode ordinal integers to Unicode strings, |
| 194 |
Unicode ordinal integers or None (causing deletion of the character). |
| 195 |
|
| 196 |
Mapping tables may be dictionaries or sequences. Unmapped character |
| 197 |
ordinals (ones which cause a LookupError) are left untouched and |
| 198 |
are copied as-is. |
| 199 |
*/ |
| 200 |
extern PyObject* _PyUnicode_EncodeCharmap( |
| 201 |
PyObject *unicode, /* Unicode object */ |
| 202 |
PyObject *mapping, /* encoding mapping */ |
| 203 |
const char *errors); /* error handling */ |
| 204 |
|
| 205 |
/* --- Decimal Encoder ---------------------------------------------------- */ |
| 206 |
|
| 207 |
// Coverts a Unicode object holding a decimal value to an ASCII string |
| 208 |
// for using in int, float and complex parsers. |
| 209 |
// Transforms code points that have decimal digit property to the |
| 210 |
// corresponding ASCII digit code points. Transforms spaces to ASCII. |
| 211 |
// Transforms code points starting from the first non-ASCII code point that |
| 212 |
// is neither a decimal digit nor a space to the end into '?'. |
| 213 |
// |
| 214 |
// Export for '_testinternalcapi' shared extension. |
| 215 |
PyAPI_FUNC(PyObject*) _PyUnicode_TransformDecimalAndSpaceToASCII( |
| 216 |
PyObject *unicode); /* Unicode object */ |
| 217 |
|
| 218 |
/* --- Methods & Slots ---------------------------------------------------- */ |
| 219 |
|
| 220 |
PyAPI_FUNC(PyObject*) _PyUnicode_JoinArray( |
| 221 |
PyObject *separator, |
| 222 |
PyObject *const *items, |
| 223 |
Py_ssize_t seqlen |
| 224 |
); |
| 225 |
|
| 226 |
/* Test whether a unicode is equal to ASCII identifier. Return 1 if true, |
| 227 |
0 otherwise. The right argument must be ASCII identifier. |
| 228 |
Any error occurs inside will be cleared before return. */ |
| 229 |
extern int _PyUnicode_EqualToASCIIId( |
| 230 |
PyObject *left, /* Left string */ |
| 231 |
_Py_Identifier *right /* Right identifier */ |
| 232 |
); |
| 233 |
|
| 234 |
// Test whether a unicode is equal to ASCII string. Return 1 if true, |
| 235 |
// 0 otherwise. The right argument must be ASCII-encoded string. |
| 236 |
// Any error occurs inside will be cleared before return. |
| 237 |
// Export for '_ctypes' shared extension |
| 238 |
PyAPI_FUNC(int) _PyUnicode_EqualToASCIIString( |
| 239 |
PyObject *left, |
| 240 |
const char *right /* ASCII-encoded string */ |
| 241 |
); |
| 242 |
|
| 243 |
/* Externally visible for str.strip(unicode) */ |
| 244 |
extern PyObject* _PyUnicode_XStrip( |
| 245 |
PyObject *self, |
| 246 |
int striptype, |
| 247 |
PyObject *sepobj |
| 248 |
); |
| 249 |
|
| 250 |
|
| 251 |
/* Using explicit passed-in values, insert the thousands grouping |
| 252 |
into the string pointed to by buffer. For the argument descriptions, |
| 253 |
see Objects/stringlib/localeutil.h */ |
| 254 |
extern Py_ssize_t _PyUnicode_InsertThousandsGrouping( |
| 255 |
_PyUnicodeWriter *writer, |
| 256 |
Py_ssize_t n_buffer, |
| 257 |
PyObject *digits, |
| 258 |
Py_ssize_t d_pos, |
| 259 |
Py_ssize_t n_digits, |
| 260 |
Py_ssize_t min_width, |
| 261 |
const char *grouping, |
| 262 |
PyObject *thousands_sep, |
| 263 |
Py_UCS4 *maxchar); |
| 264 |
|
| 265 |
/* --- Misc functions ----------------------------------------------------- */ |
| 266 |
|
| 267 |
extern PyObject* _PyUnicode_FormatLong(PyObject *, int, int, int); |
| 268 |
|
| 269 |
/* Fast equality check when the inputs are known to be exact unicode types |
| 270 |
and where the hash values are equal (i.e. a very probable match) */ |
| 271 |
extern int _PyUnicode_EQ(PyObject *, PyObject *); |
| 272 |
|
| 273 |
// Equality check. |
| 274 |
// Export for '_pickle' shared extension. |
| 275 |
PyAPI_FUNC(int) _PyUnicode_Equal(PyObject *, PyObject *); |
| 276 |
|
| 277 |
extern int _PyUnicode_WideCharString_Converter(PyObject *, void *); |
| 278 |
extern int _PyUnicode_WideCharString_Opt_Converter(PyObject *, void *); |
| 279 |
|
| 280 |
// Export for test_peg_generator |
| 281 |
PyAPI_FUNC(Py_ssize_t) _PyUnicode_ScanIdentifier(PyObject *); |
| 282 |
|
| 283 |
/* --- Runtime lifecycle -------------------------------------------------- */ |
| 284 |
|
| 285 |
extern void _PyUnicode_InitState(PyInterpreterState *); |
| 286 |
extern PyStatus _PyUnicode_InitGlobalObjects(PyInterpreterState *); |
| 287 |
extern PyStatus _PyUnicode_InitTypes(PyInterpreterState *); |
| 288 |
extern void _PyUnicode_Fini(PyInterpreterState *); |
| 289 |
extern void _PyUnicode_FiniTypes(PyInterpreterState *); |
| 290 |
|
| 291 |
extern PyTypeObject _PyUnicodeASCIIIter_Type; |
| 292 |
|
| 293 |
/* --- Interning ---------------------------------------------------------- */ |
| 294 |
|
| 295 |
// All these are "ref-neutral", like the public PyUnicode_InternInPlace. |
| 296 |
|
| 297 |
// Explicit interning routines: |
| 298 |
PyAPI_FUNC(void) _PyUnicode_InternMortal(PyInterpreterState *interp, PyObject **); |
| 299 |
PyAPI_FUNC(void) _PyUnicode_InternImmortal(PyInterpreterState *interp, PyObject **); |
| 300 |
// Left here to help backporting: |
| 301 |
PyAPI_FUNC(void) _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p); |
| 302 |
// Only for singletons in the _PyRuntime struct: |
| 303 |
extern void _PyUnicode_InternStatic(PyInterpreterState *interp, PyObject **); |
| 304 |
|
| 305 |
/* --- Other API ---------------------------------------------------------- */ |
| 306 |
|
| 307 |
struct _Py_unicode_runtime_ids { |
| 308 |
PyMutex mutex; |
| 309 |
// next_index value must be preserved when Py_Initialize()/Py_Finalize() |
| 310 |
// is called multiple times: see _PyUnicode_FromId() implementation. |
| 311 |
Py_ssize_t next_index; |
| 312 |
}; |
| 313 |
|
| 314 |
struct _Py_unicode_runtime_state { |
| 315 |
struct _Py_unicode_runtime_ids ids; |
| 316 |
}; |
| 317 |
|
| 318 |
/* fs_codec.encoding is initialized to NULL. |
| 319 |
Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */ |
| 320 |
struct _Py_unicode_fs_codec { |
| 321 |
char *encoding; // Filesystem encoding (encoded to UTF-8) |
| 322 |
int utf8; // encoding=="utf-8"? |
| 323 |
char *errors; // Filesystem errors (encoded to UTF-8) |
| 324 |
_Py_error_handler error_handler; |
| 325 |
}; |
| 326 |
|
| 327 |
struct _Py_unicode_ids { |
| 328 |
Py_ssize_t size; |
| 329 |
PyObject **array; |
| 330 |
}; |
| 331 |
|
| 332 |
struct _Py_unicode_state { |
| 333 |
struct _Py_unicode_fs_codec fs_codec; |
| 334 |
|
| 335 |
_PyUnicode_Name_CAPI *ucnhash_capi; |
| 336 |
|
| 337 |
// Unicode identifiers (_Py_Identifier): see _PyUnicode_FromId() |
| 338 |
struct _Py_unicode_ids ids; |
| 339 |
}; |
| 340 |
|
| 341 |
extern void _PyUnicode_ClearInterned(PyInterpreterState *interp); |
| 342 |
|
| 343 |
// Like PyUnicode_AsUTF8(), but check for embedded null characters. |
| 344 |
// Export for '_sqlite3' shared extension. |
| 345 |
PyAPI_FUNC(const char *) _PyUnicode_AsUTF8NoNUL(PyObject *); |
| 346 |
|
| 347 |
|
| 348 |
#ifdef __cplusplus |
| 349 |
} |
| 350 |
#endif |
| 351 |
#endif /* !Py_INTERNAL_UNICODEOBJECT_H */ |