Coverage Report

Created: 2025-08-26 06:26

/src/cpython/Objects/unicodeobject.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
3
Unicode implementation based on original code by Fredrik Lundh,
4
modified by Marc-Andre Lemburg <mal@lemburg.com>.
5
6
Major speed upgrades to the method implementations at the Reykjavik
7
NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke.
8
9
Copyright (c) Corporation for National Research Initiatives.
10
11
--------------------------------------------------------------------
12
The original string type implementation is:
13
14
  Copyright (c) 1999 by Secret Labs AB
15
  Copyright (c) 1999 by Fredrik Lundh
16
17
By obtaining, using, and/or copying this software and/or its
18
associated documentation, you agree that you have read, understood,
19
and will comply with the following terms and conditions:
20
21
Permission to use, copy, modify, and distribute this software and its
22
associated documentation for any purpose and without fee is hereby
23
granted, provided that the above copyright notice appears in all
24
copies, and that both that copyright notice and this permission notice
25
appear in supporting documentation, and that the name of Secret Labs
26
AB or the author not be used in advertising or publicity pertaining to
27
distribution of the software without specific, written prior
28
permission.
29
30
SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
31
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
32
FITNESS.  IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR
33
ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
34
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
35
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
36
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37
--------------------------------------------------------------------
38
39
*/
40
41
#include "Python.h"
42
#include "pycore_abstract.h"      // _PyIndex_Check()
43
#include "pycore_bytes_methods.h" // _Py_bytes_lower()
44
#include "pycore_bytesobject.h"   // _PyBytes_Repeat()
45
#include "pycore_ceval.h"         // _PyEval_GetBuiltin()
46
#include "pycore_codecs.h"        // _PyCodec_Lookup()
47
#include "pycore_critical_section.h" // Py_*_CRITICAL_SECTION_SEQUENCE_FAST
48
#include "pycore_format.h"        // F_LJUST
49
#include "pycore_freelist.h"      // _Py_FREELIST_FREE(), _Py_FREELIST_POP()
50
#include "pycore_initconfig.h"    // _PyStatus_OK()
51
#include "pycore_interp.h"        // PyInterpreterState.fs_codec
52
#include "pycore_long.h"          // _PyLong_FormatWriter()
53
#include "pycore_object.h"        // _PyObject_GC_TRACK(), _Py_FatalRefcountError()
54
#include "pycore_pathconfig.h"    // _Py_DumpPathConfig()
55
#include "pycore_pyerrors.h"      // _PyUnicodeTranslateError_Create()
56
#include "pycore_pyhash.h"        // _Py_HashSecret_t
57
#include "pycore_pylifecycle.h"   // _Py_SetFileSystemEncoding()
58
#include "pycore_pystate.h"       // _PyInterpreterState_GET()
59
#include "pycore_tuple.h"         // _PyTuple_FromArray()
60
#include "pycore_ucnhash.h"       // _PyUnicode_Name_CAPI
61
#include "pycore_unicodeobject.h" // struct _Py_unicode_state
62
#include "pycore_unicodeobject_generated.h"  // _PyUnicode_InitStaticStrings()
63
64
#include "stringlib/eq.h"         // unicode_eq()
65
#include <stddef.h>               // ptrdiff_t
66
67
#ifdef MS_WINDOWS
68
#include <windows.h>
69
#endif
70
71
#ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION
72
#  include "pycore_fileutils.h"   // _Py_LocaleUsesNonUnicodeWchar()
73
#endif
74
75
/* Uncomment to display statistics on interned strings at exit
76
   in _PyUnicode_ClearInterned(). */
77
/* #define INTERNED_STATS 1 */
78
79
80
/*[clinic input]
81
class str "PyObject *" "&PyUnicode_Type"
82
[clinic start generated code]*/
83
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4884c934de622cf6]*/
84
85
/*[python input]
86
class Py_UCS4_converter(CConverter):
87
    type = 'Py_UCS4'
88
    converter = 'convert_uc'
89
90
    def converter_init(self):
91
        if self.default is not unspecified:
92
            self.c_default = ascii(self.default)
93
            if len(self.c_default) > 4 or self.c_default[0] != "'":
94
                self.c_default = hex(ord(self.default))
95
96
[python start generated code]*/
97
/*[python end generated code: output=da39a3ee5e6b4b0d input=88f5dd06cd8e7a61]*/
98
99
/* --- Globals ------------------------------------------------------------
100
101
NOTE: In the interpreter's initialization phase, some globals are currently
102
      initialized dynamically as needed. In the process Unicode objects may
103
      be created before the Unicode type is ready.
104
105
*/
106
107
// Maximum code point of Unicode 6.0: 0x10ffff (1,114,111).
108
// The value must be the same in fileutils.c.
109
92.3M
#define MAX_UNICODE 0x10ffff
110
111
#ifdef Py_DEBUG
112
#  define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0)
113
#else
114
#  define _PyUnicode_CHECK(op) PyUnicode_Check(op)
115
#endif
116
117
static inline char* _PyUnicode_UTF8(PyObject *op)
118
257M
{
119
257M
    return FT_ATOMIC_LOAD_PTR_ACQUIRE(_PyCompactUnicodeObject_CAST(op)->utf8);
120
257M
}
121
122
static inline char* PyUnicode_UTF8(PyObject *op)
123
63.2M
{
124
63.2M
    assert(_PyUnicode_CHECK(op));
125
63.2M
    if (PyUnicode_IS_COMPACT_ASCII(op)) {
126
50.1M
        return ((char*)(_PyASCIIObject_CAST(op) + 1));
127
50.1M
    }
128
13.0M
    else {
129
13.0M
         return _PyUnicode_UTF8(op);
130
13.0M
    }
131
63.2M
}
132
133
static inline void PyUnicode_SET_UTF8(PyObject *op, char *utf8)
134
20.8M
{
135
20.8M
    FT_ATOMIC_STORE_PTR_RELEASE(_PyCompactUnicodeObject_CAST(op)->utf8, utf8);
136
20.8M
}
137
138
static inline Py_ssize_t PyUnicode_UTF8_LENGTH(PyObject *op)
139
28.4M
{
140
28.4M
    assert(_PyUnicode_CHECK(op));
141
28.4M
    if (PyUnicode_IS_COMPACT_ASCII(op)) {
142
25.0M
         return _PyASCIIObject_CAST(op)->length;
143
25.0M
    }
144
3.42M
    else {
145
3.42M
         return _PyCompactUnicodeObject_CAST(op)->utf8_length;
146
3.42M
    }
147
28.4M
}
148
149
static inline void PyUnicode_SET_UTF8_LENGTH(PyObject *op, Py_ssize_t length)
150
20.8M
{
151
20.8M
    _PyCompactUnicodeObject_CAST(op)->utf8_length = length;
152
20.8M
}
153
154
#define _PyUnicode_LENGTH(op)                           \
155
572M
    (_PyASCIIObject_CAST(op)->length)
156
#define _PyUnicode_STATE(op)                            \
157
3.57G
    (_PyASCIIObject_CAST(op)->state)
158
#define _PyUnicode_HASH(op)                             \
159
520M
    (_PyASCIIObject_CAST(op)->hash)
160
161
108M
#define PyUnicode_HASH PyUnstable_Unicode_GET_CACHED_HASH
162
163
static inline void PyUnicode_SET_HASH(PyObject *op, Py_hash_t hash)
164
43.5M
{
165
43.5M
    FT_ATOMIC_STORE_SSIZE_RELAXED(_PyASCIIObject_CAST(op)->hash, hash);
166
43.5M
}
167
168
#define _PyUnicode_DATA_ANY(op)                         \
169
43.9M
    (_PyUnicodeObject_CAST(op)->data.any)
170
171
static inline int _PyUnicode_SHARE_UTF8(PyObject *op)
172
0
{
173
0
    assert(_PyUnicode_CHECK(op));
174
0
    assert(!PyUnicode_IS_COMPACT_ASCII(op));
175
0
    return (_PyUnicode_UTF8(op) == PyUnicode_DATA(op));
176
0
}
177
178
/* true if the Unicode object has an allocated UTF-8 memory block
179
   (not shared with other data) */
180
static inline int _PyUnicode_HAS_UTF8_MEMORY(PyObject *op)
181
572M
{
182
572M
    return (!PyUnicode_IS_COMPACT_ASCII(op)
183
572M
            && _PyUnicode_UTF8(op) != NULL
184
572M
            && _PyUnicode_UTF8(op) != PyUnicode_DATA(op));
185
572M
}
186
187
188
/* Generic helper macro to convert characters of different types.
189
   from_type and to_type have to be valid type names, begin and end
190
   are pointers to the source characters which should be of type
191
   "from_type *".  to is a pointer of type "to_type *" and points to the
192
   buffer where the result characters are written to. */
193
#define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \
194
169M
    do {                                                \
195
169M
        to_type *_to = (to_type *)(to);                 \
196
169M
        const from_type *_iter = (const from_type *)(begin);\
197
169M
        const from_type *_end = (const from_type *)(end);\
198
169M
        Py_ssize_t n = (_end) - (_iter);                \
199
169M
        const from_type *_unrolled_end =                \
200
169M
            _iter + _Py_SIZE_ROUND_DOWN(n, 4);          \
201
1.09G
        while (_iter < (_unrolled_end)) {               \
202
924M
            _to[0] = (to_type) _iter[0];                \
203
924M
            _to[1] = (to_type) _iter[1];                \
204
924M
            _to[2] = (to_type) _iter[2];                \
205
924M
            _to[3] = (to_type) _iter[3];                \
206
924M
            _iter += 4; _to += 4;                       \
207
924M
        }                                               \
208
386M
        while (_iter < (_end))                          \
209
216M
            *_to++ = (to_type) *_iter++;                \
210
169M
    } while (0)
211
212
263M
#define LATIN1 _Py_LATIN1_CHR
213
214
#ifdef MS_WINDOWS
215
   /* On Windows, overallocate by 50% is the best factor */
216
#  define OVERALLOCATE_FACTOR 2
217
#else
218
   /* On Linux, overallocate by 25% is the best factor */
219
109M
#  define OVERALLOCATE_FACTOR 4
220
#endif
221
222
/* Forward declaration */
223
static inline int
224
_PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch);
225
static inline void
226
_PyUnicodeWriter_InitWithBuffer(_PyUnicodeWriter *writer, PyObject *buffer);
227
static PyObject *
228
unicode_encode_utf8(PyObject *unicode, _Py_error_handler error_handler,
229
                    const char *errors);
230
static PyObject *
231
unicode_decode_utf8(const char *s, Py_ssize_t size,
232
                    _Py_error_handler error_handler, const char *errors,
233
                    Py_ssize_t *consumed);
234
static int
235
unicode_decode_utf8_writer(_PyUnicodeWriter *writer,
236
                           const char *s, Py_ssize_t size,
237
                           _Py_error_handler error_handler, const char *errors,
238
                           Py_ssize_t *consumed);
239
#ifdef Py_DEBUG
240
static inline int unicode_is_finalizing(void);
241
static int unicode_is_singleton(PyObject *unicode);
242
#endif
243
244
245
// Return a reference to the immortal empty string singleton.
246
static inline PyObject* unicode_get_empty(void)
247
111M
{
248
111M
    _Py_DECLARE_STR(empty, "");
249
111M
    return &_Py_STR(empty);
250
111M
}
251
252
/* This dictionary holds per-interpreter interned strings.
253
 * See InternalDocs/string_interning.md for details.
254
 */
255
static inline PyObject *get_interned_dict(PyInterpreterState *interp)
256
3.58M
{
257
3.58M
    return _Py_INTERP_CACHED_OBJECT(interp, interned_strings);
258
3.58M
}
259
260
/* This hashtable holds statically allocated interned strings.
261
 * See InternalDocs/string_interning.md for details.
262
 */
263
3.36M
#define INTERNED_STRINGS _PyRuntime.cached_objects.interned_strings
264
265
/* Get number of all interned strings for the current interpreter. */
266
Py_ssize_t
267
_PyUnicode_InternedSize(void)
268
0
{
269
0
    PyObject *dict = get_interned_dict(_PyInterpreterState_GET());
270
0
    return _Py_hashtable_len(INTERNED_STRINGS) + PyDict_GET_SIZE(dict);
271
0
}
272
273
/* Get number of immortal interned strings for the current interpreter. */
274
Py_ssize_t
275
_PyUnicode_InternedSize_Immortal(void)
276
0
{
277
0
    PyObject *dict = get_interned_dict(_PyInterpreterState_GET());
278
0
    PyObject *key, *value;
279
0
    Py_ssize_t pos = 0;
280
0
    Py_ssize_t count = 0;
281
282
    // It's tempting to keep a count and avoid a loop here. But, this function
283
    // is intended for refleak tests. It spends extra work to report the true
284
    // value, to help detect bugs in optimizations.
285
286
0
    while (PyDict_Next(dict, &pos, &key, &value)) {
287
0
        assert(PyUnicode_CHECK_INTERNED(key) != SSTATE_INTERNED_IMMORTAL_STATIC);
288
0
        if (PyUnicode_CHECK_INTERNED(key) == SSTATE_INTERNED_IMMORTAL) {
289
0
           count++;
290
0
       }
291
0
    }
292
0
    return _Py_hashtable_len(INTERNED_STRINGS) + count;
293
0
}
294
295
static Py_hash_t unicode_hash(PyObject *);
296
297
static Py_uhash_t
298
hashtable_unicode_hash(const void *key)
299
3.36M
{
300
3.36M
    return unicode_hash((PyObject *)key);
301
3.36M
}
302
303
static int
304
hashtable_unicode_compare(const void *key1, const void *key2)
305
273k
{
306
273k
    PyObject *obj1 = (PyObject *)key1;
307
273k
    PyObject *obj2 = (PyObject *)key2;
308
273k
    if (obj1 != NULL && obj2 != NULL) {
309
273k
        return unicode_eq(obj1, obj2);
310
273k
    }
311
0
    else {
312
0
        return obj1 == obj2;
313
0
    }
314
273k
}
315
316
/* Return true if this interpreter should share the main interpreter's
317
   intern_dict.  That's important for interpreters which load basic
318
   single-phase init extension modules (m_size == -1).  There could be interned
319
   immortal strings that are shared between interpreters, due to the
320
   PyDict_Update(mdict, m_copy) call in import_find_extension().
321
322
   It's not safe to deallocate those strings until all interpreters that
323
   potentially use them are freed.  By storing them in the main interpreter, we
324
   ensure they get freed after all other interpreters are freed.
325
*/
326
static bool
327
has_shared_intern_dict(PyInterpreterState *interp)
328
16
{
329
16
    PyInterpreterState *main_interp = _PyInterpreterState_Main();
330
16
    return interp != main_interp  && interp->feature_flags & Py_RTFLAGS_USE_MAIN_OBMALLOC;
331
16
}
332
333
static int
334
init_interned_dict(PyInterpreterState *interp)
335
16
{
336
16
    assert(get_interned_dict(interp) == NULL);
337
16
    PyObject *interned;
338
16
    if (has_shared_intern_dict(interp)) {
339
0
        interned = get_interned_dict(_PyInterpreterState_Main());
340
0
        Py_INCREF(interned);
341
0
    }
342
16
    else {
343
16
        interned = PyDict_New();
344
16
        if (interned == NULL) {
345
0
            return -1;
346
0
        }
347
16
    }
348
16
    _Py_INTERP_CACHED_OBJECT(interp, interned_strings) = interned;
349
16
    return 0;
350
16
}
351
352
static void
353
clear_interned_dict(PyInterpreterState *interp)
354
0
{
355
0
    PyObject *interned = get_interned_dict(interp);
356
0
    if (interned != NULL) {
357
0
        if (!has_shared_intern_dict(interp)) {
358
            // only clear if the dict belongs to this interpreter
359
0
            PyDict_Clear(interned);
360
0
        }
361
0
        Py_DECREF(interned);
362
0
        _Py_INTERP_CACHED_OBJECT(interp, interned_strings) = NULL;
363
0
    }
364
0
}
365
366
static PyStatus
367
init_global_interned_strings(PyInterpreterState *interp)
368
16
{
369
16
    assert(INTERNED_STRINGS == NULL);
370
16
    _Py_hashtable_allocator_t hashtable_alloc = {PyMem_RawMalloc, PyMem_RawFree};
371
372
16
    INTERNED_STRINGS = _Py_hashtable_new_full(
373
16
        hashtable_unicode_hash,
374
16
        hashtable_unicode_compare,
375
        // Objects stored here are immortal and statically allocated,
376
        // so we don't need key_destroy_func & value_destroy_func:
377
16
        NULL,
378
16
        NULL,
379
16
        &hashtable_alloc
380
16
    );
381
16
    if (INTERNED_STRINGS == NULL) {
382
0
        PyErr_Clear();
383
0
        return _PyStatus_ERR("failed to create global interned dict");
384
0
    }
385
386
    /* Intern statically allocated string identifiers, deepfreeze strings,
387
        * and one-byte latin-1 strings.
388
        * This must be done before any module initialization so that statically
389
        * allocated string identifiers are used instead of heap allocated strings.
390
        * Deepfreeze uses the interned identifiers if present to save space
391
        * else generates them and they are interned to speed up dict lookups.
392
    */
393
16
    _PyUnicode_InitStaticStrings(interp);
394
395
4.11k
    for (int i = 0; i < 256; i++) {
396
4.09k
        PyObject *s = LATIN1(i);
397
4.09k
        _PyUnicode_InternStatic(interp, &s);
398
4.09k
        assert(s == LATIN1(i));
399
4.09k
    }
400
#ifdef Py_DEBUG
401
    assert(_PyUnicode_CheckConsistency(&_Py_STR(empty), 1));
402
403
    for (int i = 0; i < 256; i++) {
404
        assert(_PyUnicode_CheckConsistency(LATIN1(i), 1));
405
    }
406
#endif
407
16
    return _PyStatus_OK();
408
16
}
409
410
static void clear_global_interned_strings(void)
411
0
{
412
0
    if (INTERNED_STRINGS != NULL) {
413
0
        _Py_hashtable_destroy(INTERNED_STRINGS);
414
0
        INTERNED_STRINGS = NULL;
415
0
    }
416
0
}
417
418
#define _Py_RETURN_UNICODE_EMPTY()   \
419
46.4M
    do {                             \
420
46.4M
        return unicode_get_empty();  \
421
46.4M
    } while (0)
422
423
static inline void
424
unicode_fill(int kind, void *data, Py_UCS4 value,
425
             Py_ssize_t start, Py_ssize_t length)
426
16.5M
{
427
16.5M
    assert(0 <= start);
428
16.5M
    switch (kind) {
429
4.64M
    case PyUnicode_1BYTE_KIND: {
430
4.64M
        assert(value <= 0xff);
431
4.64M
        Py_UCS1 ch = (unsigned char)value;
432
4.64M
        Py_UCS1 *to = (Py_UCS1 *)data + start;
433
4.64M
        memset(to, ch, length);
434
4.64M
        break;
435
0
    }
436
8.31M
    case PyUnicode_2BYTE_KIND: {
437
8.31M
        assert(value <= 0xffff);
438
8.31M
        Py_UCS2 ch = (Py_UCS2)value;
439
8.31M
        Py_UCS2 *to = (Py_UCS2 *)data + start;
440
8.31M
        const Py_UCS2 *end = to + length;
441
74.2M
        for (; to < end; ++to) *to = ch;
442
8.31M
        break;
443
0
    }
444
3.59M
    case PyUnicode_4BYTE_KIND: {
445
3.59M
        assert(value <= MAX_UNICODE);
446
3.59M
        Py_UCS4 ch = value;
447
3.59M
        Py_UCS4 * to = (Py_UCS4 *)data + start;
448
3.59M
        const Py_UCS4 *end = to + length;
449
31.5M
        for (; to < end; ++to) *to = ch;
450
3.59M
        break;
451
0
    }
452
0
    default: Py_UNREACHABLE();
453
16.5M
    }
454
16.5M
}
455
456
457
/* Fast detection of the most frequent whitespace characters */
458
const unsigned char _Py_ascii_whitespace[] = {
459
    0, 0, 0, 0, 0, 0, 0, 0,
460
/*     case 0x0009: * CHARACTER TABULATION */
461
/*     case 0x000A: * LINE FEED */
462
/*     case 0x000B: * LINE TABULATION */
463
/*     case 0x000C: * FORM FEED */
464
/*     case 0x000D: * CARRIAGE RETURN */
465
    0, 1, 1, 1, 1, 1, 0, 0,
466
    0, 0, 0, 0, 0, 0, 0, 0,
467
/*     case 0x001C: * FILE SEPARATOR */
468
/*     case 0x001D: * GROUP SEPARATOR */
469
/*     case 0x001E: * RECORD SEPARATOR */
470
/*     case 0x001F: * UNIT SEPARATOR */
471
    0, 0, 0, 0, 1, 1, 1, 1,
472
/*     case 0x0020: * SPACE */
473
    1, 0, 0, 0, 0, 0, 0, 0,
474
    0, 0, 0, 0, 0, 0, 0, 0,
475
    0, 0, 0, 0, 0, 0, 0, 0,
476
    0, 0, 0, 0, 0, 0, 0, 0,
477
478
    0, 0, 0, 0, 0, 0, 0, 0,
479
    0, 0, 0, 0, 0, 0, 0, 0,
480
    0, 0, 0, 0, 0, 0, 0, 0,
481
    0, 0, 0, 0, 0, 0, 0, 0,
482
    0, 0, 0, 0, 0, 0, 0, 0,
483
    0, 0, 0, 0, 0, 0, 0, 0,
484
    0, 0, 0, 0, 0, 0, 0, 0,
485
    0, 0, 0, 0, 0, 0, 0, 0
486
};
487
488
/* forward */
489
static PyObject* get_latin1_char(unsigned char ch);
490
static int unicode_modifiable(PyObject *unicode);
491
492
493
static PyObject *
494
_PyUnicode_FromUCS1(const Py_UCS1 *s, Py_ssize_t size);
495
static PyObject *
496
_PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size);
497
static PyObject *
498
_PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size);
499
500
static PyObject *
501
unicode_encode_call_errorhandler(const char *errors,
502
       PyObject **errorHandler,const char *encoding, const char *reason,
503
       PyObject *unicode, PyObject **exceptionObject,
504
       Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos);
505
506
static void
507
raise_encode_exception(PyObject **exceptionObject,
508
                       const char *encoding,
509
                       PyObject *unicode,
510
                       Py_ssize_t startpos, Py_ssize_t endpos,
511
                       const char *reason);
512
513
/* Same for linebreaks */
514
static const unsigned char ascii_linebreak[] = {
515
    0, 0, 0, 0, 0, 0, 0, 0,
516
/*         0x000A, * LINE FEED */
517
/*         0x000B, * LINE TABULATION */
518
/*         0x000C, * FORM FEED */
519
/*         0x000D, * CARRIAGE RETURN */
520
    0, 0, 1, 1, 1, 1, 0, 0,
521
    0, 0, 0, 0, 0, 0, 0, 0,
522
/*         0x001C, * FILE SEPARATOR */
523
/*         0x001D, * GROUP SEPARATOR */
524
/*         0x001E, * RECORD SEPARATOR */
525
    0, 0, 0, 0, 1, 1, 1, 0,
526
    0, 0, 0, 0, 0, 0, 0, 0,
527
    0, 0, 0, 0, 0, 0, 0, 0,
528
    0, 0, 0, 0, 0, 0, 0, 0,
529
    0, 0, 0, 0, 0, 0, 0, 0,
530
531
    0, 0, 0, 0, 0, 0, 0, 0,
532
    0, 0, 0, 0, 0, 0, 0, 0,
533
    0, 0, 0, 0, 0, 0, 0, 0,
534
    0, 0, 0, 0, 0, 0, 0, 0,
535
    0, 0, 0, 0, 0, 0, 0, 0,
536
    0, 0, 0, 0, 0, 0, 0, 0,
537
    0, 0, 0, 0, 0, 0, 0, 0,
538
    0, 0, 0, 0, 0, 0, 0, 0
539
};
540
541
static int convert_uc(PyObject *obj, void *addr);
542
543
struct encoding_map;
544
#include "clinic/unicodeobject.c.h"
545
546
_Py_error_handler
547
_Py_GetErrorHandler(const char *errors)
548
601k
{
549
601k
    if (errors == NULL || strcmp(errors, "strict") == 0) {
550
237k
        return _Py_ERROR_STRICT;
551
237k
    }
552
364k
    if (strcmp(errors, "surrogateescape") == 0) {
553
210k
        return _Py_ERROR_SURROGATEESCAPE;
554
210k
    }
555
154k
    if (strcmp(errors, "replace") == 0) {
556
154k
        return _Py_ERROR_REPLACE;
557
154k
    }
558
0
    if (strcmp(errors, "ignore") == 0) {
559
0
        return _Py_ERROR_IGNORE;
560
0
    }
561
0
    if (strcmp(errors, "backslashreplace") == 0) {
562
0
        return _Py_ERROR_BACKSLASHREPLACE;
563
0
    }
564
0
    if (strcmp(errors, "surrogatepass") == 0) {
565
0
        return _Py_ERROR_SURROGATEPASS;
566
0
    }
567
0
    if (strcmp(errors, "xmlcharrefreplace") == 0) {
568
0
        return _Py_ERROR_XMLCHARREFREPLACE;
569
0
    }
570
0
    return _Py_ERROR_OTHER;
571
0
}
572
573
574
static _Py_error_handler
575
get_error_handler_wide(const wchar_t *errors)
576
5.55k
{
577
5.55k
    if (errors == NULL || wcscmp(errors, L"strict") == 0) {
578
0
        return _Py_ERROR_STRICT;
579
0
    }
580
5.55k
    if (wcscmp(errors, L"surrogateescape") == 0) {
581
5.55k
        return _Py_ERROR_SURROGATEESCAPE;
582
5.55k
    }
583
0
    if (wcscmp(errors, L"replace") == 0) {
584
0
        return _Py_ERROR_REPLACE;
585
0
    }
586
0
    if (wcscmp(errors, L"ignore") == 0) {
587
0
        return _Py_ERROR_IGNORE;
588
0
    }
589
0
    if (wcscmp(errors, L"backslashreplace") == 0) {
590
0
        return _Py_ERROR_BACKSLASHREPLACE;
591
0
    }
592
0
    if (wcscmp(errors, L"surrogatepass") == 0) {
593
0
        return _Py_ERROR_SURROGATEPASS;
594
0
    }
595
0
    if (wcscmp(errors, L"xmlcharrefreplace") == 0) {
596
0
        return _Py_ERROR_XMLCHARREFREPLACE;
597
0
    }
598
0
    return _Py_ERROR_OTHER;
599
0
}
600
601
602
static inline int
603
unicode_check_encoding_errors(const char *encoding, const char *errors)
604
21.3M
{
605
21.3M
    if (encoding == NULL && errors == NULL) {
606
11.1M
        return 0;
607
11.1M
    }
608
609
10.1M
    PyInterpreterState *interp = _PyInterpreterState_GET();
610
10.1M
#ifndef Py_DEBUG
611
    /* In release mode, only check in development mode (-X dev) */
612
10.1M
    if (!_PyInterpreterState_GetConfig(interp)->dev_mode) {
613
10.1M
        return 0;
614
10.1M
    }
615
#else
616
    /* Always check in debug mode */
617
#endif
618
619
    /* Avoid calling _PyCodec_Lookup() and PyCodec_LookupError() before the
620
       codec registry is ready: before_PyUnicode_InitEncodings() is called. */
621
0
    if (!interp->unicode.fs_codec.encoding) {
622
0
        return 0;
623
0
    }
624
625
    /* Disable checks during Python finalization. For example, it allows to
626
       call _PyObject_Dump() during finalization for debugging purpose. */
627
0
    if (_PyInterpreterState_GetFinalizing(interp) != NULL) {
628
0
        return 0;
629
0
    }
630
631
0
    if (encoding != NULL
632
        // Fast path for the most common built-in encodings. Even if the codec
633
        // is cached, _PyCodec_Lookup() decodes the bytes string from UTF-8 to
634
        // create a temporary Unicode string (the key in the cache).
635
0
        && strcmp(encoding, "utf-8") != 0
636
0
        && strcmp(encoding, "utf8") != 0
637
0
        && strcmp(encoding, "ascii") != 0)
638
0
    {
639
0
        PyObject *handler = _PyCodec_Lookup(encoding);
640
0
        if (handler == NULL) {
641
0
            return -1;
642
0
        }
643
0
        Py_DECREF(handler);
644
0
    }
645
646
0
    if (errors != NULL
647
        // Fast path for the most common built-in error handlers.
648
0
        && strcmp(errors, "strict") != 0
649
0
        && strcmp(errors, "ignore") != 0
650
0
        && strcmp(errors, "replace") != 0
651
0
        && strcmp(errors, "surrogateescape") != 0
652
0
        && strcmp(errors, "surrogatepass") != 0)
653
0
    {
654
0
        PyObject *handler = PyCodec_LookupError(errors);
655
0
        if (handler == NULL) {
656
0
            return -1;
657
0
        }
658
0
        Py_DECREF(handler);
659
0
    }
660
0
    return 0;
661
0
}
662
663
664
int
665
_PyUnicode_CheckConsistency(PyObject *op, int check_content)
666
0
{
667
0
#define CHECK(expr) \
668
0
    do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
669
670
0
    assert(op != NULL);
671
0
    CHECK(PyUnicode_Check(op));
672
673
0
    PyASCIIObject *ascii = _PyASCIIObject_CAST(op);
674
0
    int kind = ascii->state.kind;
675
676
0
    if (ascii->state.ascii == 1 && ascii->state.compact == 1) {
677
0
        CHECK(kind == PyUnicode_1BYTE_KIND);
678
0
    }
679
0
    else {
680
0
        PyCompactUnicodeObject *compact = _PyCompactUnicodeObject_CAST(op);
681
0
        void *data;
682
683
0
        if (ascii->state.compact == 1) {
684
0
            data = compact + 1;
685
0
            CHECK(kind == PyUnicode_1BYTE_KIND
686
0
                                 || kind == PyUnicode_2BYTE_KIND
687
0
                                 || kind == PyUnicode_4BYTE_KIND);
688
0
            CHECK(ascii->state.ascii == 0);
689
0
            CHECK(_PyUnicode_UTF8(op) != data);
690
0
        }
691
0
        else {
692
0
            PyUnicodeObject *unicode = _PyUnicodeObject_CAST(op);
693
694
0
            data = unicode->data.any;
695
0
            CHECK(kind == PyUnicode_1BYTE_KIND
696
0
                     || kind == PyUnicode_2BYTE_KIND
697
0
                     || kind == PyUnicode_4BYTE_KIND);
698
0
            CHECK(ascii->state.compact == 0);
699
0
            CHECK(data != NULL);
700
0
            if (ascii->state.ascii) {
701
0
                CHECK(_PyUnicode_UTF8(op) == data);
702
0
                CHECK(compact->utf8_length == ascii->length);
703
0
            }
704
0
            else {
705
0
                CHECK(_PyUnicode_UTF8(op) != data);
706
0
            }
707
0
        }
708
0
#ifndef Py_GIL_DISABLED
709
0
        if (_PyUnicode_UTF8(op) == NULL)
710
0
            CHECK(compact->utf8_length == 0);
711
0
#endif
712
0
    }
713
714
    /* check that the best kind is used: O(n) operation */
715
0
    if (check_content) {
716
0
        Py_ssize_t i;
717
0
        Py_UCS4 maxchar = 0;
718
0
        const void *data;
719
0
        Py_UCS4 ch;
720
721
0
        data = PyUnicode_DATA(ascii);
722
0
        for (i=0; i < ascii->length; i++)
723
0
        {
724
0
            ch = PyUnicode_READ(kind, data, i);
725
0
            if (ch > maxchar)
726
0
                maxchar = ch;
727
0
        }
728
0
        if (kind == PyUnicode_1BYTE_KIND) {
729
0
            if (ascii->state.ascii == 0) {
730
0
                CHECK(maxchar >= 128);
731
0
                CHECK(maxchar <= 255);
732
0
            }
733
0
            else
734
0
                CHECK(maxchar < 128);
735
0
        }
736
0
        else if (kind == PyUnicode_2BYTE_KIND) {
737
0
            CHECK(maxchar >= 0x100);
738
0
            CHECK(maxchar <= 0xFFFF);
739
0
        }
740
0
        else {
741
0
            CHECK(maxchar >= 0x10000);
742
0
            CHECK(maxchar <= MAX_UNICODE);
743
0
        }
744
0
        CHECK(PyUnicode_READ(kind, data, ascii->length) == 0);
745
0
    }
746
747
    /* Check interning state */
748
#ifdef Py_DEBUG
749
    // Note that we do not check `_Py_IsImmortal(op)`, since stable ABI
750
    // extensions can make immortal strings mortal (but with a high enough
751
    // refcount).
752
    // The other way is extremely unlikely (worth a potential failed assertion
753
    // in a debug build), so we do check `!_Py_IsImmortal(op)`.
754
    switch (PyUnicode_CHECK_INTERNED(op)) {
755
        case SSTATE_NOT_INTERNED:
756
            if (ascii->state.statically_allocated) {
757
                // This state is for two exceptions:
758
                // - strings are currently checked before they're interned
759
                // - the 256 one-latin1-character strings
760
                //   are static but use SSTATE_NOT_INTERNED
761
            }
762
            else {
763
                CHECK(!_Py_IsImmortal(op));
764
            }
765
            break;
766
        case SSTATE_INTERNED_MORTAL:
767
            CHECK(!ascii->state.statically_allocated);
768
            CHECK(!_Py_IsImmortal(op));
769
            break;
770
        case SSTATE_INTERNED_IMMORTAL:
771
            CHECK(!ascii->state.statically_allocated);
772
            break;
773
        case SSTATE_INTERNED_IMMORTAL_STATIC:
774
            CHECK(ascii->state.statically_allocated);
775
            break;
776
        default:
777
            Py_UNREACHABLE();
778
    }
779
#endif
780
781
0
    return 1;
782
783
0
#undef CHECK
784
0
}
785
786
static PyObject*
787
unicode_result(PyObject *unicode)
788
52.0M
{
789
52.0M
    assert(_PyUnicode_CHECK(unicode));
790
791
52.0M
    Py_ssize_t length = PyUnicode_GET_LENGTH(unicode);
792
52.0M
    if (length == 0) {
793
234
        PyObject *empty = unicode_get_empty();
794
234
        if (unicode != empty) {
795
0
            Py_DECREF(unicode);
796
0
        }
797
234
        return empty;
798
234
    }
799
800
52.0M
    if (length == 1) {
801
314k
        int kind = PyUnicode_KIND(unicode);
802
314k
        if (kind == PyUnicode_1BYTE_KIND) {
803
87.8k
            const Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode);
804
87.8k
            Py_UCS1 ch = data[0];
805
87.8k
            PyObject *latin1_char = LATIN1(ch);
806
87.8k
            if (unicode != latin1_char) {
807
81.7k
                Py_DECREF(unicode);
808
81.7k
            }
809
87.8k
            return latin1_char;
810
87.8k
        }
811
314k
    }
812
813
51.9M
    assert(_PyUnicode_CheckConsistency(unicode, 1));
814
51.9M
    return unicode;
815
52.0M
}
816
817
static PyObject*
818
unicode_result_unchanged(PyObject *unicode)
819
154M
{
820
154M
    if (PyUnicode_CheckExact(unicode)) {
821
151M
        return Py_NewRef(unicode);
822
151M
    }
823
3.24M
    else
824
        /* Subtype -- return genuine unicode string with the same value. */
825
3.24M
        return _PyUnicode_Copy(unicode);
826
154M
}
827
828
/* Implementation of the "backslashreplace" error handler for 8-bit encodings:
829
   ASCII, Latin1, UTF-8, etc. */
830
static char*
831
backslashreplace(_PyBytesWriter *writer, char *str,
832
                 PyObject *unicode, Py_ssize_t collstart, Py_ssize_t collend)
833
0
{
834
0
    Py_ssize_t size, i;
835
0
    Py_UCS4 ch;
836
0
    int kind;
837
0
    const void *data;
838
839
0
    kind = PyUnicode_KIND(unicode);
840
0
    data = PyUnicode_DATA(unicode);
841
842
0
    size = 0;
843
    /* determine replacement size */
844
0
    for (i = collstart; i < collend; ++i) {
845
0
        Py_ssize_t incr;
846
847
0
        ch = PyUnicode_READ(kind, data, i);
848
0
        if (ch < 0x100)
849
0
            incr = 2+2;
850
0
        else if (ch < 0x10000)
851
0
            incr = 2+4;
852
0
        else {
853
0
            assert(ch <= MAX_UNICODE);
854
0
            incr = 2+8;
855
0
        }
856
0
        if (size > PY_SSIZE_T_MAX - incr) {
857
0
            PyErr_SetString(PyExc_OverflowError,
858
0
                            "encoded result is too long for a Python string");
859
0
            return NULL;
860
0
        }
861
0
        size += incr;
862
0
    }
863
864
0
    str = _PyBytesWriter_Prepare(writer, str, size);
865
0
    if (str == NULL)
866
0
        return NULL;
867
868
    /* generate replacement */
869
0
    for (i = collstart; i < collend; ++i) {
870
0
        ch = PyUnicode_READ(kind, data, i);
871
0
        *str++ = '\\';
872
0
        if (ch >= 0x00010000) {
873
0
            *str++ = 'U';
874
0
            *str++ = Py_hexdigits[(ch>>28)&0xf];
875
0
            *str++ = Py_hexdigits[(ch>>24)&0xf];
876
0
            *str++ = Py_hexdigits[(ch>>20)&0xf];
877
0
            *str++ = Py_hexdigits[(ch>>16)&0xf];
878
0
            *str++ = Py_hexdigits[(ch>>12)&0xf];
879
0
            *str++ = Py_hexdigits[(ch>>8)&0xf];
880
0
        }
881
0
        else if (ch >= 0x100) {
882
0
            *str++ = 'u';
883
0
            *str++ = Py_hexdigits[(ch>>12)&0xf];
884
0
            *str++ = Py_hexdigits[(ch>>8)&0xf];
885
0
        }
886
0
        else
887
0
            *str++ = 'x';
888
0
        *str++ = Py_hexdigits[(ch>>4)&0xf];
889
0
        *str++ = Py_hexdigits[ch&0xf];
890
0
    }
891
0
    return str;
892
0
}
893
894
/* Implementation of the "xmlcharrefreplace" error handler for 8-bit encodings:
895
   ASCII, Latin1, UTF-8, etc. */
896
static char*
897
xmlcharrefreplace(_PyBytesWriter *writer, char *str,
898
                  PyObject *unicode, Py_ssize_t collstart, Py_ssize_t collend)
899
0
{
900
0
    Py_ssize_t size, i;
901
0
    Py_UCS4 ch;
902
0
    int kind;
903
0
    const void *data;
904
905
0
    kind = PyUnicode_KIND(unicode);
906
0
    data = PyUnicode_DATA(unicode);
907
908
0
    size = 0;
909
    /* determine replacement size */
910
0
    for (i = collstart; i < collend; ++i) {
911
0
        Py_ssize_t incr;
912
913
0
        ch = PyUnicode_READ(kind, data, i);
914
0
        if (ch < 10)
915
0
            incr = 2+1+1;
916
0
        else if (ch < 100)
917
0
            incr = 2+2+1;
918
0
        else if (ch < 1000)
919
0
            incr = 2+3+1;
920
0
        else if (ch < 10000)
921
0
            incr = 2+4+1;
922
0
        else if (ch < 100000)
923
0
            incr = 2+5+1;
924
0
        else if (ch < 1000000)
925
0
            incr = 2+6+1;
926
0
        else {
927
0
            assert(ch <= MAX_UNICODE);
928
0
            incr = 2+7+1;
929
0
        }
930
0
        if (size > PY_SSIZE_T_MAX - incr) {
931
0
            PyErr_SetString(PyExc_OverflowError,
932
0
                            "encoded result is too long for a Python string");
933
0
            return NULL;
934
0
        }
935
0
        size += incr;
936
0
    }
937
938
0
    str = _PyBytesWriter_Prepare(writer, str, size);
939
0
    if (str == NULL)
940
0
        return NULL;
941
942
    /* generate replacement */
943
0
    for (i = collstart; i < collend; ++i) {
944
0
        size = sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i));
945
0
        if (size < 0) {
946
0
            return NULL;
947
0
        }
948
0
        str += size;
949
0
    }
950
0
    return str;
951
0
}
952
953
/* --- Bloom Filters ----------------------------------------------------- */
954
955
/* stuff to implement simple "bloom filters" for Unicode characters.
956
   to keep things simple, we use a single bitmask, using the least 5
957
   bits from each unicode characters as the bit index. */
958
959
/* the linebreak mask is set up by _PyUnicode_Init() below */
960
961
#if LONG_BIT >= 128
962
#define BLOOM_WIDTH 128
963
#elif LONG_BIT >= 64
964
43.5M
#define BLOOM_WIDTH 64
965
#elif LONG_BIT >= 32
966
#define BLOOM_WIDTH 32
967
#else
968
#error "LONG_BIT is smaller than 32"
969
#endif
970
971
17.9M
#define BLOOM_MASK unsigned long
972
973
static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0;
974
975
61.6M
#define BLOOM(mask, ch)     ((mask &  (1UL << ((ch) & (BLOOM_WIDTH - 1)))))
976
977
#define BLOOM_LINEBREAK(ch)                                             \
978
256M
    ((ch) < 128U ? ascii_linebreak[(ch)] :                              \
979
256M
     (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch)))
980
981
static inline BLOOM_MASK
982
make_bloom_mask(int kind, const void* ptr, Py_ssize_t len)
983
8.95M
{
984
8.95M
#define BLOOM_UPDATE(TYPE, MASK, PTR, LEN)             \
985
8.95M
    do {                                               \
986
8.95M
        TYPE *data = (TYPE *)PTR;                      \
987
8.95M
        TYPE *end = data + LEN;                        \
988
8.95M
        Py_UCS4 ch;                                    \
989
19.7M
        for (; data != end; data++) {                  \
990
10.7M
            ch = *data;                                \
991
10.7M
            MASK |= (1UL << (ch & (BLOOM_WIDTH - 1))); \
992
10.7M
        }                                              \
993
8.95M
        break;                                         \
994
8.95M
    } while (0)
995
996
    /* calculate simple bloom-style bitmask for a given unicode string */
997
998
8.95M
    BLOOM_MASK mask;
999
1000
8.95M
    mask = 0;
1001
8.95M
    switch (kind) {
1002
8.95M
    case PyUnicode_1BYTE_KIND:
1003
8.95M
        BLOOM_UPDATE(Py_UCS1, mask, ptr, len);
1004
0
        break;
1005
16
    case PyUnicode_2BYTE_KIND:
1006
16
        BLOOM_UPDATE(Py_UCS2, mask, ptr, len);
1007
0
        break;
1008
0
    case PyUnicode_4BYTE_KIND:
1009
0
        BLOOM_UPDATE(Py_UCS4, mask, ptr, len);
1010
0
        break;
1011
0
    default:
1012
0
        Py_UNREACHABLE();
1013
8.95M
    }
1014
8.95M
    return mask;
1015
1016
8.95M
#undef BLOOM_UPDATE
1017
8.95M
}
1018
1019
static int
1020
ensure_unicode(PyObject *obj)
1021
169M
{
1022
169M
    if (!PyUnicode_Check(obj)) {
1023
0
        PyErr_Format(PyExc_TypeError,
1024
0
                     "must be str, not %.100s",
1025
0
                     Py_TYPE(obj)->tp_name);
1026
0
        return -1;
1027
0
    }
1028
169M
    return 0;
1029
169M
}
1030
1031
/* Compilation of templated routines */
1032
1033
1.06M
#define STRINGLIB_GET_EMPTY() unicode_get_empty()
1034
1035
#include "stringlib/asciilib.h"
1036
#include "stringlib/fastsearch.h"
1037
#include "stringlib/partition.h"
1038
#include "stringlib/split.h"
1039
#include "stringlib/count.h"
1040
#include "stringlib/find.h"
1041
#include "stringlib/find_max_char.h"
1042
#include "stringlib/undef.h"
1043
1044
#include "stringlib/ucs1lib.h"
1045
#include "stringlib/fastsearch.h"
1046
#include "stringlib/partition.h"
1047
#include "stringlib/split.h"
1048
#include "stringlib/count.h"
1049
#include "stringlib/find.h"
1050
#include "stringlib/replace.h"
1051
#include "stringlib/repr.h"
1052
#include "stringlib/find_max_char.h"
1053
#include "stringlib/undef.h"
1054
1055
#include "stringlib/ucs2lib.h"
1056
#include "stringlib/fastsearch.h"
1057
#include "stringlib/partition.h"
1058
#include "stringlib/split.h"
1059
#include "stringlib/count.h"
1060
#include "stringlib/find.h"
1061
#include "stringlib/replace.h"
1062
#include "stringlib/repr.h"
1063
#include "stringlib/find_max_char.h"
1064
#include "stringlib/undef.h"
1065
1066
#include "stringlib/ucs4lib.h"
1067
#include "stringlib/fastsearch.h"
1068
#include "stringlib/partition.h"
1069
#include "stringlib/split.h"
1070
#include "stringlib/count.h"
1071
#include "stringlib/find.h"
1072
#include "stringlib/replace.h"
1073
#include "stringlib/repr.h"
1074
#include "stringlib/find_max_char.h"
1075
#include "stringlib/undef.h"
1076
1077
#undef STRINGLIB_GET_EMPTY
1078
1079
/* --- Unicode Object ----------------------------------------------------- */
1080
1081
static inline Py_ssize_t
1082
findchar(const void *s, int kind,
1083
         Py_ssize_t size, Py_UCS4 ch,
1084
         int direction)
1085
110M
{
1086
110M
    switch (kind) {
1087
102M
    case PyUnicode_1BYTE_KIND:
1088
102M
        if ((Py_UCS1) ch != ch)
1089
3.59k
            return -1;
1090
102M
        if (direction > 0)
1091
102M
            return ucs1lib_find_char((const Py_UCS1 *) s, size, (Py_UCS1) ch);
1092
7.90k
        else
1093
7.90k
            return ucs1lib_rfind_char((const Py_UCS1 *) s, size, (Py_UCS1) ch);
1094
7.68M
    case PyUnicode_2BYTE_KIND:
1095
7.68M
        if ((Py_UCS2) ch != ch)
1096
0
            return -1;
1097
7.68M
        if (direction > 0)
1098
7.64M
            return ucs2lib_find_char((const Py_UCS2 *) s, size, (Py_UCS2) ch);
1099
35.6k
        else
1100
35.6k
            return ucs2lib_rfind_char((const Py_UCS2 *) s, size, (Py_UCS2) ch);
1101
824k
    case PyUnicode_4BYTE_KIND:
1102
824k
        if (direction > 0)
1103
708k
            return ucs4lib_find_char((const Py_UCS4 *) s, size, ch);
1104
115k
        else
1105
115k
            return ucs4lib_rfind_char((const Py_UCS4 *) s, size, ch);
1106
0
    default:
1107
0
        Py_UNREACHABLE();
1108
110M
    }
1109
110M
}
1110
1111
#ifdef Py_DEBUG
1112
/* Fill the data of a Unicode string with invalid characters to detect bugs
1113
   earlier.
1114
1115
   _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for
1116
   ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an
1117
   invalid character in Unicode 6.0. */
1118
static void
1119
unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length)
1120
{
1121
    int kind = PyUnicode_KIND(unicode);
1122
    Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode);
1123
    Py_ssize_t length = _PyUnicode_LENGTH(unicode);
1124
    if (length <= old_length)
1125
        return;
1126
    memset(data + old_length * kind, 0xff, (length - old_length) * kind);
1127
}
1128
#endif
1129
1130
static PyObject*
1131
resize_copy(PyObject *unicode, Py_ssize_t length)
1132
0
{
1133
0
    Py_ssize_t copy_length;
1134
0
    PyObject *copy;
1135
1136
0
    copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode));
1137
0
    if (copy == NULL)
1138
0
        return NULL;
1139
1140
0
    copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode));
1141
0
    _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length);
1142
0
    return copy;
1143
0
}
1144
1145
static PyObject*
1146
resize_compact(PyObject *unicode, Py_ssize_t length)
1147
63.2M
{
1148
63.2M
    Py_ssize_t char_size;
1149
63.2M
    Py_ssize_t struct_size;
1150
63.2M
    Py_ssize_t new_size;
1151
63.2M
    PyObject *new_unicode;
1152
#ifdef Py_DEBUG
1153
    Py_ssize_t old_length = _PyUnicode_LENGTH(unicode);
1154
#endif
1155
1156
63.2M
    if (!unicode_modifiable(unicode)) {
1157
0
        PyObject *copy = resize_copy(unicode, length);
1158
0
        if (copy == NULL) {
1159
0
            return NULL;
1160
0
        }
1161
0
        Py_DECREF(unicode);
1162
0
        return copy;
1163
0
    }
1164
63.2M
    assert(PyUnicode_IS_COMPACT(unicode));
1165
1166
63.2M
    char_size = PyUnicode_KIND(unicode);
1167
63.2M
    if (PyUnicode_IS_ASCII(unicode))
1168
54.6M
        struct_size = sizeof(PyASCIIObject);
1169
8.66M
    else
1170
8.66M
        struct_size = sizeof(PyCompactUnicodeObject);
1171
1172
63.2M
    if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) {
1173
0
        PyErr_NoMemory();
1174
0
        return NULL;
1175
0
    }
1176
63.2M
    new_size = (struct_size + (length + 1) * char_size);
1177
1178
63.2M
    if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) {
1179
0
        PyMem_Free(_PyUnicode_UTF8(unicode));
1180
0
        PyUnicode_SET_UTF8_LENGTH(unicode, 0);
1181
0
        PyUnicode_SET_UTF8(unicode, NULL);
1182
0
    }
1183
#ifdef Py_TRACE_REFS
1184
    _Py_ForgetReference(unicode);
1185
#endif
1186
63.2M
    _PyReftracerTrack(unicode, PyRefTracer_DESTROY);
1187
1188
63.2M
    new_unicode = (PyObject *)PyObject_Realloc(unicode, new_size);
1189
63.2M
    if (new_unicode == NULL) {
1190
0
        _Py_NewReferenceNoTotal(unicode);
1191
0
        PyErr_NoMemory();
1192
0
        return NULL;
1193
0
    }
1194
63.2M
    unicode = new_unicode;
1195
63.2M
    _Py_NewReferenceNoTotal(unicode);
1196
1197
63.2M
    _PyUnicode_LENGTH(unicode) = length;
1198
#ifdef Py_DEBUG
1199
    unicode_fill_invalid(unicode, old_length);
1200
#endif
1201
63.2M
    PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
1202
63.2M
                    length, 0);
1203
63.2M
    assert(_PyUnicode_CheckConsistency(unicode, 0));
1204
63.2M
    return unicode;
1205
63.2M
}
1206
1207
static int
1208
resize_inplace(PyObject *unicode, Py_ssize_t length)
1209
0
{
1210
0
    assert(!PyUnicode_IS_COMPACT(unicode));
1211
0
    assert(Py_REFCNT(unicode) == 1);
1212
1213
0
    Py_ssize_t new_size;
1214
0
    Py_ssize_t char_size;
1215
0
    int share_utf8;
1216
0
    void *data;
1217
#ifdef Py_DEBUG
1218
    Py_ssize_t old_length = _PyUnicode_LENGTH(unicode);
1219
#endif
1220
1221
0
    data = _PyUnicode_DATA_ANY(unicode);
1222
0
    char_size = PyUnicode_KIND(unicode);
1223
0
    share_utf8 = _PyUnicode_SHARE_UTF8(unicode);
1224
1225
0
    if (length > (PY_SSIZE_T_MAX / char_size - 1)) {
1226
0
        PyErr_NoMemory();
1227
0
        return -1;
1228
0
    }
1229
0
    new_size = (length + 1) * char_size;
1230
1231
0
    if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode))
1232
0
    {
1233
0
        PyMem_Free(_PyUnicode_UTF8(unicode));
1234
0
        PyUnicode_SET_UTF8_LENGTH(unicode, 0);
1235
0
        PyUnicode_SET_UTF8(unicode, NULL);
1236
0
    }
1237
1238
0
    data = (PyObject *)PyObject_Realloc(data, new_size);
1239
0
    if (data == NULL) {
1240
0
        PyErr_NoMemory();
1241
0
        return -1;
1242
0
    }
1243
0
    _PyUnicode_DATA_ANY(unicode) = data;
1244
0
    if (share_utf8) {
1245
0
        PyUnicode_SET_UTF8_LENGTH(unicode, length);
1246
0
        PyUnicode_SET_UTF8(unicode, data);
1247
0
    }
1248
0
    _PyUnicode_LENGTH(unicode) = length;
1249
0
    PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0);
1250
#ifdef Py_DEBUG
1251
    unicode_fill_invalid(unicode, old_length);
1252
#endif
1253
1254
    /* check for integer overflow */
1255
0
    if (length > PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(wchar_t) - 1) {
1256
0
        PyErr_NoMemory();
1257
0
        return -1;
1258
0
    }
1259
0
    assert(_PyUnicode_CheckConsistency(unicode, 0));
1260
0
    return 0;
1261
0
}
1262
1263
static const char*
1264
unicode_kind_name(PyObject *unicode)
1265
0
{
1266
    /* don't check consistency: unicode_kind_name() is called from
1267
       _PyUnicode_Dump() */
1268
0
    if (!PyUnicode_IS_COMPACT(unicode))
1269
0
    {
1270
0
        switch (PyUnicode_KIND(unicode))
1271
0
        {
1272
0
        case PyUnicode_1BYTE_KIND:
1273
0
            if (PyUnicode_IS_ASCII(unicode))
1274
0
                return "legacy ascii";
1275
0
            else
1276
0
                return "legacy latin1";
1277
0
        case PyUnicode_2BYTE_KIND:
1278
0
            return "legacy UCS2";
1279
0
        case PyUnicode_4BYTE_KIND:
1280
0
            return "legacy UCS4";
1281
0
        default:
1282
0
            return "<legacy invalid kind>";
1283
0
        }
1284
0
    }
1285
0
    switch (PyUnicode_KIND(unicode)) {
1286
0
    case PyUnicode_1BYTE_KIND:
1287
0
        if (PyUnicode_IS_ASCII(unicode))
1288
0
            return "ascii";
1289
0
        else
1290
0
            return "latin1";
1291
0
    case PyUnicode_2BYTE_KIND:
1292
0
        return "UCS2";
1293
0
    case PyUnicode_4BYTE_KIND:
1294
0
        return "UCS4";
1295
0
    default:
1296
0
        return "<invalid compact kind>";
1297
0
    }
1298
0
}
1299
1300
#ifdef Py_DEBUG
1301
/* Functions wrapping macros for use in debugger */
1302
const char *_PyUnicode_utf8(void *unicode_raw){
1303
    PyObject *unicode = _PyObject_CAST(unicode_raw);
1304
    return PyUnicode_UTF8(unicode);
1305
}
1306
1307
const void *_PyUnicode_compact_data(void *unicode_raw) {
1308
    PyObject *unicode = _PyObject_CAST(unicode_raw);
1309
    return _PyUnicode_COMPACT_DATA(unicode);
1310
}
1311
const void *_PyUnicode_data(void *unicode_raw) {
1312
    PyObject *unicode = _PyObject_CAST(unicode_raw);
1313
    printf("obj %p\n", (void*)unicode);
1314
    printf("compact %d\n", PyUnicode_IS_COMPACT(unicode));
1315
    printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode));
1316
    printf("ascii op %p\n", (void*)(_PyASCIIObject_CAST(unicode) + 1));
1317
    printf("compact op %p\n", (void*)(_PyCompactUnicodeObject_CAST(unicode) + 1));
1318
    printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode));
1319
    return PyUnicode_DATA(unicode);
1320
}
1321
1322
void
1323
_PyUnicode_Dump(PyObject *op)
1324
{
1325
    PyASCIIObject *ascii = _PyASCIIObject_CAST(op);
1326
    PyCompactUnicodeObject *compact = _PyCompactUnicodeObject_CAST(op);
1327
    PyUnicodeObject *unicode = _PyUnicodeObject_CAST(op);
1328
    const void *data;
1329
1330
    if (ascii->state.compact)
1331
    {
1332
        if (ascii->state.ascii)
1333
            data = (ascii + 1);
1334
        else
1335
            data = (compact + 1);
1336
    }
1337
    else
1338
        data = unicode->data.any;
1339
    printf("%s: len=%zu, ", unicode_kind_name(op), ascii->length);
1340
1341
    if (!ascii->state.ascii) {
1342
        printf("utf8=%p (%zu)", (void *)compact->utf8, compact->utf8_length);
1343
    }
1344
    printf(", data=%p\n", data);
1345
}
1346
#endif
1347
1348
1349
PyObject *
1350
PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)
1351
528M
{
1352
    /* Optimization for empty strings */
1353
528M
    if (size == 0) {
1354
30.2M
        return unicode_get_empty();
1355
30.2M
    }
1356
1357
498M
    PyObject *obj;
1358
498M
    PyCompactUnicodeObject *unicode;
1359
498M
    void *data;
1360
498M
    int kind;
1361
498M
    int is_ascii;
1362
498M
    Py_ssize_t char_size;
1363
498M
    Py_ssize_t struct_size;
1364
1365
498M
    is_ascii = 0;
1366
498M
    struct_size = sizeof(PyCompactUnicodeObject);
1367
498M
    if (maxchar < 128) {
1368
283M
        kind = PyUnicode_1BYTE_KIND;
1369
283M
        char_size = 1;
1370
283M
        is_ascii = 1;
1371
283M
        struct_size = sizeof(PyASCIIObject);
1372
283M
    }
1373
215M
    else if (maxchar < 256) {
1374
24.3M
        kind = PyUnicode_1BYTE_KIND;
1375
24.3M
        char_size = 1;
1376
24.3M
    }
1377
190M
    else if (maxchar < 65536) {
1378
182M
        kind = PyUnicode_2BYTE_KIND;
1379
182M
        char_size = 2;
1380
182M
    }
1381
8.50M
    else {
1382
8.50M
        if (maxchar > MAX_UNICODE) {
1383
0
            PyErr_SetString(PyExc_SystemError,
1384
0
                            "invalid maximum character passed to PyUnicode_New");
1385
0
            return NULL;
1386
0
        }
1387
8.50M
        kind = PyUnicode_4BYTE_KIND;
1388
8.50M
        char_size = 4;
1389
8.50M
    }
1390
1391
    /* Ensure we won't overflow the size. */
1392
498M
    if (size < 0) {
1393
0
        PyErr_SetString(PyExc_SystemError,
1394
0
                        "Negative size passed to PyUnicode_New");
1395
0
        return NULL;
1396
0
    }
1397
498M
    if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1))
1398
0
        return PyErr_NoMemory();
1399
1400
    /* Duplicated allocation code from _PyObject_New() instead of a call to
1401
     * PyObject_New() so we are able to allocate space for the object and
1402
     * it's data buffer.
1403
     */
1404
498M
    obj = (PyObject *) PyObject_Malloc(struct_size + (size + 1) * char_size);
1405
498M
    if (obj == NULL) {
1406
0
        return PyErr_NoMemory();
1407
0
    }
1408
498M
    _PyObject_Init(obj, &PyUnicode_Type);
1409
1410
498M
    unicode = (PyCompactUnicodeObject *)obj;
1411
498M
    if (is_ascii)
1412
283M
        data = ((PyASCIIObject*)obj) + 1;
1413
215M
    else
1414
215M
        data = unicode + 1;
1415
498M
    _PyUnicode_LENGTH(unicode) = size;
1416
498M
    _PyUnicode_HASH(unicode) = -1;
1417
498M
    _PyUnicode_STATE(unicode).interned = 0;
1418
498M
    _PyUnicode_STATE(unicode).kind = kind;
1419
498M
    _PyUnicode_STATE(unicode).compact = 1;
1420
498M
    _PyUnicode_STATE(unicode).ascii = is_ascii;
1421
498M
    _PyUnicode_STATE(unicode).statically_allocated = 0;
1422
498M
    if (is_ascii) {
1423
283M
        ((char*)data)[size] = 0;
1424
283M
    }
1425
215M
    else if (kind == PyUnicode_1BYTE_KIND) {
1426
24.3M
        ((char*)data)[size] = 0;
1427
24.3M
        unicode->utf8 = NULL;
1428
24.3M
        unicode->utf8_length = 0;
1429
24.3M
    }
1430
190M
    else {
1431
190M
        unicode->utf8 = NULL;
1432
190M
        unicode->utf8_length = 0;
1433
190M
        if (kind == PyUnicode_2BYTE_KIND)
1434
182M
            ((Py_UCS2*)data)[size] = 0;
1435
8.50M
        else /* kind == PyUnicode_4BYTE_KIND */
1436
8.50M
            ((Py_UCS4*)data)[size] = 0;
1437
190M
    }
1438
#ifdef Py_DEBUG
1439
    unicode_fill_invalid((PyObject*)unicode, 0);
1440
#endif
1441
498M
    assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0));
1442
498M
    return obj;
1443
498M
}
1444
1445
static int
1446
unicode_check_modifiable(PyObject *unicode)
1447
756
{
1448
756
    if (!unicode_modifiable(unicode)) {
1449
0
        PyErr_SetString(PyExc_SystemError,
1450
0
                        "Cannot modify a string currently used");
1451
0
        return -1;
1452
0
    }
1453
756
    return 0;
1454
756
}
1455
1456
static int
1457
_copy_characters(PyObject *to, Py_ssize_t to_start,
1458
                 PyObject *from, Py_ssize_t from_start,
1459
                 Py_ssize_t how_many, int check_maxchar)
1460
278M
{
1461
278M
    int from_kind, to_kind;
1462
278M
    const void *from_data;
1463
278M
    void *to_data;
1464
1465
278M
    assert(0 <= how_many);
1466
278M
    assert(0 <= from_start);
1467
278M
    assert(0 <= to_start);
1468
278M
    assert(PyUnicode_Check(from));
1469
278M
    assert(from_start + how_many <= PyUnicode_GET_LENGTH(from));
1470
1471
278M
    assert(to == NULL || PyUnicode_Check(to));
1472
1473
278M
    if (how_many == 0) {
1474
272k
        return 0;
1475
272k
    }
1476
1477
278M
    assert(to != NULL);
1478
278M
    assert(to_start + how_many <= PyUnicode_GET_LENGTH(to));
1479
1480
278M
    from_kind = PyUnicode_KIND(from);
1481
278M
    from_data = PyUnicode_DATA(from);
1482
278M
    to_kind = PyUnicode_KIND(to);
1483
278M
    to_data = PyUnicode_DATA(to);
1484
1485
#ifdef Py_DEBUG
1486
    if (!check_maxchar
1487
        && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to))
1488
    {
1489
        Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to);
1490
        Py_UCS4 ch;
1491
        Py_ssize_t i;
1492
        for (i=0; i < how_many; i++) {
1493
            ch = PyUnicode_READ(from_kind, from_data, from_start + i);
1494
            assert(ch <= to_maxchar);
1495
        }
1496
    }
1497
#endif
1498
1499
278M
    if (from_kind == to_kind) {
1500
176M
        if (check_maxchar
1501
176M
            && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))
1502
0
        {
1503
            /* Writing Latin-1 characters into an ASCII string requires to
1504
               check that all written characters are pure ASCII */
1505
0
            Py_UCS4 max_char;
1506
0
            max_char = ucs1lib_find_max_char(from_data,
1507
0
                                             (const Py_UCS1*)from_data + how_many);
1508
0
            if (max_char >= 128)
1509
0
                return -1;
1510
0
        }
1511
176M
        memcpy((char*)to_data + to_kind * to_start,
1512
176M
                  (const char*)from_data + from_kind * from_start,
1513
176M
                  to_kind * how_many);
1514
176M
    }
1515
102M
    else if (from_kind == PyUnicode_1BYTE_KIND
1516
102M
             && to_kind == PyUnicode_2BYTE_KIND)
1517
84.0M
    {
1518
84.0M
        _PyUnicode_CONVERT_BYTES(
1519
84.0M
            Py_UCS1, Py_UCS2,
1520
84.0M
            PyUnicode_1BYTE_DATA(from) + from_start,
1521
84.0M
            PyUnicode_1BYTE_DATA(from) + from_start + how_many,
1522
84.0M
            PyUnicode_2BYTE_DATA(to) + to_start
1523
84.0M
            );
1524
84.0M
    }
1525
18.0M
    else if (from_kind == PyUnicode_1BYTE_KIND
1526
18.0M
             && to_kind == PyUnicode_4BYTE_KIND)
1527
15.9M
    {
1528
15.9M
        _PyUnicode_CONVERT_BYTES(
1529
15.9M
            Py_UCS1, Py_UCS4,
1530
15.9M
            PyUnicode_1BYTE_DATA(from) + from_start,
1531
15.9M
            PyUnicode_1BYTE_DATA(from) + from_start + how_many,
1532
15.9M
            PyUnicode_4BYTE_DATA(to) + to_start
1533
15.9M
            );
1534
15.9M
    }
1535
2.07M
    else if (from_kind == PyUnicode_2BYTE_KIND
1536
2.07M
             && to_kind == PyUnicode_4BYTE_KIND)
1537
2.04M
    {
1538
2.04M
        _PyUnicode_CONVERT_BYTES(
1539
2.04M
            Py_UCS2, Py_UCS4,
1540
2.04M
            PyUnicode_2BYTE_DATA(from) + from_start,
1541
2.04M
            PyUnicode_2BYTE_DATA(from) + from_start + how_many,
1542
2.04M
            PyUnicode_4BYTE_DATA(to) + to_start
1543
2.04M
            );
1544
2.04M
    }
1545
24.1k
    else {
1546
24.1k
        assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to));
1547
1548
24.1k
        if (!check_maxchar) {
1549
24.1k
            if (from_kind == PyUnicode_2BYTE_KIND
1550
24.1k
                && to_kind == PyUnicode_1BYTE_KIND)
1551
2.16k
            {
1552
2.16k
                _PyUnicode_CONVERT_BYTES(
1553
2.16k
                    Py_UCS2, Py_UCS1,
1554
2.16k
                    PyUnicode_2BYTE_DATA(from) + from_start,
1555
2.16k
                    PyUnicode_2BYTE_DATA(from) + from_start + how_many,
1556
2.16k
                    PyUnicode_1BYTE_DATA(to) + to_start
1557
2.16k
                    );
1558
2.16k
            }
1559
21.9k
            else if (from_kind == PyUnicode_4BYTE_KIND
1560
21.9k
                     && to_kind == PyUnicode_1BYTE_KIND)
1561
9.97k
            {
1562
9.97k
                _PyUnicode_CONVERT_BYTES(
1563
9.97k
                    Py_UCS4, Py_UCS1,
1564
9.97k
                    PyUnicode_4BYTE_DATA(from) + from_start,
1565
9.97k
                    PyUnicode_4BYTE_DATA(from) + from_start + how_many,
1566
9.97k
                    PyUnicode_1BYTE_DATA(to) + to_start
1567
9.97k
                    );
1568
9.97k
            }
1569
12.0k
            else if (from_kind == PyUnicode_4BYTE_KIND
1570
12.0k
                     && to_kind == PyUnicode_2BYTE_KIND)
1571
12.0k
            {
1572
12.0k
                _PyUnicode_CONVERT_BYTES(
1573
12.0k
                    Py_UCS4, Py_UCS2,
1574
12.0k
                    PyUnicode_4BYTE_DATA(from) + from_start,
1575
12.0k
                    PyUnicode_4BYTE_DATA(from) + from_start + how_many,
1576
12.0k
                    PyUnicode_2BYTE_DATA(to) + to_start
1577
12.0k
                    );
1578
12.0k
            }
1579
0
            else {
1580
0
                Py_UNREACHABLE();
1581
0
            }
1582
24.1k
        }
1583
0
        else {
1584
0
            const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to);
1585
0
            Py_UCS4 ch;
1586
0
            Py_ssize_t i;
1587
1588
0
            for (i=0; i < how_many; i++) {
1589
0
                ch = PyUnicode_READ(from_kind, from_data, from_start + i);
1590
0
                if (ch > to_maxchar)
1591
0
                    return -1;
1592
0
                PyUnicode_WRITE(to_kind, to_data, to_start + i, ch);
1593
0
            }
1594
0
        }
1595
24.1k
    }
1596
278M
    return 0;
1597
278M
}
1598
1599
void
1600
_PyUnicode_FastCopyCharacters(
1601
    PyObject *to, Py_ssize_t to_start,
1602
    PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many)
1603
278M
{
1604
278M
    (void)_copy_characters(to, to_start, from, from_start, how_many, 0);
1605
278M
}
1606
1607
Py_ssize_t
1608
PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start,
1609
                         PyObject *from, Py_ssize_t from_start,
1610
                         Py_ssize_t how_many)
1611
0
{
1612
0
    int err;
1613
1614
0
    if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) {
1615
0
        PyErr_BadInternalCall();
1616
0
        return -1;
1617
0
    }
1618
1619
0
    if ((size_t)from_start > (size_t)PyUnicode_GET_LENGTH(from)) {
1620
0
        PyErr_SetString(PyExc_IndexError, "string index out of range");
1621
0
        return -1;
1622
0
    }
1623
0
    if ((size_t)to_start > (size_t)PyUnicode_GET_LENGTH(to)) {
1624
0
        PyErr_SetString(PyExc_IndexError, "string index out of range");
1625
0
        return -1;
1626
0
    }
1627
0
    if (how_many < 0) {
1628
0
        PyErr_SetString(PyExc_SystemError, "how_many cannot be negative");
1629
0
        return -1;
1630
0
    }
1631
0
    how_many = Py_MIN(PyUnicode_GET_LENGTH(from)-from_start, how_many);
1632
0
    if (to_start + how_many > PyUnicode_GET_LENGTH(to)) {
1633
0
        PyErr_Format(PyExc_SystemError,
1634
0
                     "Cannot write %zi characters at %zi "
1635
0
                     "in a string of %zi characters",
1636
0
                     how_many, to_start, PyUnicode_GET_LENGTH(to));
1637
0
        return -1;
1638
0
    }
1639
1640
0
    if (how_many == 0)
1641
0
        return 0;
1642
1643
0
    if (unicode_check_modifiable(to))
1644
0
        return -1;
1645
1646
0
    err = _copy_characters(to, to_start, from, from_start, how_many, 1);
1647
0
    if (err) {
1648
0
        PyErr_Format(PyExc_SystemError,
1649
0
                     "Cannot copy %s characters "
1650
0
                     "into a string of %s characters",
1651
0
                     unicode_kind_name(from),
1652
0
                     unicode_kind_name(to));
1653
0
        return -1;
1654
0
    }
1655
0
    return how_many;
1656
0
}
1657
1658
/* Find the maximum code point and count the number of surrogate pairs so a
1659
   correct string length can be computed before converting a string to UCS4.
1660
   This function counts single surrogates as a character and not as a pair.
1661
1662
   Return 0 on success, or -1 on error. */
1663
static int
1664
find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end,
1665
                        Py_UCS4 *maxchar, Py_ssize_t *num_surrogates)
1666
17.9k
{
1667
17.9k
    const wchar_t *iter;
1668
17.9k
    Py_UCS4 ch;
1669
1670
17.9k
    assert(num_surrogates != NULL && maxchar != NULL);
1671
17.9k
    *num_surrogates = 0;
1672
17.9k
    *maxchar = 0;
1673
1674
402k
    for (iter = begin; iter < end; ) {
1675
#if SIZEOF_WCHAR_T == 2
1676
        if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0])
1677
            && (iter+1) < end
1678
            && Py_UNICODE_IS_LOW_SURROGATE(iter[1]))
1679
        {
1680
            ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]);
1681
            ++(*num_surrogates);
1682
            iter += 2;
1683
        }
1684
        else
1685
#endif
1686
384k
        {
1687
384k
            ch = *iter;
1688
384k
            iter++;
1689
384k
        }
1690
384k
        if (ch > *maxchar) {
1691
78.6k
            *maxchar = ch;
1692
78.6k
            if (*maxchar > MAX_UNICODE) {
1693
0
                PyErr_Format(PyExc_ValueError,
1694
0
                             "character U+%x is not in range [U+0000; U+%x]",
1695
0
                             ch, MAX_UNICODE);
1696
0
                return -1;
1697
0
            }
1698
78.6k
        }
1699
384k
    }
1700
17.9k
    return 0;
1701
17.9k
}
1702
1703
static void
1704
unicode_dealloc(PyObject *unicode)
1705
508M
{
1706
#ifdef Py_DEBUG
1707
    if (!unicode_is_finalizing() && unicode_is_singleton(unicode)) {
1708
        _Py_FatalRefcountError("deallocating an Unicode singleton");
1709
    }
1710
#endif
1711
508M
    if (_PyUnicode_STATE(unicode).statically_allocated) {
1712
        /* This should never get called, but we also don't want to SEGV if
1713
        * we accidentally decref an immortal string out of existence. Since
1714
        * the string is an immortal object, just re-set the reference count.
1715
        */
1716
#ifdef Py_DEBUG
1717
        Py_UNREACHABLE();
1718
#endif
1719
0
        _Py_SetImmortal(unicode);
1720
0
        return;
1721
0
    }
1722
508M
    switch (_PyUnicode_STATE(unicode).interned) {
1723
508M
        case SSTATE_NOT_INTERNED:
1724
508M
            break;
1725
531k
        case SSTATE_INTERNED_MORTAL:
1726
            /* Remove the object from the intern dict.
1727
             * Before doing so, we set the refcount to 2: the key and value
1728
             * in the interned_dict.
1729
             */
1730
531k
            assert(Py_REFCNT(unicode) == 0);
1731
531k
            Py_SET_REFCNT(unicode, 2);
1732
#ifdef Py_REF_DEBUG
1733
            /* let's be pedantic with the ref total */
1734
            _Py_IncRefTotal(_PyThreadState_GET());
1735
            _Py_IncRefTotal(_PyThreadState_GET());
1736
#endif
1737
531k
            PyInterpreterState *interp = _PyInterpreterState_GET();
1738
531k
            PyObject *interned = get_interned_dict(interp);
1739
531k
            assert(interned != NULL);
1740
531k
            PyObject *popped;
1741
531k
            int r = PyDict_Pop(interned, unicode, &popped);
1742
531k
            if (r == -1) {
1743
0
                PyErr_FormatUnraisable("Exception ignored while "
1744
0
                                       "removing an interned string %R",
1745
0
                                       unicode);
1746
                // We don't know what happened to the string. It's probably
1747
                // best to leak it:
1748
                // - if it was popped, there are no more references to it
1749
                //   so it can't cause trouble (except wasted memory)
1750
                // - if it wasn't popped, it'll remain interned
1751
0
                _Py_SetImmortal(unicode);
1752
0
                _PyUnicode_STATE(unicode).interned = SSTATE_INTERNED_IMMORTAL;
1753
0
                return;
1754
0
            }
1755
531k
            if (r == 0) {
1756
                // The interned string was not found in the interned_dict.
1757
#ifdef Py_DEBUG
1758
                Py_UNREACHABLE();
1759
#endif
1760
0
                _Py_SetImmortal(unicode);
1761
0
                return;
1762
0
            }
1763
            // Successfully popped.
1764
531k
            assert(popped == unicode);
1765
            // Only our `popped` reference should be left; remove it too.
1766
531k
            assert(Py_REFCNT(unicode) == 1);
1767
531k
            Py_SET_REFCNT(unicode, 0);
1768
#ifdef Py_REF_DEBUG
1769
            /* let's be pedantic with the ref total */
1770
            _Py_DecRefTotal(_PyThreadState_GET());
1771
#endif
1772
531k
            break;
1773
0
        default:
1774
            // As with `statically_allocated` above.
1775
#ifdef Py_REF_DEBUG
1776
            Py_UNREACHABLE();
1777
#endif
1778
0
            _Py_SetImmortal(unicode);
1779
0
            return;
1780
508M
    }
1781
508M
    if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) {
1782
159k
        PyMem_Free(_PyUnicode_UTF8(unicode));
1783
159k
    }
1784
508M
    if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) {
1785
10.9M
        PyMem_Free(_PyUnicode_DATA_ANY(unicode));
1786
10.9M
    }
1787
1788
508M
    Py_TYPE(unicode)->tp_free(unicode);
1789
508M
}
1790
1791
#ifdef Py_DEBUG
1792
static int
1793
unicode_is_singleton(PyObject *unicode)
1794
{
1795
    if (unicode == &_Py_STR(empty)) {
1796
        return 1;
1797
    }
1798
1799
    PyASCIIObject *ascii = _PyASCIIObject_CAST(unicode);
1800
    if (ascii->length == 1) {
1801
        Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0);
1802
        if (ch < 256 && LATIN1(ch) == unicode) {
1803
            return 1;
1804
        }
1805
    }
1806
    return 0;
1807
}
1808
#endif
1809
1810
static int
1811
unicode_modifiable(PyObject *unicode)
1812
64.8M
{
1813
64.8M
    assert(_PyUnicode_CHECK(unicode));
1814
64.8M
    if (!_PyObject_IsUniquelyReferenced(unicode))
1815
40.9k
        return 0;
1816
64.8M
    if (PyUnicode_HASH(unicode) != -1)
1817
0
        return 0;
1818
64.8M
    if (PyUnicode_CHECK_INTERNED(unicode))
1819
0
        return 0;
1820
64.8M
    if (!PyUnicode_CheckExact(unicode))
1821
0
        return 0;
1822
#ifdef Py_DEBUG
1823
    /* singleton refcount is greater than 1 */
1824
    assert(!unicode_is_singleton(unicode));
1825
#endif
1826
64.8M
    return 1;
1827
64.8M
}
1828
1829
static int
1830
unicode_resize(PyObject **p_unicode, Py_ssize_t length)
1831
760k
{
1832
760k
    PyObject *unicode;
1833
760k
    Py_ssize_t old_length;
1834
1835
760k
    assert(p_unicode != NULL);
1836
760k
    unicode = *p_unicode;
1837
1838
760k
    assert(unicode != NULL);
1839
760k
    assert(PyUnicode_Check(unicode));
1840
760k
    assert(0 <= length);
1841
1842
760k
    old_length = PyUnicode_GET_LENGTH(unicode);
1843
760k
    if (old_length == length)
1844
0
        return 0;
1845
1846
760k
    if (length == 0) {
1847
0
        PyObject *empty = unicode_get_empty();
1848
0
        Py_SETREF(*p_unicode, empty);
1849
0
        return 0;
1850
0
    }
1851
1852
760k
    if (!unicode_modifiable(unicode)) {
1853
0
        PyObject *copy = resize_copy(unicode, length);
1854
0
        if (copy == NULL)
1855
0
            return -1;
1856
0
        Py_SETREF(*p_unicode, copy);
1857
0
        return 0;
1858
0
    }
1859
1860
760k
    if (PyUnicode_IS_COMPACT(unicode)) {
1861
760k
        PyObject *new_unicode = resize_compact(unicode, length);
1862
760k
        if (new_unicode == NULL)
1863
0
            return -1;
1864
760k
        *p_unicode = new_unicode;
1865
760k
        return 0;
1866
760k
    }
1867
0
    return resize_inplace(unicode, length);
1868
760k
}
1869
1870
int
1871
PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length)
1872
0
{
1873
0
    PyObject *unicode;
1874
0
    if (p_unicode == NULL) {
1875
0
        PyErr_BadInternalCall();
1876
0
        return -1;
1877
0
    }
1878
0
    unicode = *p_unicode;
1879
0
    if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0)
1880
0
    {
1881
0
        PyErr_BadInternalCall();
1882
0
        return -1;
1883
0
    }
1884
0
    return unicode_resize(p_unicode, length);
1885
0
}
1886
1887
/* Copy an ASCII or latin1 char* string into a Python Unicode string.
1888
1889
   WARNING: The function doesn't copy the terminating null character and
1890
   doesn't check the maximum character (may write a latin1 character in an
1891
   ASCII string). */
1892
static void
1893
unicode_write_cstr(PyObject *unicode, Py_ssize_t index,
1894
                   const char *str, Py_ssize_t len)
1895
0
{
1896
0
    int kind = PyUnicode_KIND(unicode);
1897
0
    const void *data = PyUnicode_DATA(unicode);
1898
0
    const char *end = str + len;
1899
1900
0
    assert(index + len <= PyUnicode_GET_LENGTH(unicode));
1901
0
    switch (kind) {
1902
0
    case PyUnicode_1BYTE_KIND: {
1903
#ifdef Py_DEBUG
1904
        if (PyUnicode_IS_ASCII(unicode)) {
1905
            Py_UCS4 maxchar = ucs1lib_find_max_char(
1906
                (const Py_UCS1*)str,
1907
                (const Py_UCS1*)str + len);
1908
            assert(maxchar < 128);
1909
        }
1910
#endif
1911
0
        memcpy((char *) data + index, str, len);
1912
0
        break;
1913
0
    }
1914
0
    case PyUnicode_2BYTE_KIND: {
1915
0
        Py_UCS2 *start = (Py_UCS2 *)data + index;
1916
0
        Py_UCS2 *ucs2 = start;
1917
1918
0
        for (; str < end; ++ucs2, ++str)
1919
0
            *ucs2 = (Py_UCS2)*str;
1920
1921
0
        assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode));
1922
0
        break;
1923
0
    }
1924
0
    case PyUnicode_4BYTE_KIND: {
1925
0
        Py_UCS4 *start = (Py_UCS4 *)data + index;
1926
0
        Py_UCS4 *ucs4 = start;
1927
1928
0
        for (; str < end; ++ucs4, ++str)
1929
0
            *ucs4 = (Py_UCS4)*str;
1930
1931
0
        assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode));
1932
0
        break;
1933
0
    }
1934
0
    default:
1935
0
        Py_UNREACHABLE();
1936
0
    }
1937
0
}
1938
1939
static PyObject*
1940
get_latin1_char(Py_UCS1 ch)
1941
263M
{
1942
263M
    PyObject *o = LATIN1(ch);
1943
263M
    return o;
1944
263M
}
1945
1946
static PyObject*
1947
unicode_char(Py_UCS4 ch)
1948
323M
{
1949
323M
    PyObject *unicode;
1950
1951
323M
    assert(ch <= MAX_UNICODE);
1952
1953
323M
    if (ch < 256) {
1954
201M
        return get_latin1_char(ch);
1955
201M
    }
1956
1957
122M
    unicode = PyUnicode_New(1, ch);
1958
122M
    if (unicode == NULL)
1959
0
        return NULL;
1960
1961
122M
    assert(PyUnicode_KIND(unicode) != PyUnicode_1BYTE_KIND);
1962
122M
    if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) {
1963
116M
        PyUnicode_2BYTE_DATA(unicode)[0] = (Py_UCS2)ch;
1964
116M
    } else {
1965
5.71M
        assert(PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND);
1966
5.71M
        PyUnicode_4BYTE_DATA(unicode)[0] = ch;
1967
5.71M
    }
1968
122M
    assert(_PyUnicode_CheckConsistency(unicode, 1));
1969
122M
    return unicode;
1970
122M
}
1971
1972
1973
static inline void
1974
unicode_write_widechar(int kind, void *data,
1975
                       const wchar_t *u, Py_ssize_t size,
1976
                       Py_ssize_t num_surrogates)
1977
17.9k
{
1978
17.9k
    switch (kind) {
1979
17.9k
    case PyUnicode_1BYTE_KIND:
1980
17.9k
        _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, u, u + size, data);
1981
17.9k
        break;
1982
1983
0
    case PyUnicode_2BYTE_KIND:
1984
#if SIZEOF_WCHAR_T == 2
1985
        memcpy(data, u, size * 2);
1986
#else
1987
0
        _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, u, u + size, data);
1988
0
#endif
1989
0
        break;
1990
1991
0
    case PyUnicode_4BYTE_KIND:
1992
0
    {
1993
#if SIZEOF_WCHAR_T == 2
1994
        // Convert a 16-bits wchar_t representation to UCS4, this will decode
1995
        // surrogate pairs.
1996
        const wchar_t *end = u + size;
1997
        Py_UCS4 *ucs4_out = (Py_UCS4*)data;
1998
#  ifndef NDEBUG
1999
        Py_UCS4 *ucs4_end = (Py_UCS4*)data + (size - num_surrogates);
2000
#  endif
2001
        for (const wchar_t *iter = u; iter < end; ) {
2002
            assert(ucs4_out < ucs4_end);
2003
            if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0])
2004
                && (iter+1) < end
2005
                && Py_UNICODE_IS_LOW_SURROGATE(iter[1]))
2006
            {
2007
                *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]);
2008
                iter += 2;
2009
            }
2010
            else {
2011
                *ucs4_out++ = *iter;
2012
                iter++;
2013
            }
2014
        }
2015
        assert(ucs4_out == ucs4_end);
2016
#else
2017
0
        assert(num_surrogates == 0);
2018
0
        memcpy(data, u, size * 4);
2019
0
#endif
2020
0
        break;
2021
0
    }
2022
0
    default:
2023
0
        Py_UNREACHABLE();
2024
17.9k
    }
2025
17.9k
}
2026
2027
2028
PyObject *
2029
PyUnicode_FromWideChar(const wchar_t *u, Py_ssize_t size)
2030
18.0k
{
2031
18.0k
    PyObject *unicode;
2032
18.0k
    Py_UCS4 maxchar = 0;
2033
18.0k
    Py_ssize_t num_surrogates;
2034
2035
18.0k
    if (u == NULL && size != 0) {
2036
0
        PyErr_BadInternalCall();
2037
0
        return NULL;
2038
0
    }
2039
2040
18.0k
    if (size == -1) {
2041
576
        size = wcslen(u);
2042
576
    }
2043
2044
    /* If the Unicode data is known at construction time, we can apply
2045
       some optimizations which share commonly used objects. */
2046
2047
    /* Optimization for empty strings */
2048
18.0k
    if (size == 0)
2049
32
        _Py_RETURN_UNICODE_EMPTY();
2050
2051
#ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION
2052
    /* Oracle Solaris uses non-Unicode internal wchar_t form for
2053
       non-Unicode locales and hence needs conversion to UCS-4 first. */
2054
    if (_Py_LocaleUsesNonUnicodeWchar()) {
2055
        wchar_t* converted = _Py_DecodeNonUnicodeWchar(u, size);
2056
        if (!converted) {
2057
            return NULL;
2058
        }
2059
        PyObject *unicode = _PyUnicode_FromUCS4(converted, size);
2060
        PyMem_Free(converted);
2061
        return unicode;
2062
    }
2063
#endif
2064
2065
    /* Single character Unicode objects in the Latin-1 range are
2066
       shared when using this constructor */
2067
17.9k
    if (size == 1 && (Py_UCS4)*u < 256)
2068
0
        return get_latin1_char((unsigned char)*u);
2069
2070
    /* If not empty and not single character, copy the Unicode data
2071
       into the new object */
2072
17.9k
    if (find_maxchar_surrogates(u, u + size,
2073
17.9k
                                &maxchar, &num_surrogates) == -1)
2074
0
        return NULL;
2075
2076
17.9k
    unicode = PyUnicode_New(size - num_surrogates, maxchar);
2077
17.9k
    if (!unicode)
2078
0
        return NULL;
2079
2080
17.9k
    unicode_write_widechar(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
2081
17.9k
                           u, size, num_surrogates);
2082
2083
17.9k
    return unicode_result(unicode);
2084
17.9k
}
2085
2086
2087
int
2088
PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *pub_writer,
2089
                              const wchar_t *str,
2090
                              Py_ssize_t size)
2091
0
{
2092
0
    _PyUnicodeWriter *writer = (_PyUnicodeWriter *)pub_writer;
2093
2094
0
    if (size < 0) {
2095
0
        size = wcslen(str);
2096
0
    }
2097
2098
0
    if (size == 0) {
2099
0
        return 0;
2100
0
    }
2101
2102
#ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION
2103
    /* Oracle Solaris uses non-Unicode internal wchar_t form for
2104
       non-Unicode locales and hence needs conversion to UCS-4 first. */
2105
    if (_Py_LocaleUsesNonUnicodeWchar()) {
2106
        wchar_t* converted = _Py_DecodeNonUnicodeWchar(str, size);
2107
        if (!converted) {
2108
            return -1;
2109
        }
2110
2111
        int res = PyUnicodeWriter_WriteUCS4(pub_writer, converted, size);
2112
        PyMem_Free(converted);
2113
        return res;
2114
    }
2115
#endif
2116
2117
0
    Py_UCS4 maxchar = 0;
2118
0
    Py_ssize_t num_surrogates;
2119
0
    if (find_maxchar_surrogates(str, str + size,
2120
0
                                &maxchar, &num_surrogates) == -1) {
2121
0
        return -1;
2122
0
    }
2123
2124
0
    if (_PyUnicodeWriter_Prepare(writer, size - num_surrogates, maxchar) < 0) {
2125
0
        return -1;
2126
0
    }
2127
2128
0
    int kind = writer->kind;
2129
0
    void *data = (Py_UCS1*)writer->data + writer->pos * kind;
2130
0
    unicode_write_widechar(kind, data, str, size, num_surrogates);
2131
2132
0
    writer->pos += size - num_surrogates;
2133
0
    return 0;
2134
0
}
2135
2136
2137
PyObject *
2138
PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
2139
665k
{
2140
665k
    if (size < 0) {
2141
0
        PyErr_SetString(PyExc_SystemError,
2142
0
                        "Negative size passed to PyUnicode_FromStringAndSize");
2143
0
        return NULL;
2144
0
    }
2145
665k
    if (u != NULL) {
2146
665k
        return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL);
2147
665k
    }
2148
0
    if (size > 0) {
2149
0
        PyErr_SetString(PyExc_SystemError,
2150
0
            "NULL string with positive size with NULL passed to PyUnicode_FromStringAndSize");
2151
0
        return NULL;
2152
0
    }
2153
0
    return unicode_get_empty();
2154
0
}
2155
2156
PyObject *
2157
PyUnicode_FromString(const char *u)
2158
7.45M
{
2159
7.45M
    size_t size = strlen(u);
2160
7.45M
    if (size > PY_SSIZE_T_MAX) {
2161
0
        PyErr_SetString(PyExc_OverflowError, "input too long");
2162
0
        return NULL;
2163
0
    }
2164
7.45M
    return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL);
2165
7.45M
}
2166
2167
2168
PyObject *
2169
_PyUnicode_FromId(_Py_Identifier *id)
2170
0
{
2171
0
    PyMutex_Lock((PyMutex *)&id->mutex);
2172
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
2173
0
    struct _Py_unicode_ids *ids = &interp->unicode.ids;
2174
2175
0
    Py_ssize_t index = _Py_atomic_load_ssize(&id->index);
2176
0
    if (index < 0) {
2177
0
        struct _Py_unicode_runtime_ids *rt_ids = &interp->runtime->unicode_state.ids;
2178
2179
0
        PyMutex_Lock(&rt_ids->mutex);
2180
        // Check again to detect concurrent access. Another thread can have
2181
        // initialized the index while this thread waited for the lock.
2182
0
        index = _Py_atomic_load_ssize(&id->index);
2183
0
        if (index < 0) {
2184
0
            assert(rt_ids->next_index < PY_SSIZE_T_MAX);
2185
0
            index = rt_ids->next_index;
2186
0
            rt_ids->next_index++;
2187
0
            _Py_atomic_store_ssize(&id->index, index);
2188
0
        }
2189
0
        PyMutex_Unlock(&rt_ids->mutex);
2190
0
    }
2191
0
    assert(index >= 0);
2192
2193
0
    PyObject *obj;
2194
0
    if (index < ids->size) {
2195
0
        obj = ids->array[index];
2196
0
        if (obj) {
2197
            // Return a borrowed reference
2198
0
            goto end;
2199
0
        }
2200
0
    }
2201
2202
0
    obj = PyUnicode_DecodeUTF8Stateful(id->string, strlen(id->string),
2203
0
                                       NULL, NULL);
2204
0
    if (!obj) {
2205
0
        goto end;
2206
0
    }
2207
0
    _PyUnicode_InternImmortal(interp, &obj);
2208
2209
0
    if (index >= ids->size) {
2210
        // Overallocate to reduce the number of realloc
2211
0
        Py_ssize_t new_size = Py_MAX(index * 2, 16);
2212
0
        Py_ssize_t item_size = sizeof(ids->array[0]);
2213
0
        PyObject **new_array = PyMem_Realloc(ids->array, new_size * item_size);
2214
0
        if (new_array == NULL) {
2215
0
            PyErr_NoMemory();
2216
0
            obj = NULL;
2217
0
            goto end;
2218
0
        }
2219
0
        memset(&new_array[ids->size], 0, (new_size - ids->size) * item_size);
2220
0
        ids->array = new_array;
2221
0
        ids->size = new_size;
2222
0
    }
2223
2224
    // The array stores a strong reference
2225
0
    ids->array[index] = obj;
2226
2227
0
end:
2228
0
    PyMutex_Unlock((PyMutex *)&id->mutex);
2229
    // Return a borrowed reference
2230
0
    return obj;
2231
0
}
2232
2233
2234
static void
2235
unicode_clear_identifiers(struct _Py_unicode_state *state)
2236
0
{
2237
0
    struct _Py_unicode_ids *ids = &state->ids;
2238
0
    for (Py_ssize_t i=0; i < ids->size; i++) {
2239
0
        Py_XDECREF(ids->array[i]);
2240
0
    }
2241
0
    ids->size = 0;
2242
0
    PyMem_Free(ids->array);
2243
0
    ids->array = NULL;
2244
    // Don't reset _PyRuntime next_index: _Py_Identifier.id remains valid
2245
    // after Py_Finalize().
2246
0
}
2247
2248
2249
/* Internal function, doesn't check maximum character */
2250
2251
PyObject*
2252
_PyUnicode_FromASCII(const char *buffer, Py_ssize_t size)
2253
102M
{
2254
102M
    const unsigned char *s = (const unsigned char *)buffer;
2255
102M
    PyObject *unicode;
2256
102M
    if (size == 1) {
2257
#ifdef Py_DEBUG
2258
        assert((unsigned char)s[0] < 128);
2259
#endif
2260
38.1M
        return get_latin1_char(s[0]);
2261
38.1M
    }
2262
63.9M
    unicode = PyUnicode_New(size, 127);
2263
63.9M
    if (!unicode)
2264
0
        return NULL;
2265
63.9M
    memcpy(PyUnicode_1BYTE_DATA(unicode), s, size);
2266
63.9M
    assert(_PyUnicode_CheckConsistency(unicode, 1));
2267
63.9M
    return unicode;
2268
63.9M
}
2269
2270
static Py_UCS4
2271
kind_maxchar_limit(int kind)
2272
0
{
2273
0
    switch (kind) {
2274
0
    case PyUnicode_1BYTE_KIND:
2275
0
        return 0x80;
2276
0
    case PyUnicode_2BYTE_KIND:
2277
0
        return 0x100;
2278
0
    case PyUnicode_4BYTE_KIND:
2279
0
        return 0x10000;
2280
0
    default:
2281
0
        Py_UNREACHABLE();
2282
0
    }
2283
0
}
2284
2285
static PyObject*
2286
_PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size)
2287
63.4M
{
2288
63.4M
    PyObject *res;
2289
63.4M
    unsigned char max_char;
2290
2291
63.4M
    if (size == 0) {
2292
4.55M
        _Py_RETURN_UNICODE_EMPTY();
2293
4.55M
    }
2294
58.9M
    assert(size > 0);
2295
58.9M
    if (size == 1) {
2296
22.6M
        return get_latin1_char(u[0]);
2297
22.6M
    }
2298
2299
36.2M
    max_char = ucs1lib_find_max_char(u, u + size);
2300
36.2M
    res = PyUnicode_New(size, max_char);
2301
36.2M
    if (!res)
2302
0
        return NULL;
2303
36.2M
    memcpy(PyUnicode_1BYTE_DATA(res), u, size);
2304
36.2M
    assert(_PyUnicode_CheckConsistency(res, 1));
2305
36.2M
    return res;
2306
36.2M
}
2307
2308
static PyObject*
2309
_PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size)
2310
103M
{
2311
103M
    PyObject *res;
2312
103M
    Py_UCS2 max_char;
2313
2314
103M
    if (size == 0)
2315
9.99M
        _Py_RETURN_UNICODE_EMPTY();
2316
93.1M
    assert(size > 0);
2317
93.1M
    if (size == 1)
2318
63.1M
        return unicode_char(u[0]);
2319
2320
29.9M
    max_char = ucs2lib_find_max_char(u, u + size);
2321
29.9M
    res = PyUnicode_New(size, max_char);
2322
29.9M
    if (!res)
2323
0
        return NULL;
2324
29.9M
    if (max_char >= 256)
2325
17.5M
        memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size);
2326
12.4M
    else {
2327
12.4M
        _PyUnicode_CONVERT_BYTES(
2328
12.4M
            Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res));
2329
12.4M
    }
2330
29.9M
    assert(_PyUnicode_CheckConsistency(res, 1));
2331
29.9M
    return res;
2332
29.9M
}
2333
2334
static PyObject*
2335
_PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size)
2336
80.2M
{
2337
80.2M
    PyObject *res;
2338
80.2M
    Py_UCS4 max_char;
2339
2340
80.2M
    if (size == 0)
2341
9.04M
        _Py_RETURN_UNICODE_EMPTY();
2342
71.2M
    assert(size > 0);
2343
71.2M
    if (size == 1)
2344
51.6M
        return unicode_char(u[0]);
2345
2346
19.6M
    max_char = ucs4lib_find_max_char(u, u + size);
2347
19.6M
    res = PyUnicode_New(size, max_char);
2348
19.6M
    if (!res)
2349
0
        return NULL;
2350
19.6M
    if (max_char < 256)
2351
13.8M
        _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size,
2352
19.6M
                                 PyUnicode_1BYTE_DATA(res));
2353
5.79M
    else if (max_char < 0x10000)
2354
3.80M
        _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size,
2355
5.79M
                                 PyUnicode_2BYTE_DATA(res));
2356
1.99M
    else
2357
1.99M
        memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size);
2358
19.6M
    assert(_PyUnicode_CheckConsistency(res, 1));
2359
19.6M
    return res;
2360
19.6M
}
2361
2362
2363
int
2364
PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *pub_writer,
2365
                          Py_UCS4 *str,
2366
                          Py_ssize_t size)
2367
0
{
2368
0
    _PyUnicodeWriter *writer = (_PyUnicodeWriter*)pub_writer;
2369
2370
0
    if (size < 0) {
2371
0
        PyErr_SetString(PyExc_ValueError,
2372
0
                        "size must be positive");
2373
0
        return -1;
2374
0
    }
2375
2376
0
    if (size == 0) {
2377
0
        return 0;
2378
0
    }
2379
2380
0
    Py_UCS4 max_char = ucs4lib_find_max_char(str, str + size);
2381
2382
0
    if (_PyUnicodeWriter_Prepare(writer, size, max_char) < 0) {
2383
0
        return -1;
2384
0
    }
2385
2386
0
    int kind = writer->kind;
2387
0
    void *data = (Py_UCS1*)writer->data + writer->pos * kind;
2388
0
    if (kind == PyUnicode_1BYTE_KIND) {
2389
0
        _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1,
2390
0
                                 str, str + size,
2391
0
                                 data);
2392
0
    }
2393
0
    else if (kind == PyUnicode_2BYTE_KIND) {
2394
0
        _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2,
2395
0
                                 str, str + size,
2396
0
                                 data);
2397
0
    }
2398
0
    else {
2399
0
        memcpy(data, str, size * sizeof(Py_UCS4));
2400
0
    }
2401
0
    writer->pos += size;
2402
2403
0
    return 0;
2404
0
}
2405
2406
2407
PyObject*
2408
PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size)
2409
201M
{
2410
201M
    if (size < 0) {
2411
0
        PyErr_SetString(PyExc_ValueError, "size must be positive");
2412
0
        return NULL;
2413
0
    }
2414
201M
    switch (kind) {
2415
43.5M
    case PyUnicode_1BYTE_KIND:
2416
43.5M
        return _PyUnicode_FromUCS1(buffer, size);
2417
89.8M
    case PyUnicode_2BYTE_KIND:
2418
89.8M
        return _PyUnicode_FromUCS2(buffer, size);
2419
67.5M
    case PyUnicode_4BYTE_KIND:
2420
67.5M
        return _PyUnicode_FromUCS4(buffer, size);
2421
0
    default:
2422
0
        PyErr_SetString(PyExc_SystemError, "invalid kind");
2423
0
        return NULL;
2424
201M
    }
2425
201M
}
2426
2427
Py_UCS4
2428
_PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end)
2429
15.3M
{
2430
15.3M
    int kind;
2431
15.3M
    const void *startptr, *endptr;
2432
2433
15.3M
    assert(0 <= start);
2434
15.3M
    assert(end <= PyUnicode_GET_LENGTH(unicode));
2435
15.3M
    assert(start <= end);
2436
2437
15.3M
    if (start == 0 && end == PyUnicode_GET_LENGTH(unicode))
2438
0
        return PyUnicode_MAX_CHAR_VALUE(unicode);
2439
2440
15.3M
    if (start == end)
2441
0
        return 127;
2442
2443
15.3M
    if (PyUnicode_IS_ASCII(unicode))
2444
15.3M
        return 127;
2445
2446
37.1k
    kind = PyUnicode_KIND(unicode);
2447
37.1k
    startptr = PyUnicode_DATA(unicode);
2448
37.1k
    endptr = (char *)startptr + end * kind;
2449
37.1k
    startptr = (char *)startptr + start * kind;
2450
37.1k
    switch(kind) {
2451
1.63k
    case PyUnicode_1BYTE_KIND:
2452
1.63k
        return ucs1lib_find_max_char(startptr, endptr);
2453
8.57k
    case PyUnicode_2BYTE_KIND:
2454
8.57k
        return ucs2lib_find_max_char(startptr, endptr);
2455
26.9k
    case PyUnicode_4BYTE_KIND:
2456
26.9k
        return ucs4lib_find_max_char(startptr, endptr);
2457
0
    default:
2458
0
        Py_UNREACHABLE();
2459
37.1k
    }
2460
37.1k
}
2461
2462
/* Ensure that a string uses the most efficient storage, if it is not the
2463
   case: create a new string with of the right kind. Write NULL into *p_unicode
2464
   on error. */
2465
static void
2466
unicode_adjust_maxchar(PyObject **p_unicode)
2467
0
{
2468
0
    PyObject *unicode, *copy;
2469
0
    Py_UCS4 max_char;
2470
0
    Py_ssize_t len;
2471
0
    int kind;
2472
2473
0
    assert(p_unicode != NULL);
2474
0
    unicode = *p_unicode;
2475
0
    if (PyUnicode_IS_ASCII(unicode))
2476
0
        return;
2477
2478
0
    len = PyUnicode_GET_LENGTH(unicode);
2479
0
    kind = PyUnicode_KIND(unicode);
2480
0
    if (kind == PyUnicode_1BYTE_KIND) {
2481
0
        const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode);
2482
0
        max_char = ucs1lib_find_max_char(u, u + len);
2483
0
        if (max_char >= 128)
2484
0
            return;
2485
0
    }
2486
0
    else if (kind == PyUnicode_2BYTE_KIND) {
2487
0
        const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode);
2488
0
        max_char = ucs2lib_find_max_char(u, u + len);
2489
0
        if (max_char >= 256)
2490
0
            return;
2491
0
    }
2492
0
    else if (kind == PyUnicode_4BYTE_KIND) {
2493
0
        const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode);
2494
0
        max_char = ucs4lib_find_max_char(u, u + len);
2495
0
        if (max_char >= 0x10000)
2496
0
            return;
2497
0
    }
2498
0
    else
2499
0
        Py_UNREACHABLE();
2500
2501
0
    copy = PyUnicode_New(len, max_char);
2502
0
    if (copy != NULL)
2503
0
        _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len);
2504
0
    Py_DECREF(unicode);
2505
0
    *p_unicode = copy;
2506
0
}
2507
2508
PyObject*
2509
_PyUnicode_Copy(PyObject *unicode)
2510
3.24M
{
2511
3.24M
    Py_ssize_t length;
2512
3.24M
    PyObject *copy;
2513
2514
3.24M
    if (!PyUnicode_Check(unicode)) {
2515
0
        PyErr_BadInternalCall();
2516
0
        return NULL;
2517
0
    }
2518
2519
3.24M
    length = PyUnicode_GET_LENGTH(unicode);
2520
3.24M
    copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode));
2521
3.24M
    if (!copy)
2522
0
        return NULL;
2523
3.24M
    assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode));
2524
2525
3.24M
    memcpy(PyUnicode_DATA(copy), PyUnicode_DATA(unicode),
2526
3.24M
              length * PyUnicode_KIND(unicode));
2527
3.24M
    assert(_PyUnicode_CheckConsistency(copy, 1));
2528
3.24M
    return copy;
2529
3.24M
}
2530
2531
2532
/* Widen Unicode objects to larger buffers. Don't write terminating null
2533
   character. Return NULL on error. */
2534
2535
static void*
2536
unicode_askind(int skind, void const *data, Py_ssize_t len, int kind)
2537
10.9M
{
2538
10.9M
    void *result;
2539
2540
10.9M
    assert(skind < kind);
2541
10.9M
    switch (kind) {
2542
10.0M
    case PyUnicode_2BYTE_KIND:
2543
10.0M
        result = PyMem_New(Py_UCS2, len);
2544
10.0M
        if (!result)
2545
0
            return PyErr_NoMemory();
2546
10.0M
        assert(skind == PyUnicode_1BYTE_KIND);
2547
10.0M
        _PyUnicode_CONVERT_BYTES(
2548
10.0M
            Py_UCS1, Py_UCS2,
2549
10.0M
            (const Py_UCS1 *)data,
2550
10.0M
            ((const Py_UCS1 *)data) + len,
2551
10.0M
            result);
2552
10.0M
        return result;
2553
930k
    case PyUnicode_4BYTE_KIND:
2554
930k
        result = PyMem_New(Py_UCS4, len);
2555
930k
        if (!result)
2556
0
            return PyErr_NoMemory();
2557
930k
        if (skind == PyUnicode_2BYTE_KIND) {
2558
0
            _PyUnicode_CONVERT_BYTES(
2559
0
                Py_UCS2, Py_UCS4,
2560
0
                (const Py_UCS2 *)data,
2561
0
                ((const Py_UCS2 *)data) + len,
2562
0
                result);
2563
0
        }
2564
930k
        else {
2565
930k
            assert(skind == PyUnicode_1BYTE_KIND);
2566
930k
            _PyUnicode_CONVERT_BYTES(
2567
930k
                Py_UCS1, Py_UCS4,
2568
930k
                (const Py_UCS1 *)data,
2569
930k
                ((const Py_UCS1 *)data) + len,
2570
930k
                result);
2571
930k
        }
2572
930k
        return result;
2573
0
    default:
2574
0
        Py_UNREACHABLE();
2575
0
        return NULL;
2576
10.9M
    }
2577
10.9M
}
2578
2579
static Py_UCS4*
2580
as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize,
2581
        int copy_null)
2582
76.0k
{
2583
76.0k
    int kind;
2584
76.0k
    const void *data;
2585
76.0k
    Py_ssize_t len, targetlen;
2586
76.0k
    kind = PyUnicode_KIND(string);
2587
76.0k
    data = PyUnicode_DATA(string);
2588
76.0k
    len = PyUnicode_GET_LENGTH(string);
2589
76.0k
    targetlen = len;
2590
76.0k
    if (copy_null)
2591
0
        targetlen++;
2592
76.0k
    if (!target) {
2593
0
        target = PyMem_New(Py_UCS4, targetlen);
2594
0
        if (!target) {
2595
0
            PyErr_NoMemory();
2596
0
            return NULL;
2597
0
        }
2598
0
    }
2599
76.0k
    else {
2600
76.0k
        if (targetsize < targetlen) {
2601
0
            PyErr_Format(PyExc_SystemError,
2602
0
                         "string is longer than the buffer");
2603
0
            if (copy_null && 0 < targetsize)
2604
0
                target[0] = 0;
2605
0
            return NULL;
2606
0
        }
2607
76.0k
    }
2608
76.0k
    if (kind == PyUnicode_1BYTE_KIND) {
2609
55.9k
        const Py_UCS1 *start = (const Py_UCS1 *) data;
2610
55.9k
        _PyUnicode_CONVERT_BYTES(Py_UCS1, Py_UCS4, start, start + len, target);
2611
55.9k
    }
2612
20.0k
    else if (kind == PyUnicode_2BYTE_KIND) {
2613
15.4k
        const Py_UCS2 *start = (const Py_UCS2 *) data;
2614
15.4k
        _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target);
2615
15.4k
    }
2616
4.65k
    else if (kind == PyUnicode_4BYTE_KIND) {
2617
4.65k
        memcpy(target, data, len * sizeof(Py_UCS4));
2618
4.65k
    }
2619
0
    else {
2620
0
        Py_UNREACHABLE();
2621
0
    }
2622
76.0k
    if (copy_null)
2623
0
        target[len] = 0;
2624
76.0k
    return target;
2625
76.0k
}
2626
2627
Py_UCS4*
2628
PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize,
2629
                 int copy_null)
2630
76.0k
{
2631
76.0k
    if (target == NULL || targetsize < 0) {
2632
0
        PyErr_BadInternalCall();
2633
0
        return NULL;
2634
0
    }
2635
76.0k
    return as_ucs4(string, target, targetsize, copy_null);
2636
76.0k
}
2637
2638
Py_UCS4*
2639
PyUnicode_AsUCS4Copy(PyObject *string)
2640
0
{
2641
0
    return as_ucs4(string, NULL, 0, 1);
2642
0
}
2643
2644
/* maximum number of characters required for output of %jo or %jd or %p.
2645
   We need at most ceil(log8(256)*sizeof(intmax_t)) digits,
2646
   plus 1 for the sign, plus 2 for the 0x prefix (for %p),
2647
   plus 1 for the terminal NUL. */
2648
#define MAX_INTMAX_CHARS (5 + (sizeof(intmax_t)*8-1) / 3)
2649
2650
static int
2651
unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str,
2652
                             Py_ssize_t width, Py_ssize_t precision, int flags)
2653
15.5k
{
2654
15.5k
    Py_ssize_t length, fill, arglen;
2655
15.5k
    Py_UCS4 maxchar;
2656
2657
15.5k
    length = PyUnicode_GET_LENGTH(str);
2658
15.5k
    if ((precision == -1 || precision >= length)
2659
15.5k
        && width <= length)
2660
15.4k
        return _PyUnicodeWriter_WriteStr(writer, str);
2661
2662
50
    if (precision != -1)
2663
50
        length = Py_MIN(precision, length);
2664
2665
50
    arglen = Py_MAX(length, width);
2666
50
    if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar)
2667
29
        maxchar = _PyUnicode_FindMaxChar(str, 0, length);
2668
21
    else
2669
21
        maxchar = writer->maxchar;
2670
2671
50
    if (_PyUnicodeWriter_Prepare(writer, arglen, maxchar) == -1)
2672
0
        return -1;
2673
2674
50
    fill = Py_MAX(width - length, 0);
2675
50
    if (fill && !(flags & F_LJUST)) {
2676
0
        if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1)
2677
0
            return -1;
2678
0
        writer->pos += fill;
2679
0
    }
2680
2681
50
    _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
2682
50
                                  str, 0, length);
2683
50
    writer->pos += length;
2684
2685
50
    if (fill && (flags & F_LJUST)) {
2686
0
        if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1)
2687
0
            return -1;
2688
0
        writer->pos += fill;
2689
0
    }
2690
2691
50
    return 0;
2692
50
}
2693
2694
static int
2695
unicode_fromformat_write_utf8(_PyUnicodeWriter *writer, const char *str,
2696
                              Py_ssize_t width, Py_ssize_t precision, int flags)
2697
5.41M
{
2698
    /* UTF-8 */
2699
5.41M
    Py_ssize_t *pconsumed = NULL;
2700
5.41M
    Py_ssize_t length;
2701
5.41M
    if (precision == -1) {
2702
249k
        length = strlen(str);
2703
249k
    }
2704
5.16M
    else {
2705
5.16M
        length = 0;
2706
21.3M
        while (length < precision && str[length]) {
2707
16.1M
            length++;
2708
16.1M
        }
2709
5.16M
        if (length == precision) {
2710
            /* The input string is not NUL-terminated.  If it ends with an
2711
             * incomplete UTF-8 sequence, truncate the string just before it.
2712
             * Incomplete sequences in the middle and sequences which cannot
2713
             * be valid prefixes are still treated as errors and replaced
2714
             * with \xfffd. */
2715
2.60k
            pconsumed = &length;
2716
2.60k
        }
2717
5.16M
    }
2718
2719
5.41M
    if (width < 0) {
2720
5.41M
        return unicode_decode_utf8_writer(writer, str, length,
2721
5.41M
                                          _Py_ERROR_REPLACE, "replace", pconsumed);
2722
5.41M
    }
2723
2724
0
    PyObject *unicode = PyUnicode_DecodeUTF8Stateful(str, length,
2725
0
                                                     "replace", pconsumed);
2726
0
    if (unicode == NULL)
2727
0
        return -1;
2728
2729
0
    int res = unicode_fromformat_write_str(writer, unicode,
2730
0
                                           width, -1, flags);
2731
0
    Py_DECREF(unicode);
2732
0
    return res;
2733
0
}
2734
2735
static int
2736
unicode_fromformat_write_wcstr(_PyUnicodeWriter *writer, const wchar_t *str,
2737
                              Py_ssize_t width, Py_ssize_t precision, int flags)
2738
0
{
2739
0
    Py_ssize_t length;
2740
0
    if (precision == -1) {
2741
0
        length = wcslen(str);
2742
0
    }
2743
0
    else {
2744
0
        length = 0;
2745
0
        while (length < precision && str[length]) {
2746
0
            length++;
2747
0
        }
2748
0
    }
2749
2750
0
    if (width < 0) {
2751
0
        return PyUnicodeWriter_WriteWideChar((PyUnicodeWriter*)writer,
2752
0
                                             str, length);
2753
0
    }
2754
2755
0
    PyObject *unicode = PyUnicode_FromWideChar(str, length);
2756
0
    if (unicode == NULL)
2757
0
        return -1;
2758
2759
0
    int res = unicode_fromformat_write_str(writer, unicode, width, -1, flags);
2760
0
    Py_DECREF(unicode);
2761
0
    return res;
2762
0
}
2763
2764
0
#define F_LONG 1
2765
0
#define F_LONGLONG 2
2766
94.0k
#define F_SIZE 3
2767
0
#define F_PTRDIFF 4
2768
0
#define F_INTMAX 5
2769
2770
static const char*
2771
unicode_fromformat_arg(_PyUnicodeWriter *writer,
2772
                       const char *f, va_list *vargs)
2773
35.4M
{
2774
35.4M
    const char *p;
2775
35.4M
    Py_ssize_t len;
2776
35.4M
    int flags = 0;
2777
35.4M
    Py_ssize_t width;
2778
35.4M
    Py_ssize_t precision;
2779
2780
35.4M
    p = f;
2781
35.4M
    f++;
2782
35.4M
    if (*f == '%') {
2783
5.15M
        if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0)
2784
0
            return NULL;
2785
5.15M
        f++;
2786
5.15M
        return f;
2787
5.15M
    }
2788
2789
    /* Parse flags. Example: "%-i" => flags=F_LJUST. */
2790
    /* Flags '+', ' ' and '#' are not particularly useful.
2791
     * They are not worth the implementation and maintenance costs.
2792
     * In addition, '#' should add "0" for "o" conversions for compatibility
2793
     * with printf, but it would confuse Python users. */
2794
30.2M
    while (1) {
2795
30.2M
        switch (*f++) {
2796
0
        case '-': flags |= F_LJUST; continue;
2797
2.78k
        case '0': flags |= F_ZERO; continue;
2798
0
        case '#': flags |= F_ALT; continue;
2799
30.2M
        }
2800
30.2M
        f--;
2801
30.2M
        break;
2802
30.2M
    }
2803
2804
    /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */
2805
30.2M
    width = -1;
2806
30.2M
    if (*f == '*') {
2807
0
        width = va_arg(*vargs, int);
2808
0
        if (width < 0) {
2809
0
            flags |= F_LJUST;
2810
0
            width = -width;
2811
0
        }
2812
0
        f++;
2813
0
    }
2814
30.2M
    else if (Py_ISDIGIT((unsigned)*f)) {
2815
2.78k
        width = *f - '0';
2816
2.78k
        f++;
2817
2.78k
        while (Py_ISDIGIT((unsigned)*f)) {
2818
0
            if (width > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) {
2819
0
                PyErr_SetString(PyExc_ValueError,
2820
0
                                "width too big");
2821
0
                return NULL;
2822
0
            }
2823
0
            width = (width * 10) + (*f - '0');
2824
0
            f++;
2825
0
        }
2826
2.78k
    }
2827
30.2M
    precision = -1;
2828
30.2M
    if (*f == '.') {
2829
5.16M
        f++;
2830
5.16M
        if (*f == '*') {
2831
0
            precision = va_arg(*vargs, int);
2832
0
            if (precision < 0) {
2833
0
                precision = -2;
2834
0
            }
2835
0
            f++;
2836
0
        }
2837
5.16M
        else if (Py_ISDIGIT((unsigned)*f)) {
2838
5.16M
            precision = (*f - '0');
2839
5.16M
            f++;
2840
15.4M
            while (Py_ISDIGIT((unsigned)*f)) {
2841
10.3M
                if (precision > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) {
2842
0
                    PyErr_SetString(PyExc_ValueError,
2843
0
                                    "precision too big");
2844
0
                    return NULL;
2845
0
                }
2846
10.3M
                precision = (precision * 10) + (*f - '0');
2847
10.3M
                f++;
2848
10.3M
            }
2849
5.16M
        }
2850
5.16M
    }
2851
2852
30.2M
    int sizemod = 0;
2853
30.2M
    if (*f == 'l') {
2854
0
        if (f[1] == 'l') {
2855
0
            sizemod = F_LONGLONG;
2856
0
            f += 2;
2857
0
        }
2858
0
        else {
2859
0
            sizemod = F_LONG;
2860
0
            ++f;
2861
0
        }
2862
0
    }
2863
30.2M
    else if (*f == 'z') {
2864
47.0k
        sizemod = F_SIZE;
2865
47.0k
        ++f;
2866
47.0k
    }
2867
30.1M
    else if (*f == 't') {
2868
0
        sizemod = F_PTRDIFF;
2869
0
        ++f;
2870
0
    }
2871
30.1M
    else if (*f == 'j') {
2872
0
        sizemod = F_INTMAX;
2873
0
        ++f;
2874
0
    }
2875
30.2M
    if (f[0] != '\0' && f[1] == '\0')
2876
5.27M
        writer->overallocate = 0;
2877
2878
30.2M
    switch (*f) {
2879
19.6M
    case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
2880
19.6M
        break;
2881
5.16M
    case 'c': case 'p':
2882
5.16M
        if (sizemod || width >= 0 || precision >= 0) goto invalid_format;
2883
5.16M
        break;
2884
5.41M
    case 's':
2885
5.41M
    case 'V':
2886
5.41M
        if (sizemod && sizemod != F_LONG) goto invalid_format;
2887
5.41M
        break;
2888
5.41M
    default:
2889
15.5k
        if (sizemod) goto invalid_format;
2890
15.5k
        break;
2891
30.2M
    }
2892
2893
30.2M
    switch (*f) {
2894
5.16M
    case 'c':
2895
5.16M
    {
2896
5.16M
        int ordinal = va_arg(*vargs, int);
2897
5.16M
        if (ordinal < 0 || ordinal > MAX_UNICODE) {
2898
0
            PyErr_SetString(PyExc_OverflowError,
2899
0
                            "character argument not in range(0x110000)");
2900
0
            return NULL;
2901
0
        }
2902
5.16M
        if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0)
2903
0
            return NULL;
2904
5.16M
        break;
2905
5.16M
    }
2906
2907
19.6M
    case 'd': case 'i':
2908
19.6M
    case 'o': case 'u': case 'x': case 'X':
2909
19.6M
    {
2910
19.6M
        char buffer[MAX_INTMAX_CHARS];
2911
2912
        // Fill buffer using sprinf, with one of many possible format
2913
        // strings, like "%llX" for `long long` in hexadecimal.
2914
        // The type/size is in `sizemod`; the format is in `*f`.
2915
2916
        // Use macros with nested switches to keep the sprintf format strings
2917
        // as compile-time literals, avoiding warnings and maybe allowing
2918
        // optimizations.
2919
2920
        // `SPRINT` macro does one sprintf
2921
        // Example usage: SPRINT("l", "X", unsigned long) expands to
2922
        // sprintf(buffer, "%" "l" "X", va_arg(*vargs, unsigned long))
2923
19.6M
        #define SPRINT(SIZE_SPEC, FMT_CHAR, TYPE) \
2924
19.6M
            sprintf(buffer, "%" SIZE_SPEC FMT_CHAR, va_arg(*vargs, TYPE))
2925
2926
        // One inner switch to handle all format variants
2927
19.6M
        #define DO_SPRINTS(SIZE_SPEC, SIGNED_TYPE, UNSIGNED_TYPE)             \
2928
19.6M
            switch (*f) {                                                     \
2929
0
                case 'o': len = SPRINT(SIZE_SPEC, "o", UNSIGNED_TYPE); break; \
2930
0
                case 'u': len = SPRINT(SIZE_SPEC, "u", UNSIGNED_TYPE); break; \
2931
1.58k
                case 'x': len = SPRINT(SIZE_SPEC, "x", UNSIGNED_TYPE); break; \
2932
1.20k
                case 'X': len = SPRINT(SIZE_SPEC, "X", UNSIGNED_TYPE); break; \
2933
19.6M
                default:  len = SPRINT(SIZE_SPEC, "d", SIGNED_TYPE); break;   \
2934
19.6M
            }
2935
2936
        // Outer switch to handle all the sizes/types
2937
19.6M
        switch (sizemod) {
2938
0
            case F_LONG:     DO_SPRINTS("l", long, unsigned long); break;
2939
0
            case F_LONGLONG: DO_SPRINTS("ll", long long, unsigned long long); break;
2940
47.0k
            case F_SIZE:     DO_SPRINTS("z", Py_ssize_t, size_t); break;
2941
0
            case F_PTRDIFF:  DO_SPRINTS("t", ptrdiff_t, ptrdiff_t); break;
2942
0
            case F_INTMAX:   DO_SPRINTS("j", intmax_t, uintmax_t); break;
2943
19.6M
            default:         DO_SPRINTS("", int, unsigned int); break;
2944
19.6M
        }
2945
19.6M
        #undef SPRINT
2946
19.6M
        #undef DO_SPRINTS
2947
2948
19.6M
        assert(len >= 0);
2949
2950
19.6M
        int sign = (buffer[0] == '-');
2951
19.6M
        len -= sign;
2952
2953
19.6M
        precision = Py_MAX(precision, len);
2954
19.6M
        width = Py_MAX(width, precision + sign);
2955
19.6M
        if ((flags & F_ZERO) && !(flags & F_LJUST)) {
2956
2.78k
            precision = width - sign;
2957
2.78k
        }
2958
2959
19.6M
        Py_ssize_t spacepad = Py_MAX(width - precision - sign, 0);
2960
19.6M
        Py_ssize_t zeropad = Py_MAX(precision - len, 0);
2961
2962
19.6M
        if (_PyUnicodeWriter_Prepare(writer, width, 127) == -1)
2963
0
            return NULL;
2964
2965
19.6M
        if (spacepad && !(flags & F_LJUST)) {
2966
0
            if (PyUnicode_Fill(writer->buffer, writer->pos, spacepad, ' ') == -1)
2967
0
                return NULL;
2968
0
            writer->pos += spacepad;
2969
0
        }
2970
2971
19.6M
        if (sign) {
2972
0
            if (_PyUnicodeWriter_WriteChar(writer, '-') == -1)
2973
0
                return NULL;
2974
0
        }
2975
2976
19.6M
        if (zeropad) {
2977
756
            if (PyUnicode_Fill(writer->buffer, writer->pos, zeropad, '0') == -1)
2978
0
                return NULL;
2979
756
            writer->pos += zeropad;
2980
756
        }
2981
2982
19.6M
        if (_PyUnicodeWriter_WriteASCIIString(writer, &buffer[sign], len) < 0)
2983
0
            return NULL;
2984
2985
19.6M
        if (spacepad && (flags & F_LJUST)) {
2986
0
            if (PyUnicode_Fill(writer->buffer, writer->pos, spacepad, ' ') == -1)
2987
0
                return NULL;
2988
0
            writer->pos += spacepad;
2989
0
        }
2990
19.6M
        break;
2991
19.6M
    }
2992
2993
19.6M
    case 'p':
2994
0
    {
2995
0
        char number[MAX_INTMAX_CHARS];
2996
2997
0
        len = sprintf(number, "%p", va_arg(*vargs, void*));
2998
0
        assert(len >= 0);
2999
3000
        /* %p is ill-defined:  ensure leading 0x. */
3001
0
        if (number[1] == 'X')
3002
0
            number[1] = 'x';
3003
0
        else if (number[1] != 'x') {
3004
0
            memmove(number + 2, number,
3005
0
                    strlen(number) + 1);
3006
0
            number[0] = '0';
3007
0
            number[1] = 'x';
3008
0
            len += 2;
3009
0
        }
3010
3011
0
        if (_PyUnicodeWriter_WriteASCIIString(writer, number, len) < 0)
3012
0
            return NULL;
3013
0
        break;
3014
0
    }
3015
3016
5.41M
    case 's':
3017
5.41M
    {
3018
5.41M
        if (sizemod) {
3019
0
            const wchar_t *s = va_arg(*vargs, const wchar_t*);
3020
0
            if (unicode_fromformat_write_wcstr(writer, s, width, precision, flags) < 0)
3021
0
                return NULL;
3022
0
        }
3023
5.41M
        else {
3024
            /* UTF-8 */
3025
5.41M
            const char *s = va_arg(*vargs, const char*);
3026
5.41M
            if (unicode_fromformat_write_utf8(writer, s, width, precision, flags) < 0)
3027
0
                return NULL;
3028
5.41M
        }
3029
5.41M
        break;
3030
5.41M
    }
3031
3032
5.41M
    case 'U':
3033
14.8k
    {
3034
14.8k
        PyObject *obj = va_arg(*vargs, PyObject *);
3035
14.8k
        assert(obj && _PyUnicode_CHECK(obj));
3036
3037
14.8k
        if (unicode_fromformat_write_str(writer, obj, width, precision, flags) == -1)
3038
0
            return NULL;
3039
14.8k
        break;
3040
14.8k
    }
3041
3042
14.8k
    case 'V':
3043
0
    {
3044
0
        PyObject *obj = va_arg(*vargs, PyObject *);
3045
0
        const char *str;
3046
0
        const wchar_t *wstr;
3047
0
        if (sizemod) {
3048
0
            wstr = va_arg(*vargs, const wchar_t*);
3049
0
        }
3050
0
        else {
3051
0
            str = va_arg(*vargs, const char *);
3052
0
        }
3053
0
        if (obj) {
3054
0
            assert(_PyUnicode_CHECK(obj));
3055
0
            if (unicode_fromformat_write_str(writer, obj, width, precision, flags) == -1)
3056
0
                return NULL;
3057
0
        }
3058
0
        else if (sizemod) {
3059
0
            assert(wstr != NULL);
3060
0
            if (unicode_fromformat_write_wcstr(writer, wstr, width, precision, flags) < 0)
3061
0
                return NULL;
3062
0
        }
3063
0
        else {
3064
0
            assert(str != NULL);
3065
0
            if (unicode_fromformat_write_utf8(writer, str, width, precision, flags) < 0)
3066
0
                return NULL;
3067
0
        }
3068
0
        break;
3069
0
    }
3070
3071
23
    case 'S':
3072
23
    {
3073
23
        PyObject *obj = va_arg(*vargs, PyObject *);
3074
23
        PyObject *str;
3075
23
        assert(obj);
3076
23
        str = PyObject_Str(obj);
3077
23
        if (!str)
3078
0
            return NULL;
3079
23
        if (unicode_fromformat_write_str(writer, str, width, precision, flags) == -1) {
3080
0
            Py_DECREF(str);
3081
0
            return NULL;
3082
0
        }
3083
23
        Py_DECREF(str);
3084
23
        break;
3085
23
    }
3086
3087
637
    case 'R':
3088
637
    {
3089
637
        PyObject *obj = va_arg(*vargs, PyObject *);
3090
637
        PyObject *repr;
3091
637
        assert(obj);
3092
637
        repr = PyObject_Repr(obj);
3093
637
        if (!repr)
3094
0
            return NULL;
3095
637
        if (unicode_fromformat_write_str(writer, repr, width, precision, flags) == -1) {
3096
0
            Py_DECREF(repr);
3097
0
            return NULL;
3098
0
        }
3099
637
        Py_DECREF(repr);
3100
637
        break;
3101
637
    }
3102
3103
0
    case 'A':
3104
0
    {
3105
0
        PyObject *obj = va_arg(*vargs, PyObject *);
3106
0
        PyObject *ascii;
3107
0
        assert(obj);
3108
0
        ascii = PyObject_ASCII(obj);
3109
0
        if (!ascii)
3110
0
            return NULL;
3111
0
        if (unicode_fromformat_write_str(writer, ascii, width, precision, flags) == -1) {
3112
0
            Py_DECREF(ascii);
3113
0
            return NULL;
3114
0
        }
3115
0
        Py_DECREF(ascii);
3116
0
        break;
3117
0
    }
3118
3119
0
    case 'T':
3120
0
    {
3121
0
        PyObject *obj = va_arg(*vargs, PyObject *);
3122
0
        PyTypeObject *type = (PyTypeObject *)Py_NewRef(Py_TYPE(obj));
3123
3124
0
        PyObject *type_name;
3125
0
        if (flags & F_ALT) {
3126
0
            type_name = _PyType_GetFullyQualifiedName(type, ':');
3127
0
        }
3128
0
        else {
3129
0
            type_name = PyType_GetFullyQualifiedName(type);
3130
0
        }
3131
0
        Py_DECREF(type);
3132
0
        if (!type_name) {
3133
0
            return NULL;
3134
0
        }
3135
3136
0
        if (unicode_fromformat_write_str(writer, type_name,
3137
0
                                         width, precision, flags) == -1) {
3138
0
            Py_DECREF(type_name);
3139
0
            return NULL;
3140
0
        }
3141
0
        Py_DECREF(type_name);
3142
0
        break;
3143
0
    }
3144
3145
0
    case 'N':
3146
0
    {
3147
0
        PyObject *type_raw = va_arg(*vargs, PyObject *);
3148
0
        assert(type_raw != NULL);
3149
3150
0
        if (!PyType_Check(type_raw)) {
3151
0
            PyErr_SetString(PyExc_TypeError, "%N argument must be a type");
3152
0
            return NULL;
3153
0
        }
3154
0
        PyTypeObject *type = (PyTypeObject*)type_raw;
3155
3156
0
        PyObject *type_name;
3157
0
        if (flags & F_ALT) {
3158
0
            type_name = _PyType_GetFullyQualifiedName(type, ':');
3159
0
        }
3160
0
        else {
3161
0
            type_name = PyType_GetFullyQualifiedName(type);
3162
0
        }
3163
0
        if (!type_name) {
3164
0
            return NULL;
3165
0
        }
3166
0
        if (unicode_fromformat_write_str(writer, type_name,
3167
0
                                         width, precision, flags) == -1) {
3168
0
            Py_DECREF(type_name);
3169
0
            return NULL;
3170
0
        }
3171
0
        Py_DECREF(type_name);
3172
0
        break;
3173
0
    }
3174
3175
0
    default:
3176
0
    invalid_format:
3177
0
        PyErr_Format(PyExc_SystemError, "invalid format string: %s", p);
3178
0
        return NULL;
3179
30.2M
    }
3180
3181
30.2M
    f++;
3182
30.2M
    return f;
3183
30.2M
}
3184
3185
static int
3186
unicode_from_format(_PyUnicodeWriter *writer, const char *format, va_list vargs)
3187
15.1M
{
3188
15.1M
    Py_ssize_t len = strlen(format);
3189
15.1M
    writer->min_length += len + 100;
3190
15.1M
    writer->overallocate = 1;
3191
3192
    // Copy varags to be able to pass a reference to a subfunction.
3193
15.1M
    va_list vargs2;
3194
15.1M
    va_copy(vargs2, vargs);
3195
3196
    // _PyUnicodeWriter_WriteASCIIString() below requires the format string
3197
    // to be encoded to ASCII.
3198
15.1M
    int is_ascii = (ucs1lib_find_max_char((Py_UCS1*)format, (Py_UCS1*)format + len) < 128);
3199
15.1M
    if (!is_ascii) {
3200
0
        Py_ssize_t i;
3201
0
        for (i=0; i < len && (unsigned char)format[i] <= 127; i++);
3202
0
        PyErr_Format(PyExc_ValueError,
3203
0
            "PyUnicode_FromFormatV() expects an ASCII-encoded format "
3204
0
            "string, got a non-ASCII byte: 0x%02x",
3205
0
            (unsigned char)format[i]);
3206
0
        goto fail;
3207
0
    }
3208
3209
85.5M
    for (const char *f = format; *f; ) {
3210
70.3M
        if (*f == '%') {
3211
35.4M
            f = unicode_fromformat_arg(writer, f, &vargs2);
3212
35.4M
            if (f == NULL)
3213
0
                goto fail;
3214
35.4M
        }
3215
34.9M
        else {
3216
34.9M
            const char *p = strchr(f, '%');
3217
34.9M
            if (p != NULL) {
3218
25.0M
                len = p - f;
3219
25.0M
            }
3220
9.91M
            else {
3221
9.91M
                len = strlen(f);
3222
9.91M
                writer->overallocate = 0;
3223
9.91M
            }
3224
3225
34.9M
            if (_PyUnicodeWriter_WriteASCIIString(writer, f, len) < 0) {
3226
0
                goto fail;
3227
0
            }
3228
34.9M
            f += len;
3229
34.9M
        }
3230
70.3M
    }
3231
15.1M
    va_end(vargs2);
3232
15.1M
    return 0;
3233
3234
0
  fail:
3235
0
    va_end(vargs2);
3236
0
    return -1;
3237
15.1M
}
3238
3239
PyObject *
3240
PyUnicode_FromFormatV(const char *format, va_list vargs)
3241
15.1M
{
3242
15.1M
    _PyUnicodeWriter writer;
3243
15.1M
    _PyUnicodeWriter_Init(&writer);
3244
3245
15.1M
    if (unicode_from_format(&writer, format, vargs) < 0) {
3246
0
        _PyUnicodeWriter_Dealloc(&writer);
3247
0
        return NULL;
3248
0
    }
3249
15.1M
    return _PyUnicodeWriter_Finish(&writer);
3250
15.1M
}
3251
3252
PyObject *
3253
PyUnicode_FromFormat(const char *format, ...)
3254
13.9k
{
3255
13.9k
    PyObject* ret;
3256
13.9k
    va_list vargs;
3257
3258
13.9k
    va_start(vargs, format);
3259
13.9k
    ret = PyUnicode_FromFormatV(format, vargs);
3260
13.9k
    va_end(vargs);
3261
13.9k
    return ret;
3262
13.9k
}
3263
3264
int
3265
PyUnicodeWriter_Format(PyUnicodeWriter *writer, const char *format, ...)
3266
0
{
3267
0
    _PyUnicodeWriter *_writer = (_PyUnicodeWriter*)writer;
3268
0
    Py_ssize_t old_pos = _writer->pos;
3269
3270
0
    va_list vargs;
3271
0
    va_start(vargs, format);
3272
0
    int res = unicode_from_format(_writer, format, vargs);
3273
0
    va_end(vargs);
3274
3275
0
    if (res < 0) {
3276
0
        _writer->pos = old_pos;
3277
0
    }
3278
0
    return res;
3279
0
}
3280
3281
static Py_ssize_t
3282
unicode_get_widechar_size(PyObject *unicode)
3283
1.73k
{
3284
1.73k
    Py_ssize_t res;
3285
3286
1.73k
    assert(unicode != NULL);
3287
1.73k
    assert(_PyUnicode_CHECK(unicode));
3288
3289
1.73k
    res = _PyUnicode_LENGTH(unicode);
3290
#if SIZEOF_WCHAR_T == 2
3291
    if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) {
3292
        const Py_UCS4 *s = PyUnicode_4BYTE_DATA(unicode);
3293
        const Py_UCS4 *end = s + res;
3294
        for (; s < end; ++s) {
3295
            if (*s > 0xFFFF) {
3296
                ++res;
3297
            }
3298
        }
3299
    }
3300
#endif
3301
1.73k
    return res;
3302
1.73k
}
3303
3304
static void
3305
unicode_copy_as_widechar(PyObject *unicode, wchar_t *w, Py_ssize_t size)
3306
1.73k
{
3307
1.73k
    assert(unicode != NULL);
3308
1.73k
    assert(_PyUnicode_CHECK(unicode));
3309
3310
1.73k
    if (PyUnicode_KIND(unicode) == sizeof(wchar_t)) {
3311
0
        memcpy(w, PyUnicode_DATA(unicode), size * sizeof(wchar_t));
3312
0
        return;
3313
0
    }
3314
3315
1.73k
    if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) {
3316
1.73k
        const Py_UCS1 *s = PyUnicode_1BYTE_DATA(unicode);
3317
110k
        for (; size--; ++s, ++w) {
3318
108k
            *w = *s;
3319
108k
        }
3320
1.73k
    }
3321
0
    else {
3322
0
#if SIZEOF_WCHAR_T == 4
3323
0
        assert(PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND);
3324
0
        const Py_UCS2 *s = PyUnicode_2BYTE_DATA(unicode);
3325
0
        for (; size--; ++s, ++w) {
3326
0
            *w = *s;
3327
0
        }
3328
#else
3329
        assert(PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND);
3330
        const Py_UCS4 *s = PyUnicode_4BYTE_DATA(unicode);
3331
        for (; size--; ++s, ++w) {
3332
            Py_UCS4 ch = *s;
3333
            if (ch > 0xFFFF) {
3334
                assert(ch <= MAX_UNICODE);
3335
                /* encode surrogate pair in this case */
3336
                *w++ = Py_UNICODE_HIGH_SURROGATE(ch);
3337
                if (!size--)
3338
                    break;
3339
                *w = Py_UNICODE_LOW_SURROGATE(ch);
3340
            }
3341
            else {
3342
                *w = ch;
3343
            }
3344
        }
3345
#endif
3346
0
    }
3347
1.73k
}
3348
3349
#ifdef HAVE_WCHAR_H
3350
3351
/* Convert a Unicode object to a wide character string.
3352
3353
   - If w is NULL: return the number of wide characters (including the null
3354
     character) required to convert the unicode object. Ignore size argument.
3355
3356
   - Otherwise: return the number of wide characters (excluding the null
3357
     character) written into w. Write at most size wide characters (including
3358
     the null character). */
3359
Py_ssize_t
3360
PyUnicode_AsWideChar(PyObject *unicode,
3361
                     wchar_t *w,
3362
                     Py_ssize_t size)
3363
469
{
3364
469
    Py_ssize_t res;
3365
3366
469
    if (unicode == NULL) {
3367
0
        PyErr_BadInternalCall();
3368
0
        return -1;
3369
0
    }
3370
469
    if (!PyUnicode_Check(unicode)) {
3371
0
        PyErr_BadArgument();
3372
0
        return -1;
3373
0
    }
3374
3375
469
    res = unicode_get_widechar_size(unicode);
3376
469
    if (w == NULL) {
3377
0
        return res + 1;
3378
0
    }
3379
3380
469
    if (size > res) {
3381
469
        size = res + 1;
3382
469
    }
3383
0
    else {
3384
0
        res = size;
3385
0
    }
3386
469
    unicode_copy_as_widechar(unicode, w, size);
3387
3388
#ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION
3389
    /* Oracle Solaris uses non-Unicode internal wchar_t form for
3390
       non-Unicode locales and hence needs conversion first. */
3391
    if (_Py_LocaleUsesNonUnicodeWchar()) {
3392
        if (_Py_EncodeNonUnicodeWchar_InPlace(w, size) < 0) {
3393
            return -1;
3394
        }
3395
    }
3396
#endif
3397
3398
469
    return res;
3399
469
}
3400
3401
wchar_t*
3402
PyUnicode_AsWideCharString(PyObject *unicode,
3403
                           Py_ssize_t *size)
3404
1.26k
{
3405
1.26k
    wchar_t *buffer;
3406
1.26k
    Py_ssize_t buflen;
3407
3408
1.26k
    if (unicode == NULL) {
3409
0
        PyErr_BadInternalCall();
3410
0
        return NULL;
3411
0
    }
3412
1.26k
    if (!PyUnicode_Check(unicode)) {
3413
0
        PyErr_BadArgument();
3414
0
        return NULL;
3415
0
    }
3416
3417
1.26k
    buflen = unicode_get_widechar_size(unicode);
3418
1.26k
    buffer = (wchar_t *) PyMem_New(wchar_t, (buflen + 1));
3419
1.26k
    if (buffer == NULL) {
3420
0
        PyErr_NoMemory();
3421
0
        return NULL;
3422
0
    }
3423
1.26k
    unicode_copy_as_widechar(unicode, buffer, buflen + 1);
3424
3425
#ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION
3426
    /* Oracle Solaris uses non-Unicode internal wchar_t form for
3427
       non-Unicode locales and hence needs conversion first. */
3428
    if (_Py_LocaleUsesNonUnicodeWchar()) {
3429
        if (_Py_EncodeNonUnicodeWchar_InPlace(buffer, (buflen + 1)) < 0) {
3430
            return NULL;
3431
        }
3432
    }
3433
#endif
3434
3435
1.26k
    if (size != NULL) {
3436
814
        *size = buflen;
3437
814
    }
3438
448
    else if (wcslen(buffer) != (size_t)buflen) {
3439
0
        PyMem_Free(buffer);
3440
0
        PyErr_SetString(PyExc_ValueError,
3441
0
                        "embedded null character");
3442
0
        return NULL;
3443
0
    }
3444
1.26k
    return buffer;
3445
1.26k
}
3446
3447
#endif /* HAVE_WCHAR_H */
3448
3449
int
3450
_PyUnicode_WideCharString_Converter(PyObject *obj, void *ptr)
3451
0
{
3452
0
    wchar_t **p = (wchar_t **)ptr;
3453
0
    if (obj == NULL) {
3454
0
        PyMem_Free(*p);
3455
0
        *p = NULL;
3456
0
        return 1;
3457
0
    }
3458
0
    if (PyUnicode_Check(obj)) {
3459
0
        *p = PyUnicode_AsWideCharString(obj, NULL);
3460
0
        if (*p == NULL) {
3461
0
            return 0;
3462
0
        }
3463
0
        return Py_CLEANUP_SUPPORTED;
3464
0
    }
3465
0
    PyErr_Format(PyExc_TypeError,
3466
0
                 "argument must be str, not %.50s",
3467
0
                 Py_TYPE(obj)->tp_name);
3468
0
    return 0;
3469
0
}
3470
3471
int
3472
_PyUnicode_WideCharString_Opt_Converter(PyObject *obj, void *ptr)
3473
0
{
3474
0
    wchar_t **p = (wchar_t **)ptr;
3475
0
    if (obj == NULL) {
3476
0
        PyMem_Free(*p);
3477
0
        *p = NULL;
3478
0
        return 1;
3479
0
    }
3480
0
    if (obj == Py_None) {
3481
0
        *p = NULL;
3482
0
        return 1;
3483
0
    }
3484
0
    if (PyUnicode_Check(obj)) {
3485
0
        *p = PyUnicode_AsWideCharString(obj, NULL);
3486
0
        if (*p == NULL) {
3487
0
            return 0;
3488
0
        }
3489
0
        return Py_CLEANUP_SUPPORTED;
3490
0
    }
3491
0
    PyErr_Format(PyExc_TypeError,
3492
0
                 "argument must be str or None, not %.50s",
3493
0
                 Py_TYPE(obj)->tp_name);
3494
0
    return 0;
3495
0
}
3496
3497
PyObject *
3498
PyUnicode_FromOrdinal(int ordinal)
3499
234k
{
3500
234k
    if (ordinal < 0 || ordinal > MAX_UNICODE) {
3501
0
        PyErr_SetString(PyExc_ValueError,
3502
0
                        "chr() arg not in range(0x110000)");
3503
0
        return NULL;
3504
0
    }
3505
3506
234k
    return unicode_char((Py_UCS4)ordinal);
3507
234k
}
3508
3509
PyObject *
3510
PyUnicode_FromObject(PyObject *obj)
3511
5.29M
{
3512
    /* XXX Perhaps we should make this API an alias of
3513
       PyObject_Str() instead ?! */
3514
5.29M
    if (PyUnicode_CheckExact(obj)) {
3515
5.29M
        return Py_NewRef(obj);
3516
5.29M
    }
3517
0
    if (PyUnicode_Check(obj)) {
3518
        /* For a Unicode subtype that's not a Unicode object,
3519
           return a true Unicode object with the same data. */
3520
0
        return _PyUnicode_Copy(obj);
3521
0
    }
3522
0
    PyErr_Format(PyExc_TypeError,
3523
0
                 "Can't convert '%.100s' object to str implicitly",
3524
0
                 Py_TYPE(obj)->tp_name);
3525
0
    return NULL;
3526
0
}
3527
3528
PyObject *
3529
PyUnicode_FromEncodedObject(PyObject *obj,
3530
                            const char *encoding,
3531
                            const char *errors)
3532
5.41M
{
3533
5.41M
    Py_buffer buffer;
3534
5.41M
    PyObject *v;
3535
3536
5.41M
    if (obj == NULL) {
3537
0
        PyErr_BadInternalCall();
3538
0
        return NULL;
3539
0
    }
3540
3541
    /* Decoding bytes objects is the most common case and should be fast */
3542
5.41M
    if (PyBytes_Check(obj)) {
3543
4.88M
        if (PyBytes_GET_SIZE(obj) == 0) {
3544
529k
            if (unicode_check_encoding_errors(encoding, errors) < 0) {
3545
0
                return NULL;
3546
0
            }
3547
529k
            _Py_RETURN_UNICODE_EMPTY();
3548
529k
        }
3549
4.35M
        return PyUnicode_Decode(
3550
4.35M
                PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj),
3551
4.35M
                encoding, errors);
3552
4.88M
    }
3553
3554
533k
    if (PyUnicode_Check(obj)) {
3555
0
        PyErr_SetString(PyExc_TypeError,
3556
0
                        "decoding str is not supported");
3557
0
        return NULL;
3558
0
    }
3559
3560
    /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */
3561
533k
    if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) {
3562
0
        PyErr_Format(PyExc_TypeError,
3563
0
                     "decoding to str: need a bytes-like object, %.80s found",
3564
0
                     Py_TYPE(obj)->tp_name);
3565
0
        return NULL;
3566
0
    }
3567
3568
533k
    if (buffer.len == 0) {
3569
0
        PyBuffer_Release(&buffer);
3570
0
        if (unicode_check_encoding_errors(encoding, errors) < 0) {
3571
0
            return NULL;
3572
0
        }
3573
0
        _Py_RETURN_UNICODE_EMPTY();
3574
0
    }
3575
3576
533k
    v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors);
3577
533k
    PyBuffer_Release(&buffer);
3578
533k
    return v;
3579
533k
}
3580
3581
/* Normalize an encoding name: similar to encodings.normalize_encoding(), but
3582
   also convert to lowercase. Return 1 on success, or 0 on error (encoding is
3583
   longer than lower_len-1). */
3584
int
3585
_Py_normalize_encoding(const char *encoding,
3586
                       char *lower,
3587
                       size_t lower_len)
3588
10.8M
{
3589
10.8M
    const char *e;
3590
10.8M
    char *l;
3591
10.8M
    char *l_end;
3592
10.8M
    int punct;
3593
3594
10.8M
    assert(encoding != NULL);
3595
3596
10.8M
    e = encoding;
3597
10.8M
    l = lower;
3598
10.8M
    l_end = &lower[lower_len - 1];
3599
10.8M
    punct = 0;
3600
179M
    while (1) {
3601
179M
        char c = *e;
3602
179M
        if (c == 0) {
3603
9.97M
            break;
3604
9.97M
        }
3605
3606
169M
        if (Py_ISALNUM(c) || c == '.') {
3607
75.9M
            if (punct && l != lower) {
3608
9.97M
                if (l == l_end) {
3609
1.44k
                    return 0;
3610
1.44k
                }
3611
9.97M
                *l++ = '_';
3612
9.97M
            }
3613
75.9M
            punct = 0;
3614
3615
75.9M
            if (l == l_end) {
3616
879k
                return 0;
3617
879k
            }
3618
75.0M
            *l++ = Py_TOLOWER(c);
3619
75.0M
        }
3620
93.7M
        else {
3621
93.7M
            punct = 1;
3622
93.7M
        }
3623
3624
168M
        e++;
3625
168M
    }
3626
9.97M
    *l = '\0';
3627
9.97M
    return 1;
3628
10.8M
}
3629
3630
PyObject *
3631
PyUnicode_Decode(const char *s,
3632
                 Py_ssize_t size,
3633
                 const char *encoding,
3634
                 const char *errors)
3635
4.89M
{
3636
4.89M
    PyObject *buffer = NULL, *unicode;
3637
4.89M
    Py_buffer info;
3638
4.89M
    char buflower[11];   /* strlen("iso-8859-1\0") == 11, longest shortcut */
3639
3640
4.89M
    if (unicode_check_encoding_errors(encoding, errors) < 0) {
3641
0
        return NULL;
3642
0
    }
3643
3644
4.89M
    if (size == 0) {
3645
0
        _Py_RETURN_UNICODE_EMPTY();
3646
0
    }
3647
3648
4.89M
    if (encoding == NULL) {
3649
33.1k
        return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL);
3650
33.1k
    }
3651
3652
    /* Shortcuts for common default encodings */
3653
4.85M
    if (_Py_normalize_encoding(encoding, buflower, sizeof(buflower))) {
3654
4.84M
        char *lower = buflower;
3655
3656
        /* Fast paths */
3657
4.84M
        if (lower[0] == 'u' && lower[1] == 't' && lower[2] == 'f') {
3658
921k
            lower += 3;
3659
921k
            if (*lower == '_') {
3660
                /* Match "utf8" and "utf_8" */
3661
921k
                lower++;
3662
921k
            }
3663
3664
921k
            if (lower[0] == '8' && lower[1] == 0) {
3665
920k
                return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL);
3666
920k
            }
3667
919
            else if (lower[0] == '1' && lower[1] == '6' && lower[2] == 0) {
3668
125
                return PyUnicode_DecodeUTF16(s, size, errors, 0);
3669
125
            }
3670
794
            else if (lower[0] == '3' && lower[1] == '2' && lower[2] == 0) {
3671
129
                return PyUnicode_DecodeUTF32(s, size, errors, 0);
3672
129
            }
3673
921k
        }
3674
3.92M
        else {
3675
3.92M
            if (strcmp(lower, "ascii") == 0
3676
3.92M
                || strcmp(lower, "us_ascii") == 0) {
3677
896k
                return PyUnicode_DecodeASCII(s, size, errors);
3678
896k
            }
3679
    #ifdef MS_WINDOWS
3680
            else if (strcmp(lower, "mbcs") == 0) {
3681
                return PyUnicode_DecodeMBCS(s, size, errors);
3682
            }
3683
    #endif
3684
3.03M
            else if (strcmp(lower, "latin1") == 0
3685
3.03M
                     || strcmp(lower, "latin_1") == 0
3686
3.03M
                     || strcmp(lower, "iso_8859_1") == 0
3687
3.03M
                     || strcmp(lower, "iso8859_1") == 0) {
3688
2.67M
                return PyUnicode_DecodeLatin1(s, size, errors);
3689
2.67M
            }
3690
3.92M
        }
3691
4.84M
    }
3692
3693
    /* Decode via the codec registry */
3694
367k
    buffer = NULL;
3695
367k
    if (PyBuffer_FillInfo(&info, NULL, (void *)s, size, 1, PyBUF_FULL_RO) < 0)
3696
0
        goto onError;
3697
367k
    buffer = PyMemoryView_FromBuffer(&info);
3698
367k
    if (buffer == NULL)
3699
0
        goto onError;
3700
367k
    unicode = _PyCodec_DecodeText(buffer, encoding, errors);
3701
367k
    if (unicode == NULL)
3702
167k
        goto onError;
3703
199k
    if (!PyUnicode_Check(unicode)) {
3704
0
        PyErr_Format(PyExc_TypeError,
3705
0
                     "'%.400s' decoder returned '%.400s' instead of 'str'; "
3706
0
                     "use codecs.decode() to decode to arbitrary types",
3707
0
                     encoding,
3708
0
                     Py_TYPE(unicode)->tp_name);
3709
0
        Py_DECREF(unicode);
3710
0
        goto onError;
3711
0
    }
3712
199k
    Py_DECREF(buffer);
3713
199k
    return unicode_result(unicode);
3714
3715
167k
  onError:
3716
167k
    Py_XDECREF(buffer);
3717
167k
    return NULL;
3718
199k
}
3719
3720
PyAPI_FUNC(PyObject *)
3721
PyUnicode_AsDecodedObject(PyObject *unicode,
3722
                          const char *encoding,
3723
                          const char *errors)
3724
0
{
3725
0
    if (!PyUnicode_Check(unicode)) {
3726
0
        PyErr_BadArgument();
3727
0
        return NULL;
3728
0
    }
3729
3730
0
    if (encoding == NULL)
3731
0
        encoding = PyUnicode_GetDefaultEncoding();
3732
3733
    /* Decode via the codec registry */
3734
0
    return PyCodec_Decode(unicode, encoding, errors);
3735
0
}
3736
3737
PyAPI_FUNC(PyObject *)
3738
PyUnicode_AsDecodedUnicode(PyObject *unicode,
3739
                           const char *encoding,
3740
                           const char *errors)
3741
0
{
3742
0
    PyObject *v;
3743
3744
0
    if (!PyUnicode_Check(unicode)) {
3745
0
        PyErr_BadArgument();
3746
0
        goto onError;
3747
0
    }
3748
3749
0
    if (encoding == NULL)
3750
0
        encoding = PyUnicode_GetDefaultEncoding();
3751
3752
    /* Decode via the codec registry */
3753
0
    v = PyCodec_Decode(unicode, encoding, errors);
3754
0
    if (v == NULL)
3755
0
        goto onError;
3756
0
    if (!PyUnicode_Check(v)) {
3757
0
        PyErr_Format(PyExc_TypeError,
3758
0
                     "'%.400s' decoder returned '%.400s' instead of 'str'; "
3759
0
                     "use codecs.decode() to decode to arbitrary types",
3760
0
                     encoding,
3761
0
                     Py_TYPE(unicode)->tp_name);
3762
0
        Py_DECREF(v);
3763
0
        goto onError;
3764
0
    }
3765
0
    return unicode_result(v);
3766
3767
0
  onError:
3768
0
    return NULL;
3769
0
}
3770
3771
PyAPI_FUNC(PyObject *)
3772
PyUnicode_AsEncodedObject(PyObject *unicode,
3773
                          const char *encoding,
3774
                          const char *errors)
3775
0
{
3776
0
    PyObject *v;
3777
3778
0
    if (!PyUnicode_Check(unicode)) {
3779
0
        PyErr_BadArgument();
3780
0
        goto onError;
3781
0
    }
3782
3783
0
    if (encoding == NULL)
3784
0
        encoding = PyUnicode_GetDefaultEncoding();
3785
3786
    /* Encode via the codec registry */
3787
0
    v = PyCodec_Encode(unicode, encoding, errors);
3788
0
    if (v == NULL)
3789
0
        goto onError;
3790
0
    return v;
3791
3792
0
  onError:
3793
0
    return NULL;
3794
0
}
3795
3796
3797
static PyObject *
3798
unicode_encode_locale(PyObject *unicode, _Py_error_handler error_handler,
3799
                      int current_locale)
3800
414
{
3801
414
    Py_ssize_t wlen;
3802
414
    wchar_t *wstr = PyUnicode_AsWideCharString(unicode, &wlen);
3803
414
    if (wstr == NULL) {
3804
0
        return NULL;
3805
0
    }
3806
3807
414
    if ((size_t)wlen != wcslen(wstr)) {
3808
0
        PyErr_SetString(PyExc_ValueError, "embedded null character");
3809
0
        PyMem_Free(wstr);
3810
0
        return NULL;
3811
0
    }
3812
3813
414
    char *str;
3814
414
    size_t error_pos;
3815
414
    const char *reason;
3816
414
    int res = _Py_EncodeLocaleEx(wstr, &str, &error_pos, &reason,
3817
414
                                 current_locale, error_handler);
3818
414
    PyMem_Free(wstr);
3819
3820
414
    if (res != 0) {
3821
0
        if (res == -2) {
3822
0
            PyObject *exc;
3823
0
            exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnns",
3824
0
                    "locale", unicode,
3825
0
                    (Py_ssize_t)error_pos,
3826
0
                    (Py_ssize_t)(error_pos+1),
3827
0
                    reason);
3828
0
            if (exc != NULL) {
3829
0
                PyCodec_StrictErrors(exc);
3830
0
                Py_DECREF(exc);
3831
0
            }
3832
0
        }
3833
0
        else if (res == -3) {
3834
0
            PyErr_SetString(PyExc_ValueError, "unsupported error handler");
3835
0
        }
3836
0
        else {
3837
0
            PyErr_NoMemory();
3838
0
        }
3839
0
        return NULL;
3840
0
    }
3841
3842
414
    PyObject *bytes = PyBytes_FromString(str);
3843
414
    PyMem_RawFree(str);
3844
414
    return bytes;
3845
414
}
3846
3847
PyObject *
3848
PyUnicode_EncodeLocale(PyObject *unicode, const char *errors)
3849
0
{
3850
0
    _Py_error_handler error_handler = _Py_GetErrorHandler(errors);
3851
0
    return unicode_encode_locale(unicode, error_handler, 1);
3852
0
}
3853
3854
PyObject *
3855
PyUnicode_EncodeFSDefault(PyObject *unicode)
3856
18.6k
{
3857
18.6k
    PyInterpreterState *interp = _PyInterpreterState_GET();
3858
18.6k
    struct _Py_unicode_fs_codec *fs_codec = &interp->unicode.fs_codec;
3859
18.6k
    if (fs_codec->utf8) {
3860
18.2k
        return unicode_encode_utf8(unicode,
3861
18.2k
                                   fs_codec->error_handler,
3862
18.2k
                                   fs_codec->errors);
3863
18.2k
    }
3864
414
#ifndef _Py_FORCE_UTF8_FS_ENCODING
3865
414
    else if (fs_codec->encoding) {
3866
0
        return PyUnicode_AsEncodedString(unicode,
3867
0
                                         fs_codec->encoding,
3868
0
                                         fs_codec->errors);
3869
0
    }
3870
414
#endif
3871
414
    else {
3872
        /* Before _PyUnicode_InitEncodings() is called, the Python codec
3873
           machinery is not ready and so cannot be used:
3874
           use wcstombs() in this case. */
3875
414
        const PyConfig *config = _PyInterpreterState_GetConfig(interp);
3876
414
        const wchar_t *filesystem_errors = config->filesystem_errors;
3877
414
        assert(filesystem_errors != NULL);
3878
414
        _Py_error_handler errors = get_error_handler_wide(filesystem_errors);
3879
414
        assert(errors != _Py_ERROR_UNKNOWN);
3880
#ifdef _Py_FORCE_UTF8_FS_ENCODING
3881
        return unicode_encode_utf8(unicode, errors, NULL);
3882
#else
3883
414
        return unicode_encode_locale(unicode, errors, 0);
3884
414
#endif
3885
414
    }
3886
18.6k
}
3887
3888
PyObject *
3889
PyUnicode_AsEncodedString(PyObject *unicode,
3890
                          const char *encoding,
3891
                          const char *errors)
3892
15.9M
{
3893
15.9M
    PyObject *v;
3894
15.9M
    char buflower[11];   /* strlen("iso_8859_1\0") == 11, longest shortcut */
3895
3896
15.9M
    if (!PyUnicode_Check(unicode)) {
3897
0
        PyErr_BadArgument();
3898
0
        return NULL;
3899
0
    }
3900
3901
15.9M
    if (unicode_check_encoding_errors(encoding, errors) < 0) {
3902
0
        return NULL;
3903
0
    }
3904
3905
15.9M
    if (encoding == NULL) {
3906
11.1M
        return _PyUnicode_AsUTF8String(unicode, errors);
3907
11.1M
    }
3908
3909
    /* Shortcuts for common default encodings */
3910
4.73M
    if (_Py_normalize_encoding(encoding, buflower, sizeof(buflower))) {
3911
3.86M
        char *lower = buflower;
3912
3913
        /* Fast paths */
3914
3.86M
        if (lower[0] == 'u' && lower[1] == 't' && lower[2] == 'f') {
3915
3.73M
            lower += 3;
3916
3.73M
            if (*lower == '_') {
3917
                /* Match "utf8" and "utf_8" */
3918
3.73M
                lower++;
3919
3.73M
            }
3920
3921
3.73M
            if (lower[0] == '8' && lower[1] == 0) {
3922
3.73M
                return _PyUnicode_AsUTF8String(unicode, errors);
3923
3.73M
            }
3924
0
            else if (lower[0] == '1' && lower[1] == '6' && lower[2] == 0) {
3925
0
                return _PyUnicode_EncodeUTF16(unicode, errors, 0);
3926
0
            }
3927
0
            else if (lower[0] == '3' && lower[1] == '2' && lower[2] == 0) {
3928
0
                return _PyUnicode_EncodeUTF32(unicode, errors, 0);
3929
0
            }
3930
3.73M
        }
3931
126k
        else {
3932
126k
            if (strcmp(lower, "ascii") == 0
3933
126k
                || strcmp(lower, "us_ascii") == 0) {
3934
107k
                return _PyUnicode_AsASCIIString(unicode, errors);
3935
107k
            }
3936
#ifdef MS_WINDOWS
3937
            else if (strcmp(lower, "mbcs") == 0) {
3938
                return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors);
3939
            }
3940
#endif
3941
19.0k
            else if (strcmp(lower, "latin1") == 0 ||
3942
19.0k
                     strcmp(lower, "latin_1") == 0 ||
3943
19.0k
                     strcmp(lower, "iso_8859_1") == 0 ||
3944
19.0k
                     strcmp(lower, "iso8859_1") == 0) {
3945
0
                return _PyUnicode_AsLatin1String(unicode, errors);
3946
0
            }
3947
126k
        }
3948
3.86M
    }
3949
3950
    /* Encode via the codec registry */
3951
892k
    v = _PyCodec_EncodeText(unicode, encoding, errors);
3952
892k
    if (v == NULL)
3953
0
        return NULL;
3954
3955
    /* The normal path */
3956
892k
    if (PyBytes_Check(v))
3957
892k
        return v;
3958
3959
    /* If the codec returns a buffer, raise a warning and convert to bytes */
3960
0
    if (PyByteArray_Check(v)) {
3961
0
        int error;
3962
0
        PyObject *b;
3963
3964
0
        error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
3965
0
            "encoder %s returned bytearray instead of bytes; "
3966
0
            "use codecs.encode() to encode to arbitrary types",
3967
0
            encoding);
3968
0
        if (error) {
3969
0
            Py_DECREF(v);
3970
0
            return NULL;
3971
0
        }
3972
3973
0
        b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v),
3974
0
                                      PyByteArray_GET_SIZE(v));
3975
0
        Py_DECREF(v);
3976
0
        return b;
3977
0
    }
3978
3979
0
    PyErr_Format(PyExc_TypeError,
3980
0
                 "'%.400s' encoder returned '%.400s' instead of 'bytes'; "
3981
0
                 "use codecs.encode() to encode to arbitrary types",
3982
0
                 encoding,
3983
0
                 Py_TYPE(v)->tp_name);
3984
0
    Py_DECREF(v);
3985
0
    return NULL;
3986
0
}
3987
3988
PyAPI_FUNC(PyObject *)
3989
PyUnicode_AsEncodedUnicode(PyObject *unicode,
3990
                           const char *encoding,
3991
                           const char *errors)
3992
0
{
3993
0
    PyObject *v;
3994
3995
0
    if (!PyUnicode_Check(unicode)) {
3996
0
        PyErr_BadArgument();
3997
0
        goto onError;
3998
0
    }
3999
4000
0
    if (encoding == NULL)
4001
0
        encoding = PyUnicode_GetDefaultEncoding();
4002
4003
    /* Encode via the codec registry */
4004
0
    v = PyCodec_Encode(unicode, encoding, errors);
4005
0
    if (v == NULL)
4006
0
        goto onError;
4007
0
    if (!PyUnicode_Check(v)) {
4008
0
        PyErr_Format(PyExc_TypeError,
4009
0
                     "'%.400s' encoder returned '%.400s' instead of 'str'; "
4010
0
                     "use codecs.encode() to encode to arbitrary types",
4011
0
                     encoding,
4012
0
                     Py_TYPE(v)->tp_name);
4013
0
        Py_DECREF(v);
4014
0
        goto onError;
4015
0
    }
4016
0
    return v;
4017
4018
0
  onError:
4019
0
    return NULL;
4020
0
}
4021
4022
static PyObject*
4023
unicode_decode_locale(const char *str, Py_ssize_t len,
4024
                      _Py_error_handler errors, int current_locale)
4025
17.2k
{
4026
17.2k
    if (str[len] != '\0' || (size_t)len != strlen(str))  {
4027
0
        PyErr_SetString(PyExc_ValueError, "embedded null byte");
4028
0
        return NULL;
4029
0
    }
4030
4031
17.2k
    wchar_t *wstr;
4032
17.2k
    size_t wlen;
4033
17.2k
    const char *reason;
4034
17.2k
    int res = _Py_DecodeLocaleEx(str, &wstr, &wlen, &reason,
4035
17.2k
                                 current_locale, errors);
4036
17.2k
    if (res != 0) {
4037
0
        if (res == -2) {
4038
0
            PyObject *exc;
4039
0
            exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nns",
4040
0
                                        "locale", str, len,
4041
0
                                        (Py_ssize_t)wlen,
4042
0
                                        (Py_ssize_t)(wlen + 1),
4043
0
                                        reason);
4044
0
            if (exc != NULL) {
4045
0
                PyCodec_StrictErrors(exc);
4046
0
                Py_DECREF(exc);
4047
0
            }
4048
0
        }
4049
0
        else if (res == -3) {
4050
0
            PyErr_SetString(PyExc_ValueError, "unsupported error handler");
4051
0
        }
4052
0
        else {
4053
0
            PyErr_NoMemory();
4054
0
        }
4055
0
        return NULL;
4056
0
    }
4057
4058
17.2k
    PyObject *unicode = PyUnicode_FromWideChar(wstr, wlen);
4059
17.2k
    PyMem_RawFree(wstr);
4060
17.2k
    return unicode;
4061
17.2k
}
4062
4063
PyObject*
4064
PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len,
4065
                              const char *errors)
4066
0
{
4067
0
    _Py_error_handler error_handler = _Py_GetErrorHandler(errors);
4068
0
    return unicode_decode_locale(str, len, error_handler, 1);
4069
0
}
4070
4071
PyObject*
4072
PyUnicode_DecodeLocale(const char *str, const char *errors)
4073
12.1k
{
4074
12.1k
    Py_ssize_t size = (Py_ssize_t)strlen(str);
4075
12.1k
    _Py_error_handler error_handler = _Py_GetErrorHandler(errors);
4076
12.1k
    return unicode_decode_locale(str, size, error_handler, 1);
4077
12.1k
}
4078
4079
4080
PyObject*
4081
0
PyUnicode_DecodeFSDefault(const char *s) {
4082
0
    Py_ssize_t size = (Py_ssize_t)strlen(s);
4083
0
    return PyUnicode_DecodeFSDefaultAndSize(s, size);
4084
0
}
4085
4086
PyObject*
4087
PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
4088
6.71k
{
4089
6.71k
    PyInterpreterState *interp = _PyInterpreterState_GET();
4090
6.71k
    struct _Py_unicode_fs_codec *fs_codec = &interp->unicode.fs_codec;
4091
6.71k
    if (fs_codec->utf8) {
4092
1.59k
        return unicode_decode_utf8(s, size,
4093
1.59k
                                   fs_codec->error_handler,
4094
1.59k
                                   fs_codec->errors,
4095
1.59k
                                   NULL);
4096
1.59k
    }
4097
5.12k
#ifndef _Py_FORCE_UTF8_FS_ENCODING
4098
5.12k
    else if (fs_codec->encoding) {
4099
0
        return PyUnicode_Decode(s, size,
4100
0
                                fs_codec->encoding,
4101
0
                                fs_codec->errors);
4102
0
    }
4103
5.12k
#endif
4104
5.12k
    else {
4105
        /* Before _PyUnicode_InitEncodings() is called, the Python codec
4106
           machinery is not ready and so cannot be used:
4107
           use mbstowcs() in this case. */
4108
5.12k
        const PyConfig *config = _PyInterpreterState_GetConfig(interp);
4109
5.12k
        const wchar_t *filesystem_errors = config->filesystem_errors;
4110
5.12k
        assert(filesystem_errors != NULL);
4111
5.12k
        _Py_error_handler errors = get_error_handler_wide(filesystem_errors);
4112
5.12k
        assert(errors != _Py_ERROR_UNKNOWN);
4113
#ifdef _Py_FORCE_UTF8_FS_ENCODING
4114
        return unicode_decode_utf8(s, size, errors, NULL, NULL);
4115
#else
4116
5.12k
        return unicode_decode_locale(s, size, errors, 0);
4117
5.12k
#endif
4118
5.12k
    }
4119
6.71k
}
4120
4121
4122
int
4123
PyUnicode_FSConverter(PyObject* arg, void* addr)
4124
12.7k
{
4125
12.7k
    PyObject *path = NULL;
4126
12.7k
    PyObject *output = NULL;
4127
12.7k
    Py_ssize_t size;
4128
12.7k
    const char *data;
4129
12.7k
    if (arg == NULL) {
4130
0
        Py_DECREF(*(PyObject**)addr);
4131
0
        *(PyObject**)addr = NULL;
4132
0
        return 1;
4133
0
    }
4134
12.7k
    path = PyOS_FSPath(arg);
4135
12.7k
    if (path == NULL) {
4136
0
        return 0;
4137
0
    }
4138
12.7k
    if (PyBytes_Check(path)) {
4139
0
        output = path;
4140
0
    }
4141
12.7k
    else {  // PyOS_FSPath() guarantees its returned value is bytes or str.
4142
12.7k
        output = PyUnicode_EncodeFSDefault(path);
4143
12.7k
        Py_DECREF(path);
4144
12.7k
        if (!output) {
4145
0
            return 0;
4146
0
        }
4147
12.7k
        assert(PyBytes_Check(output));
4148
12.7k
    }
4149
4150
12.7k
    size = PyBytes_GET_SIZE(output);
4151
12.7k
    data = PyBytes_AS_STRING(output);
4152
12.7k
    if ((size_t)size != strlen(data)) {
4153
0
        PyErr_SetString(PyExc_ValueError, "embedded null byte");
4154
0
        Py_DECREF(output);
4155
0
        return 0;
4156
0
    }
4157
12.7k
    *(PyObject**)addr = output;
4158
12.7k
    return Py_CLEANUP_SUPPORTED;
4159
12.7k
}
4160
4161
4162
int
4163
PyUnicode_FSDecoder(PyObject* arg, void* addr)
4164
23.0k
{
4165
23.0k
    if (arg == NULL) {
4166
0
        Py_DECREF(*(PyObject**)addr);
4167
0
        *(PyObject**)addr = NULL;
4168
0
        return 1;
4169
0
    }
4170
4171
23.0k
    PyObject *path = PyOS_FSPath(arg);
4172
23.0k
    if (path == NULL) {
4173
0
        return 0;
4174
0
    }
4175
4176
23.0k
    PyObject *output = NULL;
4177
23.0k
    if (PyUnicode_Check(path)) {
4178
23.0k
        output = path;
4179
23.0k
    }
4180
0
    else if (PyBytes_Check(path)) {
4181
0
        output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(path),
4182
0
                                                  PyBytes_GET_SIZE(path));
4183
0
        Py_DECREF(path);
4184
0
        if (!output) {
4185
0
            return 0;
4186
0
        }
4187
0
    }
4188
0
    else {
4189
0
        PyErr_Format(PyExc_TypeError,
4190
0
                     "path should be string, bytes, or os.PathLike, not %.200s",
4191
0
                     Py_TYPE(arg)->tp_name);
4192
0
        Py_DECREF(path);
4193
0
        return 0;
4194
0
    }
4195
4196
23.0k
    if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output),
4197
23.0k
                 PyUnicode_GET_LENGTH(output), 0, 1) >= 0) {
4198
0
        PyErr_SetString(PyExc_ValueError, "embedded null character");
4199
0
        Py_DECREF(output);
4200
0
        return 0;
4201
0
    }
4202
23.0k
    *(PyObject**)addr = output;
4203
23.0k
    return Py_CLEANUP_SUPPORTED;
4204
23.0k
}
4205
4206
4207
static int unicode_fill_utf8(PyObject *unicode);
4208
4209
4210
static int
4211
unicode_ensure_utf8(PyObject *unicode)
4212
19.6M
{
4213
19.6M
    int err = 0;
4214
19.6M
    if (PyUnicode_UTF8(unicode) == NULL) {
4215
160k
        Py_BEGIN_CRITICAL_SECTION(unicode);
4216
160k
        if (PyUnicode_UTF8(unicode) == NULL) {
4217
160k
            err = unicode_fill_utf8(unicode);
4218
160k
        }
4219
160k
        Py_END_CRITICAL_SECTION();
4220
160k
    }
4221
19.6M
    return err;
4222
19.6M
}
4223
4224
const char *
4225
PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
4226
19.6M
{
4227
19.6M
    if (!PyUnicode_Check(unicode)) {
4228
0
        PyErr_BadArgument();
4229
0
        if (psize) {
4230
0
            *psize = -1;
4231
0
        }
4232
0
        return NULL;
4233
0
    }
4234
4235
19.6M
    if (unicode_ensure_utf8(unicode) == -1) {
4236
206
        if (psize) {
4237
206
            *psize = -1;
4238
206
        }
4239
206
        return NULL;
4240
206
    }
4241
4242
19.6M
    if (psize) {
4243
19.5M
        *psize = PyUnicode_UTF8_LENGTH(unicode);
4244
19.5M
    }
4245
19.6M
    return PyUnicode_UTF8(unicode);
4246
19.6M
}
4247
4248
const char *
4249
PyUnicode_AsUTF8(PyObject *unicode)
4250
69.6k
{
4251
69.6k
    return PyUnicode_AsUTF8AndSize(unicode, NULL);
4252
69.6k
}
4253
4254
const char *
4255
_PyUnicode_AsUTF8NoNUL(PyObject *unicode)
4256
955k
{
4257
955k
    Py_ssize_t size;
4258
955k
    const char *s = PyUnicode_AsUTF8AndSize(unicode, &size);
4259
955k
    if (s && strlen(s) != (size_t)size) {
4260
174
        PyErr_SetString(PyExc_ValueError, "embedded null character");
4261
174
        return NULL;
4262
174
    }
4263
955k
    return s;
4264
955k
}
4265
4266
/*
4267
PyUnicode_GetSize() has been deprecated since Python 3.3
4268
because it returned length of Py_UNICODE.
4269
4270
But this function is part of stable abi, because it doesn't
4271
include Py_UNICODE in signature and it was not excluded from
4272
stable ABI in PEP 384.
4273
*/
4274
PyAPI_FUNC(Py_ssize_t)
4275
PyUnicode_GetSize(PyObject *unicode)
4276
0
{
4277
0
    PyErr_SetString(PyExc_RuntimeError,
4278
0
                    "PyUnicode_GetSize has been removed.");
4279
0
    return -1;
4280
0
}
4281
4282
Py_ssize_t
4283
PyUnicode_GetLength(PyObject *unicode)
4284
32.9k
{
4285
32.9k
    if (!PyUnicode_Check(unicode)) {
4286
0
        PyErr_BadArgument();
4287
0
        return -1;
4288
0
    }
4289
32.9k
    return PyUnicode_GET_LENGTH(unicode);
4290
32.9k
}
4291
4292
Py_UCS4
4293
PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
4294
34
{
4295
34
    const void *data;
4296
34
    int kind;
4297
4298
34
    if (!PyUnicode_Check(unicode)) {
4299
0
        PyErr_BadArgument();
4300
0
        return (Py_UCS4)-1;
4301
0
    }
4302
34
    if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) {
4303
0
        PyErr_SetString(PyExc_IndexError, "string index out of range");
4304
0
        return (Py_UCS4)-1;
4305
0
    }
4306
34
    data = PyUnicode_DATA(unicode);
4307
34
    kind = PyUnicode_KIND(unicode);
4308
34
    return PyUnicode_READ(kind, data, index);
4309
34
}
4310
4311
int
4312
PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch)
4313
0
{
4314
0
    if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) {
4315
0
        PyErr_BadArgument();
4316
0
        return -1;
4317
0
    }
4318
0
    if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) {
4319
0
        PyErr_SetString(PyExc_IndexError, "string index out of range");
4320
0
        return -1;
4321
0
    }
4322
0
    if (unicode_check_modifiable(unicode))
4323
0
        return -1;
4324
0
    if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) {
4325
0
        PyErr_SetString(PyExc_ValueError, "character out of range");
4326
0
        return -1;
4327
0
    }
4328
0
    PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
4329
0
                    index, ch);
4330
0
    return 0;
4331
0
}
4332
4333
const char *
4334
PyUnicode_GetDefaultEncoding(void)
4335
0
{
4336
0
    return "utf-8";
4337
0
}
4338
4339
/* create or adjust a UnicodeDecodeError */
4340
static void
4341
make_decode_exception(PyObject **exceptionObject,
4342
                      const char *encoding,
4343
                      const char *input, Py_ssize_t length,
4344
                      Py_ssize_t startpos, Py_ssize_t endpos,
4345
                      const char *reason)
4346
306k
{
4347
306k
    if (*exceptionObject == NULL) {
4348
88.2k
        *exceptionObject = PyUnicodeDecodeError_Create(
4349
88.2k
            encoding, input, length, startpos, endpos, reason);
4350
88.2k
    }
4351
217k
    else {
4352
217k
        if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos))
4353
0
            goto onError;
4354
217k
        if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos))
4355
0
            goto onError;
4356
217k
        if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason))
4357
0
            goto onError;
4358
217k
    }
4359
306k
    return;
4360
4361
306k
onError:
4362
0
    Py_CLEAR(*exceptionObject);
4363
0
}
4364
4365
#ifdef MS_WINDOWS
4366
static int
4367
widechar_resize(wchar_t **buf, Py_ssize_t *size, Py_ssize_t newsize)
4368
{
4369
    if (newsize > *size) {
4370
        wchar_t *newbuf = *buf;
4371
        if (PyMem_Resize(newbuf, wchar_t, newsize) == NULL) {
4372
            PyErr_NoMemory();
4373
            return -1;
4374
        }
4375
        *buf = newbuf;
4376
    }
4377
    *size = newsize;
4378
    return 0;
4379
}
4380
4381
/* error handling callback helper:
4382
   build arguments, call the callback and check the arguments,
4383
   if no exception occurred, copy the replacement to the output
4384
   and adjust various state variables.
4385
   return 0 on success, -1 on error
4386
*/
4387
4388
static int
4389
unicode_decode_call_errorhandler_wchar(
4390
    const char *errors, PyObject **errorHandler,
4391
    const char *encoding, const char *reason,
4392
    const char **input, const char **inend, Py_ssize_t *startinpos,
4393
    Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr,
4394
    wchar_t **buf, Py_ssize_t *bufsize, Py_ssize_t *outpos)
4395
{
4396
    static const char *argparse = "Un;decoding error handler must return (str, int) tuple";
4397
4398
    PyObject *restuple = NULL;
4399
    PyObject *repunicode = NULL;
4400
    Py_ssize_t outsize;
4401
    Py_ssize_t insize;
4402
    Py_ssize_t requiredsize;
4403
    Py_ssize_t newpos;
4404
    PyObject *inputobj = NULL;
4405
    Py_ssize_t repwlen;
4406
4407
    if (*errorHandler == NULL) {
4408
        *errorHandler = PyCodec_LookupError(errors);
4409
        if (*errorHandler == NULL)
4410
            goto onError;
4411
    }
4412
4413
    make_decode_exception(exceptionObject,
4414
        encoding,
4415
        *input, *inend - *input,
4416
        *startinpos, *endinpos,
4417
        reason);
4418
    if (*exceptionObject == NULL)
4419
        goto onError;
4420
4421
    restuple = PyObject_CallOneArg(*errorHandler, *exceptionObject);
4422
    if (restuple == NULL)
4423
        goto onError;
4424
    if (!PyTuple_Check(restuple)) {
4425
        PyErr_SetString(PyExc_TypeError, &argparse[3]);
4426
        goto onError;
4427
    }
4428
    if (!PyArg_ParseTuple(restuple, argparse, &repunicode, &newpos))
4429
        goto onError;
4430
4431
    /* Copy back the bytes variables, which might have been modified by the
4432
       callback */
4433
    inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject);
4434
    if (!inputobj)
4435
        goto onError;
4436
    *input = PyBytes_AS_STRING(inputobj);
4437
    insize = PyBytes_GET_SIZE(inputobj);
4438
    *inend = *input + insize;
4439
    /* we can DECREF safely, as the exception has another reference,
4440
       so the object won't go away. */
4441
    Py_DECREF(inputobj);
4442
4443
    if (newpos<0)
4444
        newpos = insize+newpos;
4445
    if (newpos<0 || newpos>insize) {
4446
        PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos);
4447
        goto onError;
4448
    }
4449
4450
    repwlen = PyUnicode_AsWideChar(repunicode, NULL, 0);
4451
    if (repwlen < 0)
4452
        goto onError;
4453
    repwlen--;
4454
    /* need more space? (at least enough for what we
4455
       have+the replacement+the rest of the string (starting
4456
       at the new input position), so we won't have to check space
4457
       when there are no errors in the rest of the string) */
4458
    requiredsize = *outpos;
4459
    if (requiredsize > PY_SSIZE_T_MAX - repwlen)
4460
        goto overflow;
4461
    requiredsize += repwlen;
4462
    if (requiredsize > PY_SSIZE_T_MAX - (insize - newpos))
4463
        goto overflow;
4464
    requiredsize += insize - newpos;
4465
    outsize = *bufsize;
4466
    if (requiredsize > outsize) {
4467
        if (outsize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*outsize)
4468
            requiredsize = 2*outsize;
4469
        if (widechar_resize(buf, bufsize, requiredsize) < 0) {
4470
            goto onError;
4471
        }
4472
    }
4473
    PyUnicode_AsWideChar(repunicode, *buf + *outpos, repwlen);
4474
    *outpos += repwlen;
4475
    *endinpos = newpos;
4476
    *inptr = *input + newpos;
4477
4478
    /* we made it! */
4479
    Py_DECREF(restuple);
4480
    return 0;
4481
4482
  overflow:
4483
    PyErr_SetString(PyExc_OverflowError,
4484
                    "decoded result is too long for a Python string");
4485
4486
  onError:
4487
    Py_XDECREF(restuple);
4488
    return -1;
4489
}
4490
#endif   /* MS_WINDOWS */
4491
4492
static int
4493
unicode_decode_call_errorhandler_writer(
4494
    const char *errors, PyObject **errorHandler,
4495
    const char *encoding, const char *reason,
4496
    const char **input, const char **inend, Py_ssize_t *startinpos,
4497
    Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr,
4498
    _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */)
4499
306k
{
4500
306k
    static const char *argparse = "Un;decoding error handler must return (str, int) tuple";
4501
4502
306k
    PyObject *restuple = NULL;
4503
306k
    PyObject *repunicode = NULL;
4504
306k
    Py_ssize_t insize;
4505
306k
    Py_ssize_t newpos;
4506
306k
    Py_ssize_t replen;
4507
306k
    Py_ssize_t remain;
4508
306k
    PyObject *inputobj = NULL;
4509
306k
    int need_to_grow = 0;
4510
306k
    const char *new_inptr;
4511
4512
306k
    if (*errorHandler == NULL) {
4513
88.2k
        *errorHandler = PyCodec_LookupError(errors);
4514
88.2k
        if (*errorHandler == NULL)
4515
0
            goto onError;
4516
88.2k
    }
4517
4518
306k
    make_decode_exception(exceptionObject,
4519
306k
        encoding,
4520
306k
        *input, *inend - *input,
4521
306k
        *startinpos, *endinpos,
4522
306k
        reason);
4523
306k
    if (*exceptionObject == NULL)
4524
0
        goto onError;
4525
4526
306k
    restuple = PyObject_CallOneArg(*errorHandler, *exceptionObject);
4527
306k
    if (restuple == NULL)
4528
56.9k
        goto onError;
4529
249k
    if (!PyTuple_Check(restuple)) {
4530
0
        PyErr_SetString(PyExc_TypeError, &argparse[3]);
4531
0
        goto onError;
4532
0
    }
4533
249k
    if (!PyArg_ParseTuple(restuple, argparse, &repunicode, &newpos))
4534
0
        goto onError;
4535
4536
    /* Copy back the bytes variables, which might have been modified by the
4537
       callback */
4538
249k
    inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject);
4539
249k
    if (!inputobj)
4540
0
        goto onError;
4541
249k
    remain = *inend - *input - *endinpos;
4542
249k
    *input = PyBytes_AS_STRING(inputobj);
4543
249k
    insize = PyBytes_GET_SIZE(inputobj);
4544
249k
    *inend = *input + insize;
4545
    /* we can DECREF safely, as the exception has another reference,
4546
       so the object won't go away. */
4547
249k
    Py_DECREF(inputobj);
4548
4549
249k
    if (newpos<0)
4550
0
        newpos = insize+newpos;
4551
249k
    if (newpos<0 || newpos>insize) {
4552
0
        PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos);
4553
0
        goto onError;
4554
0
    }
4555
4556
249k
    replen = PyUnicode_GET_LENGTH(repunicode);
4557
249k
    if (replen > 1) {
4558
24.6k
        writer->min_length += replen - 1;
4559
24.6k
        need_to_grow = 1;
4560
24.6k
    }
4561
249k
    new_inptr = *input + newpos;
4562
249k
    if (*inend - new_inptr > remain) {
4563
        /* We don't know the decoding algorithm here so we make the worst
4564
           assumption that one byte decodes to one unicode character.
4565
           If unfortunately one byte could decode to more unicode characters,
4566
           the decoder may write out-of-bound then.  Is it possible for the
4567
           algorithms using this function? */
4568
13.1k
        writer->min_length += *inend - new_inptr - remain;
4569
13.1k
        need_to_grow = 1;
4570
13.1k
    }
4571
249k
    if (need_to_grow) {
4572
24.8k
        writer->overallocate = 1;
4573
24.8k
        if (_PyUnicodeWriter_Prepare(writer, writer->min_length - writer->pos,
4574
24.8k
                            PyUnicode_MAX_CHAR_VALUE(repunicode)) == -1)
4575
0
            goto onError;
4576
24.8k
    }
4577
249k
    if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1)
4578
0
        goto onError;
4579
4580
249k
    *endinpos = newpos;
4581
249k
    *inptr = new_inptr;
4582
4583
    /* we made it! */
4584
249k
    Py_DECREF(restuple);
4585
249k
    return 0;
4586
4587
56.9k
  onError:
4588
56.9k
    Py_XDECREF(restuple);
4589
56.9k
    return -1;
4590
249k
}
4591
4592
/* --- UTF-7 Codec -------------------------------------------------------- */
4593
4594
/* See RFC2152 for details.  We encode conservatively and decode liberally. */
4595
4596
/* Three simple macros defining base-64. */
4597
4598
/* Is c a base-64 character? */
4599
4600
#define IS_BASE64(c) \
4601
299k
    (((c) >= 'A' && (c) <= 'Z') ||     \
4602
299k
     ((c) >= 'a' && (c) <= 'z') ||     \
4603
299k
     ((c) >= '0' && (c) <= '9') ||     \
4604
299k
     (c) == '+' || (c) == '/')
4605
4606
/* given that c is a base-64 character, what is its base-64 value? */
4607
4608
#define FROM_BASE64(c)                                                  \
4609
252k
    (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' :                           \
4610
252k
     ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 :                      \
4611
179k
     ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 :                      \
4612
104k
     (c) == '+' ? 62 : 63)
4613
4614
/* What is the base-64 character of the bottom 6 bits of n? */
4615
4616
#define TO_BASE64(n)  \
4617
0
    ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f])
4618
4619
/* DECODE_DIRECT: this byte encountered in a UTF-7 string should be
4620
 * decoded as itself.  We are permissive on decoding; the only ASCII
4621
 * byte not decoding to itself is the + which begins a base64
4622
 * string. */
4623
4624
#define DECODE_DIRECT(c)                                \
4625
4.13M
    ((c) <= 127 && (c) != '+')
4626
4627
/* The UTF-7 encoder treats ASCII characters differently according to
4628
 * whether they are Set D, Set O, Whitespace, or special (i.e. none of
4629
 * the above).  See RFC2152.  This array identifies these different
4630
 * sets:
4631
 * 0 : "Set D"
4632
 *     alphanumeric and '(),-./:?
4633
 * 1 : "Set O"
4634
 *     !"#$%&*;<=>@[]^_`{|}
4635
 * 2 : "whitespace"
4636
 *     ht nl cr sp
4637
 * 3 : special (must be base64 encoded)
4638
 *     everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127)
4639
 */
4640
4641
static
4642
char utf7_category[128] = {
4643
/* nul soh stx etx eot enq ack bel bs  ht  nl  vt  np  cr  so  si  */
4644
    3,  3,  3,  3,  3,  3,  3,  3,  3,  2,  2,  3,  3,  2,  3,  3,
4645
/* dle dc1 dc2 dc3 dc4 nak syn etb can em  sub esc fs  gs  rs  us  */
4646
    3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,
4647
/* sp   !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /  */
4648
    2,  1,  1,  1,  1,  1,  1,  0,  0,  0,  1,  3,  0,  0,  0,  0,
4649
/*  0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?  */
4650
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  0,
4651
/*  @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O  */
4652
    1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
4653
/*  P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _  */
4654
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  3,  1,  1,  1,
4655
/*  `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o  */
4656
    1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
4657
/*  p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~  del */
4658
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  3,  3,
4659
};
4660
4661
/* ENCODE_DIRECT: this character should be encoded as itself.  The
4662
 * answer depends on whether we are encoding set O as itself, and also
4663
 * on whether we are encoding whitespace as itself.  RFC2152 makes it
4664
 * clear that the answers to these questions vary between
4665
 * applications, so this code needs to be flexible.  */
4666
4667
#define ENCODE_DIRECT(c, directO, directWS)             \
4668
0
    ((c) < 128 && (c) > 0 &&                            \
4669
0
     ((utf7_category[(c)] == 0) ||                      \
4670
0
      (directWS && (utf7_category[(c)] == 2)) ||        \
4671
0
      (directO && (utf7_category[(c)] == 1))))
4672
4673
PyObject *
4674
PyUnicode_DecodeUTF7(const char *s,
4675
                     Py_ssize_t size,
4676
                     const char *errors)
4677
0
{
4678
0
    return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL);
4679
0
}
4680
4681
/* The decoder.  The only state we preserve is our read position,
4682
 * i.e. how many characters we have consumed.  So if we end in the
4683
 * middle of a shift sequence we have to back off the read position
4684
 * and the output to the beginning of the sequence, otherwise we lose
4685
 * all the shift state (seen bits, number of bits seen, high
4686
 * surrogate). */
4687
4688
PyObject *
4689
PyUnicode_DecodeUTF7Stateful(const char *s,
4690
                             Py_ssize_t size,
4691
                             const char *errors,
4692
                             Py_ssize_t *consumed)
4693
23.0k
{
4694
23.0k
    const char *starts = s;
4695
23.0k
    Py_ssize_t startinpos;
4696
23.0k
    Py_ssize_t endinpos;
4697
23.0k
    const char *e;
4698
23.0k
    _PyUnicodeWriter writer;
4699
23.0k
    const char *errmsg = "";
4700
23.0k
    int inShift = 0;
4701
23.0k
    Py_ssize_t shiftOutStart;
4702
23.0k
    unsigned int base64bits = 0;
4703
23.0k
    unsigned long base64buffer = 0;
4704
23.0k
    Py_UCS4 surrogate = 0;
4705
23.0k
    PyObject *errorHandler = NULL;
4706
23.0k
    PyObject *exc = NULL;
4707
4708
23.0k
    if (size == 0) {
4709
0
        if (consumed)
4710
0
            *consumed = 0;
4711
0
        _Py_RETURN_UNICODE_EMPTY();
4712
0
    }
4713
4714
    /* Start off assuming it's all ASCII. Widen later as necessary. */
4715
23.0k
    _PyUnicodeWriter_Init(&writer);
4716
23.0k
    writer.min_length = size;
4717
4718
23.0k
    shiftOutStart = 0;
4719
23.0k
    e = s + size;
4720
4721
4.44M
    while (s < e) {
4722
4.43M
        Py_UCS4 ch;
4723
4.43M
      restart:
4724
4.43M
        ch = (unsigned char) *s;
4725
4726
4.43M
        if (inShift) { /* in a base-64 section */
4727
272k
            if (IS_BASE64(ch)) { /* consume a base-64 character */
4728
252k
                base64buffer = (base64buffer << 6) | FROM_BASE64(ch);
4729
252k
                base64bits += 6;
4730
252k
                s++;
4731
252k
                if (base64bits >= 16) {
4732
                    /* we have enough bits for a UTF-16 value */
4733
86.9k
                    Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16));
4734
86.9k
                    base64bits -= 16;
4735
86.9k
                    base64buffer &= (1 << base64bits) - 1; /* clear high bits */
4736
86.9k
                    assert(outCh <= 0xffff);
4737
86.9k
                    if (surrogate) {
4738
                        /* expecting a second surrogate */
4739
8.55k
                        if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) {
4740
3.13k
                            Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh);
4741
3.13k
                            if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0)
4742
0
                                goto onError;
4743
3.13k
                            surrogate = 0;
4744
3.13k
                            continue;
4745
3.13k
                        }
4746
5.42k
                        else {
4747
5.42k
                            if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0)
4748
0
                                goto onError;
4749
5.42k
                            surrogate = 0;
4750
5.42k
                        }
4751
8.55k
                    }
4752
83.8k
                    if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) {
4753
                        /* first surrogate */
4754
11.9k
                        surrogate = outCh;
4755
11.9k
                    }
4756
71.8k
                    else {
4757
71.8k
                        if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0)
4758
0
                            goto onError;
4759
71.8k
                    }
4760
83.8k
                }
4761
252k
            }
4762
20.0k
            else { /* now leaving a base-64 section */
4763
20.0k
                inShift = 0;
4764
20.0k
                if (base64bits > 0) { /* left-over bits */
4765
16.8k
                    if (base64bits >= 6) {
4766
                        /* We've seen at least one base-64 character */
4767
9.43k
                        s++;
4768
9.43k
                        errmsg = "partial character in shift sequence";
4769
9.43k
                        goto utf7Error;
4770
9.43k
                    }
4771
7.42k
                    else {
4772
                        /* Some bits remain; they should be zero */
4773
7.42k
                        if (base64buffer != 0) {
4774
1.55k
                            s++;
4775
1.55k
                            errmsg = "non-zero padding bits in shift sequence";
4776
1.55k
                            goto utf7Error;
4777
1.55k
                        }
4778
7.42k
                    }
4779
16.8k
                }
4780
9.03k
                if (surrogate && DECODE_DIRECT(ch)) {
4781
2.53k
                    if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0)
4782
0
                        goto onError;
4783
2.53k
                }
4784
9.03k
                surrogate = 0;
4785
9.03k
                if (ch == '-') {
4786
                    /* '-' is absorbed; other terminating
4787
                       characters are preserved */
4788
2.46k
                    s++;
4789
2.46k
                }
4790
9.03k
            }
4791
272k
        }
4792
4.15M
        else if ( ch == '+' ) {
4793
30.0k
            startinpos = s-starts;
4794
30.0k
            s++; /* consume '+' */
4795
30.0k
            if (s < e && *s == '-') { /* '+-' encodes '+' */
4796
2.33k
                s++;
4797
2.33k
                if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0)
4798
0
                    goto onError;
4799
2.33k
            }
4800
27.7k
            else if (s < e && !IS_BASE64(*s)) {
4801
4.22k
                s++;
4802
4.22k
                errmsg = "ill-formed sequence";
4803
4.22k
                goto utf7Error;
4804
4.22k
            }
4805
23.4k
            else { /* begin base64-encoded section */
4806
23.4k
                inShift = 1;
4807
23.4k
                surrogate = 0;
4808
23.4k
                shiftOutStart = writer.pos;
4809
23.4k
                base64bits = 0;
4810
23.4k
                base64buffer = 0;
4811
23.4k
            }
4812
30.0k
        }
4813
4.12M
        else if (DECODE_DIRECT(ch)) { /* character decodes as itself */
4814
4.02M
            s++;
4815
4.02M
            if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0)
4816
0
                goto onError;
4817
4.02M
        }
4818
101k
        else {
4819
101k
            startinpos = s-starts;
4820
101k
            s++;
4821
101k
            errmsg = "unexpected special character";
4822
101k
            goto utf7Error;
4823
101k
        }
4824
4.31M
        continue;
4825
4.31M
utf7Error:
4826
116k
        endinpos = s-starts;
4827
116k
        if (unicode_decode_call_errorhandler_writer(
4828
116k
                errors, &errorHandler,
4829
116k
                "utf7", errmsg,
4830
116k
                &starts, &e, &startinpos, &endinpos, &exc, &s,
4831
116k
                &writer))
4832
9.50k
            goto onError;
4833
116k
    }
4834
4835
    /* end of string */
4836
4837
13.5k
    if (inShift && !consumed) { /* in shift sequence, no more to follow */
4838
        /* if we're in an inconsistent state, that's an error */
4839
3.47k
        inShift = 0;
4840
3.47k
        if (surrogate ||
4841
3.47k
                (base64bits >= 6) ||
4842
3.47k
                (base64bits > 0 && base64buffer != 0)) {
4843
2.08k
            endinpos = size;
4844
2.08k
            if (unicode_decode_call_errorhandler_writer(
4845
2.08k
                    errors, &errorHandler,
4846
2.08k
                    "utf7", "unterminated shift sequence",
4847
2.08k
                    &starts, &e, &startinpos, &endinpos, &exc, &s,
4848
2.08k
                    &writer))
4849
1.73k
                goto onError;
4850
349
            if (s < e)
4851
0
                goto restart;
4852
349
        }
4853
3.47k
    }
4854
4855
    /* return state */
4856
11.7k
    if (consumed) {
4857
0
        if (inShift) {
4858
0
            *consumed = startinpos;
4859
0
            if (writer.pos != shiftOutStart && writer.maxchar > 127) {
4860
0
                PyObject *result = PyUnicode_FromKindAndData(
4861
0
                        writer.kind, writer.data, shiftOutStart);
4862
0
                Py_XDECREF(errorHandler);
4863
0
                Py_XDECREF(exc);
4864
0
                _PyUnicodeWriter_Dealloc(&writer);
4865
0
                return result;
4866
0
            }
4867
0
            writer.pos = shiftOutStart; /* back off output */
4868
0
        }
4869
0
        else {
4870
0
            *consumed = s-starts;
4871
0
        }
4872
0
    }
4873
4874
11.7k
    Py_XDECREF(errorHandler);
4875
11.7k
    Py_XDECREF(exc);
4876
11.7k
    return _PyUnicodeWriter_Finish(&writer);
4877
4878
11.2k
  onError:
4879
11.2k
    Py_XDECREF(errorHandler);
4880
11.2k
    Py_XDECREF(exc);
4881
11.2k
    _PyUnicodeWriter_Dealloc(&writer);
4882
11.2k
    return NULL;
4883
11.7k
}
4884
4885
4886
PyObject *
4887
_PyUnicode_EncodeUTF7(PyObject *str,
4888
                      int base64SetO,
4889
                      int base64WhiteSpace,
4890
                      const char *errors)
4891
0
{
4892
0
    int kind;
4893
0
    const void *data;
4894
0
    Py_ssize_t len;
4895
0
    PyObject *v;
4896
0
    int inShift = 0;
4897
0
    Py_ssize_t i;
4898
0
    unsigned int base64bits = 0;
4899
0
    unsigned long base64buffer = 0;
4900
0
    char * out;
4901
0
    const char * start;
4902
4903
0
    kind = PyUnicode_KIND(str);
4904
0
    data = PyUnicode_DATA(str);
4905
0
    len = PyUnicode_GET_LENGTH(str);
4906
4907
0
    if (len == 0)
4908
0
        return PyBytes_FromStringAndSize(NULL, 0);
4909
4910
    /* It might be possible to tighten this worst case */
4911
0
    if (len > PY_SSIZE_T_MAX / 8)
4912
0
        return PyErr_NoMemory();
4913
0
    v = PyBytes_FromStringAndSize(NULL, len * 8);
4914
0
    if (v == NULL)
4915
0
        return NULL;
4916
4917
0
    start = out = PyBytes_AS_STRING(v);
4918
0
    for (i = 0; i < len; ++i) {
4919
0
        Py_UCS4 ch = PyUnicode_READ(kind, data, i);
4920
4921
0
        if (inShift) {
4922
0
            if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) {
4923
                /* shifting out */
4924
0
                if (base64bits) { /* output remaining bits */
4925
0
                    *out++ = TO_BASE64(base64buffer << (6-base64bits));
4926
0
                    base64buffer = 0;
4927
0
                    base64bits = 0;
4928
0
                }
4929
0
                inShift = 0;
4930
                /* Characters not in the BASE64 set implicitly unshift the sequence
4931
                   so no '-' is required, except if the character is itself a '-' */
4932
0
                if (IS_BASE64(ch) || ch == '-') {
4933
0
                    *out++ = '-';
4934
0
                }
4935
0
                *out++ = (char) ch;
4936
0
            }
4937
0
            else {
4938
0
                goto encode_char;
4939
0
            }
4940
0
        }
4941
0
        else { /* not in a shift sequence */
4942
0
            if (ch == '+') {
4943
0
                *out++ = '+';
4944
0
                        *out++ = '-';
4945
0
            }
4946
0
            else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) {
4947
0
                *out++ = (char) ch;
4948
0
            }
4949
0
            else {
4950
0
                *out++ = '+';
4951
0
                inShift = 1;
4952
0
                goto encode_char;
4953
0
            }
4954
0
        }
4955
0
        continue;
4956
0
encode_char:
4957
0
        if (ch >= 0x10000) {
4958
0
            assert(ch <= MAX_UNICODE);
4959
4960
            /* code first surrogate */
4961
0
            base64bits += 16;
4962
0
            base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch);
4963
0
            while (base64bits >= 6) {
4964
0
                *out++ = TO_BASE64(base64buffer >> (base64bits-6));
4965
0
                base64bits -= 6;
4966
0
            }
4967
            /* prepare second surrogate */
4968
0
            ch = Py_UNICODE_LOW_SURROGATE(ch);
4969
0
        }
4970
0
        base64bits += 16;
4971
0
        base64buffer = (base64buffer << 16) | ch;
4972
0
        while (base64bits >= 6) {
4973
0
            *out++ = TO_BASE64(base64buffer >> (base64bits-6));
4974
0
            base64bits -= 6;
4975
0
        }
4976
0
    }
4977
0
    if (base64bits)
4978
0
        *out++= TO_BASE64(base64buffer << (6-base64bits) );
4979
0
    if (inShift)
4980
0
        *out++ = '-';
4981
0
    if (_PyBytes_Resize(&v, out - start) < 0)
4982
0
        return NULL;
4983
0
    return v;
4984
0
}
4985
4986
#undef IS_BASE64
4987
#undef FROM_BASE64
4988
#undef TO_BASE64
4989
#undef DECODE_DIRECT
4990
#undef ENCODE_DIRECT
4991
4992
/* --- UTF-8 Codec -------------------------------------------------------- */
4993
4994
PyObject *
4995
PyUnicode_DecodeUTF8(const char *s,
4996
                     Py_ssize_t size,
4997
                     const char *errors)
4998
2.39M
{
4999
2.39M
    return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL);
5000
2.39M
}
5001
5002
#include "stringlib/asciilib.h"
5003
#include "stringlib/codecs.h"
5004
#include "stringlib/undef.h"
5005
5006
#include "stringlib/ucs1lib.h"
5007
#include "stringlib/codecs.h"
5008
#include "stringlib/undef.h"
5009
5010
#include "stringlib/ucs2lib.h"
5011
#include "stringlib/codecs.h"
5012
#include "stringlib/undef.h"
5013
5014
#include "stringlib/ucs4lib.h"
5015
#include "stringlib/codecs.h"
5016
#include "stringlib/undef.h"
5017
5018
#if (SIZEOF_SIZE_T == 8)
5019
/* Mask to quickly check whether a C 'size_t' contains a
5020
   non-ASCII, UTF8-encoded char. */
5021
126M
# define ASCII_CHAR_MASK 0x8080808080808080ULL
5022
// used to count codepoints in UTF-8 string.
5023
290M
# define VECTOR_0101     0x0101010101010101ULL
5024
2.32M
# define VECTOR_00FF     0x00ff00ff00ff00ffULL
5025
#elif (SIZEOF_SIZE_T == 4)
5026
# define ASCII_CHAR_MASK 0x80808080U
5027
# define VECTOR_0101     0x01010101U
5028
# define VECTOR_00FF     0x00ff00ffU
5029
#else
5030
# error C 'size_t' size should be either 4 or 8!
5031
#endif
5032
5033
#if (defined(__clang__) || defined(__GNUC__))
5034
#define HAVE_CTZ 1
5035
static inline unsigned int
5036
ctz(size_t v)
5037
419k
{
5038
419k
    return __builtin_ctzll((unsigned long long)v);
5039
419k
}
5040
#elif defined(_MSC_VER)
5041
#define HAVE_CTZ 1
5042
static inline unsigned int
5043
ctz(size_t v)
5044
{
5045
    unsigned long pos;
5046
#if SIZEOF_SIZE_T == 4
5047
    _BitScanForward(&pos, v);
5048
#else
5049
    _BitScanForward64(&pos, v);
5050
#endif /* SIZEOF_SIZE_T */
5051
    return pos;
5052
}
5053
#else
5054
#define HAVE_CTZ 0
5055
#endif
5056
5057
#if HAVE_CTZ && PY_LITTLE_ENDIAN
5058
// load p[0]..p[size-1] as a size_t without unaligned access nor read ahead.
5059
static size_t
5060
load_unaligned(const unsigned char *p, size_t size)
5061
14.8M
{
5062
14.8M
    union {
5063
14.8M
        size_t s;
5064
14.8M
        unsigned char b[SIZEOF_SIZE_T];
5065
14.8M
    } u;
5066
14.8M
    u.s = 0;
5067
    // This switch statement assumes little endian because:
5068
    // * union is faster than bitwise or and shift.
5069
    // * big endian machine is rare and hard to maintain.
5070
14.8M
    switch (size) {
5071
0
    default:
5072
0
#if SIZEOF_SIZE_T == 8
5073
0
    case 8:
5074
0
        u.b[7] = p[7];
5075
0
        _Py_FALLTHROUGH;
5076
901k
    case 7:
5077
901k
        u.b[6] = p[6];
5078
901k
        _Py_FALLTHROUGH;
5079
3.37M
    case 6:
5080
3.37M
        u.b[5] = p[5];
5081
3.37M
        _Py_FALLTHROUGH;
5082
4.00M
    case 5:
5083
4.00M
        u.b[4] = p[4];
5084
4.00M
        _Py_FALLTHROUGH;
5085
4.00M
#endif
5086
4.49M
    case 4:
5087
4.49M
        u.b[3] = p[3];
5088
4.49M
        _Py_FALLTHROUGH;
5089
10.7M
    case 3:
5090
10.7M
        u.b[2] = p[2];
5091
10.7M
        _Py_FALLTHROUGH;
5092
14.3M
    case 2:
5093
14.3M
        u.b[1] = p[1];
5094
14.3M
        _Py_FALLTHROUGH;
5095
14.6M
    case 1:
5096
14.6M
        u.b[0] = p[0];
5097
14.6M
        break;
5098
210k
    case 0:
5099
210k
        break;
5100
14.8M
    }
5101
14.8M
    return u.s;
5102
14.8M
}
5103
#endif
5104
5105
/*
5106
 * Find the first non-ASCII character in a byte sequence.
5107
 *
5108
 * This function scans a range of bytes from `start` to `end` and returns the
5109
 * index of the first byte that is not an ASCII character (i.e., has the most
5110
 * significant bit set). If all characters in the range are ASCII, it returns
5111
 * `end - start`.
5112
 */
5113
static Py_ssize_t
5114
find_first_nonascii(const unsigned char *start, const unsigned char *end)
5115
15.1M
{
5116
    // The search is done in `size_t` chunks.
5117
    // The start and end might not be aligned at `size_t` boundaries,
5118
    // so they're handled specially.
5119
5120
15.1M
    const unsigned char *p = start;
5121
5122
15.1M
    if (end - start >= SIZEOF_SIZE_T) {
5123
        // Avoid unaligned read.
5124
3.64M
#if PY_LITTLE_ENDIAN && HAVE_CTZ
5125
3.64M
        size_t u;
5126
3.64M
        memcpy(&u, p, sizeof(size_t));
5127
3.64M
        u &= ASCII_CHAR_MASK;
5128
3.64M
        if (u) {
5129
144k
            return (ctz(u) - 7) / 8;
5130
144k
        }
5131
3.49M
        p = _Py_ALIGN_DOWN(p + SIZEOF_SIZE_T, SIZEOF_SIZE_T);
5132
#else /* PY_LITTLE_ENDIAN && HAVE_CTZ */
5133
        const unsigned char *p2 = _Py_ALIGN_UP(p, SIZEOF_SIZE_T);
5134
        while (p < p2) {
5135
            if (*p & 0x80) {
5136
                return p - start;
5137
            }
5138
            p++;
5139
        }
5140
#endif
5141
5142
3.49M
        const unsigned char *e = end - SIZEOF_SIZE_T;
5143
110M
        while (p <= e) {
5144
107M
            size_t u = (*(const size_t *)p) & ASCII_CHAR_MASK;
5145
107M
            if (u) {
5146
100k
#if PY_LITTLE_ENDIAN && HAVE_CTZ
5147
100k
                return p - start + (ctz(u) - 7) / 8;
5148
#else
5149
                // big endian and minor compilers are difficult to test.
5150
                // fallback to per byte check.
5151
                break;
5152
#endif
5153
100k
            }
5154
107M
            p += SIZEOF_SIZE_T;
5155
107M
        }
5156
3.49M
    }
5157
14.8M
#if PY_LITTLE_ENDIAN && HAVE_CTZ
5158
14.8M
    assert((end - p) < SIZEOF_SIZE_T);
5159
    // we can not use *(const size_t*)p to avoid buffer overrun.
5160
14.8M
    size_t u = load_unaligned(p, end - p) & ASCII_CHAR_MASK;
5161
14.8M
    if (u) {
5162
174k
        return p - start + (ctz(u) - 7) / 8;
5163
174k
    }
5164
14.7M
    return end - start;
5165
#else
5166
    while (p < end) {
5167
        if (*p & 0x80) {
5168
            break;
5169
        }
5170
        p++;
5171
    }
5172
    return p - start;
5173
#endif
5174
14.8M
}
5175
5176
static inline int
5177
scalar_utf8_start_char(unsigned int ch)
5178
363k
{
5179
    // 0xxxxxxx or 11xxxxxx are first byte.
5180
363k
    return (~ch >> 7 | ch >> 6) & 1;
5181
363k
}
5182
5183
static inline size_t
5184
vector_utf8_start_chars(size_t v)
5185
290M
{
5186
290M
    return ((~v >> 7) | (v >> 6)) & VECTOR_0101;
5187
290M
}
5188
5189
5190
// Count the number of UTF-8 code points in a given byte sequence.
5191
static Py_ssize_t
5192
utf8_count_codepoints(const unsigned char *s, const unsigned char *end)
5193
99.9k
{
5194
99.9k
    Py_ssize_t len = 0;
5195
5196
99.9k
    if (end - s >= SIZEOF_SIZE_T) {
5197
52.0k
        while (!_Py_IS_ALIGNED(s, ALIGNOF_SIZE_T)) {
5198
20.8k
            len += scalar_utf8_start_char(*s++);
5199
20.8k
        }
5200
5201
1.19M
        while (s + SIZEOF_SIZE_T <= end) {
5202
1.16M
            const unsigned char *e = end;
5203
1.16M
            if (e - s > SIZEOF_SIZE_T * 255) {
5204
1.13M
                e = s + SIZEOF_SIZE_T * 255;
5205
1.13M
            }
5206
1.16M
            Py_ssize_t vstart = 0;
5207
291M
            while (s + SIZEOF_SIZE_T <= e) {
5208
290M
                size_t v = *(size_t*)s;
5209
290M
                size_t vs = vector_utf8_start_chars(v);
5210
290M
                vstart += vs;
5211
290M
                s += SIZEOF_SIZE_T;
5212
290M
            }
5213
1.16M
            vstart = (vstart & VECTOR_00FF) + ((vstart >> 8) & VECTOR_00FF);
5214
1.16M
            vstart += vstart >> 16;
5215
1.16M
#if SIZEOF_SIZE_T == 8
5216
1.16M
            vstart += vstart >> 32;
5217
1.16M
#endif
5218
1.16M
            len += vstart & 0x7ff;
5219
1.16M
        }
5220
31.1k
    }
5221
443k
    while (s < end) {
5222
343k
        len += scalar_utf8_start_char(*s++);
5223
343k
    }
5224
99.9k
    return len;
5225
99.9k
}
5226
5227
static Py_ssize_t
5228
ascii_decode(const char *start, const char *end, Py_UCS1 *dest)
5229
6.31M
{
5230
6.31M
#if SIZEOF_SIZE_T <= SIZEOF_VOID_P
5231
6.31M
    if (_Py_IS_ALIGNED(start, ALIGNOF_SIZE_T)
5232
6.31M
        && _Py_IS_ALIGNED(dest, ALIGNOF_SIZE_T))
5233
909k
    {
5234
        /* Fast path, see in STRINGLIB(utf8_decode) for
5235
           an explanation. */
5236
909k
        const char *p = start;
5237
909k
        Py_UCS1 *q = dest;
5238
1.89M
        while (p + SIZEOF_SIZE_T <= end) {
5239
1.12M
            size_t value = *(const size_t *) p;
5240
1.12M
            if (value & ASCII_CHAR_MASK)
5241
141k
                break;
5242
982k
            *((size_t *)q) = value;
5243
982k
            p += SIZEOF_SIZE_T;
5244
982k
            q += SIZEOF_SIZE_T;
5245
982k
        }
5246
4.11M
        while (p < end) {
5247
3.38M
            if ((unsigned char)*p & 0x80)
5248
171k
                break;
5249
3.20M
            *q++ = *p++;
5250
3.20M
        }
5251
909k
        return p - start;
5252
909k
    }
5253
5.40M
#endif
5254
5.40M
    Py_ssize_t pos = find_first_nonascii((const unsigned char*)start,
5255
5.40M
                                         (const unsigned char*)end);
5256
5.40M
    memcpy(dest, start, pos);
5257
5.40M
    return pos;
5258
6.31M
}
5259
5260
static int
5261
unicode_decode_utf8_impl(_PyUnicodeWriter *writer,
5262
                         const char *starts, const char *s, const char *end,
5263
                         _Py_error_handler error_handler,
5264
                         const char *errors,
5265
                         Py_ssize_t *consumed)
5266
421k
{
5267
421k
    Py_ssize_t startinpos, endinpos;
5268
421k
    const char *errmsg = "";
5269
421k
    PyObject *error_handler_obj = NULL;
5270
421k
    PyObject *exc = NULL;
5271
5272
177M
    while (s < end) {
5273
177M
        Py_UCS4 ch;
5274
177M
        int kind = writer->kind;
5275
5276
177M
        if (kind == PyUnicode_1BYTE_KIND) {
5277
406k
            if (PyUnicode_IS_ASCII(writer->buffer))
5278
319k
                ch = asciilib_utf8_decode(&s, end, writer->data, &writer->pos);
5279
86.5k
            else
5280
86.5k
                ch = ucs1lib_utf8_decode(&s, end, writer->data, &writer->pos);
5281
176M
        } else if (kind == PyUnicode_2BYTE_KIND) {
5282
94.2M
            ch = ucs2lib_utf8_decode(&s, end, writer->data, &writer->pos);
5283
94.2M
        } else {
5284
82.5M
            assert(kind == PyUnicode_4BYTE_KIND);
5285
82.5M
            ch = ucs4lib_utf8_decode(&s, end, writer->data, &writer->pos);
5286
82.5M
        }
5287
5288
177M
        switch (ch) {
5289
335k
        case 0:
5290
335k
            if (s == end || consumed)
5291
312k
                goto End;
5292
22.7k
            errmsg = "unexpected end of data";
5293
22.7k
            startinpos = s - starts;
5294
22.7k
            endinpos = end - starts;
5295
22.7k
            break;
5296
142M
        case 1:
5297
142M
            errmsg = "invalid start byte";
5298
142M
            startinpos = s - starts;
5299
142M
            endinpos = startinpos + 1;
5300
142M
            break;
5301
33.1M
        case 2:
5302
33.1M
            if (consumed && (unsigned char)s[0] == 0xED && end - s == 2
5303
33.1M
                && (unsigned char)s[1] >= 0xA0 && (unsigned char)s[1] <= 0xBF)
5304
0
            {
5305
                /* Truncated surrogate code in range D800-DFFF */
5306
0
                goto End;
5307
0
            }
5308
33.1M
            _Py_FALLTHROUGH;
5309
34.2M
        case 3:
5310
34.3M
        case 4:
5311
34.3M
            errmsg = "invalid continuation byte";
5312
34.3M
            startinpos = s - starts;
5313
34.3M
            endinpos = startinpos + ch - 1;
5314
34.3M
            break;
5315
308k
        default:
5316
            // ch doesn't fit into kind, so change the buffer kind to write
5317
            // the character
5318
308k
            if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0)
5319
0
                goto onError;
5320
308k
            continue;
5321
177M
        }
5322
5323
176M
        if (error_handler == _Py_ERROR_UNKNOWN)
5324
112k
            error_handler = _Py_GetErrorHandler(errors);
5325
5326
176M
        switch (error_handler) {
5327
0
        case _Py_ERROR_IGNORE:
5328
0
            s += (endinpos - startinpos);
5329
0
            break;
5330
5331
176M
        case _Py_ERROR_REPLACE:
5332
176M
            if (_PyUnicodeWriter_WriteCharInline(writer, 0xfffd) < 0)
5333
0
                goto onError;
5334
176M
            s += (endinpos - startinpos);
5335
176M
            break;
5336
5337
3.33k
        case _Py_ERROR_SURROGATEESCAPE:
5338
3.33k
        {
5339
3.33k
            Py_ssize_t i;
5340
5341
3.33k
            if (_PyUnicodeWriter_PrepareKind(writer, PyUnicode_2BYTE_KIND) < 0)
5342
0
                goto onError;
5343
7.08k
            for (i=startinpos; i<endinpos; i++) {
5344
3.75k
                ch = (Py_UCS4)(unsigned char)(starts[i]);
5345
3.75k
                PyUnicode_WRITE(writer->kind, writer->data, writer->pos,
5346
3.75k
                                ch + 0xdc00);
5347
3.75k
                writer->pos++;
5348
3.75k
            }
5349
3.33k
            s += (endinpos - startinpos);
5350
3.33k
            break;
5351
3.33k
        }
5352
5353
3.80k
        default:
5354
3.80k
            if (unicode_decode_call_errorhandler_writer(
5355
3.80k
                    errors, &error_handler_obj,
5356
3.80k
                    "utf-8", errmsg,
5357
3.80k
                    &starts, &end, &startinpos, &endinpos, &exc, &s,
5358
3.80k
                    writer)) {
5359
3.80k
                goto onError;
5360
3.80k
            }
5361
5362
0
            if (_PyUnicodeWriter_Prepare(writer, end - s, 127) < 0) {
5363
0
                return -1;
5364
0
            }
5365
176M
        }
5366
176M
    }
5367
5368
418k
End:
5369
418k
    if (consumed)
5370
1.40k
        *consumed = s - starts;
5371
5372
418k
    Py_XDECREF(error_handler_obj);
5373
418k
    Py_XDECREF(exc);
5374
418k
    return 0;
5375
5376
3.80k
onError:
5377
3.80k
    Py_XDECREF(error_handler_obj);
5378
3.80k
    Py_XDECREF(exc);
5379
3.80k
    return -1;
5380
421k
}
5381
5382
5383
static PyObject *
5384
unicode_decode_utf8(const char *s, Py_ssize_t size,
5385
                    _Py_error_handler error_handler, const char *errors,
5386
                    Py_ssize_t *consumed)
5387
11.6M
{
5388
11.6M
    if (size == 0) {
5389
83.7k
        if (consumed) {
5390
0
            *consumed = 0;
5391
0
        }
5392
83.7k
        _Py_RETURN_UNICODE_EMPTY();
5393
83.7k
    }
5394
5395
    /* ASCII is equivalent to the first 128 ordinals in Unicode. */
5396
11.5M
    if (size == 1 && (unsigned char)s[0] < 128) {
5397
1.79M
        if (consumed) {
5398
0
            *consumed = 1;
5399
0
        }
5400
1.79M
        return get_latin1_char((unsigned char)s[0]);
5401
1.79M
    }
5402
5403
    // I don't know this check is necessary or not. But there is a test
5404
    // case that requires size=PY_SSIZE_T_MAX cause MemoryError.
5405
9.73M
    if (PY_SSIZE_T_MAX - sizeof(PyCompactUnicodeObject) < (size_t)size) {
5406
0
        PyErr_NoMemory();
5407
0
        return NULL;
5408
0
    }
5409
5410
9.73M
    const char *starts = s;
5411
9.73M
    const char *end = s + size;
5412
5413
9.73M
    Py_ssize_t pos = find_first_nonascii((const unsigned char*)starts, (const unsigned char*)end);
5414
9.73M
    if (pos == size) {  // fast path: ASCII string.
5415
9.36M
        PyObject *u = PyUnicode_New(size, 127);
5416
9.36M
        if (u == NULL) {
5417
0
            return NULL;
5418
0
        }
5419
9.36M
        memcpy(PyUnicode_1BYTE_DATA(u), s, size);
5420
9.36M
        if (consumed) {
5421
0
            *consumed = size;
5422
0
        }
5423
9.36M
        return u;
5424
9.36M
    }
5425
5426
370k
    int maxchr = 127;
5427
370k
    Py_ssize_t maxsize = size;
5428
5429
370k
    unsigned char ch = (unsigned char)(s[pos]);
5430
    // error handler other than strict may remove/replace the invalid byte.
5431
    // consumed != NULL allows 1~3 bytes remainings.
5432
    // 0x80 <= ch < 0xc2 is invalid start byte that cause UnicodeDecodeError.
5433
    // otherwise: check the input and decide the maxchr and maxsize to reduce
5434
    // reallocation and copy.
5435
370k
    if (error_handler == _Py_ERROR_STRICT && !consumed && ch >= 0xc2) {
5436
        // we only calculate the number of codepoints and don't determine the exact maxchr.
5437
        // This is because writing fast and portable SIMD code to find maxchr is difficult.
5438
        // If reallocation occurs for a larger maxchar, knowing the exact number of codepoints
5439
        // means that it is no longer necessary to allocate several times the required amount
5440
        // of memory.
5441
99.9k
        maxsize = utf8_count_codepoints((const unsigned char *)s, (const unsigned char *)end);
5442
99.9k
        if (ch < 0xc4) { // latin1
5443
14.7k
            maxchr = 0xff;
5444
14.7k
        }
5445
85.1k
        else if (ch < 0xf0) { // ucs2
5446
74.8k
            maxchr = 0xffff;
5447
74.8k
        }
5448
10.2k
        else { // ucs4
5449
10.2k
            maxchr = 0x10ffff;
5450
10.2k
        }
5451
99.9k
    }
5452
370k
    PyObject *u = PyUnicode_New(maxsize, maxchr);
5453
370k
    if (!u) {
5454
0
        return NULL;
5455
0
    }
5456
5457
    // Use _PyUnicodeWriter after fast path is failed.
5458
370k
    _PyUnicodeWriter writer;
5459
370k
    _PyUnicodeWriter_InitWithBuffer(&writer, u);
5460
370k
    if (maxchr <= 255) {
5461
285k
        memcpy(PyUnicode_1BYTE_DATA(u), s, pos);
5462
285k
        s += pos;
5463
285k
        size -= pos;
5464
285k
        writer.pos = pos;
5465
285k
    }
5466
5467
370k
    if (unicode_decode_utf8_impl(&writer, starts, s, end,
5468
370k
                                 error_handler, errors,
5469
370k
                                 consumed) < 0) {
5470
3.80k
        _PyUnicodeWriter_Dealloc(&writer);
5471
3.80k
        return NULL;
5472
3.80k
    }
5473
367k
    return _PyUnicodeWriter_Finish(&writer);
5474
370k
}
5475
5476
5477
// Used by PyUnicodeWriter_WriteUTF8() implementation
5478
static int
5479
unicode_decode_utf8_writer(_PyUnicodeWriter *writer,
5480
                           const char *s, Py_ssize_t size,
5481
                           _Py_error_handler error_handler, const char *errors,
5482
                           Py_ssize_t *consumed)
5483
5.41M
{
5484
5.41M
    if (size == 0) {
5485
8.17k
        if (consumed) {
5486
0
            *consumed = 0;
5487
0
        }
5488
8.17k
        return 0;
5489
8.17k
    }
5490
5491
    // fast path: try ASCII string.
5492
5.40M
    if (_PyUnicodeWriter_Prepare(writer, size, 127) < 0) {
5493
0
        return -1;
5494
0
    }
5495
5496
5.40M
    const char *starts = s;
5497
5.40M
    const char *end = s + size;
5498
5.40M
    Py_ssize_t decoded = 0;
5499
5.40M
    Py_UCS1 *dest = (Py_UCS1*)writer->data + writer->pos * writer->kind;
5500
5.40M
    if (writer->kind == PyUnicode_1BYTE_KIND) {
5501
5.40M
        decoded = ascii_decode(s, end, dest);
5502
5.40M
        writer->pos += decoded;
5503
5504
5.40M
        if (decoded == size) {
5505
5.35M
            if (consumed) {
5506
1.20k
                *consumed = size;
5507
1.20k
            }
5508
5.35M
            return 0;
5509
5.35M
        }
5510
48.7k
        s += decoded;
5511
48.7k
        size -= decoded;
5512
48.7k
    }
5513
5514
50.9k
    return unicode_decode_utf8_impl(writer, starts, s, end,
5515
50.9k
                                    error_handler, errors, consumed);
5516
5.40M
}
5517
5518
5519
PyObject *
5520
PyUnicode_DecodeUTF8Stateful(const char *s,
5521
                             Py_ssize_t size,
5522
                             const char *errors,
5523
                             Py_ssize_t *consumed)
5524
11.6M
{
5525
11.6M
    return unicode_decode_utf8(s, size,
5526
11.6M
                               errors ? _Py_ERROR_UNKNOWN : _Py_ERROR_STRICT,
5527
11.6M
                               errors, consumed);
5528
11.6M
}
5529
5530
5531
/* UTF-8 decoder: use surrogateescape error handler if 'surrogateescape' is
5532
   non-zero, use strict error handler otherwise.
5533
5534
   On success, write a pointer to a newly allocated wide character string into
5535
   *wstr (use PyMem_RawFree() to free the memory) and write the output length
5536
   (in number of wchar_t units) into *wlen (if wlen is set).
5537
5538
   On memory allocation failure, return -1.
5539
5540
   On decoding error (if surrogateescape is zero), return -2. If wlen is
5541
   non-NULL, write the start of the illegal byte sequence into *wlen. If reason
5542
   is not NULL, write the decoding error message into *reason. */
5543
int
5544
_Py_DecodeUTF8Ex(const char *s, Py_ssize_t size, wchar_t **wstr, size_t *wlen,
5545
                 const char **reason, _Py_error_handler errors)
5546
5.23k
{
5547
5.23k
    const char *orig_s = s;
5548
5.23k
    const char *e;
5549
5.23k
    wchar_t *unicode;
5550
5.23k
    Py_ssize_t outpos;
5551
5552
5.23k
    int surrogateescape = 0;
5553
5.23k
    int surrogatepass = 0;
5554
5.23k
    switch (errors)
5555
5.23k
    {
5556
0
    case _Py_ERROR_STRICT:
5557
0
        break;
5558
5.23k
    case _Py_ERROR_SURROGATEESCAPE:
5559
5.23k
        surrogateescape = 1;
5560
5.23k
        break;
5561
0
    case _Py_ERROR_SURROGATEPASS:
5562
0
        surrogatepass = 1;
5563
0
        break;
5564
0
    default:
5565
0
        return -3;
5566
5.23k
    }
5567
5568
    /* Note: size will always be longer than the resulting Unicode
5569
       character count */
5570
5.23k
    if (PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(wchar_t) - 1 < size) {
5571
0
        return -1;
5572
0
    }
5573
5574
5.23k
    unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t));
5575
5.23k
    if (!unicode) {
5576
0
        return -1;
5577
0
    }
5578
5579
    /* Unpack UTF-8 encoded data */
5580
5.23k
    e = s + size;
5581
5.23k
    outpos = 0;
5582
5.23k
    while (s < e) {
5583
5.23k
        Py_UCS4 ch;
5584
5.23k
#if SIZEOF_WCHAR_T == 4
5585
5.23k
        ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos);
5586
#else
5587
        ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos);
5588
#endif
5589
5.23k
        if (ch > 0xFF) {
5590
0
#if SIZEOF_WCHAR_T == 4
5591
0
            Py_UNREACHABLE();
5592
#else
5593
            assert(ch > 0xFFFF && ch <= MAX_UNICODE);
5594
            /* write a surrogate pair */
5595
            unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch);
5596
            unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch);
5597
#endif
5598
0
        }
5599
5.23k
        else {
5600
5.23k
            if (!ch && s == e) {
5601
5.23k
                break;
5602
5.23k
            }
5603
5604
0
            if (surrogateescape) {
5605
0
                unicode[outpos++] = 0xDC00 + (unsigned char)*s++;
5606
0
            }
5607
0
            else {
5608
                /* Is it a valid three-byte code? */
5609
0
                if (surrogatepass
5610
0
                    && (e - s) >= 3
5611
0
                    && (s[0] & 0xf0) == 0xe0
5612
0
                    && (s[1] & 0xc0) == 0x80
5613
0
                    && (s[2] & 0xc0) == 0x80)
5614
0
                {
5615
0
                    ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f);
5616
0
                    s += 3;
5617
0
                    unicode[outpos++] = ch;
5618
0
                }
5619
0
                else {
5620
0
                    PyMem_RawFree(unicode );
5621
0
                    if (reason != NULL) {
5622
0
                        switch (ch) {
5623
0
                        case 0:
5624
0
                            *reason = "unexpected end of data";
5625
0
                            break;
5626
0
                        case 1:
5627
0
                            *reason = "invalid start byte";
5628
0
                            break;
5629
                        /* 2, 3, 4 */
5630
0
                        default:
5631
0
                            *reason = "invalid continuation byte";
5632
0
                            break;
5633
0
                        }
5634
0
                    }
5635
0
                    if (wlen != NULL) {
5636
0
                        *wlen = s - orig_s;
5637
0
                    }
5638
0
                    return -2;
5639
0
                }
5640
0
            }
5641
0
        }
5642
5.23k
    }
5643
5.23k
    unicode[outpos] = L'\0';
5644
5.23k
    if (wlen) {
5645
5.23k
        *wlen = outpos;
5646
5.23k
    }
5647
5.23k
    *wstr = unicode;
5648
5.23k
    return 0;
5649
5.23k
}
5650
5651
5652
wchar_t*
5653
_Py_DecodeUTF8_surrogateescape(const char *arg, Py_ssize_t arglen,
5654
                               size_t *wlen)
5655
0
{
5656
0
    wchar_t *wstr;
5657
0
    int res = _Py_DecodeUTF8Ex(arg, arglen,
5658
0
                               &wstr, wlen,
5659
0
                               NULL, _Py_ERROR_SURROGATEESCAPE);
5660
0
    if (res != 0) {
5661
        /* _Py_DecodeUTF8Ex() must support _Py_ERROR_SURROGATEESCAPE */
5662
0
        assert(res != -3);
5663
0
        if (wlen) {
5664
0
            *wlen = (size_t)res;
5665
0
        }
5666
0
        return NULL;
5667
0
    }
5668
0
    return wstr;
5669
0
}
5670
5671
5672
/* UTF-8 encoder.
5673
5674
   On success, return 0 and write the newly allocated character string (use
5675
   PyMem_Free() to free the memory) into *str.
5676
5677
   On encoding failure, return -2 and write the position of the invalid
5678
   surrogate character into *error_pos (if error_pos is set) and the decoding
5679
   error message into *reason (if reason is set).
5680
5681
   On memory allocation failure, return -1. */
5682
int
5683
_Py_EncodeUTF8Ex(const wchar_t *text, char **str, size_t *error_pos,
5684
                 const char **reason, int raw_malloc, _Py_error_handler errors)
5685
638
{
5686
638
    const Py_ssize_t max_char_size = 4;
5687
638
    Py_ssize_t len = wcslen(text);
5688
5689
638
    assert(len >= 0);
5690
5691
638
    int surrogateescape = 0;
5692
638
    int surrogatepass = 0;
5693
638
    switch (errors)
5694
638
    {
5695
64
    case _Py_ERROR_STRICT:
5696
64
        break;
5697
574
    case _Py_ERROR_SURROGATEESCAPE:
5698
574
        surrogateescape = 1;
5699
574
        break;
5700
0
    case _Py_ERROR_SURROGATEPASS:
5701
0
        surrogatepass = 1;
5702
0
        break;
5703
0
    default:
5704
0
        return -3;
5705
638
    }
5706
5707
638
    if (len > PY_SSIZE_T_MAX / max_char_size - 1) {
5708
0
        return -1;
5709
0
    }
5710
638
    char *bytes;
5711
638
    if (raw_malloc) {
5712
638
        bytes = PyMem_RawMalloc((len + 1) * max_char_size);
5713
638
    }
5714
0
    else {
5715
0
        bytes = PyMem_Malloc((len + 1) * max_char_size);
5716
0
    }
5717
638
    if (bytes == NULL) {
5718
0
        return -1;
5719
0
    }
5720
5721
638
    char *p = bytes;
5722
638
    Py_ssize_t i;
5723
42.3k
    for (i = 0; i < len; ) {
5724
41.6k
        Py_ssize_t ch_pos = i;
5725
41.6k
        Py_UCS4 ch = text[i];
5726
41.6k
        i++;
5727
#if Py_UNICODE_SIZE == 2
5728
        if (Py_UNICODE_IS_HIGH_SURROGATE(ch)
5729
            && i < len
5730
            && Py_UNICODE_IS_LOW_SURROGATE(text[i]))
5731
        {
5732
            ch = Py_UNICODE_JOIN_SURROGATES(ch, text[i]);
5733
            i++;
5734
        }
5735
#endif
5736
5737
41.6k
        if (ch < 0x80) {
5738
            /* Encode ASCII */
5739
41.6k
            *p++ = (char) ch;
5740
5741
41.6k
        }
5742
0
        else if (ch < 0x0800) {
5743
            /* Encode Latin-1 */
5744
0
            *p++ = (char)(0xc0 | (ch >> 6));
5745
0
            *p++ = (char)(0x80 | (ch & 0x3f));
5746
0
        }
5747
0
        else if (Py_UNICODE_IS_SURROGATE(ch) && !surrogatepass) {
5748
            /* surrogateescape error handler */
5749
0
            if (!surrogateescape || !(0xDC80 <= ch && ch <= 0xDCFF)) {
5750
0
                if (error_pos != NULL) {
5751
0
                    *error_pos = (size_t)ch_pos;
5752
0
                }
5753
0
                if (reason != NULL) {
5754
0
                    *reason = "encoding error";
5755
0
                }
5756
0
                if (raw_malloc) {
5757
0
                    PyMem_RawFree(bytes);
5758
0
                }
5759
0
                else {
5760
0
                    PyMem_Free(bytes);
5761
0
                }
5762
0
                return -2;
5763
0
            }
5764
0
            *p++ = (char)(ch & 0xff);
5765
0
        }
5766
0
        else if (ch < 0x10000) {
5767
0
            *p++ = (char)(0xe0 | (ch >> 12));
5768
0
            *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
5769
0
            *p++ = (char)(0x80 | (ch & 0x3f));
5770
0
        }
5771
0
        else {  /* ch >= 0x10000 */
5772
0
            assert(ch <= MAX_UNICODE);
5773
            /* Encode UCS4 Unicode ordinals */
5774
0
            *p++ = (char)(0xf0 | (ch >> 18));
5775
0
            *p++ = (char)(0x80 | ((ch >> 12) & 0x3f));
5776
0
            *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
5777
0
            *p++ = (char)(0x80 | (ch & 0x3f));
5778
0
        }
5779
41.6k
    }
5780
638
    *p++ = '\0';
5781
5782
638
    size_t final_size = (p - bytes);
5783
638
    char *bytes2;
5784
638
    if (raw_malloc) {
5785
638
        bytes2 = PyMem_RawRealloc(bytes, final_size);
5786
638
    }
5787
0
    else {
5788
0
        bytes2 = PyMem_Realloc(bytes, final_size);
5789
0
    }
5790
638
    if (bytes2 == NULL) {
5791
0
        if (error_pos != NULL) {
5792
0
            *error_pos = (size_t)-1;
5793
0
        }
5794
0
        if (raw_malloc) {
5795
0
            PyMem_RawFree(bytes);
5796
0
        }
5797
0
        else {
5798
0
            PyMem_Free(bytes);
5799
0
        }
5800
0
        return -1;
5801
0
    }
5802
638
    *str = bytes2;
5803
638
    return 0;
5804
638
}
5805
5806
5807
/* Primary internal function which creates utf8 encoded bytes objects.
5808
5809
   Allocation strategy:  if the string is short, convert into a stack buffer
5810
   and allocate exactly as much space needed at the end.  Else allocate the
5811
   maximum possible needed (4 result bytes per Unicode character), and return
5812
   the excess memory at the end.
5813
*/
5814
static PyObject *
5815
unicode_encode_utf8(PyObject *unicode, _Py_error_handler error_handler,
5816
                    const char *errors)
5817
14.9M
{
5818
14.9M
    if (!PyUnicode_Check(unicode)) {
5819
0
        PyErr_BadArgument();
5820
0
        return NULL;
5821
0
    }
5822
5823
14.9M
    if (PyUnicode_UTF8(unicode))
5824
8.91M
        return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode),
5825
8.91M
                                         PyUnicode_UTF8_LENGTH(unicode));
5826
5827
6.00M
    int kind = PyUnicode_KIND(unicode);
5828
6.00M
    const void *data = PyUnicode_DATA(unicode);
5829
6.00M
    Py_ssize_t size = PyUnicode_GET_LENGTH(unicode);
5830
5831
6.00M
    _PyBytesWriter writer;
5832
6.00M
    char *end;
5833
5834
6.00M
    switch (kind) {
5835
0
    default:
5836
0
        Py_UNREACHABLE();
5837
4.43M
    case PyUnicode_1BYTE_KIND:
5838
        /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */
5839
4.43M
        assert(!PyUnicode_IS_ASCII(unicode));
5840
4.43M
        end = ucs1lib_utf8_encoder(&writer, unicode, data, size, error_handler, errors);
5841
4.43M
        break;
5842
1.50M
    case PyUnicode_2BYTE_KIND:
5843
1.50M
        end = ucs2lib_utf8_encoder(&writer, unicode, data, size, error_handler, errors);
5844
1.50M
        break;
5845
65.0k
    case PyUnicode_4BYTE_KIND:
5846
65.0k
        end = ucs4lib_utf8_encoder(&writer, unicode, data, size, error_handler, errors);
5847
65.0k
        break;
5848
6.00M
    }
5849
5850
6.00M
    if (end == NULL) {
5851
189k
        _PyBytesWriter_Dealloc(&writer);
5852
189k
        return NULL;
5853
189k
    }
5854
5.81M
    return _PyBytesWriter_Finish(&writer, end);
5855
6.00M
}
5856
5857
static int
5858
unicode_fill_utf8(PyObject *unicode)
5859
160k
{
5860
160k
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(unicode);
5861
    /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */
5862
160k
    assert(!PyUnicode_IS_ASCII(unicode));
5863
5864
160k
    int kind = PyUnicode_KIND(unicode);
5865
160k
    const void *data = PyUnicode_DATA(unicode);
5866
160k
    Py_ssize_t size = PyUnicode_GET_LENGTH(unicode);
5867
5868
160k
    _PyBytesWriter writer;
5869
160k
    char *end;
5870
5871
160k
    switch (kind) {
5872
0
    default:
5873
0
        Py_UNREACHABLE();
5874
126k
    case PyUnicode_1BYTE_KIND:
5875
126k
        end = ucs1lib_utf8_encoder(&writer, unicode, data, size,
5876
126k
                                   _Py_ERROR_STRICT, NULL);
5877
126k
        break;
5878
28.2k
    case PyUnicode_2BYTE_KIND:
5879
28.2k
        end = ucs2lib_utf8_encoder(&writer, unicode, data, size,
5880
28.2k
                                   _Py_ERROR_STRICT, NULL);
5881
28.2k
        break;
5882
4.91k
    case PyUnicode_4BYTE_KIND:
5883
4.91k
        end = ucs4lib_utf8_encoder(&writer, unicode, data, size,
5884
4.91k
                                   _Py_ERROR_STRICT, NULL);
5885
4.91k
        break;
5886
160k
    }
5887
160k
    if (end == NULL) {
5888
206
        _PyBytesWriter_Dealloc(&writer);
5889
206
        return -1;
5890
206
    }
5891
5892
159k
    const char *start = writer.use_small_buffer ? writer.small_buffer :
5893
159k
                    PyBytes_AS_STRING(writer.buffer);
5894
159k
    Py_ssize_t len = end - start;
5895
5896
159k
    char *cache = PyMem_Malloc(len + 1);
5897
159k
    if (cache == NULL) {
5898
0
        _PyBytesWriter_Dealloc(&writer);
5899
0
        PyErr_NoMemory();
5900
0
        return -1;
5901
0
    }
5902
159k
    memcpy(cache, start, len);
5903
159k
    cache[len] = '\0';
5904
159k
    PyUnicode_SET_UTF8_LENGTH(unicode, len);
5905
159k
    PyUnicode_SET_UTF8(unicode, cache);
5906
159k
    _PyBytesWriter_Dealloc(&writer);
5907
159k
    return 0;
5908
159k
}
5909
5910
PyObject *
5911
_PyUnicode_AsUTF8String(PyObject *unicode, const char *errors)
5912
14.9M
{
5913
14.9M
    return unicode_encode_utf8(unicode, _Py_ERROR_UNKNOWN, errors);
5914
14.9M
}
5915
5916
5917
PyObject *
5918
PyUnicode_AsUTF8String(PyObject *unicode)
5919
3.08k
{
5920
3.08k
    return _PyUnicode_AsUTF8String(unicode, NULL);
5921
3.08k
}
5922
5923
/* --- UTF-32 Codec ------------------------------------------------------- */
5924
5925
PyObject *
5926
PyUnicode_DecodeUTF32(const char *s,
5927
                      Py_ssize_t size,
5928
                      const char *errors,
5929
                      int *byteorder)
5930
129
{
5931
129
    return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL);
5932
129
}
5933
5934
PyObject *
5935
PyUnicode_DecodeUTF32Stateful(const char *s,
5936
                              Py_ssize_t size,
5937
                              const char *errors,
5938
                              int *byteorder,
5939
                              Py_ssize_t *consumed)
5940
31.4k
{
5941
31.4k
    const char *starts = s;
5942
31.4k
    Py_ssize_t startinpos;
5943
31.4k
    Py_ssize_t endinpos;
5944
31.4k
    _PyUnicodeWriter writer;
5945
31.4k
    const unsigned char *q, *e;
5946
31.4k
    int le, bo = 0;       /* assume native ordering by default */
5947
31.4k
    const char *encoding;
5948
31.4k
    const char *errmsg = "";
5949
31.4k
    PyObject *errorHandler = NULL;
5950
31.4k
    PyObject *exc = NULL;
5951
5952
31.4k
    q = (const unsigned char *)s;
5953
31.4k
    e = q + size;
5954
5955
31.4k
    if (byteorder)
5956
31.3k
        bo = *byteorder;
5957
5958
    /* Check for BOM marks (U+FEFF) in the input and adjust current
5959
       byte order setting accordingly. In native mode, the leading BOM
5960
       mark is skipped, in all other modes, it is copied to the output
5961
       stream as-is (giving a ZWNBSP character). */
5962
31.4k
    if (bo == 0 && size >= 4) {
5963
29.6k
        Py_UCS4 bom = ((unsigned int)q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0];
5964
29.6k
        if (bom == 0x0000FEFF) {
5965
151
            bo = -1;
5966
151
            q += 4;
5967
151
        }
5968
29.4k
        else if (bom == 0xFFFE0000) {
5969
210
            bo = 1;
5970
210
            q += 4;
5971
210
        }
5972
29.6k
        if (byteorder)
5973
29.4k
            *byteorder = bo;
5974
29.6k
    }
5975
5976
31.4k
    if (q == e) {
5977
82
        if (consumed)
5978
0
            *consumed = size;
5979
82
        _Py_RETURN_UNICODE_EMPTY();
5980
82
    }
5981
5982
#ifdef WORDS_BIGENDIAN
5983
    le = bo < 0;
5984
#else
5985
31.3k
    le = bo <= 0;
5986
31.3k
#endif
5987
31.3k
    encoding = le ? "utf-32-le" : "utf-32-be";
5988
5989
31.3k
    _PyUnicodeWriter_Init(&writer);
5990
31.3k
    writer.min_length = (e - q + 3) / 4;
5991
31.3k
    if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1)
5992
0
        goto onError;
5993
5994
112k
    while (1) {
5995
112k
        Py_UCS4 ch = 0;
5996
112k
        Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer);
5997
5998
112k
        if (e - q >= 4) {
5999
93.7k
            int kind = writer.kind;
6000
93.7k
            void *data = writer.data;
6001
93.7k
            const unsigned char *last = e - 4;
6002
93.7k
            Py_ssize_t pos = writer.pos;
6003
93.7k
            if (le) {
6004
112k
                do {
6005
112k
                    ch = ((unsigned int)q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0];
6006
112k
                    if (ch > maxch)
6007
89.0k
                        break;
6008
23.4k
                    if (kind != PyUnicode_1BYTE_KIND &&
6009
23.4k
                        Py_UNICODE_IS_SURROGATE(ch))
6010
207
                        break;
6011
23.2k
                    PyUnicode_WRITE(kind, data, pos++, ch);
6012
23.2k
                    q += 4;
6013
23.2k
                } while (q <= last);
6014
90.2k
            }
6015
3.51k
            else {
6016
37.2k
                do {
6017
37.2k
                    ch = ((unsigned int)q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3];
6018
37.2k
                    if (ch > maxch)
6019
3.29k
                        break;
6020
33.9k
                    if (kind != PyUnicode_1BYTE_KIND &&
6021
33.9k
                        Py_UNICODE_IS_SURROGATE(ch))
6022
100
                        break;
6023
33.8k
                    PyUnicode_WRITE(kind, data, pos++, ch);
6024
33.8k
                    q += 4;
6025
33.8k
                } while (q <= last);
6026
3.51k
            }
6027
0
            writer.pos = pos;
6028
93.7k
        }
6029
6030
112k
        if (Py_UNICODE_IS_SURROGATE(ch)) {
6031
313
            errmsg = "code point in surrogate code point range(0xd800, 0xe000)";
6032
313
            startinpos = ((const char *)q) - starts;
6033
313
            endinpos = startinpos + 4;
6034
313
        }
6035
111k
        else if (ch <= maxch) {
6036
19.6k
            if (q == e || consumed)
6037
2.92k
                break;
6038
            /* remaining bytes at the end? (size should be divisible by 4) */
6039
16.7k
            errmsg = "truncated data";
6040
16.7k
            startinpos = ((const char *)q) - starts;
6041
16.7k
            endinpos = ((const char *)e) - starts;
6042
16.7k
        }
6043
92.3k
        else {
6044
92.3k
            if (ch < 0x110000) {
6045
3.63k
                if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0)
6046
0
                    goto onError;
6047
3.63k
                q += 4;
6048
3.63k
                continue;
6049
3.63k
            }
6050
88.6k
            errmsg = "code point not in range(0x110000)";
6051
88.6k
            startinpos = ((const char *)q) - starts;
6052
88.6k
            endinpos = startinpos + 4;
6053
88.6k
        }
6054
6055
        /* The remaining input chars are ignored if the callback
6056
           chooses to skip the input */
6057
105k
        if (unicode_decode_call_errorhandler_writer(
6058
105k
                errors, &errorHandler,
6059
105k
                encoding, errmsg,
6060
105k
                &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q,
6061
105k
                &writer))
6062
28.4k
            goto onError;
6063
105k
    }
6064
6065
2.92k
    if (consumed)
6066
0
        *consumed = (const char *)q-starts;
6067
6068
2.92k
    Py_XDECREF(errorHandler);
6069
2.92k
    Py_XDECREF(exc);
6070
2.92k
    return _PyUnicodeWriter_Finish(&writer);
6071
6072
28.4k
  onError:
6073
28.4k
    _PyUnicodeWriter_Dealloc(&writer);
6074
28.4k
    Py_XDECREF(errorHandler);
6075
28.4k
    Py_XDECREF(exc);
6076
28.4k
    return NULL;
6077
31.3k
}
6078
6079
PyObject *
6080
_PyUnicode_EncodeUTF32(PyObject *str,
6081
                       const char *errors,
6082
                       int byteorder)
6083
0
{
6084
0
    int kind;
6085
0
    const void *data;
6086
0
    Py_ssize_t len;
6087
0
    PyObject *v;
6088
0
    uint32_t *out;
6089
0
#if PY_LITTLE_ENDIAN
6090
0
    int native_ordering = byteorder <= 0;
6091
#else
6092
    int native_ordering = byteorder >= 0;
6093
#endif
6094
0
    const char *encoding;
6095
0
    Py_ssize_t nsize, pos;
6096
0
    PyObject *errorHandler = NULL;
6097
0
    PyObject *exc = NULL;
6098
0
    PyObject *rep = NULL;
6099
6100
0
    if (!PyUnicode_Check(str)) {
6101
0
        PyErr_BadArgument();
6102
0
        return NULL;
6103
0
    }
6104
0
    kind = PyUnicode_KIND(str);
6105
0
    data = PyUnicode_DATA(str);
6106
0
    len = PyUnicode_GET_LENGTH(str);
6107
6108
0
    if (len > PY_SSIZE_T_MAX / 4 - (byteorder == 0))
6109
0
        return PyErr_NoMemory();
6110
0
    nsize = len + (byteorder == 0);
6111
0
    v = PyBytes_FromStringAndSize(NULL, nsize * 4);
6112
0
    if (v == NULL)
6113
0
        return NULL;
6114
6115
    /* output buffer is 4-bytes aligned */
6116
0
    assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 4));
6117
0
    out = (uint32_t *)PyBytes_AS_STRING(v);
6118
0
    if (byteorder == 0)
6119
0
        *out++ = 0xFEFF;
6120
0
    if (len == 0)
6121
0
        goto done;
6122
6123
0
    if (byteorder == -1)
6124
0
        encoding = "utf-32-le";
6125
0
    else if (byteorder == 1)
6126
0
        encoding = "utf-32-be";
6127
0
    else
6128
0
        encoding = "utf-32";
6129
6130
0
    if (kind == PyUnicode_1BYTE_KIND) {
6131
0
        ucs1lib_utf32_encode((const Py_UCS1 *)data, len, &out, native_ordering);
6132
0
        goto done;
6133
0
    }
6134
6135
0
    pos = 0;
6136
0
    while (pos < len) {
6137
0
        Py_ssize_t newpos, repsize, moreunits;
6138
6139
0
        if (kind == PyUnicode_2BYTE_KIND) {
6140
0
            pos += ucs2lib_utf32_encode((const Py_UCS2 *)data + pos, len - pos,
6141
0
                                        &out, native_ordering);
6142
0
        }
6143
0
        else {
6144
0
            assert(kind == PyUnicode_4BYTE_KIND);
6145
0
            pos += ucs4lib_utf32_encode((const Py_UCS4 *)data + pos, len - pos,
6146
0
                                        &out, native_ordering);
6147
0
        }
6148
0
        if (pos == len)
6149
0
            break;
6150
6151
0
        rep = unicode_encode_call_errorhandler(
6152
0
                errors, &errorHandler,
6153
0
                encoding, "surrogates not allowed",
6154
0
                str, &exc, pos, pos + 1, &newpos);
6155
0
        if (!rep)
6156
0
            goto error;
6157
6158
0
        if (PyBytes_Check(rep)) {
6159
0
            repsize = PyBytes_GET_SIZE(rep);
6160
0
            if (repsize & 3) {
6161
0
                raise_encode_exception(&exc, encoding,
6162
0
                                       str, pos, pos + 1,
6163
0
                                       "surrogates not allowed");
6164
0
                goto error;
6165
0
            }
6166
0
            moreunits = repsize / 4;
6167
0
        }
6168
0
        else {
6169
0
            assert(PyUnicode_Check(rep));
6170
0
            moreunits = repsize = PyUnicode_GET_LENGTH(rep);
6171
0
            if (!PyUnicode_IS_ASCII(rep)) {
6172
0
                raise_encode_exception(&exc, encoding,
6173
0
                                       str, pos, pos + 1,
6174
0
                                       "surrogates not allowed");
6175
0
                goto error;
6176
0
            }
6177
0
        }
6178
0
        moreunits += pos - newpos;
6179
0
        pos = newpos;
6180
6181
        /* four bytes are reserved for each surrogate */
6182
0
        if (moreunits > 0) {
6183
0
            Py_ssize_t outpos = out - (uint32_t*) PyBytes_AS_STRING(v);
6184
0
            if (moreunits >= (PY_SSIZE_T_MAX - PyBytes_GET_SIZE(v)) / 4) {
6185
                /* integer overflow */
6186
0
                PyErr_NoMemory();
6187
0
                goto error;
6188
0
            }
6189
0
            if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + 4 * moreunits) < 0)
6190
0
                goto error;
6191
0
            out = (uint32_t*) PyBytes_AS_STRING(v) + outpos;
6192
0
        }
6193
6194
0
        if (PyBytes_Check(rep)) {
6195
0
            memcpy(out, PyBytes_AS_STRING(rep), repsize);
6196
0
            out += repsize / 4;
6197
0
        } else /* rep is unicode */ {
6198
0
            assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND);
6199
0
            ucs1lib_utf32_encode(PyUnicode_1BYTE_DATA(rep), repsize,
6200
0
                                 &out, native_ordering);
6201
0
        }
6202
6203
0
        Py_CLEAR(rep);
6204
0
    }
6205
6206
    /* Cut back to size actually needed. This is necessary for, for example,
6207
       encoding of a string containing isolated surrogates and the 'ignore'
6208
       handler is used. */
6209
0
    nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v);
6210
0
    if (nsize != PyBytes_GET_SIZE(v))
6211
0
      _PyBytes_Resize(&v, nsize);
6212
0
    Py_XDECREF(errorHandler);
6213
0
    Py_XDECREF(exc);
6214
0
  done:
6215
0
    return v;
6216
0
  error:
6217
0
    Py_XDECREF(rep);
6218
0
    Py_XDECREF(errorHandler);
6219
0
    Py_XDECREF(exc);
6220
0
    Py_XDECREF(v);
6221
0
    return NULL;
6222
0
}
6223
6224
PyObject *
6225
PyUnicode_AsUTF32String(PyObject *unicode)
6226
0
{
6227
0
    return _PyUnicode_EncodeUTF32(unicode, NULL, 0);
6228
0
}
6229
6230
/* --- UTF-16 Codec ------------------------------------------------------- */
6231
6232
PyObject *
6233
PyUnicode_DecodeUTF16(const char *s,
6234
                      Py_ssize_t size,
6235
                      const char *errors,
6236
                      int *byteorder)
6237
125
{
6238
125
    return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL);
6239
125
}
6240
6241
PyObject *
6242
PyUnicode_DecodeUTF16Stateful(const char *s,
6243
                              Py_ssize_t size,
6244
                              const char *errors,
6245
                              int *byteorder,
6246
                              Py_ssize_t *consumed)
6247
14.9k
{
6248
14.9k
    const char *starts = s;
6249
14.9k
    Py_ssize_t startinpos;
6250
14.9k
    Py_ssize_t endinpos;
6251
14.9k
    _PyUnicodeWriter writer;
6252
14.9k
    const unsigned char *q, *e;
6253
14.9k
    int bo = 0;       /* assume native ordering by default */
6254
14.9k
    int native_ordering;
6255
14.9k
    const char *errmsg = "";
6256
14.9k
    PyObject *errorHandler = NULL;
6257
14.9k
    PyObject *exc = NULL;
6258
14.9k
    const char *encoding;
6259
6260
14.9k
    q = (const unsigned char *)s;
6261
14.9k
    e = q + size;
6262
6263
14.9k
    if (byteorder)
6264
14.8k
        bo = *byteorder;
6265
6266
    /* Check for BOM marks (U+FEFF) in the input and adjust current
6267
       byte order setting accordingly. In native mode, the leading BOM
6268
       mark is skipped, in all other modes, it is copied to the output
6269
       stream as-is (giving a ZWNBSP character). */
6270
14.9k
    if (bo == 0 && size >= 2) {
6271
14.2k
        const Py_UCS4 bom = (q[1] << 8) | q[0];
6272
14.2k
        if (bom == 0xFEFF) {
6273
366
            q += 2;
6274
366
            bo = -1;
6275
366
        }
6276
13.8k
        else if (bom == 0xFFFE) {
6277
2.38k
            q += 2;
6278
2.38k
            bo = 1;
6279
2.38k
        }
6280
14.2k
        if (byteorder)
6281
14.1k
            *byteorder = bo;
6282
14.2k
    }
6283
6284
14.9k
    if (q == e) {
6285
71
        if (consumed)
6286
0
            *consumed = size;
6287
71
        _Py_RETURN_UNICODE_EMPTY();
6288
71
    }
6289
6290
14.9k
#if PY_LITTLE_ENDIAN
6291
14.9k
    native_ordering = bo <= 0;
6292
14.9k
    encoding = bo <= 0 ? "utf-16-le" : "utf-16-be";
6293
#else
6294
    native_ordering = bo >= 0;
6295
    encoding = bo >= 0 ? "utf-16-be" : "utf-16-le";
6296
#endif
6297
6298
    /* Note: size will always be longer than the resulting Unicode
6299
       character count normally.  Error handler will take care of
6300
       resizing when needed. */
6301
14.9k
    _PyUnicodeWriter_Init(&writer);
6302
14.9k
    writer.min_length = (e - q + 1) / 2;
6303
14.9k
    if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1)
6304
0
        goto onError;
6305
6306
54.6k
    while (1) {
6307
54.6k
        Py_UCS4 ch = 0;
6308
54.6k
        if (e - q >= 2) {
6309
46.5k
            int kind = writer.kind;
6310
46.5k
            if (kind == PyUnicode_1BYTE_KIND) {
6311
18.0k
                if (PyUnicode_IS_ASCII(writer.buffer))
6312
14.3k
                    ch = asciilib_utf16_decode(&q, e,
6313
14.3k
                            (Py_UCS1*)writer.data, &writer.pos,
6314
14.3k
                            native_ordering);
6315
3.67k
                else
6316
3.67k
                    ch = ucs1lib_utf16_decode(&q, e,
6317
3.67k
                            (Py_UCS1*)writer.data, &writer.pos,
6318
3.67k
                            native_ordering);
6319
28.5k
            } else if (kind == PyUnicode_2BYTE_KIND) {
6320
12.8k
                ch = ucs2lib_utf16_decode(&q, e,
6321
12.8k
                        (Py_UCS2*)writer.data, &writer.pos,
6322
12.8k
                        native_ordering);
6323
15.6k
            } else {
6324
15.6k
                assert(kind == PyUnicode_4BYTE_KIND);
6325
15.6k
                ch = ucs4lib_utf16_decode(&q, e,
6326
15.6k
                        (Py_UCS4*)writer.data, &writer.pos,
6327
15.6k
                        native_ordering);
6328
15.6k
            }
6329
46.5k
        }
6330
6331
54.6k
        switch (ch)
6332
54.6k
        {
6333
14.9k
        case 0:
6334
            /* remaining byte at the end? (size should be even) */
6335
14.9k
            if (q == e || consumed)
6336
9.91k
                goto End;
6337
5.01k
            errmsg = "truncated data";
6338
5.01k
            startinpos = ((const char *)q) - starts;
6339
5.01k
            endinpos = ((const char *)e) - starts;
6340
5.01k
            break;
6341
            /* The remaining input chars are ignored if the callback
6342
               chooses to skip the input */
6343
1.56k
        case 1:
6344
1.56k
            q -= 2;
6345
1.56k
            if (consumed)
6346
0
                goto End;
6347
1.56k
            errmsg = "unexpected end of data";
6348
1.56k
            startinpos = ((const char *)q) - starts;
6349
1.56k
            endinpos = ((const char *)e) - starts;
6350
1.56k
            break;
6351
14.6k
        case 2:
6352
14.6k
            errmsg = "illegal encoding";
6353
14.6k
            startinpos = ((const char *)q) - 2 - starts;
6354
14.6k
            endinpos = startinpos + 2;
6355
14.6k
            break;
6356
6.68k
        case 3:
6357
6.68k
            errmsg = "illegal UTF-16 surrogate";
6358
6.68k
            startinpos = ((const char *)q) - 4 - starts;
6359
6.68k
            endinpos = startinpos + 2;
6360
6.68k
            break;
6361
16.8k
        default:
6362
16.8k
            if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0)
6363
0
                goto onError;
6364
16.8k
            continue;
6365
54.6k
        }
6366
6367
27.8k
        if (unicode_decode_call_errorhandler_writer(
6368
27.8k
                errors,
6369
27.8k
                &errorHandler,
6370
27.8k
                encoding, errmsg,
6371
27.8k
                &starts,
6372
27.8k
                (const char **)&e,
6373
27.8k
                &startinpos,
6374
27.8k
                &endinpos,
6375
27.8k
                &exc,
6376
27.8k
                (const char **)&q,
6377
27.8k
                &writer))
6378
5.00k
            goto onError;
6379
27.8k
    }
6380
6381
9.91k
End:
6382
9.91k
    if (consumed)
6383
0
        *consumed = (const char *)q-starts;
6384
6385
9.91k
    Py_XDECREF(errorHandler);
6386
9.91k
    Py_XDECREF(exc);
6387
9.91k
    return _PyUnicodeWriter_Finish(&writer);
6388
6389
5.00k
  onError:
6390
5.00k
    _PyUnicodeWriter_Dealloc(&writer);
6391
5.00k
    Py_XDECREF(errorHandler);
6392
5.00k
    Py_XDECREF(exc);
6393
5.00k
    return NULL;
6394
14.9k
}
6395
6396
PyObject *
6397
_PyUnicode_EncodeUTF16(PyObject *str,
6398
                       const char *errors,
6399
                       int byteorder)
6400
0
{
6401
0
    int kind;
6402
0
    const void *data;
6403
0
    Py_ssize_t len;
6404
0
    PyObject *v;
6405
0
    unsigned short *out;
6406
0
    Py_ssize_t pairs;
6407
#if PY_BIG_ENDIAN
6408
    int native_ordering = byteorder >= 0;
6409
#else
6410
0
    int native_ordering = byteorder <= 0;
6411
0
#endif
6412
0
    const char *encoding;
6413
0
    Py_ssize_t nsize, pos;
6414
0
    PyObject *errorHandler = NULL;
6415
0
    PyObject *exc = NULL;
6416
0
    PyObject *rep = NULL;
6417
6418
0
    if (!PyUnicode_Check(str)) {
6419
0
        PyErr_BadArgument();
6420
0
        return NULL;
6421
0
    }
6422
0
    kind = PyUnicode_KIND(str);
6423
0
    data = PyUnicode_DATA(str);
6424
0
    len = PyUnicode_GET_LENGTH(str);
6425
6426
0
    pairs = 0;
6427
0
    if (kind == PyUnicode_4BYTE_KIND) {
6428
0
        const Py_UCS4 *in = (const Py_UCS4 *)data;
6429
0
        const Py_UCS4 *end = in + len;
6430
0
        while (in < end) {
6431
0
            if (*in++ >= 0x10000) {
6432
0
                pairs++;
6433
0
            }
6434
0
        }
6435
0
    }
6436
0
    if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) {
6437
0
        return PyErr_NoMemory();
6438
0
    }
6439
0
    nsize = len + pairs + (byteorder == 0);
6440
0
    v = PyBytes_FromStringAndSize(NULL, nsize * 2);
6441
0
    if (v == NULL) {
6442
0
        return NULL;
6443
0
    }
6444
6445
    /* output buffer is 2-bytes aligned */
6446
0
    assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2));
6447
0
    out = (unsigned short *)PyBytes_AS_STRING(v);
6448
0
    if (byteorder == 0) {
6449
0
        *out++ = 0xFEFF;
6450
0
    }
6451
0
    if (len == 0) {
6452
0
        goto done;
6453
0
    }
6454
6455
0
    if (kind == PyUnicode_1BYTE_KIND) {
6456
0
        ucs1lib_utf16_encode((const Py_UCS1 *)data, len, &out, native_ordering);
6457
0
        goto done;
6458
0
    }
6459
6460
0
    if (byteorder < 0) {
6461
0
        encoding = "utf-16-le";
6462
0
    }
6463
0
    else if (byteorder > 0) {
6464
0
        encoding = "utf-16-be";
6465
0
    }
6466
0
    else {
6467
0
        encoding = "utf-16";
6468
0
    }
6469
6470
0
    pos = 0;
6471
0
    while (pos < len) {
6472
0
        Py_ssize_t newpos, repsize, moreunits;
6473
6474
0
        if (kind == PyUnicode_2BYTE_KIND) {
6475
0
            pos += ucs2lib_utf16_encode((const Py_UCS2 *)data + pos, len - pos,
6476
0
                                        &out, native_ordering);
6477
0
        }
6478
0
        else {
6479
0
            assert(kind == PyUnicode_4BYTE_KIND);
6480
0
            pos += ucs4lib_utf16_encode((const Py_UCS4 *)data + pos, len - pos,
6481
0
                                        &out, native_ordering);
6482
0
        }
6483
0
        if (pos == len)
6484
0
            break;
6485
6486
0
        rep = unicode_encode_call_errorhandler(
6487
0
                errors, &errorHandler,
6488
0
                encoding, "surrogates not allowed",
6489
0
                str, &exc, pos, pos + 1, &newpos);
6490
0
        if (!rep)
6491
0
            goto error;
6492
6493
0
        if (PyBytes_Check(rep)) {
6494
0
            repsize = PyBytes_GET_SIZE(rep);
6495
0
            if (repsize & 1) {
6496
0
                raise_encode_exception(&exc, encoding,
6497
0
                                       str, pos, pos + 1,
6498
0
                                       "surrogates not allowed");
6499
0
                goto error;
6500
0
            }
6501
0
            moreunits = repsize / 2;
6502
0
        }
6503
0
        else {
6504
0
            assert(PyUnicode_Check(rep));
6505
0
            moreunits = repsize = PyUnicode_GET_LENGTH(rep);
6506
0
            if (!PyUnicode_IS_ASCII(rep)) {
6507
0
                raise_encode_exception(&exc, encoding,
6508
0
                                       str, pos, pos + 1,
6509
0
                                       "surrogates not allowed");
6510
0
                goto error;
6511
0
            }
6512
0
        }
6513
0
        moreunits += pos - newpos;
6514
0
        pos = newpos;
6515
6516
        /* two bytes are reserved for each surrogate */
6517
0
        if (moreunits > 0) {
6518
0
            Py_ssize_t outpos = out - (unsigned short*) PyBytes_AS_STRING(v);
6519
0
            if (moreunits >= (PY_SSIZE_T_MAX - PyBytes_GET_SIZE(v)) / 2) {
6520
                /* integer overflow */
6521
0
                PyErr_NoMemory();
6522
0
                goto error;
6523
0
            }
6524
0
            if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + 2 * moreunits) < 0)
6525
0
                goto error;
6526
0
            out = (unsigned short*) PyBytes_AS_STRING(v) + outpos;
6527
0
        }
6528
6529
0
        if (PyBytes_Check(rep)) {
6530
0
            memcpy(out, PyBytes_AS_STRING(rep), repsize);
6531
0
            out += repsize / 2;
6532
0
        } else /* rep is unicode */ {
6533
0
            assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND);
6534
0
            ucs1lib_utf16_encode(PyUnicode_1BYTE_DATA(rep), repsize,
6535
0
                                 &out, native_ordering);
6536
0
        }
6537
6538
0
        Py_CLEAR(rep);
6539
0
    }
6540
6541
    /* Cut back to size actually needed. This is necessary for, for example,
6542
    encoding of a string containing isolated surrogates and the 'ignore' handler
6543
    is used. */
6544
0
    nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v);
6545
0
    if (nsize != PyBytes_GET_SIZE(v))
6546
0
      _PyBytes_Resize(&v, nsize);
6547
0
    Py_XDECREF(errorHandler);
6548
0
    Py_XDECREF(exc);
6549
0
  done:
6550
0
    return v;
6551
0
  error:
6552
0
    Py_XDECREF(rep);
6553
0
    Py_XDECREF(errorHandler);
6554
0
    Py_XDECREF(exc);
6555
0
    Py_XDECREF(v);
6556
0
    return NULL;
6557
0
#undef STORECHAR
6558
0
}
6559
6560
PyObject *
6561
PyUnicode_AsUTF16String(PyObject *unicode)
6562
0
{
6563
0
    return _PyUnicode_EncodeUTF16(unicode, NULL, 0);
6564
0
}
6565
6566
_PyUnicode_Name_CAPI *
6567
_PyUnicode_GetNameCAPI(void)
6568
2.62k
{
6569
2.62k
    PyInterpreterState *interp = _PyInterpreterState_GET();
6570
2.62k
    _PyUnicode_Name_CAPI *ucnhash_capi;
6571
6572
2.62k
    ucnhash_capi = _Py_atomic_load_ptr(&interp->unicode.ucnhash_capi);
6573
2.62k
    if (ucnhash_capi == NULL) {
6574
1
        ucnhash_capi = (_PyUnicode_Name_CAPI *)PyCapsule_Import(
6575
1
                PyUnicodeData_CAPSULE_NAME, 1);
6576
6577
        // It's fine if we overwrite the value here. It's always the same value.
6578
1
        _Py_atomic_store_ptr(&interp->unicode.ucnhash_capi, ucnhash_capi);
6579
1
    }
6580
2.62k
    return ucnhash_capi;
6581
2.62k
}
6582
6583
/* --- Unicode Escape Codec ----------------------------------------------- */
6584
6585
PyObject *
6586
_PyUnicode_DecodeUnicodeEscapeInternal2(const char *s,
6587
                               Py_ssize_t size,
6588
                               const char *errors,
6589
                               Py_ssize_t *consumed,
6590
                               int *first_invalid_escape_char,
6591
                               const char **first_invalid_escape_ptr)
6592
32.1k
{
6593
32.1k
    const char *starts = s;
6594
32.1k
    const char *initial_starts = starts;
6595
32.1k
    _PyUnicodeWriter writer;
6596
32.1k
    const char *end;
6597
32.1k
    PyObject *errorHandler = NULL;
6598
32.1k
    PyObject *exc = NULL;
6599
32.1k
    _PyUnicode_Name_CAPI *ucnhash_capi;
6600
6601
    // so we can remember if we've seen an invalid escape char or not
6602
32.1k
    *first_invalid_escape_char = -1;
6603
32.1k
    *first_invalid_escape_ptr = NULL;
6604
6605
32.1k
    if (size == 0) {
6606
2.12k
        if (consumed) {
6607
0
            *consumed = 0;
6608
0
        }
6609
2.12k
        _Py_RETURN_UNICODE_EMPTY();
6610
2.12k
    }
6611
    /* Escaped strings will always be longer than the resulting
6612
       Unicode string, so we start with size here and then reduce the
6613
       length after conversion to the true value.
6614
       (but if the error callback returns a long replacement string
6615
       we'll have to allocate more space) */
6616
30.0k
    _PyUnicodeWriter_Init(&writer);
6617
30.0k
    writer.min_length = size;
6618
30.0k
    if (_PyUnicodeWriter_Prepare(&writer, size, 127) < 0) {
6619
0
        goto onError;
6620
0
    }
6621
6622
30.0k
    end = s + size;
6623
212k
    while (s < end) {
6624
182k
        unsigned char c = (unsigned char) *s++;
6625
182k
        Py_UCS4 ch;
6626
182k
        int count;
6627
182k
        const char *message;
6628
6629
182k
#define WRITE_ASCII_CHAR(ch)                                                  \
6630
182k
            do {                                                              \
6631
15.5k
                assert(ch <= 127);                                            \
6632
15.5k
                assert(writer.pos < writer.size);                             \
6633
15.5k
                PyUnicode_WRITE(writer.kind, writer.data, writer.pos++, ch);  \
6634
15.5k
            } while(0)
6635
6636
182k
#define WRITE_CHAR(ch)                                                        \
6637
182k
            do {                                                              \
6638
170k
                if (ch <= writer.maxchar) {                                   \
6639
154k
                    assert(writer.pos < writer.size);                         \
6640
154k
                    PyUnicode_WRITE(writer.kind, writer.data, writer.pos++, ch); \
6641
154k
                }                                                             \
6642
170k
                else if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) { \
6643
0
                    goto onError;                                             \
6644
0
                }                                                             \
6645
170k
            } while(0)
6646
6647
        /* Non-escape characters are interpreted as Unicode ordinals */
6648
182k
        if (c != '\\') {
6649
109k
            WRITE_CHAR(c);
6650
109k
            continue;
6651
109k
        }
6652
6653
72.5k
        Py_ssize_t startinpos = s - starts - 1;
6654
        /* \ - Escapes */
6655
72.5k
        if (s >= end) {
6656
0
            message = "\\ at end of string";
6657
0
            goto incomplete;
6658
0
        }
6659
72.5k
        c = (unsigned char) *s++;
6660
6661
72.5k
        assert(writer.pos < writer.size);
6662
72.5k
        switch (c) {
6663
6664
            /* \x escapes */
6665
1.19k
        case '\n': continue;
6666
1.93k
        case '\\': WRITE_ASCII_CHAR('\\'); continue;
6667
1.00k
        case '\'': WRITE_ASCII_CHAR('\''); continue;
6668
1.08k
        case '\"': WRITE_ASCII_CHAR('\"'); continue;
6669
637
        case 'b': WRITE_ASCII_CHAR('\b'); continue;
6670
        /* FF */
6671
1.26k
        case 'f': WRITE_ASCII_CHAR('\014'); continue;
6672
784
        case 't': WRITE_ASCII_CHAR('\t'); continue;
6673
971
        case 'n': WRITE_ASCII_CHAR('\n'); continue;
6674
1.44k
        case 'r': WRITE_ASCII_CHAR('\r'); continue;
6675
        /* VT */
6676
694
        case 'v': WRITE_ASCII_CHAR('\013'); continue;
6677
        /* BEL, not classic C */
6678
679
        case 'a': WRITE_ASCII_CHAR('\007'); continue;
6679
6680
            /* \OOO (octal) escapes */
6681
3.77k
        case '0': case '1': case '2': case '3':
6682
7.60k
        case '4': case '5': case '6': case '7':
6683
7.60k
            ch = c - '0';
6684
7.60k
            if (s < end && '0' <= *s && *s <= '7') {
6685
3.34k
                ch = (ch<<3) + *s++ - '0';
6686
3.34k
                if (s < end && '0' <= *s && *s <= '7') {
6687
1.78k
                    ch = (ch<<3) + *s++ - '0';
6688
1.78k
                }
6689
3.34k
            }
6690
7.60k
            if (ch > 0377) {
6691
1.45k
                if (*first_invalid_escape_char == -1) {
6692
1.08k
                    *first_invalid_escape_char = ch;
6693
1.08k
                    if (starts == initial_starts) {
6694
                        /* Back up 3 chars, since we've already incremented s. */
6695
1.08k
                        *first_invalid_escape_ptr = s - 3;
6696
1.08k
                    }
6697
1.08k
                }
6698
1.45k
            }
6699
7.60k
            WRITE_CHAR(ch);
6700
7.60k
            continue;
6701
6702
            /* hex escapes */
6703
            /* \xXX */
6704
7.60k
        case 'x':
6705
6.17k
            count = 2;
6706
6.17k
            message = "truncated \\xXX escape";
6707
6.17k
            goto hexescape;
6708
6709
            /* \uXXXX */
6710
9.11k
        case 'u':
6711
9.11k
            count = 4;
6712
9.11k
            message = "truncated \\uXXXX escape";
6713
9.11k
            goto hexescape;
6714
6715
            /* \UXXXXXXXX */
6716
30.2k
        case 'U':
6717
30.2k
            count = 8;
6718
30.2k
            message = "truncated \\UXXXXXXXX escape";
6719
45.5k
        hexescape:
6720
336k
            for (ch = 0; count; ++s, --count) {
6721
291k
                if (s >= end) {
6722
6
                    goto incomplete;
6723
6
                }
6724
291k
                c = (unsigned char)*s;
6725
291k
                ch <<= 4;
6726
291k
                if (c >= '0' && c <= '9') {
6727
206k
                    ch += c - '0';
6728
206k
                }
6729
84.5k
                else if (c >= 'a' && c <= 'f') {
6730
84.2k
                    ch += c - ('a' - 10);
6731
84.2k
                }
6732
254
                else if (c >= 'A' && c <= 'F') {
6733
246
                    ch += c - ('A' - 10);
6734
246
                }
6735
8
                else {
6736
8
                    goto error;
6737
8
                }
6738
291k
            }
6739
6740
            /* when we get here, ch is a 32-bit unicode character */
6741
45.5k
            if (ch > MAX_UNICODE) {
6742
1
                message = "illegal Unicode character";
6743
1
                goto error;
6744
1
            }
6745
6746
45.5k
            WRITE_CHAR(ch);
6747
45.5k
            continue;
6748
6749
            /* \N{name} */
6750
45.5k
        case 'N':
6751
2.62k
            ucnhash_capi = _PyUnicode_GetNameCAPI();
6752
2.62k
            if (ucnhash_capi == NULL) {
6753
0
                PyErr_SetString(
6754
0
                        PyExc_UnicodeError,
6755
0
                        "\\N escapes not supported (can't load unicodedata module)"
6756
0
                );
6757
0
                goto onError;
6758
0
            }
6759
6760
2.62k
            message = "malformed \\N character escape";
6761
2.62k
            if (s >= end) {
6762
4
                goto incomplete;
6763
4
            }
6764
2.62k
            if (*s == '{') {
6765
2.61k
                const char *start = ++s;
6766
2.61k
                size_t namelen;
6767
                /* look for the closing brace */
6768
39.5k
                while (s < end && *s != '}')
6769
36.9k
                    s++;
6770
2.61k
                if (s >= end) {
6771
11
                    goto incomplete;
6772
11
                }
6773
2.60k
                namelen = s - start;
6774
2.60k
                if (namelen) {
6775
                    /* found a name.  look it up in the unicode database */
6776
2.60k
                    s++;
6777
2.60k
                    ch = 0xffffffff; /* in case 'getcode' messes up */
6778
2.60k
                    if (namelen <= INT_MAX &&
6779
2.60k
                        ucnhash_capi->getcode(start, (int)namelen,
6780
2.60k
                                              &ch, 0)) {
6781
2.52k
                        assert(ch <= MAX_UNICODE);
6782
2.52k
                        WRITE_CHAR(ch);
6783
2.52k
                        continue;
6784
2.52k
                    }
6785
76
                    message = "unknown Unicode character name";
6786
76
                }
6787
2.60k
            }
6788
80
            goto error;
6789
6790
5.04k
        default:
6791
5.04k
            if (*first_invalid_escape_char == -1) {
6792
3.66k
                *first_invalid_escape_char = c;
6793
3.66k
                if (starts == initial_starts) {
6794
                    /* Back up one char, since we've already incremented s. */
6795
3.66k
                    *first_invalid_escape_ptr = s - 1;
6796
3.66k
                }
6797
3.66k
            }
6798
5.04k
            WRITE_ASCII_CHAR('\\');
6799
5.04k
            WRITE_CHAR(c);
6800
5.04k
            continue;
6801
72.5k
        }
6802
6803
21
      incomplete:
6804
21
        if (consumed) {
6805
0
            *consumed = startinpos;
6806
0
            break;
6807
0
        }
6808
110
      error:;
6809
110
        Py_ssize_t endinpos = s-starts;
6810
110
        writer.min_length = end - s + writer.pos;
6811
110
        if (unicode_decode_call_errorhandler_writer(
6812
110
                errors, &errorHandler,
6813
110
                "unicodeescape", message,
6814
110
                &starts, &end, &startinpos, &endinpos, &exc, &s,
6815
110
                &writer)) {
6816
110
            goto onError;
6817
110
        }
6818
0
        assert(end - s <= writer.size - writer.pos);
6819
6820
0
#undef WRITE_ASCII_CHAR
6821
0
#undef WRITE_CHAR
6822
0
    }
6823
6824
29.9k
    Py_XDECREF(errorHandler);
6825
29.9k
    Py_XDECREF(exc);
6826
29.9k
    return _PyUnicodeWriter_Finish(&writer);
6827
6828
110
  onError:
6829
110
    _PyUnicodeWriter_Dealloc(&writer);
6830
110
    Py_XDECREF(errorHandler);
6831
110
    Py_XDECREF(exc);
6832
110
    return NULL;
6833
30.0k
}
6834
6835
PyObject *
6836
_PyUnicode_DecodeUnicodeEscapeStateful(const char *s,
6837
                              Py_ssize_t size,
6838
                              const char *errors,
6839
                              Py_ssize_t *consumed)
6840
0
{
6841
0
    int first_invalid_escape_char;
6842
0
    const char *first_invalid_escape_ptr;
6843
0
    PyObject *result = _PyUnicode_DecodeUnicodeEscapeInternal2(s, size, errors,
6844
0
                                                      consumed,
6845
0
                                                      &first_invalid_escape_char,
6846
0
                                                      &first_invalid_escape_ptr);
6847
0
    if (result == NULL)
6848
0
        return NULL;
6849
0
    if (first_invalid_escape_char != -1) {
6850
0
        if (first_invalid_escape_char > 0xff) {
6851
0
            if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
6852
0
                                 "\"\\%o\" is an invalid octal escape sequence. "
6853
0
                                 "Such sequences will not work in the future. ",
6854
0
                                 first_invalid_escape_char) < 0)
6855
0
            {
6856
0
                Py_DECREF(result);
6857
0
                return NULL;
6858
0
            }
6859
0
        }
6860
0
        else {
6861
0
            if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
6862
0
                                 "\"\\%c\" is an invalid escape sequence. "
6863
0
                                 "Such sequences will not work in the future. ",
6864
0
                                 first_invalid_escape_char) < 0)
6865
0
            {
6866
0
                Py_DECREF(result);
6867
0
                return NULL;
6868
0
            }
6869
0
        }
6870
0
    }
6871
0
    return result;
6872
0
}
6873
6874
PyObject *
6875
PyUnicode_DecodeUnicodeEscape(const char *s,
6876
                              Py_ssize_t size,
6877
                              const char *errors)
6878
0
{
6879
0
    return _PyUnicode_DecodeUnicodeEscapeStateful(s, size, errors, NULL);
6880
0
}
6881
6882
/* Return a Unicode-Escape string version of the Unicode object. */
6883
6884
PyObject *
6885
PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
6886
660k
{
6887
660k
    Py_ssize_t i, len;
6888
660k
    PyObject *repr;
6889
660k
    char *p;
6890
660k
    int kind;
6891
660k
    const void *data;
6892
660k
    Py_ssize_t expandsize;
6893
6894
    /* Initial allocation is based on the longest-possible character
6895
       escape.
6896
6897
       For UCS1 strings it's '\xxx', 4 bytes per source character.
6898
       For UCS2 strings it's '\uxxxx', 6 bytes per source character.
6899
       For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character.
6900
    */
6901
6902
660k
    if (!PyUnicode_Check(unicode)) {
6903
0
        PyErr_BadArgument();
6904
0
        return NULL;
6905
0
    }
6906
6907
660k
    len = PyUnicode_GET_LENGTH(unicode);
6908
660k
    if (len == 0) {
6909
0
        return PyBytes_FromStringAndSize(NULL, 0);
6910
0
    }
6911
6912
660k
    kind = PyUnicode_KIND(unicode);
6913
660k
    data = PyUnicode_DATA(unicode);
6914
    /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6
6915
       bytes, and 1 byte characters 4. */
6916
660k
    expandsize = kind * 2 + 2;
6917
660k
    if (len > PY_SSIZE_T_MAX / expandsize) {
6918
0
        return PyErr_NoMemory();
6919
0
    }
6920
660k
    repr = PyBytes_FromStringAndSize(NULL, expandsize * len);
6921
660k
    if (repr == NULL) {
6922
0
        return NULL;
6923
0
    }
6924
6925
660k
    p = PyBytes_AS_STRING(repr);
6926
1.32M
    for (i = 0; i < len; i++) {
6927
660k
        Py_UCS4 ch = PyUnicode_READ(kind, data, i);
6928
6929
        /* U+0000-U+00ff range */
6930
660k
        if (ch < 0x100) {
6931
653k
            if (ch >= ' ' && ch < 127) {
6932
49.4k
                if (ch != '\\') {
6933
                    /* Copy printable US ASCII as-is */
6934
0
                    *p++ = (char) ch;
6935
0
                }
6936
                /* Escape backslashes */
6937
49.4k
                else {
6938
49.4k
                    *p++ = '\\';
6939
49.4k
                    *p++ = '\\';
6940
49.4k
                }
6941
49.4k
            }
6942
6943
            /* Map special whitespace to '\t', \n', '\r' */
6944
604k
            else if (ch == '\t') {
6945
4.11k
                *p++ = '\\';
6946
4.11k
                *p++ = 't';
6947
4.11k
            }
6948
600k
            else if (ch == '\n') {
6949
1.62k
                *p++ = '\\';
6950
1.62k
                *p++ = 'n';
6951
1.62k
            }
6952
598k
            else if (ch == '\r') {
6953
567
                *p++ = '\\';
6954
567
                *p++ = 'r';
6955
567
            }
6956
6957
            /* Map non-printable US ASCII and 8-bit characters to '\xHH' */
6958
597k
            else {
6959
597k
                *p++ = '\\';
6960
597k
                *p++ = 'x';
6961
597k
                *p++ = Py_hexdigits[(ch >> 4) & 0x000F];
6962
597k
                *p++ = Py_hexdigits[ch & 0x000F];
6963
597k
            }
6964
653k
        }
6965
        /* U+0100-U+ffff range: Map 16-bit characters to '\uHHHH' */
6966
6.66k
        else if (ch < 0x10000) {
6967
5.32k
            *p++ = '\\';
6968
5.32k
            *p++ = 'u';
6969
5.32k
            *p++ = Py_hexdigits[(ch >> 12) & 0x000F];
6970
5.32k
            *p++ = Py_hexdigits[(ch >> 8) & 0x000F];
6971
5.32k
            *p++ = Py_hexdigits[(ch >> 4) & 0x000F];
6972
5.32k
            *p++ = Py_hexdigits[ch & 0x000F];
6973
5.32k
        }
6974
        /* U+010000-U+10ffff range: Map 21-bit characters to '\U00HHHHHH' */
6975
1.34k
        else {
6976
6977
            /* Make sure that the first two digits are zero */
6978
1.34k
            assert(ch <= MAX_UNICODE && MAX_UNICODE <= 0x10ffff);
6979
1.34k
            *p++ = '\\';
6980
1.34k
            *p++ = 'U';
6981
1.34k
            *p++ = '0';
6982
1.34k
            *p++ = '0';
6983
1.34k
            *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F];
6984
1.34k
            *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F];
6985
1.34k
            *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F];
6986
1.34k
            *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F];
6987
1.34k
            *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F];
6988
1.34k
            *p++ = Py_hexdigits[ch & 0x0000000F];
6989
1.34k
        }
6990
660k
    }
6991
6992
660k
    assert(p - PyBytes_AS_STRING(repr) > 0);
6993
660k
    if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) {
6994
0
        return NULL;
6995
0
    }
6996
660k
    return repr;
6997
660k
}
6998
6999
/* --- Raw Unicode Escape Codec ------------------------------------------- */
7000
7001
PyObject *
7002
_PyUnicode_DecodeRawUnicodeEscapeStateful(const char *s,
7003
                                          Py_ssize_t size,
7004
                                          const char *errors,
7005
                                          Py_ssize_t *consumed)
7006
0
{
7007
0
    const char *starts = s;
7008
0
    _PyUnicodeWriter writer;
7009
0
    const char *end;
7010
0
    PyObject *errorHandler = NULL;
7011
0
    PyObject *exc = NULL;
7012
7013
0
    if (size == 0) {
7014
0
        if (consumed) {
7015
0
            *consumed = 0;
7016
0
        }
7017
0
        _Py_RETURN_UNICODE_EMPTY();
7018
0
    }
7019
7020
    /* Escaped strings will always be longer than the resulting
7021
       Unicode string, so we start with size here and then reduce the
7022
       length after conversion to the true value. (But decoding error
7023
       handler might have to resize the string) */
7024
0
    _PyUnicodeWriter_Init(&writer);
7025
0
    writer.min_length = size;
7026
0
    if (_PyUnicodeWriter_Prepare(&writer, size, 127) < 0) {
7027
0
        goto onError;
7028
0
    }
7029
7030
0
    end = s + size;
7031
0
    while (s < end) {
7032
0
        unsigned char c = (unsigned char) *s++;
7033
0
        Py_UCS4 ch;
7034
0
        int count;
7035
0
        const char *message;
7036
7037
0
#define WRITE_CHAR(ch)                                                        \
7038
0
            do {                                                              \
7039
0
                if (ch <= writer.maxchar) {                                   \
7040
0
                    assert(writer.pos < writer.size);                         \
7041
0
                    PyUnicode_WRITE(writer.kind, writer.data, writer.pos++, ch); \
7042
0
                }                                                             \
7043
0
                else if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) { \
7044
0
                    goto onError;                                             \
7045
0
                }                                                             \
7046
0
            } while(0)
7047
7048
        /* Non-escape characters are interpreted as Unicode ordinals */
7049
0
        if (c != '\\' || (s >= end && !consumed)) {
7050
0
            WRITE_CHAR(c);
7051
0
            continue;
7052
0
        }
7053
7054
0
        Py_ssize_t startinpos = s - starts - 1;
7055
        /* \ - Escapes */
7056
0
        if (s >= end) {
7057
0
            assert(consumed);
7058
            // Set message to silent compiler warning.
7059
            // Actually it is never used.
7060
0
            message = "\\ at end of string";
7061
0
            goto incomplete;
7062
0
        }
7063
7064
0
        c = (unsigned char) *s++;
7065
0
        if (c == 'u') {
7066
0
            count = 4;
7067
0
            message = "truncated \\uXXXX escape";
7068
0
        }
7069
0
        else if (c == 'U') {
7070
0
            count = 8;
7071
0
            message = "truncated \\UXXXXXXXX escape";
7072
0
        }
7073
0
        else {
7074
0
            assert(writer.pos < writer.size);
7075
0
            PyUnicode_WRITE(writer.kind, writer.data, writer.pos++, '\\');
7076
0
            WRITE_CHAR(c);
7077
0
            continue;
7078
0
        }
7079
7080
        /* \uHHHH with 4 hex digits, \U00HHHHHH with 8 */
7081
0
        for (ch = 0; count; ++s, --count) {
7082
0
            if (s >= end) {
7083
0
                goto incomplete;
7084
0
            }
7085
0
            c = (unsigned char)*s;
7086
0
            ch <<= 4;
7087
0
            if (c >= '0' && c <= '9') {
7088
0
                ch += c - '0';
7089
0
            }
7090
0
            else if (c >= 'a' && c <= 'f') {
7091
0
                ch += c - ('a' - 10);
7092
0
            }
7093
0
            else if (c >= 'A' && c <= 'F') {
7094
0
                ch += c - ('A' - 10);
7095
0
            }
7096
0
            else {
7097
0
                goto error;
7098
0
            }
7099
0
        }
7100
0
        if (ch > MAX_UNICODE) {
7101
0
            message = "\\Uxxxxxxxx out of range";
7102
0
            goto error;
7103
0
        }
7104
0
        WRITE_CHAR(ch);
7105
0
        continue;
7106
7107
0
      incomplete:
7108
0
        if (consumed) {
7109
0
            *consumed = startinpos;
7110
0
            break;
7111
0
        }
7112
0
      error:;
7113
0
        Py_ssize_t endinpos = s-starts;
7114
0
        writer.min_length = end - s + writer.pos;
7115
0
        if (unicode_decode_call_errorhandler_writer(
7116
0
                errors, &errorHandler,
7117
0
                "rawunicodeescape", message,
7118
0
                &starts, &end, &startinpos, &endinpos, &exc, &s,
7119
0
                &writer)) {
7120
0
            goto onError;
7121
0
        }
7122
0
        assert(end - s <= writer.size - writer.pos);
7123
7124
0
#undef WRITE_CHAR
7125
0
    }
7126
0
    Py_XDECREF(errorHandler);
7127
0
    Py_XDECREF(exc);
7128
0
    return _PyUnicodeWriter_Finish(&writer);
7129
7130
0
  onError:
7131
0
    _PyUnicodeWriter_Dealloc(&writer);
7132
0
    Py_XDECREF(errorHandler);
7133
0
    Py_XDECREF(exc);
7134
0
    return NULL;
7135
0
}
7136
7137
PyObject *
7138
PyUnicode_DecodeRawUnicodeEscape(const char *s,
7139
                                 Py_ssize_t size,
7140
                                 const char *errors)
7141
0
{
7142
0
    return _PyUnicode_DecodeRawUnicodeEscapeStateful(s, size, errors, NULL);
7143
0
}
7144
7145
7146
PyObject *
7147
PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
7148
213k
{
7149
213k
    PyObject *repr;
7150
213k
    char *p;
7151
213k
    Py_ssize_t expandsize, pos;
7152
213k
    int kind;
7153
213k
    const void *data;
7154
213k
    Py_ssize_t len;
7155
7156
213k
    if (!PyUnicode_Check(unicode)) {
7157
0
        PyErr_BadArgument();
7158
0
        return NULL;
7159
0
    }
7160
213k
    kind = PyUnicode_KIND(unicode);
7161
213k
    data = PyUnicode_DATA(unicode);
7162
213k
    len = PyUnicode_GET_LENGTH(unicode);
7163
213k
    if (kind == PyUnicode_1BYTE_KIND) {
7164
212k
        return PyBytes_FromStringAndSize(data, len);
7165
212k
    }
7166
7167
    /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6
7168
       bytes, and 1 byte characters 4. */
7169
269
    expandsize = kind * 2 + 2;
7170
7171
269
    if (len > PY_SSIZE_T_MAX / expandsize) {
7172
0
        return PyErr_NoMemory();
7173
0
    }
7174
269
    repr = PyBytes_FromStringAndSize(NULL, expandsize * len);
7175
269
    if (repr == NULL) {
7176
0
        return NULL;
7177
0
    }
7178
269
    if (len == 0) {
7179
0
        return repr;
7180
0
    }
7181
7182
269
    p = PyBytes_AS_STRING(repr);
7183
4.90M
    for (pos = 0; pos < len; pos++) {
7184
4.90M
        Py_UCS4 ch = PyUnicode_READ(kind, data, pos);
7185
7186
        /* U+0000-U+00ff range: Copy 8-bit characters as-is */
7187
4.90M
        if (ch < 0x100) {
7188
4.84M
            *p++ = (char) ch;
7189
4.84M
        }
7190
        /* U+0100-U+ffff range: Map 16-bit characters to '\uHHHH' */
7191
61.6k
        else if (ch < 0x10000) {
7192
61.0k
            *p++ = '\\';
7193
61.0k
            *p++ = 'u';
7194
61.0k
            *p++ = Py_hexdigits[(ch >> 12) & 0xf];
7195
61.0k
            *p++ = Py_hexdigits[(ch >> 8) & 0xf];
7196
61.0k
            *p++ = Py_hexdigits[(ch >> 4) & 0xf];
7197
61.0k
            *p++ = Py_hexdigits[ch & 15];
7198
61.0k
        }
7199
        /* U+010000-U+10ffff range: Map 32-bit characters to '\U00HHHHHH' */
7200
599
        else {
7201
599
            assert(ch <= MAX_UNICODE && MAX_UNICODE <= 0x10ffff);
7202
599
            *p++ = '\\';
7203
599
            *p++ = 'U';
7204
599
            *p++ = '0';
7205
599
            *p++ = '0';
7206
599
            *p++ = Py_hexdigits[(ch >> 20) & 0xf];
7207
599
            *p++ = Py_hexdigits[(ch >> 16) & 0xf];
7208
599
            *p++ = Py_hexdigits[(ch >> 12) & 0xf];
7209
599
            *p++ = Py_hexdigits[(ch >> 8) & 0xf];
7210
599
            *p++ = Py_hexdigits[(ch >> 4) & 0xf];
7211
599
            *p++ = Py_hexdigits[ch & 15];
7212
599
        }
7213
4.90M
    }
7214
7215
269
    assert(p > PyBytes_AS_STRING(repr));
7216
269
    if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) {
7217
0
        return NULL;
7218
0
    }
7219
269
    return repr;
7220
269
}
7221
7222
/* --- Latin-1 Codec ------------------------------------------------------ */
7223
7224
PyObject *
7225
PyUnicode_DecodeLatin1(const char *s,
7226
                       Py_ssize_t size,
7227
                       const char *errors)
7228
2.67M
{
7229
    /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */
7230
2.67M
    return _PyUnicode_FromUCS1((const unsigned char*)s, size);
7231
2.67M
}
7232
7233
/* create or adjust a UnicodeEncodeError */
7234
static void
7235
make_encode_exception(PyObject **exceptionObject,
7236
                      const char *encoding,
7237
                      PyObject *unicode,
7238
                      Py_ssize_t startpos, Py_ssize_t endpos,
7239
                      const char *reason)
7240
237k
{
7241
237k
    if (*exceptionObject == NULL) {
7242
237k
        *exceptionObject = PyObject_CallFunction(
7243
237k
            PyExc_UnicodeEncodeError, "sOnns",
7244
237k
            encoding, unicode, startpos, endpos, reason);
7245
237k
    }
7246
0
    else {
7247
0
        if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos))
7248
0
            goto onError;
7249
0
        if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos))
7250
0
            goto onError;
7251
0
        if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason))
7252
0
            goto onError;
7253
0
        return;
7254
0
      onError:
7255
0
        Py_CLEAR(*exceptionObject);
7256
0
    }
7257
237k
}
7258
7259
/* raises a UnicodeEncodeError */
7260
static void
7261
raise_encode_exception(PyObject **exceptionObject,
7262
                       const char *encoding,
7263
                       PyObject *unicode,
7264
                       Py_ssize_t startpos, Py_ssize_t endpos,
7265
                       const char *reason)
7266
38.5k
{
7267
38.5k
    make_encode_exception(exceptionObject,
7268
38.5k
                          encoding, unicode, startpos, endpos, reason);
7269
38.5k
    if (*exceptionObject != NULL)
7270
38.5k
        PyCodec_StrictErrors(*exceptionObject);
7271
38.5k
}
7272
7273
/* error handling callback helper:
7274
   build arguments, call the callback and check the arguments,
7275
   put the result into newpos and return the replacement string, which
7276
   has to be freed by the caller */
7277
static PyObject *
7278
unicode_encode_call_errorhandler(const char *errors,
7279
                                 PyObject **errorHandler,
7280
                                 const char *encoding, const char *reason,
7281
                                 PyObject *unicode, PyObject **exceptionObject,
7282
                                 Py_ssize_t startpos, Py_ssize_t endpos,
7283
                                 Py_ssize_t *newpos)
7284
198k
{
7285
198k
    static const char *argparse = "On;encoding error handler must return (str/bytes, int) tuple";
7286
198k
    Py_ssize_t len;
7287
198k
    PyObject *restuple;
7288
198k
    PyObject *resunicode;
7289
7290
198k
    if (*errorHandler == NULL) {
7291
198k
        *errorHandler = PyCodec_LookupError(errors);
7292
198k
        if (*errorHandler == NULL)
7293
0
            return NULL;
7294
198k
    }
7295
7296
198k
    len = PyUnicode_GET_LENGTH(unicode);
7297
7298
198k
    make_encode_exception(exceptionObject,
7299
198k
                          encoding, unicode, startpos, endpos, reason);
7300
198k
    if (*exceptionObject == NULL)
7301
0
        return NULL;
7302
7303
198k
    restuple = PyObject_CallOneArg(*errorHandler, *exceptionObject);
7304
198k
    if (restuple == NULL)
7305
198k
        return NULL;
7306
0
    if (!PyTuple_Check(restuple)) {
7307
0
        PyErr_SetString(PyExc_TypeError, &argparse[3]);
7308
0
        Py_DECREF(restuple);
7309
0
        return NULL;
7310
0
    }
7311
0
    if (!PyArg_ParseTuple(restuple, argparse,
7312
0
                          &resunicode, newpos)) {
7313
0
        Py_DECREF(restuple);
7314
0
        return NULL;
7315
0
    }
7316
0
    if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) {
7317
0
        PyErr_SetString(PyExc_TypeError, &argparse[3]);
7318
0
        Py_DECREF(restuple);
7319
0
        return NULL;
7320
0
    }
7321
0
    if (*newpos<0)
7322
0
        *newpos = len + *newpos;
7323
0
    if (*newpos<0 || *newpos>len) {
7324
0
        PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);
7325
0
        Py_DECREF(restuple);
7326
0
        return NULL;
7327
0
    }
7328
0
    Py_INCREF(resunicode);
7329
0
    Py_DECREF(restuple);
7330
0
    return resunicode;
7331
0
}
7332
7333
static PyObject *
7334
unicode_encode_ucs1(PyObject *unicode,
7335
                    const char *errors,
7336
                    const Py_UCS4 limit)
7337
47.8k
{
7338
    /* input state */
7339
47.8k
    Py_ssize_t pos=0, size;
7340
47.8k
    int kind;
7341
47.8k
    const void *data;
7342
    /* pointer into the output */
7343
47.8k
    char *str;
7344
47.8k
    const char *encoding = (limit == 256) ? "latin-1" : "ascii";
7345
47.8k
    const char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)";
7346
47.8k
    PyObject *error_handler_obj = NULL;
7347
47.8k
    PyObject *exc = NULL;
7348
47.8k
    _Py_error_handler error_handler = _Py_ERROR_UNKNOWN;
7349
47.8k
    PyObject *rep = NULL;
7350
    /* output object */
7351
47.8k
    _PyBytesWriter writer;
7352
7353
47.8k
    size = PyUnicode_GET_LENGTH(unicode);
7354
47.8k
    kind = PyUnicode_KIND(unicode);
7355
47.8k
    data = PyUnicode_DATA(unicode);
7356
    /* allocate enough for a simple encoding without
7357
       replacements, if we need more, we'll resize */
7358
47.8k
    if (size == 0)
7359
0
        return PyBytes_FromStringAndSize(NULL, 0);
7360
7361
47.8k
    _PyBytesWriter_Init(&writer);
7362
47.8k
    str = _PyBytesWriter_Alloc(&writer, size);
7363
47.8k
    if (str == NULL)
7364
0
        return NULL;
7365
7366
3.77M
    while (pos < size) {
7367
3.77M
        Py_UCS4 ch = PyUnicode_READ(kind, data, pos);
7368
7369
        /* can we encode this? */
7370
3.77M
        if (ch < limit) {
7371
            /* no overflow check, because we know that the space is enough */
7372
3.72M
            *str++ = (char)ch;
7373
3.72M
            ++pos;
7374
3.72M
        }
7375
47.8k
        else {
7376
47.8k
            Py_ssize_t newpos, i;
7377
            /* startpos for collecting unencodable chars */
7378
47.8k
            Py_ssize_t collstart = pos;
7379
47.8k
            Py_ssize_t collend = collstart + 1;
7380
            /* find all unecodable characters */
7381
7382
423k
            while ((collend < size) && (PyUnicode_READ(kind, data, collend) >= limit))
7383
375k
                ++collend;
7384
7385
            /* Only overallocate the buffer if it's not the last write */
7386
47.8k
            writer.overallocate = (collend < size);
7387
7388
            /* cache callback name lookup (if not done yet, i.e. it's the first error) */
7389
47.8k
            if (error_handler == _Py_ERROR_UNKNOWN)
7390
47.8k
                error_handler = _Py_GetErrorHandler(errors);
7391
7392
47.8k
            switch (error_handler) {
7393
38.5k
            case _Py_ERROR_STRICT:
7394
38.5k
                raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason);
7395
38.5k
                goto onError;
7396
7397
0
            case _Py_ERROR_REPLACE:
7398
0
                memset(str, '?', collend - collstart);
7399
0
                str += (collend - collstart);
7400
0
                _Py_FALLTHROUGH;
7401
0
            case _Py_ERROR_IGNORE:
7402
0
                pos = collend;
7403
0
                break;
7404
7405
0
            case _Py_ERROR_BACKSLASHREPLACE:
7406
                /* subtract preallocated bytes */
7407
0
                writer.min_size -= (collend - collstart);
7408
0
                str = backslashreplace(&writer, str,
7409
0
                                       unicode, collstart, collend);
7410
0
                if (str == NULL)
7411
0
                    goto onError;
7412
0
                pos = collend;
7413
0
                break;
7414
7415
0
            case _Py_ERROR_XMLCHARREFREPLACE:
7416
                /* subtract preallocated bytes */
7417
0
                writer.min_size -= (collend - collstart);
7418
0
                str = xmlcharrefreplace(&writer, str,
7419
0
                                        unicode, collstart, collend);
7420
0
                if (str == NULL)
7421
0
                    goto onError;
7422
0
                pos = collend;
7423
0
                break;
7424
7425
9.30k
            case _Py_ERROR_SURROGATEESCAPE:
7426
9.30k
                for (i = collstart; i < collend; ++i) {
7427
9.30k
                    ch = PyUnicode_READ(kind, data, i);
7428
9.30k
                    if (ch < 0xdc80 || 0xdcff < ch) {
7429
                        /* Not a UTF-8b surrogate */
7430
9.30k
                        break;
7431
9.30k
                    }
7432
0
                    *str++ = (char)(ch - 0xdc00);
7433
0
                    ++pos;
7434
0
                }
7435
9.30k
                if (i >= collend)
7436
0
                    break;
7437
9.30k
                collstart = pos;
7438
9.30k
                assert(collstart != collend);
7439
9.30k
                _Py_FALLTHROUGH;
7440
7441
9.30k
            default:
7442
9.30k
                rep = unicode_encode_call_errorhandler(errors, &error_handler_obj,
7443
9.30k
                                                       encoding, reason, unicode, &exc,
7444
9.30k
                                                       collstart, collend, &newpos);
7445
9.30k
                if (rep == NULL)
7446
9.30k
                    goto onError;
7447
7448
0
                if (newpos < collstart) {
7449
0
                    writer.overallocate = 1;
7450
0
                    str = _PyBytesWriter_Prepare(&writer, str,
7451
0
                                                 collstart - newpos);
7452
0
                    if (str == NULL)
7453
0
                        goto onError;
7454
0
                }
7455
0
                else {
7456
                    /* subtract preallocated bytes */
7457
0
                    writer.min_size -= newpos - collstart;
7458
                    /* Only overallocate the buffer if it's not the last write */
7459
0
                    writer.overallocate = (newpos < size);
7460
0
                }
7461
7462
0
                if (PyBytes_Check(rep)) {
7463
                    /* Directly copy bytes result to output. */
7464
0
                    str = _PyBytesWriter_WriteBytes(&writer, str,
7465
0
                                                    PyBytes_AS_STRING(rep),
7466
0
                                                    PyBytes_GET_SIZE(rep));
7467
0
                }
7468
0
                else {
7469
0
                    assert(PyUnicode_Check(rep));
7470
7471
0
                    if (limit == 256 ?
7472
0
                        PyUnicode_KIND(rep) != PyUnicode_1BYTE_KIND :
7473
0
                        !PyUnicode_IS_ASCII(rep))
7474
0
                    {
7475
                        /* Not all characters are smaller than limit */
7476
0
                        raise_encode_exception(&exc, encoding, unicode,
7477
0
                                               collstart, collend, reason);
7478
0
                        goto onError;
7479
0
                    }
7480
0
                    assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND);
7481
0
                    str = _PyBytesWriter_WriteBytes(&writer, str,
7482
0
                                                    PyUnicode_DATA(rep),
7483
0
                                                    PyUnicode_GET_LENGTH(rep));
7484
0
                }
7485
0
                if (str == NULL)
7486
0
                    goto onError;
7487
7488
0
                pos = newpos;
7489
0
                Py_CLEAR(rep);
7490
47.8k
            }
7491
7492
            /* If overallocation was disabled, ensure that it was the last
7493
               write. Otherwise, we missed an optimization */
7494
0
            assert(writer.overallocate || pos == size);
7495
0
        }
7496
3.77M
    }
7497
7498
0
    Py_XDECREF(error_handler_obj);
7499
0
    Py_XDECREF(exc);
7500
0
    return _PyBytesWriter_Finish(&writer, str);
7501
7502
47.8k
  onError:
7503
47.8k
    Py_XDECREF(rep);
7504
47.8k
    _PyBytesWriter_Dealloc(&writer);
7505
47.8k
    Py_XDECREF(error_handler_obj);
7506
47.8k
    Py_XDECREF(exc);
7507
47.8k
    return NULL;
7508
47.8k
}
7509
7510
PyObject *
7511
_PyUnicode_AsLatin1String(PyObject *unicode, const char *errors)
7512
0
{
7513
0
    if (!PyUnicode_Check(unicode)) {
7514
0
        PyErr_BadArgument();
7515
0
        return NULL;
7516
0
    }
7517
    /* Fast path: if it is a one-byte string, construct
7518
       bytes object directly. */
7519
0
    if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND)
7520
0
        return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode),
7521
0
                                         PyUnicode_GET_LENGTH(unicode));
7522
    /* Non-Latin-1 characters present. Defer to above function to
7523
       raise the exception. */
7524
0
    return unicode_encode_ucs1(unicode, errors, 256);
7525
0
}
7526
7527
PyObject*
7528
PyUnicode_AsLatin1String(PyObject *unicode)
7529
0
{
7530
0
    return _PyUnicode_AsLatin1String(unicode, NULL);
7531
0
}
7532
7533
/* --- 7-bit ASCII Codec -------------------------------------------------- */
7534
7535
PyObject *
7536
PyUnicode_DecodeASCII(const char *s,
7537
                      Py_ssize_t size,
7538
                      const char *errors)
7539
916k
{
7540
916k
    const char *starts = s;
7541
916k
    const char *e = s + size;
7542
916k
    PyObject *error_handler_obj = NULL;
7543
916k
    PyObject *exc = NULL;
7544
916k
    _Py_error_handler error_handler = _Py_ERROR_UNKNOWN;
7545
7546
916k
    if (size == 0)
7547
0
        _Py_RETURN_UNICODE_EMPTY();
7548
7549
    /* ASCII is equivalent to the first 128 ordinals in Unicode. */
7550
916k
    if (size == 1 && (unsigned char)s[0] < 128) {
7551
7.10k
        return get_latin1_char((unsigned char)s[0]);
7552
7.10k
    }
7553
7554
    // Shortcut for simple case
7555
909k
    PyObject *u = PyUnicode_New(size, 127);
7556
909k
    if (u == NULL) {
7557
0
        return NULL;
7558
0
    }
7559
909k
    Py_ssize_t outpos = ascii_decode(s, e, PyUnicode_1BYTE_DATA(u));
7560
909k
    if (outpos == size) {
7561
737k
        return u;
7562
737k
    }
7563
7564
171k
    _PyUnicodeWriter writer;
7565
171k
    _PyUnicodeWriter_InitWithBuffer(&writer, u);
7566
171k
    writer.pos = outpos;
7567
7568
171k
    s += outpos;
7569
171k
    int kind = writer.kind;
7570
171k
    void *data = writer.data;
7571
171k
    Py_ssize_t startinpos, endinpos;
7572
7573
26.9M
    while (s < e) {
7574
26.7M
        unsigned char c = (unsigned char)*s;
7575
26.7M
        if (c < 128) {
7576
9.36M
            PyUnicode_WRITE(kind, data, writer.pos, c);
7577
9.36M
            writer.pos++;
7578
9.36M
            ++s;
7579
9.36M
            continue;
7580
9.36M
        }
7581
7582
        /* byte outsize range 0x00..0x7f: call the error handler */
7583
7584
17.3M
        if (error_handler == _Py_ERROR_UNKNOWN)
7585
171k
            error_handler = _Py_GetErrorHandler(errors);
7586
7587
17.3M
        switch (error_handler)
7588
17.3M
        {
7589
764k
        case _Py_ERROR_REPLACE:
7590
17.3M
        case _Py_ERROR_SURROGATEESCAPE:
7591
            /* Fast-path: the error handler only writes one character,
7592
               but we may switch to UCS2 at the first write */
7593
17.3M
            if (_PyUnicodeWriter_PrepareKind(&writer, PyUnicode_2BYTE_KIND) < 0)
7594
0
                goto onError;
7595
17.3M
            kind = writer.kind;
7596
17.3M
            data = writer.data;
7597
7598
17.3M
            if (error_handler == _Py_ERROR_REPLACE)
7599
764k
                PyUnicode_WRITE(kind, data, writer.pos, 0xfffd);
7600
16.5M
            else
7601
16.5M
                PyUnicode_WRITE(kind, data, writer.pos, c + 0xdc00);
7602
17.3M
            writer.pos++;
7603
17.3M
            ++s;
7604
17.3M
            break;
7605
7606
0
        case _Py_ERROR_IGNORE:
7607
0
            ++s;
7608
0
            break;
7609
7610
8.37k
        default:
7611
8.37k
            startinpos = s-starts;
7612
8.37k
            endinpos = startinpos + 1;
7613
8.37k
            if (unicode_decode_call_errorhandler_writer(
7614
8.37k
                    errors, &error_handler_obj,
7615
8.37k
                    "ascii", "ordinal not in range(128)",
7616
8.37k
                    &starts, &e, &startinpos, &endinpos, &exc, &s,
7617
8.37k
                    &writer))
7618
8.37k
                goto onError;
7619
0
            kind = writer.kind;
7620
0
            data = writer.data;
7621
17.3M
        }
7622
17.3M
    }
7623
163k
    Py_XDECREF(error_handler_obj);
7624
163k
    Py_XDECREF(exc);
7625
163k
    return _PyUnicodeWriter_Finish(&writer);
7626
7627
8.37k
  onError:
7628
8.37k
    _PyUnicodeWriter_Dealloc(&writer);
7629
8.37k
    Py_XDECREF(error_handler_obj);
7630
8.37k
    Py_XDECREF(exc);
7631
8.37k
    return NULL;
7632
171k
}
7633
7634
PyObject *
7635
_PyUnicode_AsASCIIString(PyObject *unicode, const char *errors)
7636
107k
{
7637
107k
    if (!PyUnicode_Check(unicode)) {
7638
0
        PyErr_BadArgument();
7639
0
        return NULL;
7640
0
    }
7641
    /* Fast path: if it is an ASCII-only string, construct bytes object
7642
       directly. Else defer to above function to raise the exception. */
7643
107k
    if (PyUnicode_IS_ASCII(unicode))
7644
59.2k
        return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode),
7645
59.2k
                                         PyUnicode_GET_LENGTH(unicode));
7646
47.8k
    return unicode_encode_ucs1(unicode, errors, 128);
7647
107k
}
7648
7649
PyObject *
7650
PyUnicode_AsASCIIString(PyObject *unicode)
7651
4
{
7652
4
    return _PyUnicode_AsASCIIString(unicode, NULL);
7653
4
}
7654
7655
#ifdef MS_WINDOWS
7656
7657
/* --- MBCS codecs for Windows -------------------------------------------- */
7658
7659
#if SIZEOF_INT < SIZEOF_SIZE_T
7660
#define NEED_RETRY
7661
#endif
7662
7663
/* INT_MAX is the theoretical largest chunk (or INT_MAX / 2 when
7664
   transcoding from UTF-16), but INT_MAX / 4 performs better in
7665
   both cases also and avoids partial characters overrunning the
7666
   length limit in MultiByteToWideChar on Windows */
7667
#define DECODING_CHUNK_SIZE (INT_MAX/4)
7668
7669
#ifndef WC_ERR_INVALID_CHARS
7670
#  define WC_ERR_INVALID_CHARS 0x0080
7671
#endif
7672
7673
static const char*
7674
code_page_name(UINT code_page, PyObject **obj)
7675
{
7676
    *obj = NULL;
7677
    if (code_page == CP_ACP)
7678
        return "mbcs";
7679
7680
    *obj = PyBytes_FromFormat("cp%u", code_page);
7681
    if (*obj == NULL)
7682
        return NULL;
7683
    return PyBytes_AS_STRING(*obj);
7684
}
7685
7686
static DWORD
7687
decode_code_page_flags(UINT code_page)
7688
{
7689
    if (code_page == CP_UTF7) {
7690
        /* The CP_UTF7 decoder only supports flags=0 */
7691
        return 0;
7692
    }
7693
    else
7694
        return MB_ERR_INVALID_CHARS;
7695
}
7696
7697
/*
7698
 * Decode a byte string from a Windows code page into unicode object in strict
7699
 * mode.
7700
 *
7701
 * Returns consumed size if succeed, returns -2 on decode error, or raise an
7702
 * OSError and returns -1 on other error.
7703
 */
7704
static int
7705
decode_code_page_strict(UINT code_page,
7706
                        wchar_t **buf,
7707
                        Py_ssize_t *bufsize,
7708
                        const char *in,
7709
                        int insize)
7710
{
7711
    DWORD flags = MB_ERR_INVALID_CHARS;
7712
    wchar_t *out;
7713
    DWORD outsize;
7714
7715
    /* First get the size of the result */
7716
    assert(insize > 0);
7717
    while ((outsize = MultiByteToWideChar(code_page, flags,
7718
                                          in, insize, NULL, 0)) <= 0)
7719
    {
7720
        if (!flags || GetLastError() != ERROR_INVALID_FLAGS) {
7721
            goto error;
7722
        }
7723
        /* For some code pages (e.g. UTF-7) flags must be set to 0. */
7724
        flags = 0;
7725
    }
7726
7727
    /* Extend a wchar_t* buffer */
7728
    Py_ssize_t n = *bufsize;   /* Get the current length */
7729
    if (widechar_resize(buf, bufsize, n + outsize) < 0) {
7730
        return -1;
7731
    }
7732
    out = *buf + n;
7733
7734
    /* Do the conversion */
7735
    outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize);
7736
    if (outsize <= 0)
7737
        goto error;
7738
    return insize;
7739
7740
error:
7741
    if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION)
7742
        return -2;
7743
    PyErr_SetFromWindowsErr(0);
7744
    return -1;
7745
}
7746
7747
/*
7748
 * Decode a byte string from a code page into unicode object with an error
7749
 * handler.
7750
 *
7751
 * Returns consumed size if succeed, or raise an OSError or
7752
 * UnicodeDecodeError exception and returns -1 on error.
7753
 */
7754
static int
7755
decode_code_page_errors(UINT code_page,
7756
                        wchar_t **buf,
7757
                        Py_ssize_t *bufsize,
7758
                        const char *in, const int size,
7759
                        const char *errors, int final)
7760
{
7761
    const char *startin = in;
7762
    const char *endin = in + size;
7763
    DWORD flags = MB_ERR_INVALID_CHARS;
7764
    /* Ideally, we should get reason from FormatMessage. This is the Windows
7765
       2000 English version of the message. */
7766
    const char *reason = "No mapping for the Unicode character exists "
7767
                         "in the target code page.";
7768
    /* each step cannot decode more than 1 character, but a character can be
7769
       represented as a surrogate pair */
7770
    wchar_t buffer[2], *out;
7771
    int insize;
7772
    Py_ssize_t outsize;
7773
    PyObject *errorHandler = NULL;
7774
    PyObject *exc = NULL;
7775
    PyObject *encoding_obj = NULL;
7776
    const char *encoding;
7777
    DWORD err;
7778
    int ret = -1;
7779
7780
    assert(size > 0);
7781
7782
    encoding = code_page_name(code_page, &encoding_obj);
7783
    if (encoding == NULL)
7784
        return -1;
7785
7786
    if ((errors == NULL || strcmp(errors, "strict") == 0) && final) {
7787
        /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a
7788
           UnicodeDecodeError. */
7789
        make_decode_exception(&exc, encoding, in, size, 0, 0, reason);
7790
        if (exc != NULL) {
7791
            PyCodec_StrictErrors(exc);
7792
            Py_CLEAR(exc);
7793
        }
7794
        goto error;
7795
    }
7796
7797
    /* Extend a wchar_t* buffer */
7798
    Py_ssize_t n = *bufsize;   /* Get the current length */
7799
    if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) {
7800
        PyErr_NoMemory();
7801
        goto error;
7802
    }
7803
    if (widechar_resize(buf, bufsize, n + size * Py_ARRAY_LENGTH(buffer)) < 0) {
7804
        goto error;
7805
    }
7806
    out = *buf + n;
7807
7808
    /* Decode the byte string character per character */
7809
    while (in < endin)
7810
    {
7811
        /* Decode a character */
7812
        insize = 1;
7813
        do
7814
        {
7815
            outsize = MultiByteToWideChar(code_page, flags,
7816
                                          in, insize,
7817
                                          buffer, Py_ARRAY_LENGTH(buffer));
7818
            if (outsize > 0)
7819
                break;
7820
            err = GetLastError();
7821
            if (err == ERROR_INVALID_FLAGS && flags) {
7822
                /* For some code pages (e.g. UTF-7) flags must be set to 0. */
7823
                flags = 0;
7824
                continue;
7825
            }
7826
            if (err != ERROR_NO_UNICODE_TRANSLATION
7827
                && err != ERROR_INSUFFICIENT_BUFFER)
7828
            {
7829
                PyErr_SetFromWindowsErr(err);
7830
                goto error;
7831
            }
7832
            insize++;
7833
        }
7834
        /* 4=maximum length of a UTF-8 sequence */
7835
        while (insize <= 4 && (in + insize) <= endin);
7836
7837
        if (outsize <= 0) {
7838
            Py_ssize_t startinpos, endinpos, outpos;
7839
7840
            /* last character in partial decode? */
7841
            if (in + insize >= endin && !final)
7842
                break;
7843
7844
            startinpos = in - startin;
7845
            endinpos = startinpos + 1;
7846
            outpos = out - *buf;
7847
            if (unicode_decode_call_errorhandler_wchar(
7848
                    errors, &errorHandler,
7849
                    encoding, reason,
7850
                    &startin, &endin, &startinpos, &endinpos, &exc, &in,
7851
                    buf, bufsize, &outpos))
7852
            {
7853
                goto error;
7854
            }
7855
            out = *buf + outpos;
7856
        }
7857
        else {
7858
            in += insize;
7859
            memcpy(out, buffer, outsize * sizeof(wchar_t));
7860
            out += outsize;
7861
        }
7862
    }
7863
7864
    /* Shrink the buffer */
7865
    assert(out - *buf <= *bufsize);
7866
    *bufsize = out - *buf;
7867
    /* (in - startin) <= size and size is an int */
7868
    ret = Py_SAFE_DOWNCAST(in - startin, Py_ssize_t, int);
7869
7870
error:
7871
    Py_XDECREF(encoding_obj);
7872
    Py_XDECREF(errorHandler);
7873
    Py_XDECREF(exc);
7874
    return ret;
7875
}
7876
7877
static PyObject *
7878
decode_code_page_stateful(int code_page,
7879
                          const char *s, Py_ssize_t size,
7880
                          const char *errors, Py_ssize_t *consumed)
7881
{
7882
    wchar_t *buf = NULL;
7883
    Py_ssize_t bufsize = 0;
7884
    int chunk_size, final, converted, done;
7885
7886
    if (code_page < 0) {
7887
        PyErr_SetString(PyExc_ValueError, "invalid code page number");
7888
        return NULL;
7889
    }
7890
    if (size < 0) {
7891
        PyErr_BadInternalCall();
7892
        return NULL;
7893
    }
7894
7895
    if (consumed)
7896
        *consumed = 0;
7897
7898
    do
7899
    {
7900
#ifdef NEED_RETRY
7901
        if (size > DECODING_CHUNK_SIZE) {
7902
            chunk_size = DECODING_CHUNK_SIZE;
7903
            final = 0;
7904
            done = 0;
7905
        }
7906
        else
7907
#endif
7908
        {
7909
            chunk_size = (int)size;
7910
            final = (consumed == NULL);
7911
            done = 1;
7912
        }
7913
7914
        if (chunk_size == 0 && done) {
7915
            if (buf != NULL)
7916
                break;
7917
            _Py_RETURN_UNICODE_EMPTY();
7918
        }
7919
7920
        converted = decode_code_page_strict(code_page, &buf, &bufsize,
7921
                                            s, chunk_size);
7922
        if (converted == -2)
7923
            converted = decode_code_page_errors(code_page, &buf, &bufsize,
7924
                                                s, chunk_size,
7925
                                                errors, final);
7926
        assert(converted != 0 || done);
7927
7928
        if (converted < 0) {
7929
            PyMem_Free(buf);
7930
            return NULL;
7931
        }
7932
7933
        if (consumed)
7934
            *consumed += converted;
7935
7936
        s += converted;
7937
        size -= converted;
7938
    } while (!done);
7939
7940
    PyObject *v = PyUnicode_FromWideChar(buf, bufsize);
7941
    PyMem_Free(buf);
7942
    return v;
7943
}
7944
7945
PyObject *
7946
PyUnicode_DecodeCodePageStateful(int code_page,
7947
                                 const char *s,
7948
                                 Py_ssize_t size,
7949
                                 const char *errors,
7950
                                 Py_ssize_t *consumed)
7951
{
7952
    return decode_code_page_stateful(code_page, s, size, errors, consumed);
7953
}
7954
7955
PyObject *
7956
PyUnicode_DecodeMBCSStateful(const char *s,
7957
                             Py_ssize_t size,
7958
                             const char *errors,
7959
                             Py_ssize_t *consumed)
7960
{
7961
    return decode_code_page_stateful(CP_ACP, s, size, errors, consumed);
7962
}
7963
7964
PyObject *
7965
PyUnicode_DecodeMBCS(const char *s,
7966
                     Py_ssize_t size,
7967
                     const char *errors)
7968
{
7969
    return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL);
7970
}
7971
7972
static DWORD
7973
encode_code_page_flags(UINT code_page, const char *errors)
7974
{
7975
    if (code_page == CP_UTF8) {
7976
        return WC_ERR_INVALID_CHARS;
7977
    }
7978
    else if (code_page == CP_UTF7) {
7979
        /* CP_UTF7 only supports flags=0 */
7980
        return 0;
7981
    }
7982
    else {
7983
        if (errors != NULL && strcmp(errors, "replace") == 0)
7984
            return 0;
7985
        else
7986
            return WC_NO_BEST_FIT_CHARS;
7987
    }
7988
}
7989
7990
/*
7991
 * Encode a Unicode string to a Windows code page into a byte string in strict
7992
 * mode.
7993
 *
7994
 * Returns consumed characters if succeed, returns -2 on encode error, or raise
7995
 * an OSError and returns -1 on other error.
7996
 */
7997
static int
7998
encode_code_page_strict(UINT code_page, PyObject **outbytes,
7999
                        PyObject *unicode, Py_ssize_t offset, int len,
8000
                        const char* errors)
8001
{
8002
    BOOL usedDefaultChar = FALSE;
8003
    BOOL *pusedDefaultChar = &usedDefaultChar;
8004
    int outsize;
8005
    wchar_t *p;
8006
    Py_ssize_t size;
8007
    const DWORD flags = encode_code_page_flags(code_page, NULL);
8008
    char *out;
8009
    /* Create a substring so that we can get the UTF-16 representation
8010
       of just the slice under consideration. */
8011
    PyObject *substring;
8012
    int ret = -1;
8013
8014
    assert(len > 0);
8015
8016
    if (code_page != CP_UTF8 && code_page != CP_UTF7)
8017
        pusedDefaultChar = &usedDefaultChar;
8018
    else
8019
        pusedDefaultChar = NULL;
8020
8021
    substring = PyUnicode_Substring(unicode, offset, offset+len);
8022
    if (substring == NULL)
8023
        return -1;
8024
    p = PyUnicode_AsWideCharString(substring, &size);
8025
    Py_CLEAR(substring);
8026
    if (p == NULL) {
8027
        return -1;
8028
    }
8029
    assert(size <= INT_MAX);
8030
8031
    /* First get the size of the result */
8032
    outsize = WideCharToMultiByte(code_page, flags,
8033
                                  p, (int)size,
8034
                                  NULL, 0,
8035
                                  NULL, pusedDefaultChar);
8036
    if (outsize <= 0)
8037
        goto error;
8038
    /* If we used a default char, then we failed! */
8039
    if (pusedDefaultChar && *pusedDefaultChar) {
8040
        ret = -2;
8041
        goto done;
8042
    }
8043
8044
    if (*outbytes == NULL) {
8045
        /* Create string object */
8046
        *outbytes = PyBytes_FromStringAndSize(NULL, outsize);
8047
        if (*outbytes == NULL) {
8048
            goto done;
8049
        }
8050
        out = PyBytes_AS_STRING(*outbytes);
8051
    }
8052
    else {
8053
        /* Extend string object */
8054
        const Py_ssize_t n = PyBytes_Size(*outbytes);
8055
        if (outsize > PY_SSIZE_T_MAX - n) {
8056
            PyErr_NoMemory();
8057
            goto done;
8058
        }
8059
        if (_PyBytes_Resize(outbytes, n + outsize) < 0) {
8060
            goto done;
8061
        }
8062
        out = PyBytes_AS_STRING(*outbytes) + n;
8063
    }
8064
8065
    /* Do the conversion */
8066
    outsize = WideCharToMultiByte(code_page, flags,
8067
                                  p, (int)size,
8068
                                  out, outsize,
8069
                                  NULL, pusedDefaultChar);
8070
    if (outsize <= 0)
8071
        goto error;
8072
    if (pusedDefaultChar && *pusedDefaultChar) {
8073
        ret = -2;
8074
        goto done;
8075
    }
8076
    ret = 0;
8077
8078
done:
8079
    PyMem_Free(p);
8080
    return ret;
8081
8082
error:
8083
    if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) {
8084
        ret = -2;
8085
        goto done;
8086
    }
8087
    PyErr_SetFromWindowsErr(0);
8088
    goto done;
8089
}
8090
8091
/*
8092
 * Encode a Unicode string to a Windows code page into a byte string using an
8093
 * error handler.
8094
 *
8095
 * Returns consumed characters if succeed, or raise an OSError and returns
8096
 * -1 on other error.
8097
 */
8098
static int
8099
encode_code_page_errors(UINT code_page, PyObject **outbytes,
8100
                        PyObject *unicode, Py_ssize_t unicode_offset,
8101
                        Py_ssize_t insize, const char* errors)
8102
{
8103
    const DWORD flags = encode_code_page_flags(code_page, errors);
8104
    Py_ssize_t pos = unicode_offset;
8105
    Py_ssize_t endin = unicode_offset + insize;
8106
    /* Ideally, we should get reason from FormatMessage. This is the Windows
8107
       2000 English version of the message. */
8108
    const char *reason = "invalid character";
8109
    /* 4=maximum length of a UTF-8 sequence */
8110
    char buffer[4];
8111
    BOOL usedDefaultChar = FALSE, *pusedDefaultChar;
8112
    Py_ssize_t outsize;
8113
    char *out;
8114
    PyObject *errorHandler = NULL;
8115
    PyObject *exc = NULL;
8116
    PyObject *encoding_obj = NULL;
8117
    const char *encoding;
8118
    Py_ssize_t newpos, newoutsize;
8119
    PyObject *rep;
8120
    int ret = -1;
8121
8122
    assert(insize > 0);
8123
8124
    encoding = code_page_name(code_page, &encoding_obj);
8125
    if (encoding == NULL)
8126
        return -1;
8127
8128
    if (errors == NULL || strcmp(errors, "strict") == 0) {
8129
        /* The last error was ERROR_NO_UNICODE_TRANSLATION,
8130
           then we raise a UnicodeEncodeError. */
8131
        make_encode_exception(&exc, encoding, unicode, 0, 0, reason);
8132
        if (exc != NULL) {
8133
            PyCodec_StrictErrors(exc);
8134
            Py_DECREF(exc);
8135
        }
8136
        Py_XDECREF(encoding_obj);
8137
        return -1;
8138
    }
8139
8140
    if (code_page != CP_UTF8 && code_page != CP_UTF7)
8141
        pusedDefaultChar = &usedDefaultChar;
8142
    else
8143
        pusedDefaultChar = NULL;
8144
8145
    if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) {
8146
        PyErr_NoMemory();
8147
        goto error;
8148
    }
8149
    outsize = insize * Py_ARRAY_LENGTH(buffer);
8150
8151
    if (*outbytes == NULL) {
8152
        /* Create string object */
8153
        *outbytes = PyBytes_FromStringAndSize(NULL, outsize);
8154
        if (*outbytes == NULL)
8155
            goto error;
8156
        out = PyBytes_AS_STRING(*outbytes);
8157
    }
8158
    else {
8159
        /* Extend string object */
8160
        Py_ssize_t n = PyBytes_Size(*outbytes);
8161
        if (n > PY_SSIZE_T_MAX - outsize) {
8162
            PyErr_NoMemory();
8163
            goto error;
8164
        }
8165
        if (_PyBytes_Resize(outbytes, n + outsize) < 0)
8166
            goto error;
8167
        out = PyBytes_AS_STRING(*outbytes) + n;
8168
    }
8169
8170
    /* Encode the string character per character */
8171
    while (pos < endin)
8172
    {
8173
        Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos);
8174
        wchar_t chars[2];
8175
        int charsize;
8176
        if (ch < 0x10000) {
8177
            chars[0] = (wchar_t)ch;
8178
            charsize = 1;
8179
        }
8180
        else {
8181
            chars[0] = Py_UNICODE_HIGH_SURROGATE(ch);
8182
            chars[1] = Py_UNICODE_LOW_SURROGATE(ch);
8183
            charsize = 2;
8184
        }
8185
8186
        outsize = WideCharToMultiByte(code_page, flags,
8187
                                      chars, charsize,
8188
                                      buffer, Py_ARRAY_LENGTH(buffer),
8189
                                      NULL, pusedDefaultChar);
8190
        if (outsize > 0) {
8191
            if (pusedDefaultChar == NULL || !(*pusedDefaultChar))
8192
            {
8193
                pos++;
8194
                memcpy(out, buffer, outsize);
8195
                out += outsize;
8196
                continue;
8197
            }
8198
        }
8199
        else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) {
8200
            PyErr_SetFromWindowsErr(0);
8201
            goto error;
8202
        }
8203
8204
        rep = unicode_encode_call_errorhandler(
8205
                  errors, &errorHandler, encoding, reason,
8206
                  unicode, &exc,
8207
                  pos, pos + 1, &newpos);
8208
        if (rep == NULL)
8209
            goto error;
8210
8211
        Py_ssize_t morebytes = pos - newpos;
8212
        if (PyBytes_Check(rep)) {
8213
            outsize = PyBytes_GET_SIZE(rep);
8214
            morebytes += outsize;
8215
            if (morebytes > 0) {
8216
                Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes);
8217
                newoutsize = PyBytes_GET_SIZE(*outbytes) + morebytes;
8218
                if (_PyBytes_Resize(outbytes, newoutsize) < 0) {
8219
                    Py_DECREF(rep);
8220
                    goto error;
8221
                }
8222
                out = PyBytes_AS_STRING(*outbytes) + offset;
8223
            }
8224
            memcpy(out, PyBytes_AS_STRING(rep), outsize);
8225
            out += outsize;
8226
        }
8227
        else {
8228
            Py_ssize_t i;
8229
            int kind;
8230
            const void *data;
8231
8232
            outsize = PyUnicode_GET_LENGTH(rep);
8233
            morebytes += outsize;
8234
            if (morebytes > 0) {
8235
                Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes);
8236
                newoutsize = PyBytes_GET_SIZE(*outbytes) + morebytes;
8237
                if (_PyBytes_Resize(outbytes, newoutsize) < 0) {
8238
                    Py_DECREF(rep);
8239
                    goto error;
8240
                }
8241
                out = PyBytes_AS_STRING(*outbytes) + offset;
8242
            }
8243
            kind = PyUnicode_KIND(rep);
8244
            data = PyUnicode_DATA(rep);
8245
            for (i=0; i < outsize; i++) {
8246
                Py_UCS4 ch = PyUnicode_READ(kind, data, i);
8247
                if (ch > 127) {
8248
                    raise_encode_exception(&exc,
8249
                        encoding, unicode,
8250
                        pos, pos + 1,
8251
                        "unable to encode error handler result to ASCII");
8252
                    Py_DECREF(rep);
8253
                    goto error;
8254
                }
8255
                *out = (unsigned char)ch;
8256
                out++;
8257
            }
8258
        }
8259
        pos = newpos;
8260
        Py_DECREF(rep);
8261
    }
8262
    /* write a NUL byte */
8263
    *out = 0;
8264
    outsize = out - PyBytes_AS_STRING(*outbytes);
8265
    assert(outsize <= PyBytes_GET_SIZE(*outbytes));
8266
    if (_PyBytes_Resize(outbytes, outsize) < 0)
8267
        goto error;
8268
    ret = 0;
8269
8270
error:
8271
    Py_XDECREF(encoding_obj);
8272
    Py_XDECREF(errorHandler);
8273
    Py_XDECREF(exc);
8274
    return ret;
8275
}
8276
8277
static PyObject *
8278
encode_code_page(int code_page,
8279
                 PyObject *unicode,
8280
                 const char *errors)
8281
{
8282
    Py_ssize_t len;
8283
    PyObject *outbytes = NULL;
8284
    Py_ssize_t offset;
8285
    int chunk_len, ret, done;
8286
8287
    if (!PyUnicode_Check(unicode)) {
8288
        PyErr_BadArgument();
8289
        return NULL;
8290
    }
8291
8292
    len = PyUnicode_GET_LENGTH(unicode);
8293
8294
    if (code_page < 0) {
8295
        PyErr_SetString(PyExc_ValueError, "invalid code page number");
8296
        return NULL;
8297
    }
8298
8299
    if (len == 0)
8300
        return PyBytes_FromStringAndSize(NULL, 0);
8301
8302
    offset = 0;
8303
    do
8304
    {
8305
#ifdef NEED_RETRY
8306
        if (len > DECODING_CHUNK_SIZE) {
8307
            chunk_len = DECODING_CHUNK_SIZE;
8308
            done = 0;
8309
        }
8310
        else
8311
#endif
8312
        {
8313
            chunk_len = (int)len;
8314
            done = 1;
8315
        }
8316
8317
        ret = encode_code_page_strict(code_page, &outbytes,
8318
                                      unicode, offset, chunk_len,
8319
                                      errors);
8320
        if (ret == -2)
8321
            ret = encode_code_page_errors(code_page, &outbytes,
8322
                                          unicode, offset,
8323
                                          chunk_len, errors);
8324
        if (ret < 0) {
8325
            Py_XDECREF(outbytes);
8326
            return NULL;
8327
        }
8328
8329
        offset += chunk_len;
8330
        len -= chunk_len;
8331
    } while (!done);
8332
8333
    return outbytes;
8334
}
8335
8336
PyObject *
8337
PyUnicode_EncodeCodePage(int code_page,
8338
                         PyObject *unicode,
8339
                         const char *errors)
8340
{
8341
    return encode_code_page(code_page, unicode, errors);
8342
}
8343
8344
PyObject *
8345
PyUnicode_AsMBCSString(PyObject *unicode)
8346
{
8347
    return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL);
8348
}
8349
8350
#undef NEED_RETRY
8351
8352
#endif /* MS_WINDOWS */
8353
8354
/* --- Character Mapping Codec -------------------------------------------- */
8355
8356
static int
8357
charmap_decode_string(const char *s,
8358
                      Py_ssize_t size,
8359
                      PyObject *mapping,
8360
                      const char *errors,
8361
                      _PyUnicodeWriter *writer)
8362
19.0k
{
8363
19.0k
    const char *starts = s;
8364
19.0k
    const char *e;
8365
19.0k
    Py_ssize_t startinpos, endinpos;
8366
19.0k
    PyObject *errorHandler = NULL, *exc = NULL;
8367
19.0k
    Py_ssize_t maplen;
8368
19.0k
    int mapkind;
8369
19.0k
    const void *mapdata;
8370
19.0k
    Py_UCS4 x;
8371
19.0k
    unsigned char ch;
8372
8373
19.0k
    maplen = PyUnicode_GET_LENGTH(mapping);
8374
19.0k
    mapdata = PyUnicode_DATA(mapping);
8375
19.0k
    mapkind = PyUnicode_KIND(mapping);
8376
8377
19.0k
    e = s + size;
8378
8379
19.0k
    if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) {
8380
        /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1
8381
         * is disabled in encoding aliases, latin1 is preferred because
8382
         * its implementation is faster. */
8383
130
        const Py_UCS1 *mapdata_ucs1 = (const Py_UCS1 *)mapdata;
8384
130
        Py_UCS1 *outdata = (Py_UCS1 *)writer->data;
8385
130
        Py_UCS4 maxchar = writer->maxchar;
8386
8387
130
        assert (writer->kind == PyUnicode_1BYTE_KIND);
8388
2.39k
        while (s < e) {
8389
2.26k
            ch = *s;
8390
2.26k
            x = mapdata_ucs1[ch];
8391
2.26k
            if (x > maxchar) {
8392
121
                if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1)
8393
0
                    goto onError;
8394
121
                maxchar = writer->maxchar;
8395
121
                outdata = (Py_UCS1 *)writer->data;
8396
121
            }
8397
2.26k
            outdata[writer->pos] = x;
8398
2.26k
            writer->pos++;
8399
2.26k
            ++s;
8400
2.26k
        }
8401
130
        return 0;
8402
130
    }
8403
8404
79.8k
    while (s < e) {
8405
69.1k
        if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) {
8406
69.1k
            int outkind = writer->kind;
8407
69.1k
            const Py_UCS2 *mapdata_ucs2 = (const Py_UCS2 *)mapdata;
8408
69.1k
            if (outkind == PyUnicode_1BYTE_KIND) {
8409
36.6k
                Py_UCS1 *outdata = (Py_UCS1 *)writer->data;
8410
36.6k
                Py_UCS4 maxchar = writer->maxchar;
8411
123k
                while (s < e) {
8412
121k
                    ch = *s;
8413
121k
                    x = mapdata_ucs2[ch];
8414
121k
                    if (x > maxchar)
8415
34.9k
                        goto Error;
8416
86.7k
                    outdata[writer->pos] = x;
8417
86.7k
                    writer->pos++;
8418
86.7k
                    ++s;
8419
86.7k
                }
8420
1.65k
                break;
8421
36.6k
            }
8422
32.5k
            else if (outkind == PyUnicode_2BYTE_KIND) {
8423
32.5k
                Py_UCS2 *outdata = (Py_UCS2 *)writer->data;
8424
684k
                while (s < e) {
8425
677k
                    ch = *s;
8426
677k
                    x = mapdata_ucs2[ch];
8427
677k
                    if (x == 0xFFFE)
8428
25.9k
                        goto Error;
8429
651k
                    outdata[writer->pos] = x;
8430
651k
                    writer->pos++;
8431
651k
                    ++s;
8432
651k
                }
8433
6.62k
                break;
8434
32.5k
            }
8435
69.1k
        }
8436
0
        ch = *s;
8437
8438
0
        if (ch < maplen)
8439
0
            x = PyUnicode_READ(mapkind, mapdata, ch);
8440
0
        else
8441
0
            x = 0xfffe; /* invalid value */
8442
60.9k
Error:
8443
60.9k
        if (x == 0xfffe)
8444
41.6k
        {
8445
            /* undefined mapping */
8446
41.6k
            startinpos = s-starts;
8447
41.6k
            endinpos = startinpos+1;
8448
41.6k
            if (unicode_decode_call_errorhandler_writer(
8449
41.6k
                    errors, &errorHandler,
8450
41.6k
                    "charmap", "character maps to <undefined>",
8451
41.6k
                    &starts, &e, &startinpos, &endinpos, &exc, &s,
8452
41.6k
                    writer)) {
8453
19
                goto onError;
8454
19
            }
8455
41.6k
            continue;
8456
41.6k
        }
8457
8458
19.2k
        if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0)
8459
0
            goto onError;
8460
19.2k
        ++s;
8461
19.2k
    }
8462
18.9k
    Py_XDECREF(errorHandler);
8463
18.9k
    Py_XDECREF(exc);
8464
18.9k
    return 0;
8465
8466
19
onError:
8467
19
    Py_XDECREF(errorHandler);
8468
19
    Py_XDECREF(exc);
8469
19
    return -1;
8470
18.9k
}
8471
8472
static int
8473
charmap_decode_mapping(const char *s,
8474
                       Py_ssize_t size,
8475
                       PyObject *mapping,
8476
                       const char *errors,
8477
                       _PyUnicodeWriter *writer)
8478
0
{
8479
0
    const char *starts = s;
8480
0
    const char *e;
8481
0
    Py_ssize_t startinpos, endinpos;
8482
0
    PyObject *errorHandler = NULL, *exc = NULL;
8483
0
    unsigned char ch;
8484
0
    PyObject *key, *item = NULL;
8485
8486
0
    e = s + size;
8487
8488
0
    while (s < e) {
8489
0
        ch = *s;
8490
8491
        /* Get mapping (char ordinal -> integer, Unicode char or None) */
8492
0
        key = PyLong_FromLong((long)ch);
8493
0
        if (key == NULL)
8494
0
            goto onError;
8495
8496
0
        int rc = PyMapping_GetOptionalItem(mapping, key, &item);
8497
0
        Py_DECREF(key);
8498
0
        if (rc == 0) {
8499
            /* No mapping found means: mapping is undefined. */
8500
0
            goto Undefined;
8501
0
        }
8502
0
        if (item == NULL) {
8503
0
            if (PyErr_ExceptionMatches(PyExc_LookupError)) {
8504
                /* No mapping found means: mapping is undefined. */
8505
0
                PyErr_Clear();
8506
0
                goto Undefined;
8507
0
            } else
8508
0
                goto onError;
8509
0
        }
8510
8511
        /* Apply mapping */
8512
0
        if (item == Py_None)
8513
0
            goto Undefined;
8514
0
        if (PyLong_Check(item)) {
8515
0
            long value = PyLong_AsLong(item);
8516
0
            if (value == 0xFFFE)
8517
0
                goto Undefined;
8518
0
            if (value < 0 || value > MAX_UNICODE) {
8519
0
                PyErr_Format(PyExc_TypeError,
8520
0
                             "character mapping must be in range(0x%x)",
8521
0
                             (unsigned long)MAX_UNICODE + 1);
8522
0
                goto onError;
8523
0
            }
8524
8525
0
            if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0)
8526
0
                goto onError;
8527
0
        }
8528
0
        else if (PyUnicode_Check(item)) {
8529
0
            if (PyUnicode_GET_LENGTH(item) == 1) {
8530
0
                Py_UCS4 value = PyUnicode_READ_CHAR(item, 0);
8531
0
                if (value == 0xFFFE)
8532
0
                    goto Undefined;
8533
0
                if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0)
8534
0
                    goto onError;
8535
0
            }
8536
0
            else {
8537
0
                writer->overallocate = 1;
8538
0
                if (_PyUnicodeWriter_WriteStr(writer, item) == -1)
8539
0
                    goto onError;
8540
0
            }
8541
0
        }
8542
0
        else {
8543
            /* wrong return value */
8544
0
            PyErr_SetString(PyExc_TypeError,
8545
0
                            "character mapping must return integer, None or str");
8546
0
            goto onError;
8547
0
        }
8548
0
        Py_CLEAR(item);
8549
0
        ++s;
8550
0
        continue;
8551
8552
0
Undefined:
8553
        /* undefined mapping */
8554
0
        Py_CLEAR(item);
8555
0
        startinpos = s-starts;
8556
0
        endinpos = startinpos+1;
8557
0
        if (unicode_decode_call_errorhandler_writer(
8558
0
                errors, &errorHandler,
8559
0
                "charmap", "character maps to <undefined>",
8560
0
                &starts, &e, &startinpos, &endinpos, &exc, &s,
8561
0
                writer)) {
8562
0
            goto onError;
8563
0
        }
8564
0
    }
8565
0
    Py_XDECREF(errorHandler);
8566
0
    Py_XDECREF(exc);
8567
0
    return 0;
8568
8569
0
onError:
8570
0
    Py_XDECREF(item);
8571
0
    Py_XDECREF(errorHandler);
8572
0
    Py_XDECREF(exc);
8573
0
    return -1;
8574
0
}
8575
8576
PyObject *
8577
PyUnicode_DecodeCharmap(const char *s,
8578
                        Py_ssize_t size,
8579
                        PyObject *mapping,
8580
                        const char *errors)
8581
19.0k
{
8582
19.0k
    _PyUnicodeWriter writer;
8583
8584
    /* Default to Latin-1 */
8585
19.0k
    if (mapping == NULL)
8586
0
        return PyUnicode_DecodeLatin1(s, size, errors);
8587
8588
19.0k
    if (size == 0)
8589
0
        _Py_RETURN_UNICODE_EMPTY();
8590
19.0k
    _PyUnicodeWriter_Init(&writer);
8591
19.0k
    writer.min_length = size;
8592
19.0k
    if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1)
8593
0
        goto onError;
8594
8595
19.0k
    if (PyUnicode_CheckExact(mapping)) {
8596
19.0k
        if (charmap_decode_string(s, size, mapping, errors, &writer) < 0)
8597
19
            goto onError;
8598
19.0k
    }
8599
0
    else {
8600
0
        if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0)
8601
0
            goto onError;
8602
0
    }
8603
19.0k
    return _PyUnicodeWriter_Finish(&writer);
8604
8605
19
  onError:
8606
19
    _PyUnicodeWriter_Dealloc(&writer);
8607
19
    return NULL;
8608
19.0k
}
8609
8610
/* Charmap encoding: the lookup table */
8611
8612
/*[clinic input]
8613
class EncodingMap "struct encoding_map *" "&EncodingMapType"
8614
[clinic start generated code]*/
8615
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=14e46bbb6c522d22]*/
8616
8617
struct encoding_map {
8618
    PyObject_HEAD
8619
    unsigned char level1[32];
8620
    int count2, count3;
8621
    unsigned char level23[1];
8622
};
8623
8624
/*[clinic input]
8625
EncodingMap.size
8626
8627
Return the size (in bytes) of this object.
8628
[clinic start generated code]*/
8629
8630
static PyObject *
8631
EncodingMap_size_impl(struct encoding_map *self)
8632
/*[clinic end generated code: output=c4c969e4c99342a4 input=004ff13f26bb5366]*/
8633
0
{
8634
0
    return PyLong_FromLong((sizeof(*self) - 1) + 16*self->count2 +
8635
0
                           128*self->count3);
8636
0
}
8637
8638
static PyMethodDef encoding_map_methods[] = {
8639
    ENCODINGMAP_SIZE_METHODDEF
8640
    {NULL, NULL}
8641
};
8642
8643
static PyTypeObject EncodingMapType = {
8644
    PyVarObject_HEAD_INIT(NULL, 0)
8645
    .tp_name = "EncodingMap",
8646
    .tp_basicsize = sizeof(struct encoding_map),
8647
    /* methods */
8648
    .tp_flags = Py_TPFLAGS_DEFAULT,
8649
    .tp_methods = encoding_map_methods,
8650
};
8651
8652
PyObject*
8653
PyUnicode_BuildEncodingMap(PyObject* string)
8654
118
{
8655
118
    PyObject *result;
8656
118
    struct encoding_map *mresult;
8657
118
    int i;
8658
118
    int need_dict = 0;
8659
118
    unsigned char level1[32];
8660
118
    unsigned char level2[512];
8661
118
    unsigned char *mlevel1, *mlevel2, *mlevel3;
8662
118
    int count2 = 0, count3 = 0;
8663
118
    int kind;
8664
118
    const void *data;
8665
118
    int length;
8666
118
    Py_UCS4 ch;
8667
8668
118
    if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) {
8669
0
        PyErr_BadArgument();
8670
0
        return NULL;
8671
0
    }
8672
118
    kind = PyUnicode_KIND(string);
8673
118
    data = PyUnicode_DATA(string);
8674
118
    length = (int)Py_MIN(PyUnicode_GET_LENGTH(string), 256);
8675
118
    memset(level1, 0xFF, sizeof level1);
8676
118
    memset(level2, 0xFF, sizeof level2);
8677
8678
    /* If there isn't a one-to-one mapping of NULL to \0,
8679
       or if there are non-BMP characters, we need to use
8680
       a mapping dictionary. */
8681
118
    if (PyUnicode_READ(kind, data, 0) != 0)
8682
0
        need_dict = 1;
8683
30.2k
    for (i = 1; i < length; i++) {
8684
30.0k
        int l1, l2;
8685
30.0k
        ch = PyUnicode_READ(kind, data, i);
8686
30.0k
        if (ch == 0 || ch > 0xFFFF) {
8687
0
            need_dict = 1;
8688
0
            break;
8689
0
        }
8690
30.0k
        if (ch == 0xFFFE)
8691
            /* unmapped character */
8692
768
            continue;
8693
29.3k
        l1 = ch >> 11;
8694
29.3k
        l2 = ch >> 7;
8695
29.3k
        if (level1[l1] == 0xFF)
8696
215
            level1[l1] = count2++;
8697
29.3k
        if (level2[l2] == 0xFF)
8698
650
            level2[l2] = count3++;
8699
29.3k
    }
8700
8701
118
    if (count2 >= 0xFF || count3 >= 0xFF)
8702
0
        need_dict = 1;
8703
8704
118
    if (need_dict) {
8705
0
        PyObject *result = PyDict_New();
8706
0
        if (!result)
8707
0
            return NULL;
8708
0
        for (i = 0; i < length; i++) {
8709
0
            Py_UCS4 c = PyUnicode_READ(kind, data, i);
8710
0
            PyObject *key = PyLong_FromLong(c);
8711
0
            if (key == NULL) {
8712
0
                Py_DECREF(result);
8713
0
                return NULL;
8714
0
            }
8715
0
            PyObject *value = PyLong_FromLong(i);
8716
0
            if (value == NULL) {
8717
0
                Py_DECREF(key);
8718
0
                Py_DECREF(result);
8719
0
                return NULL;
8720
0
            }
8721
0
            int rc = PyDict_SetItem(result, key, value);
8722
0
            Py_DECREF(key);
8723
0
            Py_DECREF(value);
8724
0
            if (rc < 0) {
8725
0
                Py_DECREF(result);
8726
0
                return NULL;
8727
0
            }
8728
0
        }
8729
0
        return result;
8730
0
    }
8731
8732
    /* Create a three-level trie */
8733
118
    result = PyObject_Malloc(sizeof(struct encoding_map) +
8734
118
                             16*count2 + 128*count3 - 1);
8735
118
    if (!result) {
8736
0
        return PyErr_NoMemory();
8737
0
    }
8738
8739
118
    _PyObject_Init(result, &EncodingMapType);
8740
118
    mresult = (struct encoding_map*)result;
8741
118
    mresult->count2 = count2;
8742
118
    mresult->count3 = count3;
8743
118
    mlevel1 = mresult->level1;
8744
118
    mlevel2 = mresult->level23;
8745
118
    mlevel3 = mresult->level23 + 16*count2;
8746
118
    memcpy(mlevel1, level1, 32);
8747
118
    memset(mlevel2, 0xFF, 16*count2);
8748
118
    memset(mlevel3, 0, 128*count3);
8749
118
    count3 = 0;
8750
30.2k
    for (i = 1; i < length; i++) {
8751
30.0k
        int o1, o2, o3, i2, i3;
8752
30.0k
        Py_UCS4 ch = PyUnicode_READ(kind, data, i);
8753
30.0k
        if (ch == 0xFFFE)
8754
            /* unmapped character */
8755
768
            continue;
8756
29.3k
        o1 = ch>>11;
8757
29.3k
        o2 = (ch>>7) & 0xF;
8758
29.3k
        i2 = 16*mlevel1[o1] + o2;
8759
29.3k
        if (mlevel2[i2] == 0xFF)
8760
650
            mlevel2[i2] = count3++;
8761
29.3k
        o3 = ch & 0x7F;
8762
29.3k
        i3 = 128*mlevel2[i2] + o3;
8763
29.3k
        mlevel3[i3] = i;
8764
29.3k
    }
8765
118
    return result;
8766
118
}
8767
8768
static int
8769
encoding_map_lookup(Py_UCS4 c, PyObject *mapping)
8770
0
{
8771
0
    struct encoding_map *map = (struct encoding_map*)mapping;
8772
0
    int l1 = c>>11;
8773
0
    int l2 = (c>>7) & 0xF;
8774
0
    int l3 = c & 0x7F;
8775
0
    int i;
8776
8777
0
    if (c > 0xFFFF)
8778
0
        return -1;
8779
0
    if (c == 0)
8780
0
        return 0;
8781
    /* level 1*/
8782
0
    i = map->level1[l1];
8783
0
    if (i == 0xFF) {
8784
0
        return -1;
8785
0
    }
8786
    /* level 2*/
8787
0
    i = map->level23[16*i+l2];
8788
0
    if (i == 0xFF) {
8789
0
        return -1;
8790
0
    }
8791
    /* level 3 */
8792
0
    i = map->level23[16*map->count2 + 128*i + l3];
8793
0
    if (i == 0) {
8794
0
        return -1;
8795
0
    }
8796
0
    return i;
8797
0
}
8798
8799
/* Lookup the character in the mapping.
8800
   On success, return PyLong, PyBytes or None (if the character can't be found).
8801
   If the result is PyLong, put its value in replace.
8802
   On error, return NULL.
8803
   */
8804
static PyObject *
8805
charmapencode_lookup(Py_UCS4 c, PyObject *mapping, unsigned char *replace)
8806
0
{
8807
0
    PyObject *w = PyLong_FromLong((long)c);
8808
0
    PyObject *x;
8809
8810
0
    if (w == NULL)
8811
0
        return NULL;
8812
0
    int rc = PyMapping_GetOptionalItem(mapping, w, &x);
8813
0
    Py_DECREF(w);
8814
0
    if (rc == 0) {
8815
        /* No mapping found means: mapping is undefined. */
8816
0
        Py_RETURN_NONE;
8817
0
    }
8818
0
    if (x == NULL) {
8819
0
        if (PyErr_ExceptionMatches(PyExc_LookupError)) {
8820
            /* No mapping found means: mapping is undefined. */
8821
0
            PyErr_Clear();
8822
0
            Py_RETURN_NONE;
8823
0
        } else
8824
0
            return NULL;
8825
0
    }
8826
0
    else if (x == Py_None)
8827
0
        return x;
8828
0
    else if (PyLong_Check(x)) {
8829
0
        long value = PyLong_AsLong(x);
8830
0
        if (value < 0 || value > 255) {
8831
0
            PyErr_SetString(PyExc_TypeError,
8832
0
                            "character mapping must be in range(256)");
8833
0
            Py_DECREF(x);
8834
0
            return NULL;
8835
0
        }
8836
0
        *replace = (unsigned char)value;
8837
0
        return x;
8838
0
    }
8839
0
    else if (PyBytes_Check(x))
8840
0
        return x;
8841
0
    else {
8842
        /* wrong return value */
8843
0
        PyErr_Format(PyExc_TypeError,
8844
0
                     "character mapping must return integer, bytes or None, not %.400s",
8845
0
                     Py_TYPE(x)->tp_name);
8846
0
        Py_DECREF(x);
8847
0
        return NULL;
8848
0
    }
8849
0
}
8850
8851
static int
8852
charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize)
8853
0
{
8854
0
    Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj);
8855
    /* exponentially overallocate to minimize reallocations */
8856
0
    if (requiredsize < 2*outsize)
8857
0
        requiredsize = 2*outsize;
8858
0
    if (_PyBytes_Resize(outobj, requiredsize))
8859
0
        return -1;
8860
0
    return 0;
8861
0
}
8862
8863
typedef enum charmapencode_result {
8864
    enc_SUCCESS, enc_FAILED, enc_EXCEPTION
8865
} charmapencode_result;
8866
/* lookup the character, put the result in the output string and adjust
8867
   various state variables. Resize the output bytes object if not enough
8868
   space is available. Return a new reference to the object that
8869
   was put in the output buffer, or Py_None, if the mapping was undefined
8870
   (in which case no character was written) or NULL, if a
8871
   reallocation error occurred. The caller must decref the result */
8872
static charmapencode_result
8873
charmapencode_output(Py_UCS4 c, PyObject *mapping,
8874
                     PyObject **outobj, Py_ssize_t *outpos)
8875
0
{
8876
0
    PyObject *rep;
8877
0
    unsigned char replace;
8878
0
    char *outstart;
8879
0
    Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj);
8880
8881
0
    if (Py_IS_TYPE(mapping, &EncodingMapType)) {
8882
0
        int res = encoding_map_lookup(c, mapping);
8883
0
        Py_ssize_t requiredsize = *outpos+1;
8884
0
        if (res == -1)
8885
0
            return enc_FAILED;
8886
0
        if (outsize<requiredsize)
8887
0
            if (charmapencode_resize(outobj, outpos, requiredsize))
8888
0
                return enc_EXCEPTION;
8889
0
        outstart = PyBytes_AS_STRING(*outobj);
8890
0
        outstart[(*outpos)++] = (char)res;
8891
0
        return enc_SUCCESS;
8892
0
    }
8893
8894
0
    rep = charmapencode_lookup(c, mapping, &replace);
8895
0
    if (rep==NULL)
8896
0
        return enc_EXCEPTION;
8897
0
    else if (rep==Py_None) {
8898
0
        Py_DECREF(rep);
8899
0
        return enc_FAILED;
8900
0
    } else {
8901
0
        if (PyLong_Check(rep)) {
8902
0
            Py_ssize_t requiredsize = *outpos+1;
8903
0
            if (outsize<requiredsize)
8904
0
                if (charmapencode_resize(outobj, outpos, requiredsize)) {
8905
0
                    Py_DECREF(rep);
8906
0
                    return enc_EXCEPTION;
8907
0
                }
8908
0
            outstart = PyBytes_AS_STRING(*outobj);
8909
0
            outstart[(*outpos)++] = (char)replace;
8910
0
        }
8911
0
        else {
8912
0
            const char *repchars = PyBytes_AS_STRING(rep);
8913
0
            Py_ssize_t repsize = PyBytes_GET_SIZE(rep);
8914
0
            Py_ssize_t requiredsize = *outpos+repsize;
8915
0
            if (outsize<requiredsize)
8916
0
                if (charmapencode_resize(outobj, outpos, requiredsize)) {
8917
0
                    Py_DECREF(rep);
8918
0
                    return enc_EXCEPTION;
8919
0
                }
8920
0
            outstart = PyBytes_AS_STRING(*outobj);
8921
0
            memcpy(outstart + *outpos, repchars, repsize);
8922
0
            *outpos += repsize;
8923
0
        }
8924
0
    }
8925
0
    Py_DECREF(rep);
8926
0
    return enc_SUCCESS;
8927
0
}
8928
8929
/* handle an error in PyUnicode_EncodeCharmap
8930
   Return 0 on success, -1 on error */
8931
static int
8932
charmap_encoding_error(
8933
    PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping,
8934
    PyObject **exceptionObject,
8935
    _Py_error_handler *error_handler, PyObject **error_handler_obj, const char *errors,
8936
    PyObject **res, Py_ssize_t *respos)
8937
0
{
8938
0
    PyObject *repunicode = NULL; /* initialize to prevent gcc warning */
8939
0
    Py_ssize_t size, repsize;
8940
0
    Py_ssize_t newpos;
8941
0
    int kind;
8942
0
    const void *data;
8943
0
    Py_ssize_t index;
8944
    /* startpos for collecting unencodable chars */
8945
0
    Py_ssize_t collstartpos = *inpos;
8946
0
    Py_ssize_t collendpos = *inpos+1;
8947
0
    Py_ssize_t collpos;
8948
0
    const char *encoding = "charmap";
8949
0
    const char *reason = "character maps to <undefined>";
8950
0
    charmapencode_result x;
8951
0
    Py_UCS4 ch;
8952
0
    int val;
8953
8954
0
    size = PyUnicode_GET_LENGTH(unicode);
8955
    /* find all unencodable characters */
8956
0
    while (collendpos < size) {
8957
0
        PyObject *rep;
8958
0
        unsigned char replace;
8959
0
        if (Py_IS_TYPE(mapping, &EncodingMapType)) {
8960
0
            ch = PyUnicode_READ_CHAR(unicode, collendpos);
8961
0
            val = encoding_map_lookup(ch, mapping);
8962
0
            if (val != -1)
8963
0
                break;
8964
0
            ++collendpos;
8965
0
            continue;
8966
0
        }
8967
8968
0
        ch = PyUnicode_READ_CHAR(unicode, collendpos);
8969
0
        rep = charmapencode_lookup(ch, mapping, &replace);
8970
0
        if (rep==NULL)
8971
0
            return -1;
8972
0
        else if (rep!=Py_None) {
8973
0
            Py_DECREF(rep);
8974
0
            break;
8975
0
        }
8976
0
        Py_DECREF(rep);
8977
0
        ++collendpos;
8978
0
    }
8979
    /* cache callback name lookup
8980
     * (if not done yet, i.e. it's the first error) */
8981
0
    if (*error_handler == _Py_ERROR_UNKNOWN)
8982
0
        *error_handler = _Py_GetErrorHandler(errors);
8983
8984
0
    switch (*error_handler) {
8985
0
    case _Py_ERROR_STRICT:
8986
0
        raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason);
8987
0
        return -1;
8988
8989
0
    case _Py_ERROR_REPLACE:
8990
0
        for (collpos = collstartpos; collpos<collendpos; ++collpos) {
8991
0
            x = charmapencode_output('?', mapping, res, respos);
8992
0
            if (x==enc_EXCEPTION) {
8993
0
                return -1;
8994
0
            }
8995
0
            else if (x==enc_FAILED) {
8996
0
                raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason);
8997
0
                return -1;
8998
0
            }
8999
0
        }
9000
0
        _Py_FALLTHROUGH;
9001
0
    case _Py_ERROR_IGNORE:
9002
0
        *inpos = collendpos;
9003
0
        break;
9004
9005
0
    case _Py_ERROR_XMLCHARREFREPLACE:
9006
        /* generate replacement (temporarily (mis)uses p) */
9007
0
        for (collpos = collstartpos; collpos < collendpos; ++collpos) {
9008
0
            char buffer[2+29+1+1];
9009
0
            char *cp;
9010
0
            sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos));
9011
0
            for (cp = buffer; *cp; ++cp) {
9012
0
                x = charmapencode_output(*cp, mapping, res, respos);
9013
0
                if (x==enc_EXCEPTION)
9014
0
                    return -1;
9015
0
                else if (x==enc_FAILED) {
9016
0
                    raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason);
9017
0
                    return -1;
9018
0
                }
9019
0
            }
9020
0
        }
9021
0
        *inpos = collendpos;
9022
0
        break;
9023
9024
0
    default:
9025
0
        repunicode = unicode_encode_call_errorhandler(errors, error_handler_obj,
9026
0
                                                      encoding, reason, unicode, exceptionObject,
9027
0
                                                      collstartpos, collendpos, &newpos);
9028
0
        if (repunicode == NULL)
9029
0
            return -1;
9030
0
        if (PyBytes_Check(repunicode)) {
9031
            /* Directly copy bytes result to output. */
9032
0
            Py_ssize_t outsize = PyBytes_Size(*res);
9033
0
            Py_ssize_t requiredsize;
9034
0
            repsize = PyBytes_Size(repunicode);
9035
0
            requiredsize = *respos + repsize;
9036
0
            if (requiredsize > outsize)
9037
                /* Make room for all additional bytes. */
9038
0
                if (charmapencode_resize(res, respos, requiredsize)) {
9039
0
                    Py_DECREF(repunicode);
9040
0
                    return -1;
9041
0
                }
9042
0
            memcpy(PyBytes_AsString(*res) + *respos,
9043
0
                   PyBytes_AsString(repunicode),  repsize);
9044
0
            *respos += repsize;
9045
0
            *inpos = newpos;
9046
0
            Py_DECREF(repunicode);
9047
0
            break;
9048
0
        }
9049
        /* generate replacement  */
9050
0
        repsize = PyUnicode_GET_LENGTH(repunicode);
9051
0
        data = PyUnicode_DATA(repunicode);
9052
0
        kind = PyUnicode_KIND(repunicode);
9053
0
        for (index = 0; index < repsize; index++) {
9054
0
            Py_UCS4 repch = PyUnicode_READ(kind, data, index);
9055
0
            x = charmapencode_output(repch, mapping, res, respos);
9056
0
            if (x==enc_EXCEPTION) {
9057
0
                Py_DECREF(repunicode);
9058
0
                return -1;
9059
0
            }
9060
0
            else if (x==enc_FAILED) {
9061
0
                Py_DECREF(repunicode);
9062
0
                raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason);
9063
0
                return -1;
9064
0
            }
9065
0
        }
9066
0
        *inpos = newpos;
9067
0
        Py_DECREF(repunicode);
9068
0
    }
9069
0
    return 0;
9070
0
}
9071
9072
PyObject *
9073
_PyUnicode_EncodeCharmap(PyObject *unicode,
9074
                         PyObject *mapping,
9075
                         const char *errors)
9076
0
{
9077
    /* output object */
9078
0
    PyObject *res = NULL;
9079
    /* current input position */
9080
0
    Py_ssize_t inpos = 0;
9081
0
    Py_ssize_t size;
9082
    /* current output position */
9083
0
    Py_ssize_t respos = 0;
9084
0
    PyObject *error_handler_obj = NULL;
9085
0
    PyObject *exc = NULL;
9086
0
    _Py_error_handler error_handler = _Py_ERROR_UNKNOWN;
9087
0
    const void *data;
9088
0
    int kind;
9089
9090
0
    size = PyUnicode_GET_LENGTH(unicode);
9091
0
    data = PyUnicode_DATA(unicode);
9092
0
    kind = PyUnicode_KIND(unicode);
9093
9094
    /* Default to Latin-1 */
9095
0
    if (mapping == NULL)
9096
0
        return unicode_encode_ucs1(unicode, errors, 256);
9097
9098
    /* allocate enough for a simple encoding without
9099
       replacements, if we need more, we'll resize */
9100
0
    res = PyBytes_FromStringAndSize(NULL, size);
9101
0
    if (res == NULL)
9102
0
        goto onError;
9103
0
    if (size == 0)
9104
0
        return res;
9105
9106
0
    while (inpos<size) {
9107
0
        Py_UCS4 ch = PyUnicode_READ(kind, data, inpos);
9108
        /* try to encode it */
9109
0
        charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos);
9110
0
        if (x==enc_EXCEPTION) /* error */
9111
0
            goto onError;
9112
0
        if (x==enc_FAILED) { /* unencodable character */
9113
0
            if (charmap_encoding_error(unicode, &inpos, mapping,
9114
0
                                       &exc,
9115
0
                                       &error_handler, &error_handler_obj, errors,
9116
0
                                       &res, &respos)) {
9117
0
                goto onError;
9118
0
            }
9119
0
        }
9120
0
        else
9121
            /* done with this character => adjust input position */
9122
0
            ++inpos;
9123
0
    }
9124
9125
    /* Resize if we allocated to much */
9126
0
    if (respos<PyBytes_GET_SIZE(res))
9127
0
        if (_PyBytes_Resize(&res, respos) < 0)
9128
0
            goto onError;
9129
9130
0
    Py_XDECREF(exc);
9131
0
    Py_XDECREF(error_handler_obj);
9132
0
    return res;
9133
9134
0
  onError:
9135
0
    Py_XDECREF(res);
9136
0
    Py_XDECREF(exc);
9137
0
    Py_XDECREF(error_handler_obj);
9138
0
    return NULL;
9139
0
}
9140
9141
PyObject *
9142
PyUnicode_AsCharmapString(PyObject *unicode,
9143
                          PyObject *mapping)
9144
0
{
9145
0
    if (!PyUnicode_Check(unicode) || mapping == NULL) {
9146
0
        PyErr_BadArgument();
9147
0
        return NULL;
9148
0
    }
9149
0
    return _PyUnicode_EncodeCharmap(unicode, mapping, NULL);
9150
0
}
9151
9152
/* create or adjust a UnicodeTranslateError */
9153
static void
9154
make_translate_exception(PyObject **exceptionObject,
9155
                         PyObject *unicode,
9156
                         Py_ssize_t startpos, Py_ssize_t endpos,
9157
                         const char *reason)
9158
0
{
9159
0
    if (*exceptionObject == NULL) {
9160
0
        *exceptionObject = _PyUnicodeTranslateError_Create(
9161
0
            unicode, startpos, endpos, reason);
9162
0
    }
9163
0
    else {
9164
0
        if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos))
9165
0
            goto onError;
9166
0
        if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos))
9167
0
            goto onError;
9168
0
        if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason))
9169
0
            goto onError;
9170
0
        return;
9171
0
      onError:
9172
0
        Py_CLEAR(*exceptionObject);
9173
0
    }
9174
0
}
9175
9176
/* error handling callback helper:
9177
   build arguments, call the callback and check the arguments,
9178
   put the result into newpos and return the replacement string, which
9179
   has to be freed by the caller */
9180
static PyObject *
9181
unicode_translate_call_errorhandler(const char *errors,
9182
                                    PyObject **errorHandler,
9183
                                    const char *reason,
9184
                                    PyObject *unicode, PyObject **exceptionObject,
9185
                                    Py_ssize_t startpos, Py_ssize_t endpos,
9186
                                    Py_ssize_t *newpos)
9187
0
{
9188
0
    static const char *argparse = "Un;translating error handler must return (str, int) tuple";
9189
9190
0
    Py_ssize_t i_newpos;
9191
0
    PyObject *restuple;
9192
0
    PyObject *resunicode;
9193
9194
0
    if (*errorHandler == NULL) {
9195
0
        *errorHandler = PyCodec_LookupError(errors);
9196
0
        if (*errorHandler == NULL)
9197
0
            return NULL;
9198
0
    }
9199
9200
0
    make_translate_exception(exceptionObject,
9201
0
                             unicode, startpos, endpos, reason);
9202
0
    if (*exceptionObject == NULL)
9203
0
        return NULL;
9204
9205
0
    restuple = PyObject_CallOneArg(*errorHandler, *exceptionObject);
9206
0
    if (restuple == NULL)
9207
0
        return NULL;
9208
0
    if (!PyTuple_Check(restuple)) {
9209
0
        PyErr_SetString(PyExc_TypeError, &argparse[3]);
9210
0
        Py_DECREF(restuple);
9211
0
        return NULL;
9212
0
    }
9213
0
    if (!PyArg_ParseTuple(restuple, argparse,
9214
0
                          &resunicode, &i_newpos)) {
9215
0
        Py_DECREF(restuple);
9216
0
        return NULL;
9217
0
    }
9218
0
    if (i_newpos<0)
9219
0
        *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos;
9220
0
    else
9221
0
        *newpos = i_newpos;
9222
0
    if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) {
9223
0
        PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);
9224
0
        Py_DECREF(restuple);
9225
0
        return NULL;
9226
0
    }
9227
0
    Py_INCREF(resunicode);
9228
0
    Py_DECREF(restuple);
9229
0
    return resunicode;
9230
0
}
9231
9232
/* Lookup the character ch in the mapping and put the result in result,
9233
   which must be decrefed by the caller.
9234
   The result can be PyLong, PyUnicode, None or NULL.
9235
   If the result is PyLong, put its value in replace.
9236
   Return 0 on success, -1 on error */
9237
static int
9238
charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result, Py_UCS4 *replace)
9239
338
{
9240
338
    PyObject *w = PyLong_FromLong((long)c);
9241
338
    PyObject *x;
9242
9243
338
    if (w == NULL)
9244
0
        return -1;
9245
338
    int rc = PyMapping_GetOptionalItem(mapping, w, &x);
9246
338
    Py_DECREF(w);
9247
338
    if (rc == 0) {
9248
        /* No mapping found means: use 1:1 mapping. */
9249
158
        *result = NULL;
9250
158
        return 0;
9251
158
    }
9252
180
    if (x == NULL) {
9253
0
        if (PyErr_ExceptionMatches(PyExc_LookupError)) {
9254
            /* No mapping found means: use 1:1 mapping. */
9255
0
            PyErr_Clear();
9256
0
            *result = NULL;
9257
0
            return 0;
9258
0
        } else
9259
0
            return -1;
9260
0
    }
9261
180
    else if (x == Py_None) {
9262
0
        *result = x;
9263
0
        return 0;
9264
0
    }
9265
180
    else if (PyLong_Check(x)) {
9266
0
        long value = PyLong_AsLong(x);
9267
0
        if (value < 0 || value > MAX_UNICODE) {
9268
0
            PyErr_Format(PyExc_ValueError,
9269
0
                         "character mapping must be in range(0x%x)",
9270
0
                         MAX_UNICODE+1);
9271
0
            Py_DECREF(x);
9272
0
            return -1;
9273
0
        }
9274
0
        *result = x;
9275
0
        *replace = (Py_UCS4)value;
9276
0
        return 0;
9277
0
    }
9278
180
    else if (PyUnicode_Check(x)) {
9279
180
        *result = x;
9280
180
        return 0;
9281
180
    }
9282
0
    else {
9283
        /* wrong return value */
9284
0
        PyErr_SetString(PyExc_TypeError,
9285
0
                        "character mapping must return integer, None or str");
9286
0
        Py_DECREF(x);
9287
0
        return -1;
9288
0
    }
9289
180
}
9290
9291
/* lookup the character, write the result into the writer.
9292
   Return 1 if the result was written into the writer, return 0 if the mapping
9293
   was undefined, raise an exception return -1 on error. */
9294
static int
9295
charmaptranslate_output(Py_UCS4 ch, PyObject *mapping,
9296
                        _PyUnicodeWriter *writer)
9297
208
{
9298
208
    PyObject *item;
9299
208
    Py_UCS4 replace;
9300
9301
208
    if (charmaptranslate_lookup(ch, mapping, &item, &replace))
9302
0
        return -1;
9303
9304
208
    if (item == NULL) {
9305
        /* not found => default to 1:1 mapping */
9306
84
        if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) {
9307
0
            return -1;
9308
0
        }
9309
84
        return 1;
9310
84
    }
9311
9312
124
    if (item == Py_None) {
9313
0
        Py_DECREF(item);
9314
0
        return 0;
9315
0
    }
9316
9317
124
    if (PyLong_Check(item)) {
9318
0
        if (_PyUnicodeWriter_WriteCharInline(writer, replace) < 0) {
9319
0
            Py_DECREF(item);
9320
0
            return -1;
9321
0
        }
9322
0
        Py_DECREF(item);
9323
0
        return 1;
9324
0
    }
9325
9326
124
    if (!PyUnicode_Check(item)) {
9327
0
        Py_DECREF(item);
9328
0
        return -1;
9329
0
    }
9330
9331
124
    if (_PyUnicodeWriter_WriteStr(writer, item) < 0) {
9332
0
        Py_DECREF(item);
9333
0
        return -1;
9334
0
    }
9335
9336
124
    Py_DECREF(item);
9337
124
    return 1;
9338
124
}
9339
9340
static int
9341
unicode_fast_translate_lookup(PyObject *mapping, Py_UCS1 ch,
9342
                              Py_UCS1 *translate)
9343
130
{
9344
130
    PyObject *item = NULL;
9345
130
    Py_UCS4 replace;
9346
130
    int ret = 0;
9347
9348
130
    if (charmaptranslate_lookup(ch, mapping, &item, &replace)) {
9349
0
        return -1;
9350
0
    }
9351
9352
130
    if (item == Py_None) {
9353
        /* deletion */
9354
0
        translate[ch] = 0xfe;
9355
0
    }
9356
130
    else if (item == NULL) {
9357
        /* not found => default to 1:1 mapping */
9358
74
        translate[ch] = ch;
9359
74
        return 1;
9360
74
    }
9361
56
    else if (PyLong_Check(item)) {
9362
0
        if (replace > 127) {
9363
            /* invalid character or character outside ASCII:
9364
               skip the fast translate */
9365
0
            goto exit;
9366
0
        }
9367
0
        translate[ch] = (Py_UCS1)replace;
9368
0
    }
9369
56
    else if (PyUnicode_Check(item)) {
9370
56
        if (PyUnicode_GET_LENGTH(item) != 1)
9371
56
            goto exit;
9372
9373
0
        replace = PyUnicode_READ_CHAR(item, 0);
9374
0
        if (replace > 127)
9375
0
            goto exit;
9376
0
        translate[ch] = (Py_UCS1)replace;
9377
0
    }
9378
0
    else {
9379
        /* not None, NULL, long or unicode */
9380
0
        goto exit;
9381
0
    }
9382
0
    ret = 1;
9383
9384
56
  exit:
9385
56
    Py_DECREF(item);
9386
56
    return ret;
9387
0
}
9388
9389
/* Fast path for ascii => ascii translation. Return 1 if the whole string
9390
   was translated into writer, return 0 if the input string was partially
9391
   translated into writer, raise an exception and return -1 on error. */
9392
static int
9393
unicode_fast_translate(PyObject *input, PyObject *mapping,
9394
                       _PyUnicodeWriter *writer, int ignore,
9395
                       Py_ssize_t *input_pos)
9396
104
{
9397
104
    Py_UCS1 ascii_table[128], ch, ch2;
9398
104
    Py_ssize_t len;
9399
104
    const Py_UCS1 *in, *end;
9400
104
    Py_UCS1 *out;
9401
104
    int res = 0;
9402
9403
104
    len = PyUnicode_GET_LENGTH(input);
9404
9405
104
    memset(ascii_table, 0xff, 128);
9406
9407
104
    in = PyUnicode_1BYTE_DATA(input);
9408
104
    end = in + len;
9409
9410
104
    assert(PyUnicode_IS_ASCII(writer->buffer));
9411
104
    assert(PyUnicode_GET_LENGTH(writer->buffer) == len);
9412
104
    out = PyUnicode_1BYTE_DATA(writer->buffer);
9413
9414
192
    for (; in < end; in++) {
9415
144
        ch = *in;
9416
144
        ch2 = ascii_table[ch];
9417
144
        if (ch2 == 0xff) {
9418
130
            int translate = unicode_fast_translate_lookup(mapping, ch,
9419
130
                                                          ascii_table);
9420
130
            if (translate < 0)
9421
0
                return -1;
9422
130
            if (translate == 0)
9423
56
                goto exit;
9424
74
            ch2 = ascii_table[ch];
9425
74
        }
9426
88
        if (ch2 == 0xfe) {
9427
0
            if (ignore)
9428
0
                continue;
9429
0
            goto exit;
9430
0
        }
9431
88
        assert(ch2 < 128);
9432
88
        *out = ch2;
9433
88
        out++;
9434
88
    }
9435
48
    res = 1;
9436
9437
104
exit:
9438
104
    writer->pos = out - PyUnicode_1BYTE_DATA(writer->buffer);
9439
104
    *input_pos = in - PyUnicode_1BYTE_DATA(input);
9440
104
    return res;
9441
48
}
9442
9443
static PyObject *
9444
_PyUnicode_TranslateCharmap(PyObject *input,
9445
                            PyObject *mapping,
9446
                            const char *errors)
9447
104
{
9448
    /* input object */
9449
104
    const void *data;
9450
104
    Py_ssize_t size, i;
9451
104
    int kind;
9452
    /* output buffer */
9453
104
    _PyUnicodeWriter writer;
9454
    /* error handler */
9455
104
    const char *reason = "character maps to <undefined>";
9456
104
    PyObject *errorHandler = NULL;
9457
104
    PyObject *exc = NULL;
9458
104
    int ignore;
9459
104
    int res;
9460
9461
104
    if (mapping == NULL) {
9462
0
        PyErr_BadArgument();
9463
0
        return NULL;
9464
0
    }
9465
9466
104
    data = PyUnicode_DATA(input);
9467
104
    kind = PyUnicode_KIND(input);
9468
104
    size = PyUnicode_GET_LENGTH(input);
9469
9470
104
    if (size == 0)
9471
0
        return PyUnicode_FromObject(input);
9472
9473
    /* allocate enough for a simple 1:1 translation without
9474
       replacements, if we need more, we'll resize */
9475
104
    _PyUnicodeWriter_Init(&writer);
9476
104
    if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1)
9477
0
        goto onError;
9478
9479
104
    ignore = (errors != NULL && strcmp(errors, "ignore") == 0);
9480
9481
104
    if (PyUnicode_IS_ASCII(input)) {
9482
104
        res = unicode_fast_translate(input, mapping, &writer, ignore, &i);
9483
104
        if (res < 0) {
9484
0
            _PyUnicodeWriter_Dealloc(&writer);
9485
0
            return NULL;
9486
0
        }
9487
104
        if (res == 1)
9488
48
            return _PyUnicodeWriter_Finish(&writer);
9489
104
    }
9490
0
    else {
9491
0
        i = 0;
9492
0
    }
9493
9494
264
    while (i<size) {
9495
        /* try to encode it */
9496
208
        int translate;
9497
208
        PyObject *repunicode = NULL; /* initialize to prevent gcc warning */
9498
208
        Py_ssize_t newpos;
9499
        /* startpos for collecting untranslatable chars */
9500
208
        Py_ssize_t collstart;
9501
208
        Py_ssize_t collend;
9502
208
        Py_UCS4 ch;
9503
9504
208
        ch = PyUnicode_READ(kind, data, i);
9505
208
        translate = charmaptranslate_output(ch, mapping, &writer);
9506
208
        if (translate < 0)
9507
0
            goto onError;
9508
9509
208
        if (translate != 0) {
9510
            /* it worked => adjust input pointer */
9511
208
            ++i;
9512
208
            continue;
9513
208
        }
9514
9515
        /* untranslatable character */
9516
0
        collstart = i;
9517
0
        collend = i+1;
9518
9519
        /* find all untranslatable characters */
9520
0
        while (collend < size) {
9521
0
            PyObject *x;
9522
0
            Py_UCS4 replace;
9523
0
            ch = PyUnicode_READ(kind, data, collend);
9524
0
            if (charmaptranslate_lookup(ch, mapping, &x, &replace))
9525
0
                goto onError;
9526
0
            Py_XDECREF(x);
9527
0
            if (x != Py_None)
9528
0
                break;
9529
0
            ++collend;
9530
0
        }
9531
9532
0
        if (ignore) {
9533
0
            i = collend;
9534
0
        }
9535
0
        else {
9536
0
            repunicode = unicode_translate_call_errorhandler(errors, &errorHandler,
9537
0
                                                             reason, input, &exc,
9538
0
                                                             collstart, collend, &newpos);
9539
0
            if (repunicode == NULL)
9540
0
                goto onError;
9541
0
            if (_PyUnicodeWriter_WriteStr(&writer, repunicode) < 0) {
9542
0
                Py_DECREF(repunicode);
9543
0
                goto onError;
9544
0
            }
9545
0
            Py_DECREF(repunicode);
9546
0
            i = newpos;
9547
0
        }
9548
0
    }
9549
56
    Py_XDECREF(exc);
9550
56
    Py_XDECREF(errorHandler);
9551
56
    return _PyUnicodeWriter_Finish(&writer);
9552
9553
0
  onError:
9554
0
    _PyUnicodeWriter_Dealloc(&writer);
9555
0
    Py_XDECREF(exc);
9556
0
    Py_XDECREF(errorHandler);
9557
0
    return NULL;
9558
56
}
9559
9560
PyObject *
9561
PyUnicode_Translate(PyObject *str,
9562
                    PyObject *mapping,
9563
                    const char *errors)
9564
0
{
9565
0
    if (ensure_unicode(str) < 0)
9566
0
        return NULL;
9567
0
    return _PyUnicode_TranslateCharmap(str, mapping, errors);
9568
0
}
9569
9570
PyObject *
9571
_PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode)
9572
3.72M
{
9573
3.72M
    if (!PyUnicode_Check(unicode)) {
9574
0
        PyErr_BadInternalCall();
9575
0
        return NULL;
9576
0
    }
9577
3.72M
    if (PyUnicode_IS_ASCII(unicode)) {
9578
        /* If the string is already ASCII, just return the same string */
9579
3.72M
        return Py_NewRef(unicode);
9580
3.72M
    }
9581
9582
2.36k
    Py_ssize_t len = PyUnicode_GET_LENGTH(unicode);
9583
2.36k
    PyObject *result = PyUnicode_New(len, 127);
9584
2.36k
    if (result == NULL) {
9585
0
        return NULL;
9586
0
    }
9587
9588
2.36k
    Py_UCS1 *out = PyUnicode_1BYTE_DATA(result);
9589
2.36k
    int kind = PyUnicode_KIND(unicode);
9590
2.36k
    const void *data = PyUnicode_DATA(unicode);
9591
2.36k
    Py_ssize_t i;
9592
40.2k
    for (i = 0; i < len; ++i) {
9593
38.0k
        Py_UCS4 ch = PyUnicode_READ(kind, data, i);
9594
38.0k
        if (ch < 127) {
9595
35.2k
            out[i] = ch;
9596
35.2k
        }
9597
2.78k
        else if (Py_UNICODE_ISSPACE(ch)) {
9598
1.07k
            out[i] = ' ';
9599
1.07k
        }
9600
1.71k
        else {
9601
1.71k
            int decimal = Py_UNICODE_TODECIMAL(ch);
9602
1.71k
            if (decimal < 0) {
9603
148
                out[i] = '?';
9604
148
                out[i+1] = '\0';
9605
148
                _PyUnicode_LENGTH(result) = i + 1;
9606
148
                break;
9607
148
            }
9608
1.56k
            out[i] = '0' + decimal;
9609
1.56k
        }
9610
38.0k
    }
9611
9612
2.36k
    assert(_PyUnicode_CheckConsistency(result, 1));
9613
2.36k
    return result;
9614
2.36k
}
9615
9616
/* --- Helpers ------------------------------------------------------------ */
9617
9618
/* helper macro to fixup start/end slice values */
9619
#define ADJUST_INDICES(start, end, len) \
9620
146M
    do {                                \
9621
146M
        if (end > len) {                \
9622
133M
            end = len;                  \
9623
133M
        }                               \
9624
146M
        else if (end < 0) {             \
9625
0
            end += len;                 \
9626
0
            if (end < 0) {              \
9627
0
                end = 0;                \
9628
0
            }                           \
9629
0
        }                               \
9630
146M
        if (start < 0) {                \
9631
0
            start += len;               \
9632
0
            if (start < 0) {            \
9633
0
                start = 0;              \
9634
0
            }                           \
9635
0
        }                               \
9636
146M
    } while (0)
9637
9638
static Py_ssize_t
9639
any_find_slice(PyObject* s1, PyObject* s2,
9640
               Py_ssize_t start,
9641
               Py_ssize_t end,
9642
               int direction)
9643
17.7M
{
9644
17.7M
    int kind1, kind2;
9645
17.7M
    const void *buf1, *buf2;
9646
17.7M
    Py_ssize_t len1, len2, result;
9647
9648
17.7M
    kind1 = PyUnicode_KIND(s1);
9649
17.7M
    kind2 = PyUnicode_KIND(s2);
9650
17.7M
    if (kind1 < kind2)
9651
0
        return -1;
9652
9653
17.7M
    len1 = PyUnicode_GET_LENGTH(s1);
9654
17.7M
    len2 = PyUnicode_GET_LENGTH(s2);
9655
17.7M
    ADJUST_INDICES(start, end, len1);
9656
17.7M
    if (end - start < len2)
9657
23.5k
        return -1;
9658
9659
17.7M
    buf1 = PyUnicode_DATA(s1);
9660
17.7M
    buf2 = PyUnicode_DATA(s2);
9661
17.7M
    if (len2 == 1) {
9662
17.7M
        Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0);
9663
17.7M
        result = findchar((const char *)buf1 + kind1*start,
9664
17.7M
                          kind1, end - start, ch, direction);
9665
17.7M
        if (result == -1)
9666
217k
            return -1;
9667
17.5M
        else
9668
17.5M
            return start + result;
9669
17.7M
    }
9670
9671
15.8k
    if (kind2 != kind1) {
9672
8.93k
        buf2 = unicode_askind(kind2, buf2, len2, kind1);
9673
8.93k
        if (!buf2)
9674
0
            return -2;
9675
8.93k
    }
9676
9677
15.8k
    if (direction > 0) {
9678
15.8k
        switch (kind1) {
9679
6.88k
        case PyUnicode_1BYTE_KIND:
9680
6.88k
            if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2))
9681
4.58k
                result = asciilib_find_slice(buf1, len1, buf2, len2, start, end);
9682
2.29k
            else
9683
2.29k
                result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end);
9684
6.88k
            break;
9685
3.40k
        case PyUnicode_2BYTE_KIND:
9686
3.40k
            result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end);
9687
3.40k
            break;
9688
5.53k
        case PyUnicode_4BYTE_KIND:
9689
5.53k
            result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end);
9690
5.53k
            break;
9691
0
        default:
9692
0
            Py_UNREACHABLE();
9693
15.8k
        }
9694
15.8k
    }
9695
0
    else {
9696
0
        switch (kind1) {
9697
0
        case PyUnicode_1BYTE_KIND:
9698
0
            if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2))
9699
0
                result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end);
9700
0
            else
9701
0
                result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end);
9702
0
            break;
9703
0
        case PyUnicode_2BYTE_KIND:
9704
0
            result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end);
9705
0
            break;
9706
0
        case PyUnicode_4BYTE_KIND:
9707
0
            result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end);
9708
0
            break;
9709
0
        default:
9710
0
            Py_UNREACHABLE();
9711
0
        }
9712
0
    }
9713
9714
15.8k
    assert((kind2 != kind1) == (buf2 != PyUnicode_DATA(s2)));
9715
15.8k
    if (kind2 != kind1)
9716
8.93k
        PyMem_Free((void *)buf2);
9717
9718
15.8k
    return result;
9719
15.8k
}
9720
9721
/* _PyUnicode_InsertThousandsGrouping() helper functions */
9722
#include "stringlib/localeutil.h"
9723
9724
/**
9725
 * InsertThousandsGrouping:
9726
 * @writer: Unicode writer.
9727
 * @n_buffer: Number of characters in @buffer.
9728
 * @digits: Digits we're reading from. If count is non-NULL, this is unused.
9729
 * @d_pos: Start of digits string.
9730
 * @n_digits: The number of digits in the string, in which we want
9731
 *            to put the grouping chars.
9732
 * @min_width: The minimum width of the digits in the output string.
9733
 *             Output will be zero-padded on the left to fill.
9734
 * @grouping: see definition in localeconv().
9735
 * @thousands_sep: see definition in localeconv().
9736
 *
9737
 * There are 2 modes: counting and filling. If @writer is NULL,
9738
 *  we are in counting mode, else filling mode.
9739
 * If counting, the required buffer size is returned.
9740
 * If filling, we know the buffer will be large enough, so we don't
9741
 *  need to pass in the buffer size.
9742
 * Inserts thousand grouping characters (as defined by grouping and
9743
 *  thousands_sep) into @writer.
9744
 *
9745
 * Return value: -1 on error, number of characters otherwise.
9746
 **/
9747
Py_ssize_t
9748
_PyUnicode_InsertThousandsGrouping(
9749
    _PyUnicodeWriter *writer,
9750
    Py_ssize_t n_buffer,
9751
    PyObject *digits,
9752
    Py_ssize_t d_pos,
9753
    Py_ssize_t n_digits,
9754
    Py_ssize_t min_width,
9755
    const char *grouping,
9756
    PyObject *thousands_sep,
9757
    Py_UCS4 *maxchar,
9758
    int forward)
9759
128
{
9760
128
    min_width = Py_MAX(0, min_width);
9761
128
    if (writer) {
9762
64
        assert(digits != NULL);
9763
64
        assert(maxchar == NULL);
9764
64
    }
9765
64
    else {
9766
64
        assert(digits == NULL);
9767
64
        assert(maxchar != NULL);
9768
64
    }
9769
128
    assert(0 <= d_pos);
9770
128
    assert(0 <= n_digits);
9771
128
    assert(grouping != NULL);
9772
9773
128
    Py_ssize_t count = 0;
9774
128
    Py_ssize_t n_zeros;
9775
128
    int loop_broken = 0;
9776
128
    int use_separator = 0; /* First time through, don't append the
9777
                              separator. They only go between
9778
                              groups. */
9779
128
    Py_ssize_t buffer_pos;
9780
128
    Py_ssize_t digits_pos;
9781
128
    Py_ssize_t len;
9782
128
    Py_ssize_t n_chars;
9783
128
    Py_ssize_t remaining = n_digits; /* Number of chars remaining to
9784
                                        be looked at */
9785
    /* A generator that returns all of the grouping widths, until it
9786
       returns 0. */
9787
128
    GroupGenerator groupgen;
9788
128
    GroupGenerator_init(&groupgen, grouping);
9789
128
    const Py_ssize_t thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep);
9790
9791
    /* if digits are not grouped, thousands separator
9792
       should be an empty string */
9793
128
    assert(!(grouping[0] == CHAR_MAX && thousands_sep_len != 0));
9794
9795
128
    digits_pos = d_pos + (forward ? 0 : n_digits);
9796
128
    if (writer) {
9797
64
        buffer_pos = writer->pos + (forward ? 0 : n_buffer);
9798
64
        assert(buffer_pos <= PyUnicode_GET_LENGTH(writer->buffer));
9799
64
        assert(digits_pos <= PyUnicode_GET_LENGTH(digits));
9800
64
    }
9801
64
    else {
9802
64
        buffer_pos = forward ? 0 : n_buffer;
9803
64
    }
9804
9805
128
    if (!writer) {
9806
64
        *maxchar = 127;
9807
64
    }
9808
9809
128
    while ((len = GroupGenerator_next(&groupgen)) > 0) {
9810
0
        len = Py_MIN(len, Py_MAX(Py_MAX(remaining, min_width), 1));
9811
0
        n_zeros = Py_MAX(0, len - remaining);
9812
0
        n_chars = Py_MAX(0, Py_MIN(remaining, len));
9813
9814
        /* Use n_zero zero's and n_chars chars */
9815
9816
        /* Count only, don't do anything. */
9817
0
        count += (use_separator ? thousands_sep_len : 0) + n_zeros + n_chars;
9818
9819
        /* Copy into the writer. */
9820
0
        InsertThousandsGrouping_fill(writer, &buffer_pos,
9821
0
                                     digits, &digits_pos,
9822
0
                                     n_chars, n_zeros,
9823
0
                                     use_separator ? thousands_sep : NULL,
9824
0
                                     thousands_sep_len, maxchar, forward);
9825
9826
        /* Use a separator next time. */
9827
0
        use_separator = 1;
9828
9829
0
        remaining -= n_chars;
9830
0
        min_width -= len;
9831
9832
0
        if (remaining <= 0 && min_width <= 0) {
9833
0
            loop_broken = 1;
9834
0
            break;
9835
0
        }
9836
0
        min_width -= thousands_sep_len;
9837
0
    }
9838
128
    if (!loop_broken) {
9839
        /* We left the loop without using a break statement. */
9840
9841
128
        len = Py_MAX(Py_MAX(remaining, min_width), 1);
9842
128
        n_zeros = Py_MAX(0, len - remaining);
9843
128
        n_chars = Py_MAX(0, Py_MIN(remaining, len));
9844
9845
        /* Use n_zero zero's and n_chars chars */
9846
128
        count += (use_separator ? thousands_sep_len : 0) + n_zeros + n_chars;
9847
9848
        /* Copy into the writer. */
9849
128
        InsertThousandsGrouping_fill(writer, &buffer_pos,
9850
128
                                     digits, &digits_pos,
9851
128
                                     n_chars, n_zeros,
9852
128
                                     use_separator ? thousands_sep : NULL,
9853
128
                                     thousands_sep_len, maxchar, forward);
9854
128
    }
9855
128
    return count;
9856
128
}
9857
9858
Py_ssize_t
9859
PyUnicode_Count(PyObject *str,
9860
                PyObject *substr,
9861
                Py_ssize_t start,
9862
                Py_ssize_t end)
9863
0
{
9864
0
    if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0)
9865
0
        return -1;
9866
9867
0
    return unicode_count_impl(str, substr, start, end);
9868
0
}
9869
9870
Py_ssize_t
9871
PyUnicode_Find(PyObject *str,
9872
               PyObject *substr,
9873
               Py_ssize_t start,
9874
               Py_ssize_t end,
9875
               int direction)
9876
0
{
9877
0
    if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0)
9878
0
        return -2;
9879
9880
0
    return any_find_slice(str, substr, start, end, direction);
9881
0
}
9882
9883
Py_ssize_t
9884
PyUnicode_FindChar(PyObject *str, Py_UCS4 ch,
9885
                   Py_ssize_t start, Py_ssize_t end,
9886
                   int direction)
9887
535k
{
9888
535k
    int kind;
9889
535k
    Py_ssize_t len, result;
9890
535k
    len = PyUnicode_GET_LENGTH(str);
9891
535k
    ADJUST_INDICES(start, end, len);
9892
535k
    if (end - start < 1)
9893
0
        return -1;
9894
535k
    kind = PyUnicode_KIND(str);
9895
535k
    result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start,
9896
535k
                      kind, end-start, ch, direction);
9897
535k
    if (result == -1)
9898
49.2k
        return -1;
9899
485k
    else
9900
485k
        return start + result;
9901
535k
}
9902
9903
static int
9904
tailmatch(PyObject *self,
9905
          PyObject *substring,
9906
          Py_ssize_t start,
9907
          Py_ssize_t end,
9908
          int direction)
9909
109M
{
9910
109M
    int kind_self;
9911
109M
    int kind_sub;
9912
109M
    const void *data_self;
9913
109M
    const void *data_sub;
9914
109M
    Py_ssize_t offset;
9915
109M
    Py_ssize_t i;
9916
109M
    Py_ssize_t end_sub;
9917
9918
109M
    ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self));
9919
109M
    end -= PyUnicode_GET_LENGTH(substring);
9920
109M
    if (end < start)
9921
14.1M
        return 0;
9922
9923
95.1M
    if (PyUnicode_GET_LENGTH(substring) == 0)
9924
0
        return 1;
9925
9926
95.1M
    kind_self = PyUnicode_KIND(self);
9927
95.1M
    data_self = PyUnicode_DATA(self);
9928
95.1M
    kind_sub = PyUnicode_KIND(substring);
9929
95.1M
    data_sub = PyUnicode_DATA(substring);
9930
95.1M
    end_sub = PyUnicode_GET_LENGTH(substring) - 1;
9931
9932
95.1M
    if (direction > 0)
9933
7.52M
        offset = end;
9934
87.5M
    else
9935
87.5M
        offset = start;
9936
9937
95.1M
    if (PyUnicode_READ(kind_self, data_self, offset) ==
9938
95.1M
        PyUnicode_READ(kind_sub, data_sub, 0) &&
9939
95.1M
        PyUnicode_READ(kind_self, data_self, offset + end_sub) ==
9940
48.1M
        PyUnicode_READ(kind_sub, data_sub, end_sub)) {
9941
        /* If both are of the same kind, memcmp is sufficient */
9942
14.9M
        if (kind_self == kind_sub) {
9943
10.2M
            return ! memcmp((char *)data_self +
9944
10.2M
                                (offset * PyUnicode_KIND(substring)),
9945
10.2M
                            data_sub,
9946
10.2M
                            PyUnicode_GET_LENGTH(substring) *
9947
10.2M
                                PyUnicode_KIND(substring));
9948
10.2M
        }
9949
        /* otherwise we have to compare each character by first accessing it */
9950
4.68M
        else {
9951
            /* We do not need to compare 0 and len(substring)-1 because
9952
               the if statement above ensured already that they are equal
9953
               when we end up here. */
9954
4.73M
            for (i = 1; i < end_sub; ++i) {
9955
55.0k
                if (PyUnicode_READ(kind_self, data_self, offset + i) !=
9956
55.0k
                    PyUnicode_READ(kind_sub, data_sub, i))
9957
3.61k
                    return 0;
9958
55.0k
            }
9959
4.67M
            return 1;
9960
4.68M
        }
9961
14.9M
    }
9962
9963
80.2M
    return 0;
9964
95.1M
}
9965
9966
Py_ssize_t
9967
PyUnicode_Tailmatch(PyObject *str,
9968
                    PyObject *substr,
9969
                    Py_ssize_t start,
9970
                    Py_ssize_t end,
9971
                    int direction)
9972
0
{
9973
0
    if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0)
9974
0
        return -1;
9975
9976
0
    return tailmatch(str, substr, start, end, direction);
9977
0
}
9978
9979
static PyObject *
9980
ascii_upper_or_lower(PyObject *self, int lower)
9981
74.0M
{
9982
74.0M
    Py_ssize_t len = PyUnicode_GET_LENGTH(self);
9983
74.0M
    const char *data = PyUnicode_DATA(self);
9984
74.0M
    char *resdata;
9985
74.0M
    PyObject *res;
9986
9987
74.0M
    res = PyUnicode_New(len, 127);
9988
74.0M
    if (res == NULL)
9989
0
        return NULL;
9990
74.0M
    resdata = PyUnicode_DATA(res);
9991
74.0M
    if (lower)
9992
74.0M
        _Py_bytes_lower(resdata, data, len);
9993
102
    else
9994
102
        _Py_bytes_upper(resdata, data, len);
9995
74.0M
    return res;
9996
74.0M
}
9997
9998
static Py_UCS4
9999
handle_capital_sigma(int kind, const void *data, Py_ssize_t length, Py_ssize_t i)
10000
31.7k
{
10001
31.7k
    Py_ssize_t j;
10002
31.7k
    int final_sigma;
10003
31.7k
    Py_UCS4 c = 0;   /* initialize to prevent gcc warning */
10004
    /* U+03A3 is in the Final_Sigma context when, it is found like this:
10005
10006
     \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased})
10007
10008
    where ! is a negation and \p{xxx} is a character with property xxx.
10009
    */
10010
78.8k
    for (j = i - 1; j >= 0; j--) {
10011
76.9k
        c = PyUnicode_READ(kind, data, j);
10012
76.9k
        if (!_PyUnicode_IsCaseIgnorable(c))
10013
29.9k
            break;
10014
76.9k
    }
10015
31.7k
    final_sigma = j >= 0 && _PyUnicode_IsCased(c);
10016
31.7k
    if (final_sigma) {
10017
60.1k
        for (j = i + 1; j < length; j++) {
10018
58.7k
            c = PyUnicode_READ(kind, data, j);
10019
58.7k
            if (!_PyUnicode_IsCaseIgnorable(c))
10020
21.5k
                break;
10021
58.7k
        }
10022
22.8k
        final_sigma = j == length || !_PyUnicode_IsCased(c);
10023
22.8k
    }
10024
31.7k
    return (final_sigma) ? 0x3C2 : 0x3C3;
10025
31.7k
}
10026
10027
static int
10028
lower_ucs4(int kind, const void *data, Py_ssize_t length, Py_ssize_t i,
10029
           Py_UCS4 c, Py_UCS4 *mapped)
10030
92.7M
{
10031
    /* Obscure special case. */
10032
92.7M
    if (c == 0x3A3) {
10033
31.7k
        mapped[0] = handle_capital_sigma(kind, data, length, i);
10034
31.7k
        return 1;
10035
31.7k
    }
10036
92.7M
    return _PyUnicode_ToLowerFull(c, mapped);
10037
92.7M
}
10038
10039
static Py_ssize_t
10040
do_capitalize(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar)
10041
0
{
10042
0
    Py_ssize_t i, k = 0;
10043
0
    int n_res, j;
10044
0
    Py_UCS4 c, mapped[3];
10045
10046
0
    c = PyUnicode_READ(kind, data, 0);
10047
0
    n_res = _PyUnicode_ToTitleFull(c, mapped);
10048
0
    for (j = 0; j < n_res; j++) {
10049
0
        *maxchar = Py_MAX(*maxchar, mapped[j]);
10050
0
        res[k++] = mapped[j];
10051
0
    }
10052
0
    for (i = 1; i < length; i++) {
10053
0
        c = PyUnicode_READ(kind, data, i);
10054
0
        n_res = lower_ucs4(kind, data, length, i, c, mapped);
10055
0
        for (j = 0; j < n_res; j++) {
10056
0
            *maxchar = Py_MAX(*maxchar, mapped[j]);
10057
0
            res[k++] = mapped[j];
10058
0
        }
10059
0
    }
10060
0
    return k;
10061
0
}
10062
10063
static Py_ssize_t
10064
0
do_swapcase(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) {
10065
0
    Py_ssize_t i, k = 0;
10066
10067
0
    for (i = 0; i < length; i++) {
10068
0
        Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3];
10069
0
        int n_res, j;
10070
0
        if (Py_UNICODE_ISUPPER(c)) {
10071
0
            n_res = lower_ucs4(kind, data, length, i, c, mapped);
10072
0
        }
10073
0
        else if (Py_UNICODE_ISLOWER(c)) {
10074
0
            n_res = _PyUnicode_ToUpperFull(c, mapped);
10075
0
        }
10076
0
        else {
10077
0
            n_res = 1;
10078
0
            mapped[0] = c;
10079
0
        }
10080
0
        for (j = 0; j < n_res; j++) {
10081
0
            *maxchar = Py_MAX(*maxchar, mapped[j]);
10082
0
            res[k++] = mapped[j];
10083
0
        }
10084
0
    }
10085
0
    return k;
10086
0
}
10087
10088
static Py_ssize_t
10089
do_upper_or_lower(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res,
10090
                  Py_UCS4 *maxchar, int lower)
10091
26.5M
{
10092
26.5M
    Py_ssize_t i, k = 0;
10093
10094
119M
    for (i = 0; i < length; i++) {
10095
92.7M
        Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3];
10096
92.7M
        int n_res, j;
10097
92.7M
        if (lower)
10098
92.7M
            n_res = lower_ucs4(kind, data, length, i, c, mapped);
10099
0
        else
10100
0
            n_res = _PyUnicode_ToUpperFull(c, mapped);
10101
185M
        for (j = 0; j < n_res; j++) {
10102
92.7M
            *maxchar = Py_MAX(*maxchar, mapped[j]);
10103
92.7M
            res[k++] = mapped[j];
10104
92.7M
        }
10105
92.7M
    }
10106
26.5M
    return k;
10107
26.5M
}
10108
10109
static Py_ssize_t
10110
do_upper(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar)
10111
0
{
10112
0
    return do_upper_or_lower(kind, data, length, res, maxchar, 0);
10113
0
}
10114
10115
static Py_ssize_t
10116
do_lower(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar)
10117
26.5M
{
10118
26.5M
    return do_upper_or_lower(kind, data, length, res, maxchar, 1);
10119
26.5M
}
10120
10121
static Py_ssize_t
10122
do_casefold(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar)
10123
0
{
10124
0
    Py_ssize_t i, k = 0;
10125
10126
0
    for (i = 0; i < length; i++) {
10127
0
        Py_UCS4 c = PyUnicode_READ(kind, data, i);
10128
0
        Py_UCS4 mapped[3];
10129
0
        int j, n_res = _PyUnicode_ToFoldedFull(c, mapped);
10130
0
        for (j = 0; j < n_res; j++) {
10131
0
            *maxchar = Py_MAX(*maxchar, mapped[j]);
10132
0
            res[k++] = mapped[j];
10133
0
        }
10134
0
    }
10135
0
    return k;
10136
0
}
10137
10138
static Py_ssize_t
10139
do_title(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar)
10140
0
{
10141
0
    Py_ssize_t i, k = 0;
10142
0
    int previous_is_cased;
10143
10144
0
    previous_is_cased = 0;
10145
0
    for (i = 0; i < length; i++) {
10146
0
        const Py_UCS4 c = PyUnicode_READ(kind, data, i);
10147
0
        Py_UCS4 mapped[3];
10148
0
        int n_res, j;
10149
10150
0
        if (previous_is_cased)
10151
0
            n_res = lower_ucs4(kind, data, length, i, c, mapped);
10152
0
        else
10153
0
            n_res = _PyUnicode_ToTitleFull(c, mapped);
10154
10155
0
        for (j = 0; j < n_res; j++) {
10156
0
            *maxchar = Py_MAX(*maxchar, mapped[j]);
10157
0
            res[k++] = mapped[j];
10158
0
        }
10159
10160
0
        previous_is_cased = _PyUnicode_IsCased(c);
10161
0
    }
10162
0
    return k;
10163
0
}
10164
10165
static PyObject *
10166
case_operation(PyObject *self,
10167
               Py_ssize_t (*perform)(int, const void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *))
10168
26.5M
{
10169
26.5M
    PyObject *res = NULL;
10170
26.5M
    Py_ssize_t length, newlength = 0;
10171
26.5M
    int kind, outkind;
10172
26.5M
    const void *data;
10173
26.5M
    void *outdata;
10174
26.5M
    Py_UCS4 maxchar = 0, *tmp, *tmpend;
10175
10176
26.5M
    kind = PyUnicode_KIND(self);
10177
26.5M
    data = PyUnicode_DATA(self);
10178
26.5M
    length = PyUnicode_GET_LENGTH(self);
10179
26.5M
    if ((size_t) length > PY_SSIZE_T_MAX / (3 * sizeof(Py_UCS4))) {
10180
0
        PyErr_SetString(PyExc_OverflowError, "string is too long");
10181
0
        return NULL;
10182
0
    }
10183
26.5M
    tmp = PyMem_Malloc(sizeof(Py_UCS4) * 3 * length);
10184
26.5M
    if (tmp == NULL)
10185
0
        return PyErr_NoMemory();
10186
26.5M
    newlength = perform(kind, data, length, tmp, &maxchar);
10187
26.5M
    res = PyUnicode_New(newlength, maxchar);
10188
26.5M
    if (res == NULL)
10189
0
        goto leave;
10190
26.5M
    tmpend = tmp + newlength;
10191
26.5M
    outdata = PyUnicode_DATA(res);
10192
26.5M
    outkind = PyUnicode_KIND(res);
10193
26.5M
    switch (outkind) {
10194
242k
    case PyUnicode_1BYTE_KIND:
10195
242k
        _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata);
10196
242k
        break;
10197
26.2M
    case PyUnicode_2BYTE_KIND:
10198
26.2M
        _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata);
10199
26.2M
        break;
10200
58.5k
    case PyUnicode_4BYTE_KIND:
10201
58.5k
        memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength);
10202
58.5k
        break;
10203
0
    default:
10204
0
        Py_UNREACHABLE();
10205
26.5M
    }
10206
26.5M
  leave:
10207
26.5M
    PyMem_Free(tmp);
10208
26.5M
    return res;
10209
26.5M
}
10210
10211
PyObject *
10212
PyUnicode_Join(PyObject *separator, PyObject *seq)
10213
22.6M
{
10214
22.6M
    PyObject *res;
10215
22.6M
    PyObject *fseq;
10216
22.6M
    Py_ssize_t seqlen;
10217
22.6M
    PyObject **items;
10218
10219
22.6M
    fseq = PySequence_Fast(seq, "can only join an iterable");
10220
22.6M
    if (fseq == NULL) {
10221
641
        return NULL;
10222
641
    }
10223
10224
22.6M
    Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST(seq);
10225
10226
22.6M
    items = PySequence_Fast_ITEMS(fseq);
10227
22.6M
    seqlen = PySequence_Fast_GET_SIZE(fseq);
10228
22.6M
    res = _PyUnicode_JoinArray(separator, items, seqlen);
10229
10230
22.6M
    Py_END_CRITICAL_SECTION_SEQUENCE_FAST();
10231
10232
22.6M
    Py_DECREF(fseq);
10233
22.6M
    return res;
10234
22.6M
}
10235
10236
PyObject *
10237
_PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seqlen)
10238
55.3M
{
10239
55.3M
    PyObject *res = NULL; /* the result */
10240
55.3M
    PyObject *sep = NULL;
10241
55.3M
    Py_ssize_t seplen;
10242
55.3M
    PyObject *item;
10243
55.3M
    Py_ssize_t sz, i, res_offset;
10244
55.3M
    Py_UCS4 maxchar;
10245
55.3M
    Py_UCS4 item_maxchar;
10246
55.3M
    int use_memcpy;
10247
55.3M
    unsigned char *res_data = NULL, *sep_data = NULL;
10248
55.3M
    PyObject *last_obj;
10249
55.3M
    int kind = 0;
10250
10251
    /* If empty sequence, return u"". */
10252
55.3M
    if (seqlen == 0) {
10253
5.05M
        _Py_RETURN_UNICODE_EMPTY();
10254
5.05M
    }
10255
10256
    /* If singleton sequence with an exact Unicode, return that. */
10257
50.2M
    last_obj = NULL;
10258
50.2M
    if (seqlen == 1) {
10259
7.27M
        if (PyUnicode_CheckExact(items[0])) {
10260
5.58M
            res = items[0];
10261
5.58M
            return Py_NewRef(res);
10262
5.58M
        }
10263
1.69M
        seplen = 0;
10264
1.69M
        maxchar = 0;
10265
1.69M
    }
10266
42.9M
    else {
10267
        /* Set up sep and seplen */
10268
42.9M
        if (separator == NULL) {
10269
            /* fall back to a blank space separator */
10270
0
            sep = PyUnicode_FromOrdinal(' ');
10271
0
            if (!sep)
10272
0
                goto onError;
10273
0
            seplen = 1;
10274
0
            maxchar = 32;
10275
0
        }
10276
42.9M
        else {
10277
42.9M
            if (!PyUnicode_Check(separator)) {
10278
0
                PyErr_Format(PyExc_TypeError,
10279
0
                             "separator: expected str instance,"
10280
0
                             " %.80s found",
10281
0
                             Py_TYPE(separator)->tp_name);
10282
0
                goto onError;
10283
0
            }
10284
42.9M
            sep = separator;
10285
42.9M
            seplen = PyUnicode_GET_LENGTH(separator);
10286
42.9M
            maxchar = PyUnicode_MAX_CHAR_VALUE(separator);
10287
            /* inc refcount to keep this code path symmetric with the
10288
               above case of a blank separator */
10289
42.9M
            Py_INCREF(sep);
10290
42.9M
        }
10291
42.9M
        last_obj = sep;
10292
42.9M
    }
10293
10294
    /* There are at least two things to join, or else we have a subclass
10295
     * of str in the sequence.
10296
     * Do a pre-pass to figure out the total amount of space we'll
10297
     * need (sz), and see whether all argument are strings.
10298
     */
10299
44.6M
    sz = 0;
10300
#ifdef Py_DEBUG
10301
    use_memcpy = 0;
10302
#else
10303
44.6M
    use_memcpy = 1;
10304
44.6M
#endif
10305
402M
    for (i = 0; i < seqlen; i++) {
10306
358M
        size_t add_sz;
10307
358M
        item = items[i];
10308
358M
        if (!PyUnicode_Check(item)) {
10309
0
            PyErr_Format(PyExc_TypeError,
10310
0
                         "sequence item %zd: expected str instance,"
10311
0
                         " %.80s found",
10312
0
                         i, Py_TYPE(item)->tp_name);
10313
0
            goto onError;
10314
0
        }
10315
358M
        add_sz = PyUnicode_GET_LENGTH(item);
10316
358M
        item_maxchar = PyUnicode_MAX_CHAR_VALUE(item);
10317
358M
        maxchar = Py_MAX(maxchar, item_maxchar);
10318
358M
        if (i != 0) {
10319
313M
            add_sz += seplen;
10320
313M
        }
10321
358M
        if (add_sz > (size_t)(PY_SSIZE_T_MAX - sz)) {
10322
0
            PyErr_SetString(PyExc_OverflowError,
10323
0
                            "join() result is too long for a Python string");
10324
0
            goto onError;
10325
0
        }
10326
358M
        sz += add_sz;
10327
358M
        if (use_memcpy && last_obj != NULL) {
10328
290M
            if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item))
10329
4.81M
                use_memcpy = 0;
10330
290M
        }
10331
358M
        last_obj = item;
10332
358M
    }
10333
10334
44.6M
    res = PyUnicode_New(sz, maxchar);
10335
44.6M
    if (res == NULL)
10336
0
        goto onError;
10337
10338
    /* Catenate everything. */
10339
#ifdef Py_DEBUG
10340
    use_memcpy = 0;
10341
#else
10342
44.6M
    if (use_memcpy) {
10343
39.8M
        res_data = PyUnicode_1BYTE_DATA(res);
10344
39.8M
        kind = PyUnicode_KIND(res);
10345
39.8M
        if (seplen != 0)
10346
15.9k
            sep_data = PyUnicode_1BYTE_DATA(sep);
10347
39.8M
    }
10348
44.6M
#endif
10349
44.6M
    if (use_memcpy) {
10350
300M
        for (i = 0; i < seqlen; ++i) {
10351
260M
            Py_ssize_t itemlen;
10352
260M
            item = items[i];
10353
10354
            /* Copy item, and maybe the separator. */
10355
260M
            if (i && seplen != 0) {
10356
22.2k
                memcpy(res_data,
10357
22.2k
                          sep_data,
10358
22.2k
                          kind * seplen);
10359
22.2k
                res_data += kind * seplen;
10360
22.2k
            }
10361
10362
260M
            itemlen = PyUnicode_GET_LENGTH(item);
10363
260M
            if (itemlen != 0) {
10364
219M
                memcpy(res_data,
10365
219M
                          PyUnicode_DATA(item),
10366
219M
                          kind * itemlen);
10367
219M
                res_data += kind * itemlen;
10368
219M
            }
10369
260M
        }
10370
39.8M
        assert(res_data == PyUnicode_1BYTE_DATA(res)
10371
39.8M
                           + kind * PyUnicode_GET_LENGTH(res));
10372
39.8M
    }
10373
4.81M
    else {
10374
102M
        for (i = 0, res_offset = 0; i < seqlen; ++i) {
10375
97.7M
            Py_ssize_t itemlen;
10376
97.7M
            item = items[i];
10377
10378
            /* Copy item, and maybe the separator. */
10379
97.7M
            if (i && seplen != 0) {
10380
64.8k
                _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen);
10381
64.8k
                res_offset += seplen;
10382
64.8k
            }
10383
10384
97.7M
            itemlen = PyUnicode_GET_LENGTH(item);
10385
97.7M
            if (itemlen != 0) {
10386
95.7M
                _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen);
10387
95.7M
                res_offset += itemlen;
10388
95.7M
            }
10389
97.7M
        }
10390
4.81M
        assert(res_offset == PyUnicode_GET_LENGTH(res));
10391
4.81M
    }
10392
10393
44.6M
    Py_XDECREF(sep);
10394
44.6M
    assert(_PyUnicode_CheckConsistency(res, 1));
10395
44.6M
    return res;
10396
10397
0
  onError:
10398
0
    Py_XDECREF(sep);
10399
0
    Py_XDECREF(res);
10400
0
    return NULL;
10401
44.6M
}
10402
10403
void
10404
_PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length,
10405
                    Py_UCS4 fill_char)
10406
756
{
10407
756
    const int kind = PyUnicode_KIND(unicode);
10408
756
    void *data = PyUnicode_DATA(unicode);
10409
756
    assert(unicode_modifiable(unicode));
10410
756
    assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode));
10411
756
    assert(start >= 0);
10412
756
    assert(start + length <= PyUnicode_GET_LENGTH(unicode));
10413
756
    unicode_fill(kind, data, fill_char, start, length);
10414
756
}
10415
10416
Py_ssize_t
10417
PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length,
10418
               Py_UCS4 fill_char)
10419
756
{
10420
756
    Py_ssize_t maxlen;
10421
10422
756
    if (!PyUnicode_Check(unicode)) {
10423
0
        PyErr_BadInternalCall();
10424
0
        return -1;
10425
0
    }
10426
756
    if (unicode_check_modifiable(unicode))
10427
0
        return -1;
10428
10429
756
    if (start < 0) {
10430
0
        PyErr_SetString(PyExc_IndexError, "string index out of range");
10431
0
        return -1;
10432
0
    }
10433
756
    if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) {
10434
0
        PyErr_SetString(PyExc_ValueError,
10435
0
                         "fill character is bigger than "
10436
0
                         "the string maximum character");
10437
0
        return -1;
10438
0
    }
10439
10440
756
    maxlen = PyUnicode_GET_LENGTH(unicode) - start;
10441
756
    length = Py_MIN(maxlen, length);
10442
756
    if (length <= 0)
10443
0
        return 0;
10444
10445
756
    _PyUnicode_FastFill(unicode, start, length, fill_char);
10446
756
    return length;
10447
756
}
10448
10449
static PyObject *
10450
pad(PyObject *self,
10451
    Py_ssize_t left,
10452
    Py_ssize_t right,
10453
    Py_UCS4 fill)
10454
0
{
10455
0
    PyObject *u;
10456
0
    Py_UCS4 maxchar;
10457
0
    int kind;
10458
0
    void *data;
10459
10460
0
    if (left < 0)
10461
0
        left = 0;
10462
0
    if (right < 0)
10463
0
        right = 0;
10464
10465
0
    if (left == 0 && right == 0)
10466
0
        return unicode_result_unchanged(self);
10467
10468
0
    if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) ||
10469
0
        right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) {
10470
0
        PyErr_SetString(PyExc_OverflowError, "padded string is too long");
10471
0
        return NULL;
10472
0
    }
10473
0
    maxchar = PyUnicode_MAX_CHAR_VALUE(self);
10474
0
    maxchar = Py_MAX(maxchar, fill);
10475
0
    u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar);
10476
0
    if (!u)
10477
0
        return NULL;
10478
10479
0
    kind = PyUnicode_KIND(u);
10480
0
    data = PyUnicode_DATA(u);
10481
0
    if (left)
10482
0
        unicode_fill(kind, data, fill, 0, left);
10483
0
    if (right)
10484
0
        unicode_fill(kind, data, fill, left + _PyUnicode_LENGTH(self), right);
10485
0
    _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self));
10486
0
    assert(_PyUnicode_CheckConsistency(u, 1));
10487
0
    return u;
10488
0
}
10489
10490
PyObject *
10491
PyUnicode_Splitlines(PyObject *string, int keepends)
10492
13.8k
{
10493
13.8k
    PyObject *list;
10494
10495
13.8k
    if (ensure_unicode(string) < 0)
10496
0
        return NULL;
10497
10498
13.8k
    switch (PyUnicode_KIND(string)) {
10499
3.57k
    case PyUnicode_1BYTE_KIND:
10500
3.57k
        if (PyUnicode_IS_ASCII(string))
10501
2.73k
            list = asciilib_splitlines(
10502
2.73k
                string, PyUnicode_1BYTE_DATA(string),
10503
2.73k
                PyUnicode_GET_LENGTH(string), keepends);
10504
842
        else
10505
842
            list = ucs1lib_splitlines(
10506
842
                string, PyUnicode_1BYTE_DATA(string),
10507
842
                PyUnicode_GET_LENGTH(string), keepends);
10508
3.57k
        break;
10509
7.21k
    case PyUnicode_2BYTE_KIND:
10510
7.21k
        list = ucs2lib_splitlines(
10511
7.21k
            string, PyUnicode_2BYTE_DATA(string),
10512
7.21k
            PyUnicode_GET_LENGTH(string), keepends);
10513
7.21k
        break;
10514
3.10k
    case PyUnicode_4BYTE_KIND:
10515
3.10k
        list = ucs4lib_splitlines(
10516
3.10k
            string, PyUnicode_4BYTE_DATA(string),
10517
3.10k
            PyUnicode_GET_LENGTH(string), keepends);
10518
3.10k
        break;
10519
0
    default:
10520
0
        Py_UNREACHABLE();
10521
13.8k
    }
10522
13.8k
    return list;
10523
13.8k
}
10524
10525
static PyObject *
10526
split(PyObject *self,
10527
      PyObject *substring,
10528
      Py_ssize_t maxcount)
10529
22.6M
{
10530
22.6M
    int kind1, kind2;
10531
22.6M
    const void *buf1, *buf2;
10532
22.6M
    Py_ssize_t len1, len2;
10533
22.6M
    PyObject* out;
10534
22.6M
    len1 = PyUnicode_GET_LENGTH(self);
10535
22.6M
    kind1 = PyUnicode_KIND(self);
10536
10537
22.6M
    if (substring == NULL) {
10538
166k
        if (maxcount < 0) {
10539
140k
            maxcount = (len1 - 1) / 2 + 1;
10540
140k
        }
10541
166k
        switch (kind1) {
10542
109k
        case PyUnicode_1BYTE_KIND:
10543
109k
            if (PyUnicode_IS_ASCII(self))
10544
82.8k
                return asciilib_split_whitespace(
10545
82.8k
                    self,  PyUnicode_1BYTE_DATA(self),
10546
82.8k
                    len1, maxcount
10547
82.8k
                    );
10548
26.7k
            else
10549
26.7k
                return ucs1lib_split_whitespace(
10550
26.7k
                    self,  PyUnicode_1BYTE_DATA(self),
10551
26.7k
                    len1, maxcount
10552
26.7k
                    );
10553
45.6k
        case PyUnicode_2BYTE_KIND:
10554
45.6k
            return ucs2lib_split_whitespace(
10555
45.6k
                self,  PyUnicode_2BYTE_DATA(self),
10556
45.6k
                len1, maxcount
10557
45.6k
                );
10558
10.7k
        case PyUnicode_4BYTE_KIND:
10559
10.7k
            return ucs4lib_split_whitespace(
10560
10.7k
                self,  PyUnicode_4BYTE_DATA(self),
10561
10.7k
                len1, maxcount
10562
10.7k
                );
10563
0
        default:
10564
0
            Py_UNREACHABLE();
10565
166k
        }
10566
166k
    }
10567
10568
22.5M
    kind2 = PyUnicode_KIND(substring);
10569
22.5M
    len2 = PyUnicode_GET_LENGTH(substring);
10570
22.5M
    if (maxcount < 0) {
10571
        // if len2 == 0, it will raise ValueError.
10572
11.3M
        maxcount = len2 == 0 ? 0 : (len1 / len2) + 1;
10573
        // handle expected overflow case: (Py_SSIZE_T_MAX / 1) + 1
10574
11.3M
        maxcount = maxcount < 0 ? len1 : maxcount;
10575
11.3M
    }
10576
22.5M
    if (kind1 < kind2 || len1 < len2) {
10577
6.41M
        out = PyList_New(1);
10578
6.41M
        if (out == NULL)
10579
0
            return NULL;
10580
6.41M
        PyList_SET_ITEM(out, 0, Py_NewRef(self));
10581
6.41M
        return out;
10582
6.41M
    }
10583
16.1M
    buf1 = PyUnicode_DATA(self);
10584
16.1M
    buf2 = PyUnicode_DATA(substring);
10585
16.1M
    if (kind2 != kind1) {
10586
193k
        buf2 = unicode_askind(kind2, buf2, len2, kind1);
10587
193k
        if (!buf2)
10588
0
            return NULL;
10589
193k
    }
10590
10591
16.1M
    switch (kind1) {
10592
15.9M
    case PyUnicode_1BYTE_KIND:
10593
15.9M
        if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring))
10594
14.8M
            out = asciilib_split(
10595
14.8M
                self,  buf1, len1, buf2, len2, maxcount);
10596
1.08M
        else
10597
1.08M
            out = ucs1lib_split(
10598
1.08M
                self,  buf1, len1, buf2, len2, maxcount);
10599
15.9M
        break;
10600
160k
    case PyUnicode_2BYTE_KIND:
10601
160k
        out = ucs2lib_split(
10602
160k
            self,  buf1, len1, buf2, len2, maxcount);
10603
160k
        break;
10604
32.9k
    case PyUnicode_4BYTE_KIND:
10605
32.9k
        out = ucs4lib_split(
10606
32.9k
            self,  buf1, len1, buf2, len2, maxcount);
10607
32.9k
        break;
10608
0
    default:
10609
0
        out = NULL;
10610
16.1M
    }
10611
16.1M
    assert((kind2 != kind1) == (buf2 != PyUnicode_DATA(substring)));
10612
16.1M
    if (kind2 != kind1)
10613
193k
        PyMem_Free((void *)buf2);
10614
16.1M
    return out;
10615
16.1M
}
10616
10617
static PyObject *
10618
rsplit(PyObject *self,
10619
       PyObject *substring,
10620
       Py_ssize_t maxcount)
10621
50
{
10622
50
    int kind1, kind2;
10623
50
    const void *buf1, *buf2;
10624
50
    Py_ssize_t len1, len2;
10625
50
    PyObject* out;
10626
10627
50
    len1 = PyUnicode_GET_LENGTH(self);
10628
50
    kind1 = PyUnicode_KIND(self);
10629
10630
50
    if (substring == NULL) {
10631
0
        if (maxcount < 0) {
10632
0
            maxcount = (len1 - 1) / 2 + 1;
10633
0
        }
10634
0
        switch (kind1) {
10635
0
        case PyUnicode_1BYTE_KIND:
10636
0
            if (PyUnicode_IS_ASCII(self))
10637
0
                return asciilib_rsplit_whitespace(
10638
0
                    self,  PyUnicode_1BYTE_DATA(self),
10639
0
                    len1, maxcount
10640
0
                    );
10641
0
            else
10642
0
                return ucs1lib_rsplit_whitespace(
10643
0
                    self,  PyUnicode_1BYTE_DATA(self),
10644
0
                    len1, maxcount
10645
0
                    );
10646
0
        case PyUnicode_2BYTE_KIND:
10647
0
            return ucs2lib_rsplit_whitespace(
10648
0
                self,  PyUnicode_2BYTE_DATA(self),
10649
0
                len1, maxcount
10650
0
                );
10651
0
        case PyUnicode_4BYTE_KIND:
10652
0
            return ucs4lib_rsplit_whitespace(
10653
0
                self,  PyUnicode_4BYTE_DATA(self),
10654
0
                len1, maxcount
10655
0
                );
10656
0
        default:
10657
0
            Py_UNREACHABLE();
10658
0
        }
10659
0
    }
10660
50
    kind2 = PyUnicode_KIND(substring);
10661
50
    len2 = PyUnicode_GET_LENGTH(substring);
10662
50
    if (maxcount < 0) {
10663
        // if len2 == 0, it will raise ValueError.
10664
0
        maxcount = len2 == 0 ? 0 : (len1 / len2) + 1;
10665
        // handle expected overflow case: (Py_SSIZE_T_MAX / 1) + 1
10666
0
        maxcount = maxcount < 0 ? len1 : maxcount;
10667
0
    }
10668
50
    if (kind1 < kind2 || len1 < len2) {
10669
0
        out = PyList_New(1);
10670
0
        if (out == NULL)
10671
0
            return NULL;
10672
0
        PyList_SET_ITEM(out, 0, Py_NewRef(self));
10673
0
        return out;
10674
0
    }
10675
50
    buf1 = PyUnicode_DATA(self);
10676
50
    buf2 = PyUnicode_DATA(substring);
10677
50
    if (kind2 != kind1) {
10678
0
        buf2 = unicode_askind(kind2, buf2, len2, kind1);
10679
0
        if (!buf2)
10680
0
            return NULL;
10681
0
    }
10682
10683
50
    switch (kind1) {
10684
50
    case PyUnicode_1BYTE_KIND:
10685
50
        if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring))
10686
50
            out = asciilib_rsplit(
10687
50
                self,  buf1, len1, buf2, len2, maxcount);
10688
0
        else
10689
0
            out = ucs1lib_rsplit(
10690
0
                self,  buf1, len1, buf2, len2, maxcount);
10691
50
        break;
10692
0
    case PyUnicode_2BYTE_KIND:
10693
0
        out = ucs2lib_rsplit(
10694
0
            self,  buf1, len1, buf2, len2, maxcount);
10695
0
        break;
10696
0
    case PyUnicode_4BYTE_KIND:
10697
0
        out = ucs4lib_rsplit(
10698
0
            self,  buf1, len1, buf2, len2, maxcount);
10699
0
        break;
10700
0
    default:
10701
0
        out = NULL;
10702
50
    }
10703
50
    assert((kind2 != kind1) == (buf2 != PyUnicode_DATA(substring)));
10704
50
    if (kind2 != kind1)
10705
0
        PyMem_Free((void *)buf2);
10706
50
    return out;
10707
50
}
10708
10709
static Py_ssize_t
10710
anylib_find(int kind, PyObject *str1, const void *buf1, Py_ssize_t len1,
10711
            PyObject *str2, const void *buf2, Py_ssize_t len2, Py_ssize_t offset)
10712
197M
{
10713
197M
    switch (kind) {
10714
27.0M
    case PyUnicode_1BYTE_KIND:
10715
27.0M
        if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2))
10716
23.1M
            return asciilib_find(buf1, len1, buf2, len2, offset);
10717
3.92M
        else
10718
3.92M
            return ucs1lib_find(buf1, len1, buf2, len2, offset);
10719
71.3M
    case PyUnicode_2BYTE_KIND:
10720
71.3M
        return ucs2lib_find(buf1, len1, buf2, len2, offset);
10721
98.9M
    case PyUnicode_4BYTE_KIND:
10722
98.9M
        return ucs4lib_find(buf1, len1, buf2, len2, offset);
10723
197M
    }
10724
197M
    Py_UNREACHABLE();
10725
197M
}
10726
10727
static Py_ssize_t
10728
anylib_count(int kind, PyObject *sstr, const void* sbuf, Py_ssize_t slen,
10729
             PyObject *str1, const void *buf1, Py_ssize_t len1, Py_ssize_t maxcount)
10730
47.1M
{
10731
47.1M
    switch (kind) {
10732
42.2M
    case PyUnicode_1BYTE_KIND:
10733
42.2M
        return ucs1lib_count(sbuf, slen, buf1, len1, maxcount);
10734
4.80M
    case PyUnicode_2BYTE_KIND:
10735
4.80M
        return ucs2lib_count(sbuf, slen, buf1, len1, maxcount);
10736
142k
    case PyUnicode_4BYTE_KIND:
10737
142k
        return ucs4lib_count(sbuf, slen, buf1, len1, maxcount);
10738
47.1M
    }
10739
47.1M
    Py_UNREACHABLE();
10740
47.1M
}
10741
10742
static void
10743
replace_1char_inplace(PyObject *u, Py_ssize_t pos,
10744
                      Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount)
10745
1.35M
{
10746
1.35M
    int kind = PyUnicode_KIND(u);
10747
1.35M
    void *data = PyUnicode_DATA(u);
10748
1.35M
    Py_ssize_t len = PyUnicode_GET_LENGTH(u);
10749
1.35M
    if (kind == PyUnicode_1BYTE_KIND) {
10750
469k
        ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos,
10751
469k
                                      (Py_UCS1 *)data + len,
10752
469k
                                      u1, u2, maxcount);
10753
469k
    }
10754
888k
    else if (kind == PyUnicode_2BYTE_KIND) {
10755
873k
        ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos,
10756
873k
                                      (Py_UCS2 *)data + len,
10757
873k
                                      u1, u2, maxcount);
10758
873k
    }
10759
15.0k
    else {
10760
15.0k
        assert(kind == PyUnicode_4BYTE_KIND);
10761
15.0k
        ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos,
10762
15.0k
                                      (Py_UCS4 *)data + len,
10763
15.0k
                                      u1, u2, maxcount);
10764
15.0k
    }
10765
1.35M
}
10766
10767
static PyObject *
10768
replace(PyObject *self, PyObject *str1,
10769
        PyObject *str2, Py_ssize_t maxcount)
10770
89.2M
{
10771
89.2M
    PyObject *u;
10772
89.2M
    const char *sbuf = PyUnicode_DATA(self);
10773
89.2M
    const void *buf1 = PyUnicode_DATA(str1);
10774
89.2M
    const void *buf2 = PyUnicode_DATA(str2);
10775
89.2M
    int srelease = 0, release1 = 0, release2 = 0;
10776
89.2M
    int skind = PyUnicode_KIND(self);
10777
89.2M
    int kind1 = PyUnicode_KIND(str1);
10778
89.2M
    int kind2 = PyUnicode_KIND(str2);
10779
89.2M
    Py_ssize_t slen = PyUnicode_GET_LENGTH(self);
10780
89.2M
    Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1);
10781
89.2M
    Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2);
10782
89.2M
    int mayshrink;
10783
89.2M
    Py_UCS4 maxchar, maxchar_str1, maxchar_str2;
10784
10785
89.2M
    if (slen < len1)
10786
36.3M
        goto nothing;
10787
10788
52.8M
    if (maxcount < 0)
10789
52.8M
        maxcount = PY_SSIZE_T_MAX;
10790
0
    else if (maxcount == 0)
10791
0
        goto nothing;
10792
10793
52.8M
    if (str1 == str2)
10794
0
        goto nothing;
10795
10796
52.8M
    maxchar = PyUnicode_MAX_CHAR_VALUE(self);
10797
52.8M
    maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1);
10798
52.8M
    if (maxchar < maxchar_str1)
10799
        /* substring too wide to be present */
10800
0
        goto nothing;
10801
52.8M
    maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2);
10802
    /* Replacing str1 with str2 may cause a maxchar reduction in the
10803
       result string. */
10804
52.8M
    mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1);
10805
52.8M
    maxchar = Py_MAX(maxchar, maxchar_str2);
10806
10807
52.8M
    if (len1 == len2) {
10808
        /* same length */
10809
5.69M
        if (len1 == 0)
10810
0
            goto nothing;
10811
5.69M
        if (len1 == 1) {
10812
            /* replace characters */
10813
5.69M
            Py_UCS4 u1, u2;
10814
5.69M
            Py_ssize_t pos;
10815
10816
5.69M
            u1 = PyUnicode_READ(kind1, buf1, 0);
10817
5.69M
            pos = findchar(sbuf, skind, slen, u1, 1);
10818
5.69M
            if (pos < 0)
10819
4.33M
                goto nothing;
10820
1.35M
            u2 = PyUnicode_READ(kind2, buf2, 0);
10821
1.35M
            u = PyUnicode_New(slen, maxchar);
10822
1.35M
            if (!u)
10823
0
                goto error;
10824
10825
1.35M
            _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen);
10826
1.35M
            replace_1char_inplace(u, pos, u1, u2, maxcount);
10827
1.35M
        }
10828
0
        else {
10829
0
            int rkind = skind;
10830
0
            char *res;
10831
0
            Py_ssize_t i;
10832
10833
0
            if (kind1 < rkind) {
10834
                /* widen substring */
10835
0
                buf1 = unicode_askind(kind1, buf1, len1, rkind);
10836
0
                if (!buf1) goto error;
10837
0
                release1 = 1;
10838
0
            }
10839
0
            i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0);
10840
0
            if (i < 0)
10841
0
                goto nothing;
10842
0
            if (rkind > kind2) {
10843
                /* widen replacement */
10844
0
                buf2 = unicode_askind(kind2, buf2, len2, rkind);
10845
0
                if (!buf2) goto error;
10846
0
                release2 = 1;
10847
0
            }
10848
0
            else if (rkind < kind2) {
10849
                /* widen self and buf1 */
10850
0
                rkind = kind2;
10851
0
                if (release1) {
10852
0
                    assert(buf1 != PyUnicode_DATA(str1));
10853
0
                    PyMem_Free((void *)buf1);
10854
0
                    buf1 = PyUnicode_DATA(str1);
10855
0
                    release1 = 0;
10856
0
                }
10857
0
                sbuf = unicode_askind(skind, sbuf, slen, rkind);
10858
0
                if (!sbuf) goto error;
10859
0
                srelease = 1;
10860
0
                buf1 = unicode_askind(kind1, buf1, len1, rkind);
10861
0
                if (!buf1) goto error;
10862
0
                release1 = 1;
10863
0
            }
10864
0
            u = PyUnicode_New(slen, maxchar);
10865
0
            if (!u)
10866
0
                goto error;
10867
0
            assert(PyUnicode_KIND(u) == rkind);
10868
0
            res = PyUnicode_DATA(u);
10869
10870
0
            memcpy(res, sbuf, rkind * slen);
10871
            /* change everything in-place, starting with this one */
10872
0
            memcpy(res + rkind * i,
10873
0
                   buf2,
10874
0
                   rkind * len2);
10875
0
            i += len1;
10876
10877
0
            while ( --maxcount > 0) {
10878
0
                i = anylib_find(rkind, self,
10879
0
                                sbuf+rkind*i, slen-i,
10880
0
                                str1, buf1, len1, i);
10881
0
                if (i == -1)
10882
0
                    break;
10883
0
                memcpy(res + rkind * i,
10884
0
                       buf2,
10885
0
                       rkind * len2);
10886
0
                i += len1;
10887
0
            }
10888
0
        }
10889
5.69M
    }
10890
47.1M
    else {
10891
47.1M
        Py_ssize_t n, i, j, ires;
10892
47.1M
        Py_ssize_t new_size;
10893
47.1M
        int rkind = skind;
10894
47.1M
        char *res;
10895
10896
47.1M
        if (kind1 < rkind) {
10897
            /* widen substring */
10898
4.95M
            buf1 = unicode_askind(kind1, buf1, len1, rkind);
10899
4.95M
            if (!buf1) goto error;
10900
4.95M
            release1 = 1;
10901
4.95M
        }
10902
47.1M
        n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount);
10903
47.1M
        if (n == 0)
10904
41.2M
            goto nothing;
10905
5.91M
        if (kind2 < rkind) {
10906
            /* widen replacement */
10907
1.19M
            buf2 = unicode_askind(kind2, buf2, len2, rkind);
10908
1.19M
            if (!buf2) goto error;
10909
1.19M
            release2 = 1;
10910
1.19M
        }
10911
4.71M
        else if (kind2 > rkind) {
10912
            /* widen self and buf1 */
10913
0
            rkind = kind2;
10914
0
            sbuf = unicode_askind(skind, sbuf, slen, rkind);
10915
0
            if (!sbuf) goto error;
10916
0
            srelease = 1;
10917
0
            if (release1) {
10918
0
                assert(buf1 != PyUnicode_DATA(str1));
10919
0
                PyMem_Free((void *)buf1);
10920
0
                buf1 = PyUnicode_DATA(str1);
10921
0
                release1 = 0;
10922
0
            }
10923
0
            buf1 = unicode_askind(kind1, buf1, len1, rkind);
10924
0
            if (!buf1) goto error;
10925
0
            release1 = 1;
10926
0
        }
10927
        /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) -
10928
           PyUnicode_GET_LENGTH(str1)); */
10929
5.91M
        if (len1 < len2 && len2 - len1 > (PY_SSIZE_T_MAX - slen) / n) {
10930
0
                PyErr_SetString(PyExc_OverflowError,
10931
0
                                "replace string is too long");
10932
0
                goto error;
10933
0
        }
10934
5.91M
        new_size = slen + n * (len2 - len1);
10935
5.91M
        if (new_size == 0) {
10936
0
            u = unicode_get_empty();
10937
0
            goto done;
10938
0
        }
10939
5.91M
        if (new_size > (PY_SSIZE_T_MAX / rkind)) {
10940
0
            PyErr_SetString(PyExc_OverflowError,
10941
0
                            "replace string is too long");
10942
0
            goto error;
10943
0
        }
10944
5.91M
        u = PyUnicode_New(new_size, maxchar);
10945
5.91M
        if (!u)
10946
0
            goto error;
10947
5.91M
        assert(PyUnicode_KIND(u) == rkind);
10948
5.91M
        res = PyUnicode_DATA(u);
10949
5.91M
        ires = i = 0;
10950
5.91M
        if (len1 > 0) {
10951
203M
            while (n-- > 0) {
10952
                /* look for next match */
10953
197M
                j = anylib_find(rkind, self,
10954
197M
                                sbuf + rkind * i, slen-i,
10955
197M
                                str1, buf1, len1, i);
10956
197M
                if (j == -1)
10957
0
                    break;
10958
197M
                else if (j > i) {
10959
                    /* copy unchanged part [i:j] */
10960
24.5M
                    memcpy(res + rkind * ires,
10961
24.5M
                           sbuf + rkind * i,
10962
24.5M
                           rkind * (j-i));
10963
24.5M
                    ires += j - i;
10964
24.5M
                }
10965
                /* copy substitution string */
10966
197M
                if (len2 > 0) {
10967
197M
                    memcpy(res + rkind * ires,
10968
197M
                           buf2,
10969
197M
                           rkind * len2);
10970
197M
                    ires += len2;
10971
197M
                }
10972
197M
                i = j + len1;
10973
197M
            }
10974
5.91M
            if (i < slen)
10975
                /* copy tail [i:] */
10976
5.82M
                memcpy(res + rkind * ires,
10977
5.82M
                       sbuf + rkind * i,
10978
5.82M
                       rkind * (slen-i));
10979
5.91M
        }
10980
0
        else {
10981
            /* interleave */
10982
0
            while (n > 0) {
10983
0
                memcpy(res + rkind * ires,
10984
0
                       buf2,
10985
0
                       rkind * len2);
10986
0
                ires += len2;
10987
0
                if (--n <= 0)
10988
0
                    break;
10989
0
                memcpy(res + rkind * ires,
10990
0
                       sbuf + rkind * i,
10991
0
                       rkind);
10992
0
                ires++;
10993
0
                i++;
10994
0
            }
10995
0
            memcpy(res + rkind * ires,
10996
0
                   sbuf + rkind * i,
10997
0
                   rkind * (slen-i));
10998
0
        }
10999
5.91M
    }
11000
11001
7.27M
    if (mayshrink) {
11002
0
        unicode_adjust_maxchar(&u);
11003
0
        if (u == NULL)
11004
0
            goto error;
11005
0
    }
11006
11007
7.27M
  done:
11008
7.27M
    assert(srelease == (sbuf != PyUnicode_DATA(self)));
11009
7.27M
    assert(release1 == (buf1 != PyUnicode_DATA(str1)));
11010
7.27M
    assert(release2 == (buf2 != PyUnicode_DATA(str2)));
11011
7.27M
    if (srelease)
11012
0
        PyMem_Free((void *)sbuf);
11013
7.27M
    if (release1)
11014
1.19M
        PyMem_Free((void *)buf1);
11015
7.27M
    if (release2)
11016
1.19M
        PyMem_Free((void *)buf2);
11017
7.27M
    assert(_PyUnicode_CheckConsistency(u, 1));
11018
7.27M
    return u;
11019
11020
81.9M
  nothing:
11021
    /* nothing to replace; return original string (when possible) */
11022
81.9M
    assert(srelease == (sbuf != PyUnicode_DATA(self)));
11023
81.9M
    assert(release1 == (buf1 != PyUnicode_DATA(str1)));
11024
81.9M
    assert(release2 == (buf2 != PyUnicode_DATA(str2)));
11025
81.9M
    if (srelease)
11026
0
        PyMem_Free((void *)sbuf);
11027
81.9M
    if (release1)
11028
3.75M
        PyMem_Free((void *)buf1);
11029
81.9M
    if (release2)
11030
0
        PyMem_Free((void *)buf2);
11031
81.9M
    return unicode_result_unchanged(self);
11032
11033
0
  error:
11034
0
    assert(srelease == (sbuf != PyUnicode_DATA(self)));
11035
0
    assert(release1 == (buf1 != PyUnicode_DATA(str1)));
11036
0
    assert(release2 == (buf2 != PyUnicode_DATA(str2)));
11037
0
    if (srelease)
11038
0
        PyMem_Free((void *)sbuf);
11039
0
    if (release1)
11040
0
        PyMem_Free((void *)buf1);
11041
0
    if (release2)
11042
0
        PyMem_Free((void *)buf2);
11043
0
    return NULL;
11044
7.27M
}
11045
11046
/* --- Unicode Object Methods --------------------------------------------- */
11047
11048
/*[clinic input]
11049
@permit_long_docstring_body
11050
str.title as unicode_title
11051
11052
Return a version of the string where each word is titlecased.
11053
11054
More specifically, words start with uppercased characters and all remaining
11055
cased characters have lower case.
11056
[clinic start generated code]*/
11057
11058
static PyObject *
11059
unicode_title_impl(PyObject *self)
11060
/*[clinic end generated code: output=c75ae03809574902 input=533ce0eb6a7f5d1b]*/
11061
0
{
11062
0
    return case_operation(self, do_title);
11063
0
}
11064
11065
/*[clinic input]
11066
@permit_long_docstring_body
11067
str.capitalize as unicode_capitalize
11068
11069
Return a capitalized version of the string.
11070
11071
More specifically, make the first character have upper case and the rest lower
11072
case.
11073
[clinic start generated code]*/
11074
11075
static PyObject *
11076
unicode_capitalize_impl(PyObject *self)
11077
/*[clinic end generated code: output=e49a4c333cdb7667 input=a4a15ade41f6f9e9]*/
11078
0
{
11079
0
    if (PyUnicode_GET_LENGTH(self) == 0)
11080
0
        return unicode_result_unchanged(self);
11081
0
    return case_operation(self, do_capitalize);
11082
0
}
11083
11084
/*[clinic input]
11085
str.casefold as unicode_casefold
11086
11087
Return a version of the string suitable for caseless comparisons.
11088
[clinic start generated code]*/
11089
11090
static PyObject *
11091
unicode_casefold_impl(PyObject *self)
11092
/*[clinic end generated code: output=0120daf657ca40af input=384d66cc2ae30daf]*/
11093
0
{
11094
0
    if (PyUnicode_IS_ASCII(self))
11095
0
        return ascii_upper_or_lower(self, 1);
11096
0
    return case_operation(self, do_casefold);
11097
0
}
11098
11099
11100
/* Argument converter. Accepts a single Unicode character. */
11101
11102
static int
11103
convert_uc(PyObject *obj, void *addr)
11104
0
{
11105
0
    Py_UCS4 *fillcharloc = (Py_UCS4 *)addr;
11106
11107
0
    if (!PyUnicode_Check(obj)) {
11108
0
        PyErr_Format(PyExc_TypeError,
11109
0
                     "The fill character must be a unicode character, "
11110
0
                     "not %.100s", Py_TYPE(obj)->tp_name);
11111
0
        return 0;
11112
0
    }
11113
0
    if (PyUnicode_GET_LENGTH(obj) != 1) {
11114
0
        PyErr_SetString(PyExc_TypeError,
11115
0
                        "The fill character must be exactly one character long");
11116
0
        return 0;
11117
0
    }
11118
0
    *fillcharloc = PyUnicode_READ_CHAR(obj, 0);
11119
0
    return 1;
11120
0
}
11121
11122
/*[clinic input]
11123
str.center as unicode_center
11124
11125
    width: Py_ssize_t
11126
    fillchar: Py_UCS4 = ' '
11127
    /
11128
11129
Return a centered string of length width.
11130
11131
Padding is done using the specified fill character (default is a space).
11132
[clinic start generated code]*/
11133
11134
static PyObject *
11135
unicode_center_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar)
11136
/*[clinic end generated code: output=420c8859effc7c0c input=b42b247eb26e6519]*/
11137
0
{
11138
0
    Py_ssize_t marg, left;
11139
11140
0
    if (PyUnicode_GET_LENGTH(self) >= width)
11141
0
        return unicode_result_unchanged(self);
11142
11143
0
    marg = width - PyUnicode_GET_LENGTH(self);
11144
0
    left = marg / 2 + (marg & width & 1);
11145
11146
0
    return pad(self, left, marg - left, fillchar);
11147
0
}
11148
11149
/* This function assumes that str1 and str2 are readied by the caller. */
11150
11151
static int
11152
unicode_compare(PyObject *str1, PyObject *str2)
11153
24.5M
{
11154
24.5M
#define COMPARE(TYPE1, TYPE2) \
11155
24.5M
    do { \
11156
22.8M
        TYPE1* p1 = (TYPE1 *)data1; \
11157
22.8M
        TYPE2* p2 = (TYPE2 *)data2; \
11158
22.8M
        TYPE1* end = p1 + len; \
11159
22.8M
        Py_UCS4 c1, c2; \
11160
22.8M
        for (; p1 != end; p1++, p2++) { \
11161
22.8M
            c1 = *p1; \
11162
22.8M
            c2 = *p2; \
11163
22.8M
            if (c1 != c2) \
11164
22.8M
                return (c1 < c2) ? -1 : 1; \
11165
22.8M
        } \
11166
22.8M
    } \
11167
22.8M
    while (0)
11168
11169
24.5M
    int kind1, kind2;
11170
24.5M
    const void *data1, *data2;
11171
24.5M
    Py_ssize_t len1, len2, len;
11172
11173
24.5M
    kind1 = PyUnicode_KIND(str1);
11174
24.5M
    kind2 = PyUnicode_KIND(str2);
11175
24.5M
    data1 = PyUnicode_DATA(str1);
11176
24.5M
    data2 = PyUnicode_DATA(str2);
11177
24.5M
    len1 = PyUnicode_GET_LENGTH(str1);
11178
24.5M
    len2 = PyUnicode_GET_LENGTH(str2);
11179
24.5M
    len = Py_MIN(len1, len2);
11180
11181
24.5M
    switch(kind1) {
11182
1.50M
    case PyUnicode_1BYTE_KIND:
11183
1.50M
    {
11184
1.50M
        switch(kind2) {
11185
74.4k
        case PyUnicode_1BYTE_KIND:
11186
74.4k
        {
11187
74.4k
            int cmp = memcmp(data1, data2, len);
11188
            /* normalize result of memcmp() into the range [-1; 1] */
11189
74.4k
            if (cmp < 0)
11190
46.1k
                return -1;
11191
28.3k
            if (cmp > 0)
11192
27.8k
                return 1;
11193
534
            break;
11194
28.3k
        }
11195
1.20M
        case PyUnicode_2BYTE_KIND:
11196
1.20M
            COMPARE(Py_UCS1, Py_UCS2);
11197
0
            break;
11198
221k
        case PyUnicode_4BYTE_KIND:
11199
221k
            COMPARE(Py_UCS1, Py_UCS4);
11200
0
            break;
11201
0
        default:
11202
0
            Py_UNREACHABLE();
11203
1.50M
        }
11204
534
        break;
11205
1.50M
    }
11206
20.3M
    case PyUnicode_2BYTE_KIND:
11207
20.3M
    {
11208
20.3M
        switch(kind2) {
11209
6.93k
        case PyUnicode_1BYTE_KIND:
11210
6.93k
            COMPARE(Py_UCS2, Py_UCS1);
11211
0
            break;
11212
17.9M
        case PyUnicode_2BYTE_KIND:
11213
17.9M
        {
11214
17.9M
            COMPARE(Py_UCS2, Py_UCS2);
11215
0
            break;
11216
17.9M
        }
11217
2.40M
        case PyUnicode_4BYTE_KIND:
11218
2.40M
            COMPARE(Py_UCS2, Py_UCS4);
11219
0
            break;
11220
0
        default:
11221
0
            Py_UNREACHABLE();
11222
20.3M
        }
11223
0
        break;
11224
20.3M
    }
11225
2.63M
    case PyUnicode_4BYTE_KIND:
11226
2.63M
    {
11227
2.63M
        switch(kind2) {
11228
577
        case PyUnicode_1BYTE_KIND:
11229
577
            COMPARE(Py_UCS4, Py_UCS1);
11230
0
            break;
11231
1.07M
        case PyUnicode_2BYTE_KIND:
11232
1.07M
            COMPARE(Py_UCS4, Py_UCS2);
11233
0
            break;
11234
1.55M
        case PyUnicode_4BYTE_KIND:
11235
1.55M
        {
11236
1.55M
#if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4
11237
1.55M
            int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len);
11238
            /* normalize result of wmemcmp() into the range [-1; 1] */
11239
1.55M
            if (cmp < 0)
11240
760k
                return -1;
11241
794k
            if (cmp > 0)
11242
794k
                return 1;
11243
#else
11244
            COMPARE(Py_UCS4, Py_UCS4);
11245
#endif
11246
0
            break;
11247
794k
        }
11248
0
        default:
11249
0
            Py_UNREACHABLE();
11250
2.63M
        }
11251
0
        break;
11252
2.63M
    }
11253
0
    default:
11254
0
        Py_UNREACHABLE();
11255
24.5M
    }
11256
11257
534
    if (len1 == len2)
11258
531
        return 0;
11259
3
    if (len1 < len2)
11260
3
        return -1;
11261
0
    else
11262
0
        return 1;
11263
11264
3
#undef COMPARE
11265
3
}
11266
11267
11268
int
11269
_PyUnicode_Equal(PyObject *str1, PyObject *str2)
11270
297M
{
11271
297M
    assert(PyUnicode_Check(str1));
11272
297M
    assert(PyUnicode_Check(str2));
11273
297M
    if (str1 == str2) {
11274
88.4M
        return 1;
11275
88.4M
    }
11276
208M
    return unicode_eq(str1, str2);
11277
297M
}
11278
11279
11280
int
11281
PyUnicode_Equal(PyObject *str1, PyObject *str2)
11282
0
{
11283
0
    if (!PyUnicode_Check(str1)) {
11284
0
        PyErr_Format(PyExc_TypeError,
11285
0
                     "first argument must be str, not %T", str1);
11286
0
        return -1;
11287
0
    }
11288
0
    if (!PyUnicode_Check(str2)) {
11289
0
        PyErr_Format(PyExc_TypeError,
11290
0
                     "second argument must be str, not %T", str2);
11291
0
        return -1;
11292
0
    }
11293
11294
0
    return _PyUnicode_Equal(str1, str2);
11295
0
}
11296
11297
11298
int
11299
PyUnicode_Compare(PyObject *left, PyObject *right)
11300
7.11k
{
11301
7.11k
    if (PyUnicode_Check(left) && PyUnicode_Check(right)) {
11302
        /* a string is equal to itself */
11303
7.11k
        if (left == right)
11304
0
            return 0;
11305
11306
7.11k
        return unicode_compare(left, right);
11307
7.11k
    }
11308
0
    PyErr_Format(PyExc_TypeError,
11309
0
                 "Can't compare %.100s and %.100s",
11310
0
                 Py_TYPE(left)->tp_name,
11311
0
                 Py_TYPE(right)->tp_name);
11312
0
    return -1;
11313
7.11k
}
11314
11315
int
11316
PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
11317
2.07M
{
11318
2.07M
    Py_ssize_t i;
11319
2.07M
    int kind;
11320
2.07M
    Py_UCS4 chr;
11321
11322
2.07M
    assert(_PyUnicode_CHECK(uni));
11323
2.07M
    kind = PyUnicode_KIND(uni);
11324
2.07M
    if (kind == PyUnicode_1BYTE_KIND) {
11325
2.07M
        const void *data = PyUnicode_1BYTE_DATA(uni);
11326
2.07M
        size_t len1 = (size_t)PyUnicode_GET_LENGTH(uni);
11327
2.07M
        size_t len, len2 = strlen(str);
11328
2.07M
        int cmp;
11329
11330
2.07M
        len = Py_MIN(len1, len2);
11331
2.07M
        cmp = memcmp(data, str, len);
11332
2.07M
        if (cmp != 0) {
11333
1.49M
            if (cmp < 0)
11334
7.33k
                return -1;
11335
1.48M
            else
11336
1.48M
                return 1;
11337
1.49M
        }
11338
579k
        if (len1 > len2)
11339
209
            return 1; /* uni is longer */
11340
579k
        if (len1 < len2)
11341
819
            return -1; /* str is longer */
11342
578k
        return 0;
11343
579k
    }
11344
1.59k
    else {
11345
1.59k
        const void *data = PyUnicode_DATA(uni);
11346
        /* Compare Unicode string and source character set string */
11347
2.85k
        for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++)
11348
2.61k
            if (chr != (unsigned char)str[i])
11349
1.35k
                return (chr < (unsigned char)(str[i])) ? -1 : 1;
11350
        /* This check keeps Python strings that end in '\0' from comparing equal
11351
         to C strings identical up to that point. */
11352
240
        if (PyUnicode_GET_LENGTH(uni) != i || chr)
11353
240
            return 1; /* uni is longer */
11354
0
        if (str[i])
11355
0
            return -1; /* str is longer */
11356
0
        return 0;
11357
0
    }
11358
2.07M
}
11359
11360
int
11361
PyUnicode_EqualToUTF8(PyObject *unicode, const char *str)
11362
0
{
11363
0
    return PyUnicode_EqualToUTF8AndSize(unicode, str, strlen(str));
11364
0
}
11365
11366
int
11367
PyUnicode_EqualToUTF8AndSize(PyObject *unicode, const char *str, Py_ssize_t size)
11368
0
{
11369
0
    assert(_PyUnicode_CHECK(unicode));
11370
0
    assert(str);
11371
11372
0
    if (PyUnicode_IS_ASCII(unicode)) {
11373
0
        Py_ssize_t len = PyUnicode_GET_LENGTH(unicode);
11374
0
        return size == len &&
11375
0
            memcmp(PyUnicode_1BYTE_DATA(unicode), str, len) == 0;
11376
0
    }
11377
0
    if (PyUnicode_UTF8(unicode) != NULL) {
11378
0
        Py_ssize_t len = PyUnicode_UTF8_LENGTH(unicode);
11379
0
        return size == len &&
11380
0
            memcmp(PyUnicode_UTF8(unicode), str, len) == 0;
11381
0
    }
11382
11383
0
    Py_ssize_t len = PyUnicode_GET_LENGTH(unicode);
11384
0
    if ((size_t)len >= (size_t)size || (size_t)len < (size_t)size / 4) {
11385
0
        return 0;
11386
0
    }
11387
0
    const unsigned char *s = (const unsigned char *)str;
11388
0
    const unsigned char *ends = s + (size_t)size;
11389
0
    int kind = PyUnicode_KIND(unicode);
11390
0
    const void *data = PyUnicode_DATA(unicode);
11391
    /* Compare Unicode string and UTF-8 string */
11392
0
    for (Py_ssize_t i = 0; i < len; i++) {
11393
0
        Py_UCS4 ch = PyUnicode_READ(kind, data, i);
11394
0
        if (ch < 0x80) {
11395
0
            if (ends == s || s[0] != ch) {
11396
0
                return 0;
11397
0
            }
11398
0
            s += 1;
11399
0
        }
11400
0
        else if (ch < 0x800) {
11401
0
            if ((ends - s) < 2 ||
11402
0
                s[0] != (0xc0 | (ch >> 6)) ||
11403
0
                s[1] != (0x80 | (ch & 0x3f)))
11404
0
            {
11405
0
                return 0;
11406
0
            }
11407
0
            s += 2;
11408
0
        }
11409
0
        else if (ch < 0x10000) {
11410
0
            if (Py_UNICODE_IS_SURROGATE(ch) ||
11411
0
                (ends - s) < 3 ||
11412
0
                s[0] != (0xe0 | (ch >> 12)) ||
11413
0
                s[1] != (0x80 | ((ch >> 6) & 0x3f)) ||
11414
0
                s[2] != (0x80 | (ch & 0x3f)))
11415
0
            {
11416
0
                return 0;
11417
0
            }
11418
0
            s += 3;
11419
0
        }
11420
0
        else {
11421
0
            assert(ch <= MAX_UNICODE);
11422
0
            if ((ends - s) < 4 ||
11423
0
                s[0] != (0xf0 | (ch >> 18)) ||
11424
0
                s[1] != (0x80 | ((ch >> 12) & 0x3f)) ||
11425
0
                s[2] != (0x80 | ((ch >> 6) & 0x3f)) ||
11426
0
                s[3] != (0x80 | (ch & 0x3f)))
11427
0
            {
11428
0
                return 0;
11429
0
            }
11430
0
            s += 4;
11431
0
        }
11432
0
    }
11433
0
    return s == ends;
11434
0
}
11435
11436
int
11437
_PyUnicode_EqualToASCIIString(PyObject *unicode, const char *str)
11438
7.28M
{
11439
7.28M
    size_t len;
11440
7.28M
    assert(_PyUnicode_CHECK(unicode));
11441
7.28M
    assert(str);
11442
#ifndef NDEBUG
11443
    for (const char *p = str; *p; p++) {
11444
        assert((unsigned char)*p < 128);
11445
    }
11446
#endif
11447
7.28M
    if (!PyUnicode_IS_ASCII(unicode))
11448
154k
        return 0;
11449
7.12M
    len = (size_t)PyUnicode_GET_LENGTH(unicode);
11450
7.12M
    return strlen(str) == len &&
11451
7.12M
           memcmp(PyUnicode_1BYTE_DATA(unicode), str, len) == 0;
11452
7.28M
}
11453
11454
int
11455
_PyUnicode_EqualToASCIIId(PyObject *left, _Py_Identifier *right)
11456
0
{
11457
0
    PyObject *right_uni;
11458
11459
0
    assert(_PyUnicode_CHECK(left));
11460
0
    assert(right->string);
11461
#ifndef NDEBUG
11462
    for (const char *p = right->string; *p; p++) {
11463
        assert((unsigned char)*p < 128);
11464
    }
11465
#endif
11466
11467
0
    if (!PyUnicode_IS_ASCII(left))
11468
0
        return 0;
11469
11470
0
    right_uni = _PyUnicode_FromId(right);       /* borrowed */
11471
0
    if (right_uni == NULL) {
11472
        /* memory error or bad data */
11473
0
        PyErr_Clear();
11474
0
        return _PyUnicode_EqualToASCIIString(left, right->string);
11475
0
    }
11476
11477
0
    if (left == right_uni)
11478
0
        return 1;
11479
11480
0
    assert(PyUnicode_CHECK_INTERNED(right_uni));
11481
0
    if (PyUnicode_CHECK_INTERNED(left)) {
11482
0
        return 0;
11483
0
    }
11484
11485
0
    Py_hash_t right_hash = PyUnicode_HASH(right_uni);
11486
0
    assert(right_hash != -1);
11487
0
    Py_hash_t hash = PyUnicode_HASH(left);
11488
0
    if (hash != -1 && hash != right_hash) {
11489
0
        return 0;
11490
0
    }
11491
11492
0
    return unicode_eq(left, right_uni);
11493
0
}
11494
11495
PyObject *
11496
PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
11497
35.5M
{
11498
35.5M
    int result;
11499
11500
35.5M
    if (!PyUnicode_Check(left) || !PyUnicode_Check(right))
11501
96.7k
        Py_RETURN_NOTIMPLEMENTED;
11502
11503
35.4M
    if (left == right) {
11504
1.67k
        switch (op) {
11505
1.61k
        case Py_EQ:
11506
1.61k
        case Py_LE:
11507
1.61k
        case Py_GE:
11508
            /* a string is equal to itself */
11509
1.61k
            Py_RETURN_TRUE;
11510
67
        case Py_NE:
11511
67
        case Py_LT:
11512
67
        case Py_GT:
11513
67
            Py_RETURN_FALSE;
11514
0
        default:
11515
0
            PyErr_BadArgument();
11516
0
            return NULL;
11517
1.67k
        }
11518
1.67k
    }
11519
35.4M
    else if (op == Py_EQ || op == Py_NE) {
11520
10.9M
        result = unicode_eq(left, right);
11521
10.9M
        result ^= (op == Py_NE);
11522
10.9M
        return PyBool_FromLong(result);
11523
10.9M
    }
11524
24.5M
    else {
11525
24.5M
        result = unicode_compare(left, right);
11526
24.5M
        Py_RETURN_RICHCOMPARE(result, 0, op);
11527
24.5M
    }
11528
35.4M
}
11529
11530
int
11531
PyUnicode_Contains(PyObject *str, PyObject *substr)
11532
97.5M
{
11533
97.5M
    int kind1, kind2;
11534
97.5M
    const void *buf1, *buf2;
11535
97.5M
    Py_ssize_t len1, len2;
11536
97.5M
    int result;
11537
11538
97.5M
    if (!PyUnicode_Check(substr)) {
11539
0
        PyErr_Format(PyExc_TypeError,
11540
0
                     "'in <string>' requires string as left operand, not %.100s",
11541
0
                     Py_TYPE(substr)->tp_name);
11542
0
        return -1;
11543
0
    }
11544
97.5M
    if (ensure_unicode(str) < 0)
11545
0
        return -1;
11546
11547
97.5M
    kind1 = PyUnicode_KIND(str);
11548
97.5M
    kind2 = PyUnicode_KIND(substr);
11549
97.5M
    if (kind1 < kind2)
11550
4.31M
        return 0;
11551
93.2M
    len1 = PyUnicode_GET_LENGTH(str);
11552
93.2M
    len2 = PyUnicode_GET_LENGTH(substr);
11553
93.2M
    if (len1 < len2)
11554
6.43M
        return 0;
11555
86.7M
    buf1 = PyUnicode_DATA(str);
11556
86.7M
    buf2 = PyUnicode_DATA(substr);
11557
86.7M
    if (len2 == 1) {
11558
86.7M
        Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0);
11559
86.7M
        result = findchar((const char *)buf1, kind1, len1, ch, 1) != -1;
11560
86.7M
        return result;
11561
86.7M
    }
11562
39.0k
    if (kind2 != kind1) {
11563
22.3k
        buf2 = unicode_askind(kind2, buf2, len2, kind1);
11564
22.3k
        if (!buf2)
11565
0
            return -1;
11566
22.3k
    }
11567
11568
39.0k
    switch (kind1) {
11569
16.7k
    case PyUnicode_1BYTE_KIND:
11570
16.7k
        result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1;
11571
16.7k
        break;
11572
16.4k
    case PyUnicode_2BYTE_KIND:
11573
16.4k
        result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1;
11574
16.4k
        break;
11575
5.85k
    case PyUnicode_4BYTE_KIND:
11576
5.85k
        result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1;
11577
5.85k
        break;
11578
0
    default:
11579
0
        Py_UNREACHABLE();
11580
39.0k
    }
11581
11582
39.0k
    assert((kind2 == kind1) == (buf2 == PyUnicode_DATA(substr)));
11583
39.0k
    if (kind2 != kind1)
11584
22.3k
        PyMem_Free((void *)buf2);
11585
11586
39.0k
    return result;
11587
39.0k
}
11588
11589
/* Concat to string or Unicode object giving a new Unicode object. */
11590
11591
PyObject *
11592
PyUnicode_Concat(PyObject *left, PyObject *right)
11593
32.3M
{
11594
32.3M
    PyObject *result;
11595
32.3M
    Py_UCS4 maxchar, maxchar2;
11596
32.3M
    Py_ssize_t left_len, right_len, new_len;
11597
11598
32.3M
    if (ensure_unicode(left) < 0)
11599
0
        return NULL;
11600
11601
32.3M
    if (!PyUnicode_Check(right)) {
11602
0
        PyErr_Format(PyExc_TypeError,
11603
0
            "can only concatenate str (not \"%.200s\") to str",
11604
0
            Py_TYPE(right)->tp_name);
11605
0
        return NULL;
11606
0
    }
11607
11608
    /* Shortcuts */
11609
32.3M
    PyObject *empty = unicode_get_empty();  // Borrowed reference
11610
32.3M
    if (left == empty) {
11611
73.6k
        return PyUnicode_FromObject(right);
11612
73.6k
    }
11613
32.2M
    if (right == empty) {
11614
5.20M
        return PyUnicode_FromObject(left);
11615
5.20M
    }
11616
11617
27.0M
    left_len = PyUnicode_GET_LENGTH(left);
11618
27.0M
    right_len = PyUnicode_GET_LENGTH(right);
11619
27.0M
    if (left_len > PY_SSIZE_T_MAX - right_len) {
11620
0
        PyErr_SetString(PyExc_OverflowError,
11621
0
                        "strings are too large to concat");
11622
0
        return NULL;
11623
0
    }
11624
27.0M
    new_len = left_len + right_len;
11625
11626
27.0M
    maxchar = PyUnicode_MAX_CHAR_VALUE(left);
11627
27.0M
    maxchar2 = PyUnicode_MAX_CHAR_VALUE(right);
11628
27.0M
    maxchar = Py_MAX(maxchar, maxchar2);
11629
11630
    /* Concat the two Unicode strings */
11631
27.0M
    result = PyUnicode_New(new_len, maxchar);
11632
27.0M
    if (result == NULL)
11633
0
        return NULL;
11634
27.0M
    _PyUnicode_FastCopyCharacters(result, 0, left, 0, left_len);
11635
27.0M
    _PyUnicode_FastCopyCharacters(result, left_len, right, 0, right_len);
11636
27.0M
    assert(_PyUnicode_CheckConsistency(result, 1));
11637
27.0M
    return result;
11638
27.0M
}
11639
11640
void
11641
PyUnicode_Append(PyObject **p_left, PyObject *right)
11642
1.25M
{
11643
1.25M
    PyObject *left, *res;
11644
1.25M
    Py_UCS4 maxchar, maxchar2;
11645
1.25M
    Py_ssize_t left_len, right_len, new_len;
11646
11647
1.25M
    if (p_left == NULL) {
11648
0
        if (!PyErr_Occurred())
11649
0
            PyErr_BadInternalCall();
11650
0
        return;
11651
0
    }
11652
1.25M
    left = *p_left;
11653
1.25M
    if (right == NULL || left == NULL
11654
1.25M
        || !PyUnicode_Check(left) || !PyUnicode_Check(right)) {
11655
0
        if (!PyErr_Occurred())
11656
0
            PyErr_BadInternalCall();
11657
0
        goto error;
11658
0
    }
11659
11660
    /* Shortcuts */
11661
1.25M
    PyObject *empty = unicode_get_empty();  // Borrowed reference
11662
1.25M
    if (left == empty) {
11663
453k
        Py_DECREF(left);
11664
453k
        *p_left = Py_NewRef(right);
11665
453k
        return;
11666
453k
    }
11667
802k
    if (right == empty) {
11668
5
        return;
11669
5
    }
11670
11671
802k
    left_len = PyUnicode_GET_LENGTH(left);
11672
802k
    right_len = PyUnicode_GET_LENGTH(right);
11673
802k
    if (left_len > PY_SSIZE_T_MAX - right_len) {
11674
0
        PyErr_SetString(PyExc_OverflowError,
11675
0
                        "strings are too large to concat");
11676
0
        goto error;
11677
0
    }
11678
802k
    new_len = left_len + right_len;
11679
11680
802k
    if (unicode_modifiable(left)
11681
802k
        && PyUnicode_CheckExact(right)
11682
802k
        && PyUnicode_KIND(right) <= PyUnicode_KIND(left)
11683
        /* Don't resize for ascii += latin1. Convert ascii to latin1 requires
11684
           to change the structure size, but characters are stored just after
11685
           the structure, and so it requires to move all characters which is
11686
           not so different than duplicating the string. */
11687
802k
        && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right)))
11688
760k
    {
11689
        /* append inplace */
11690
760k
        if (unicode_resize(p_left, new_len) != 0)
11691
0
            goto error;
11692
11693
        /* copy 'right' into the newly allocated area of 'left' */
11694
760k
        _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len);
11695
760k
    }
11696
41.9k
    else {
11697
41.9k
        maxchar = PyUnicode_MAX_CHAR_VALUE(left);
11698
41.9k
        maxchar2 = PyUnicode_MAX_CHAR_VALUE(right);
11699
41.9k
        maxchar = Py_MAX(maxchar, maxchar2);
11700
11701
        /* Concat the two Unicode strings */
11702
41.9k
        res = PyUnicode_New(new_len, maxchar);
11703
41.9k
        if (res == NULL)
11704
0
            goto error;
11705
41.9k
        _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len);
11706
41.9k
        _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len);
11707
41.9k
        Py_DECREF(left);
11708
41.9k
        *p_left = res;
11709
41.9k
    }
11710
802k
    assert(_PyUnicode_CheckConsistency(*p_left, 1));
11711
802k
    return;
11712
11713
0
error:
11714
0
    Py_CLEAR(*p_left);
11715
0
}
11716
11717
void
11718
PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right)
11719
0
{
11720
0
    PyUnicode_Append(pleft, right);
11721
0
    Py_XDECREF(right);
11722
0
}
11723
11724
/*[clinic input]
11725
@permit_long_summary
11726
@text_signature "($self, sub[, start[, end]], /)"
11727
str.count as unicode_count -> Py_ssize_t
11728
11729
    self as str: self
11730
    sub as substr: unicode
11731
    start: slice_index(accept={int, NoneType}, c_default='0') = None
11732
    end: slice_index(accept={int, NoneType}, c_default='PY_SSIZE_T_MAX') = None
11733
    /
11734
11735
Return the number of non-overlapping occurrences of substring sub in string S[start:end].
11736
11737
Optional arguments start and end are interpreted as in slice notation.
11738
[clinic start generated code]*/
11739
11740
static Py_ssize_t
11741
unicode_count_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
11742
                   Py_ssize_t end)
11743
/*[clinic end generated code: output=8fcc3aef0b18edbf input=8590716ee228b935]*/
11744
19.3M
{
11745
19.3M
    assert(PyUnicode_Check(str));
11746
19.3M
    assert(PyUnicode_Check(substr));
11747
11748
19.3M
    Py_ssize_t result;
11749
19.3M
    int kind1, kind2;
11750
19.3M
    const void *buf1 = NULL, *buf2 = NULL;
11751
19.3M
    Py_ssize_t len1, len2;
11752
11753
19.3M
    kind1 = PyUnicode_KIND(str);
11754
19.3M
    kind2 = PyUnicode_KIND(substr);
11755
19.3M
    if (kind1 < kind2)
11756
0
        return 0;
11757
11758
19.3M
    len1 = PyUnicode_GET_LENGTH(str);
11759
19.3M
    len2 = PyUnicode_GET_LENGTH(substr);
11760
19.3M
    ADJUST_INDICES(start, end, len1);
11761
19.3M
    if (end - start < len2)
11762
72.8k
        return 0;
11763
11764
19.2M
    buf1 = PyUnicode_DATA(str);
11765
19.2M
    buf2 = PyUnicode_DATA(substr);
11766
19.2M
    if (kind2 != kind1) {
11767
4.48M
        buf2 = unicode_askind(kind2, buf2, len2, kind1);
11768
4.48M
        if (!buf2)
11769
0
            goto onError;
11770
4.48M
    }
11771
11772
    // We don't reuse `anylib_count` here because of the explicit casts.
11773
19.2M
    switch (kind1) {
11774
14.7M
    case PyUnicode_1BYTE_KIND:
11775
14.7M
        result = ucs1lib_count(
11776
14.7M
            ((const Py_UCS1*)buf1) + start, end - start,
11777
14.7M
            buf2, len2, PY_SSIZE_T_MAX
11778
14.7M
            );
11779
14.7M
        break;
11780
3.77M
    case PyUnicode_2BYTE_KIND:
11781
3.77M
        result = ucs2lib_count(
11782
3.77M
            ((const Py_UCS2*)buf1) + start, end - start,
11783
3.77M
            buf2, len2, PY_SSIZE_T_MAX
11784
3.77M
            );
11785
3.77M
        break;
11786
709k
    case PyUnicode_4BYTE_KIND:
11787
709k
        result = ucs4lib_count(
11788
709k
            ((const Py_UCS4*)buf1) + start, end - start,
11789
709k
            buf2, len2, PY_SSIZE_T_MAX
11790
709k
            );
11791
709k
        break;
11792
0
    default:
11793
0
        Py_UNREACHABLE();
11794
19.2M
    }
11795
11796
19.2M
    assert((kind2 != kind1) == (buf2 != PyUnicode_DATA(substr)));
11797
19.2M
    if (kind2 != kind1)
11798
4.48M
        PyMem_Free((void *)buf2);
11799
11800
19.2M
    return result;
11801
0
  onError:
11802
0
    assert((kind2 != kind1) == (buf2 != PyUnicode_DATA(substr)));
11803
0
    if (kind2 != kind1)
11804
0
        PyMem_Free((void *)buf2);
11805
0
    return -1;
11806
19.2M
}
11807
11808
/*[clinic input]
11809
str.encode as unicode_encode
11810
11811
    encoding: str(c_default="NULL") = 'utf-8'
11812
        The encoding in which to encode the string.
11813
    errors: str(c_default="NULL") = 'strict'
11814
        The error handling scheme to use for encoding errors.
11815
        The default is 'strict' meaning that encoding errors raise a
11816
        UnicodeEncodeError.  Other possible values are 'ignore', 'replace' and
11817
        'xmlcharrefreplace' as well as any other name registered with
11818
        codecs.register_error that can handle UnicodeEncodeErrors.
11819
11820
Encode the string using the codec registered for encoding.
11821
[clinic start generated code]*/
11822
11823
static PyObject *
11824
unicode_encode_impl(PyObject *self, const char *encoding, const char *errors)
11825
/*[clinic end generated code: output=bf78b6e2a9470e3c input=f0a9eb293d08fe02]*/
11826
15.6M
{
11827
15.6M
    return PyUnicode_AsEncodedString(self, encoding, errors);
11828
15.6M
}
11829
11830
/*[clinic input]
11831
str.expandtabs as unicode_expandtabs
11832
11833
    tabsize: int = 8
11834
11835
Return a copy where all tab characters are expanded using spaces.
11836
11837
If tabsize is not given, a tab size of 8 characters is assumed.
11838
[clinic start generated code]*/
11839
11840
static PyObject *
11841
unicode_expandtabs_impl(PyObject *self, int tabsize)
11842
/*[clinic end generated code: output=3457c5dcee26928f input=8a01914034af4c85]*/
11843
7.50M
{
11844
7.50M
    Py_ssize_t i, j, line_pos, src_len, incr;
11845
7.50M
    Py_UCS4 ch;
11846
7.50M
    PyObject *u;
11847
7.50M
    const void *src_data;
11848
7.50M
    void *dest_data;
11849
7.50M
    int kind;
11850
7.50M
    int found;
11851
11852
    /* First pass: determine size of output string */
11853
7.50M
    src_len = PyUnicode_GET_LENGTH(self);
11854
7.50M
    i = j = line_pos = 0;
11855
7.50M
    kind = PyUnicode_KIND(self);
11856
7.50M
    src_data = PyUnicode_DATA(self);
11857
7.50M
    found = 0;
11858
144M
    for (; i < src_len; i++) {
11859
136M
        ch = PyUnicode_READ(kind, src_data, i);
11860
136M
        if (ch == '\t') {
11861
16.5M
            found = 1;
11862
16.5M
            if (tabsize > 0) {
11863
16.5M
                incr = tabsize - (line_pos % tabsize); /* cannot overflow */
11864
16.5M
                if (j > PY_SSIZE_T_MAX - incr)
11865
0
                    goto overflow;
11866
16.5M
                line_pos += incr;
11867
16.5M
                j += incr;
11868
16.5M
            }
11869
16.5M
        }
11870
120M
        else {
11871
120M
            if (j > PY_SSIZE_T_MAX - 1)
11872
0
                goto overflow;
11873
120M
            line_pos++;
11874
120M
            j++;
11875
120M
            if (ch == '\n' || ch == '\r')
11876
12.6k
                line_pos = 0;
11877
120M
        }
11878
136M
    }
11879
7.50M
    if (!found)
11880
7.35M
        return unicode_result_unchanged(self);
11881
11882
    /* Second pass: create output string and fill it */
11883
150k
    u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self));
11884
150k
    if (!u)
11885
0
        return NULL;
11886
150k
    dest_data = PyUnicode_DATA(u);
11887
11888
150k
    i = j = line_pos = 0;
11889
11890
38.3M
    for (; i < src_len; i++) {
11891
38.2M
        ch = PyUnicode_READ(kind, src_data, i);
11892
38.2M
        if (ch == '\t') {
11893
16.5M
            if (tabsize > 0) {
11894
16.5M
                incr = tabsize - (line_pos % tabsize);
11895
16.5M
                line_pos += incr;
11896
16.5M
                unicode_fill(kind, dest_data, ' ', j, incr);
11897
16.5M
                j += incr;
11898
16.5M
            }
11899
16.5M
        }
11900
21.6M
        else {
11901
21.6M
            line_pos++;
11902
21.6M
            PyUnicode_WRITE(kind, dest_data, j, ch);
11903
21.6M
            j++;
11904
21.6M
            if (ch == '\n' || ch == '\r')
11905
0
                line_pos = 0;
11906
21.6M
        }
11907
38.2M
    }
11908
150k
    assert (j == PyUnicode_GET_LENGTH(u));
11909
150k
    return unicode_result(u);
11910
11911
0
  overflow:
11912
0
    PyErr_SetString(PyExc_OverflowError, "new string is too long");
11913
0
    return NULL;
11914
150k
}
11915
11916
/*[clinic input]
11917
@permit_long_summary
11918
str.find as unicode_find = str.count
11919
11920
Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end].
11921
11922
Optional arguments start and end are interpreted as in slice notation.
11923
Return -1 on failure.
11924
[clinic start generated code]*/
11925
11926
static Py_ssize_t
11927
unicode_find_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
11928
                  Py_ssize_t end)
11929
/*[clinic end generated code: output=51dbe6255712e278 input=3a9d650fe4c24695]*/
11930
17.0M
{
11931
17.0M
    Py_ssize_t result = any_find_slice(str, substr, start, end, 1);
11932
17.0M
    if (result < 0) {
11933
234k
        return -1;
11934
234k
    }
11935
16.8M
    return result;
11936
17.0M
}
11937
11938
static PyObject *
11939
unicode_getitem(PyObject *self, Py_ssize_t index)
11940
57.8M
{
11941
57.8M
    const void *data;
11942
57.8M
    int kind;
11943
57.8M
    Py_UCS4 ch;
11944
11945
57.8M
    if (!PyUnicode_Check(self)) {
11946
0
        PyErr_BadArgument();
11947
0
        return NULL;
11948
0
    }
11949
57.8M
    if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) {
11950
369
        PyErr_SetString(PyExc_IndexError, "string index out of range");
11951
369
        return NULL;
11952
369
    }
11953
57.8M
    kind = PyUnicode_KIND(self);
11954
57.8M
    data = PyUnicode_DATA(self);
11955
57.8M
    ch = PyUnicode_READ(kind, data, index);
11956
57.8M
    return unicode_char(ch);
11957
57.8M
}
11958
11959
/* Believe it or not, this produces the same value for ASCII strings
11960
   as bytes_hash(). */
11961
static Py_hash_t
11962
unicode_hash(PyObject *self)
11963
43.8M
{
11964
43.8M
    Py_uhash_t x;  /* Unsigned for defined overflow behavior. */
11965
11966
#ifdef Py_DEBUG
11967
    assert(_Py_HashSecret_Initialized);
11968
#endif
11969
43.8M
    Py_hash_t hash = PyUnicode_HASH(self);
11970
43.8M
    if (hash != -1) {
11971
242k
        return hash;
11972
242k
    }
11973
43.5M
    x = Py_HashBuffer(PyUnicode_DATA(self),
11974
43.5M
                      PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self));
11975
11976
43.5M
    PyUnicode_SET_HASH(self, x);
11977
43.5M
    return x;
11978
43.8M
}
11979
11980
/*[clinic input]
11981
@permit_long_summary
11982
str.index as unicode_index = str.count
11983
11984
Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end].
11985
11986
Optional arguments start and end are interpreted as in slice notation.
11987
Raises ValueError when the substring is not found.
11988
[clinic start generated code]*/
11989
11990
static Py_ssize_t
11991
unicode_index_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
11992
                   Py_ssize_t end)
11993
/*[clinic end generated code: output=77558288837cdf40 input=ae5e48f69ed75b06]*/
11994
560k
{
11995
560k
    Py_ssize_t result = any_find_slice(str, substr, start, end, 1);
11996
560k
    if (result == -1) {
11997
0
        PyErr_SetString(PyExc_ValueError, "substring not found");
11998
0
    }
11999
560k
    else if (result < 0) {
12000
0
        return -1;
12001
0
    }
12002
560k
    return result;
12003
560k
}
12004
12005
/*[clinic input]
12006
str.isascii as unicode_isascii
12007
12008
Return True if all characters in the string are ASCII, False otherwise.
12009
12010
ASCII characters have code points in the range U+0000-U+007F.
12011
Empty string is ASCII too.
12012
[clinic start generated code]*/
12013
12014
static PyObject *
12015
unicode_isascii_impl(PyObject *self)
12016
/*[clinic end generated code: output=c5910d64b5a8003f input=5a43cbc6399621d5]*/
12017
10.8M
{
12018
10.8M
    return PyBool_FromLong(PyUnicode_IS_ASCII(self));
12019
10.8M
}
12020
12021
/*[clinic input]
12022
@permit_long_docstring_body
12023
str.islower as unicode_islower
12024
12025
Return True if the string is a lowercase string, False otherwise.
12026
12027
A string is lowercase if all cased characters in the string are lowercase and
12028
there is at least one cased character in the string.
12029
[clinic start generated code]*/
12030
12031
static PyObject *
12032
unicode_islower_impl(PyObject *self)
12033
/*[clinic end generated code: output=dbd41995bd005b81 input=c6fc0295241a1aaa]*/
12034
0
{
12035
0
    Py_ssize_t i, length;
12036
0
    int kind;
12037
0
    const void *data;
12038
0
    int cased;
12039
12040
0
    length = PyUnicode_GET_LENGTH(self);
12041
0
    kind = PyUnicode_KIND(self);
12042
0
    data = PyUnicode_DATA(self);
12043
12044
    /* Shortcut for single character strings */
12045
0
    if (length == 1)
12046
0
        return PyBool_FromLong(
12047
0
            Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0)));
12048
12049
    /* Special case for empty strings */
12050
0
    if (length == 0)
12051
0
        Py_RETURN_FALSE;
12052
12053
0
    cased = 0;
12054
0
    for (i = 0; i < length; i++) {
12055
0
        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
12056
12057
0
        if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch))
12058
0
            Py_RETURN_FALSE;
12059
0
        else if (!cased && Py_UNICODE_ISLOWER(ch))
12060
0
            cased = 1;
12061
0
    }
12062
0
    return PyBool_FromLong(cased);
12063
0
}
12064
12065
/*[clinic input]
12066
@permit_long_docstring_body
12067
str.isupper as unicode_isupper
12068
12069
Return True if the string is an uppercase string, False otherwise.
12070
12071
A string is uppercase if all cased characters in the string are uppercase and
12072
there is at least one cased character in the string.
12073
[clinic start generated code]*/
12074
12075
static PyObject *
12076
unicode_isupper_impl(PyObject *self)
12077
/*[clinic end generated code: output=049209c8e7f15f59 input=8d5cb33e67efde72]*/
12078
6.98k
{
12079
6.98k
    Py_ssize_t i, length;
12080
6.98k
    int kind;
12081
6.98k
    const void *data;
12082
6.98k
    int cased;
12083
12084
6.98k
    length = PyUnicode_GET_LENGTH(self);
12085
6.98k
    kind = PyUnicode_KIND(self);
12086
6.98k
    data = PyUnicode_DATA(self);
12087
12088
    /* Shortcut for single character strings */
12089
6.98k
    if (length == 1)
12090
0
        return PyBool_FromLong(
12091
0
            Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0);
12092
12093
    /* Special case for empty strings */
12094
6.98k
    if (length == 0)
12095
0
        Py_RETURN_FALSE;
12096
12097
6.98k
    cased = 0;
12098
89.1k
    for (i = 0; i < length; i++) {
12099
83.0k
        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
12100
12101
83.0k
        if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch))
12102
840
            Py_RETURN_FALSE;
12103
82.1k
        else if (!cased && Py_UNICODE_ISUPPER(ch))
12104
6.23k
            cased = 1;
12105
83.0k
    }
12106
6.14k
    return PyBool_FromLong(cased);
12107
6.98k
}
12108
12109
/*[clinic input]
12110
str.istitle as unicode_istitle
12111
12112
Return True if the string is a title-cased string, False otherwise.
12113
12114
In a title-cased string, upper- and title-case characters may only
12115
follow uncased characters and lowercase characters only cased ones.
12116
[clinic start generated code]*/
12117
12118
static PyObject *
12119
unicode_istitle_impl(PyObject *self)
12120
/*[clinic end generated code: output=e9bf6eb91f5d3f0e input=98d32bd2e1f06f8c]*/
12121
0
{
12122
0
    Py_ssize_t i, length;
12123
0
    int kind;
12124
0
    const void *data;
12125
0
    int cased, previous_is_cased;
12126
12127
0
    length = PyUnicode_GET_LENGTH(self);
12128
0
    kind = PyUnicode_KIND(self);
12129
0
    data = PyUnicode_DATA(self);
12130
12131
    /* Shortcut for single character strings */
12132
0
    if (length == 1) {
12133
0
        Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
12134
0
        return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) ||
12135
0
                               (Py_UNICODE_ISUPPER(ch) != 0));
12136
0
    }
12137
12138
    /* Special case for empty strings */
12139
0
    if (length == 0)
12140
0
        Py_RETURN_FALSE;
12141
12142
0
    cased = 0;
12143
0
    previous_is_cased = 0;
12144
0
    for (i = 0; i < length; i++) {
12145
0
        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
12146
12147
0
        if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) {
12148
0
            if (previous_is_cased)
12149
0
                Py_RETURN_FALSE;
12150
0
            previous_is_cased = 1;
12151
0
            cased = 1;
12152
0
        }
12153
0
        else if (Py_UNICODE_ISLOWER(ch)) {
12154
0
            if (!previous_is_cased)
12155
0
                Py_RETURN_FALSE;
12156
0
            previous_is_cased = 1;
12157
0
            cased = 1;
12158
0
        }
12159
0
        else
12160
0
            previous_is_cased = 0;
12161
0
    }
12162
0
    return PyBool_FromLong(cased);
12163
0
}
12164
12165
/*[clinic input]
12166
@permit_long_docstring_body
12167
str.isspace as unicode_isspace
12168
12169
Return True if the string is a whitespace string, False otherwise.
12170
12171
A string is whitespace if all characters in the string are whitespace and there
12172
is at least one character in the string.
12173
[clinic start generated code]*/
12174
12175
static PyObject *
12176
unicode_isspace_impl(PyObject *self)
12177
/*[clinic end generated code: output=163a63bfa08ac2b9 input=44fe05e248c6e159]*/
12178
25.4M
{
12179
25.4M
    Py_ssize_t i, length;
12180
25.4M
    int kind;
12181
25.4M
    const void *data;
12182
12183
25.4M
    length = PyUnicode_GET_LENGTH(self);
12184
25.4M
    kind = PyUnicode_KIND(self);
12185
25.4M
    data = PyUnicode_DATA(self);
12186
12187
    /* Shortcut for single character strings */
12188
25.4M
    if (length == 1)
12189
25.4M
        return PyBool_FromLong(
12190
25.4M
            Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0)));
12191
12192
    /* Special case for empty strings */
12193
0
    if (length == 0)
12194
0
        Py_RETURN_FALSE;
12195
12196
0
    for (i = 0; i < length; i++) {
12197
0
        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
12198
0
        if (!Py_UNICODE_ISSPACE(ch))
12199
0
            Py_RETURN_FALSE;
12200
0
    }
12201
0
    Py_RETURN_TRUE;
12202
0
}
12203
12204
/*[clinic input]
12205
@permit_long_docstring_body
12206
str.isalpha as unicode_isalpha
12207
12208
Return True if the string is an alphabetic string, False otherwise.
12209
12210
A string is alphabetic if all characters in the string are alphabetic and there
12211
is at least one character in the string.
12212
[clinic start generated code]*/
12213
12214
static PyObject *
12215
unicode_isalpha_impl(PyObject *self)
12216
/*[clinic end generated code: output=cc81b9ac3883ec4f input=c233000624a56e0d]*/
12217
0
{
12218
0
    Py_ssize_t i, length;
12219
0
    int kind;
12220
0
    const void *data;
12221
12222
0
    length = PyUnicode_GET_LENGTH(self);
12223
0
    kind = PyUnicode_KIND(self);
12224
0
    data = PyUnicode_DATA(self);
12225
12226
    /* Shortcut for single character strings */
12227
0
    if (length == 1)
12228
0
        return PyBool_FromLong(
12229
0
            Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0)));
12230
12231
    /* Special case for empty strings */
12232
0
    if (length == 0)
12233
0
        Py_RETURN_FALSE;
12234
12235
0
    for (i = 0; i < length; i++) {
12236
0
        if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i)))
12237
0
            Py_RETURN_FALSE;
12238
0
    }
12239
0
    Py_RETURN_TRUE;
12240
0
}
12241
12242
/*[clinic input]
12243
@permit_long_docstring_body
12244
str.isalnum as unicode_isalnum
12245
12246
Return True if the string is an alpha-numeric string, False otherwise.
12247
12248
A string is alpha-numeric if all characters in the string are alpha-numeric and
12249
there is at least one character in the string.
12250
[clinic start generated code]*/
12251
12252
static PyObject *
12253
unicode_isalnum_impl(PyObject *self)
12254
/*[clinic end generated code: output=a5a23490ffc3660c input=5d63ba9c9bafdb6b]*/
12255
11.1M
{
12256
11.1M
    int kind;
12257
11.1M
    const void *data;
12258
11.1M
    Py_ssize_t len, i;
12259
12260
11.1M
    kind = PyUnicode_KIND(self);
12261
11.1M
    data = PyUnicode_DATA(self);
12262
11.1M
    len = PyUnicode_GET_LENGTH(self);
12263
12264
    /* Shortcut for single character strings */
12265
11.1M
    if (len == 1) {
12266
11.1M
        const Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
12267
11.1M
        return PyBool_FromLong(Py_UNICODE_ISALNUM(ch));
12268
11.1M
    }
12269
12270
    /* Special case for empty strings */
12271
0
    if (len == 0)
12272
0
        Py_RETURN_FALSE;
12273
12274
0
    for (i = 0; i < len; i++) {
12275
0
        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
12276
0
        if (!Py_UNICODE_ISALNUM(ch))
12277
0
            Py_RETURN_FALSE;
12278
0
    }
12279
0
    Py_RETURN_TRUE;
12280
0
}
12281
12282
/*[clinic input]
12283
@permit_long_docstring_body
12284
str.isdecimal as unicode_isdecimal
12285
12286
Return True if the string is a decimal string, False otherwise.
12287
12288
A string is a decimal string if all characters in the string are decimal and
12289
there is at least one character in the string.
12290
[clinic start generated code]*/
12291
12292
static PyObject *
12293
unicode_isdecimal_impl(PyObject *self)
12294
/*[clinic end generated code: output=fb2dcdb62d3fc548 input=8e84a58b414935a3]*/
12295
0
{
12296
0
    Py_ssize_t i, length;
12297
0
    int kind;
12298
0
    const void *data;
12299
12300
0
    length = PyUnicode_GET_LENGTH(self);
12301
0
    kind = PyUnicode_KIND(self);
12302
0
    data = PyUnicode_DATA(self);
12303
12304
    /* Shortcut for single character strings */
12305
0
    if (length == 1)
12306
0
        return PyBool_FromLong(
12307
0
            Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0)));
12308
12309
    /* Special case for empty strings */
12310
0
    if (length == 0)
12311
0
        Py_RETURN_FALSE;
12312
12313
0
    for (i = 0; i < length; i++) {
12314
0
        if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i)))
12315
0
            Py_RETURN_FALSE;
12316
0
    }
12317
0
    Py_RETURN_TRUE;
12318
0
}
12319
12320
/*[clinic input]
12321
@permit_long_docstring_body
12322
str.isdigit as unicode_isdigit
12323
12324
Return True if the string is a digit string, False otherwise.
12325
12326
A string is a digit string if all characters in the string are digits and there
12327
is at least one character in the string.
12328
[clinic start generated code]*/
12329
12330
static PyObject *
12331
unicode_isdigit_impl(PyObject *self)
12332
/*[clinic end generated code: output=10a6985311da6858 input=99e284affb54d4a0]*/
12333
1.69M
{
12334
1.69M
    Py_ssize_t i, length;
12335
1.69M
    int kind;
12336
1.69M
    const void *data;
12337
12338
1.69M
    length = PyUnicode_GET_LENGTH(self);
12339
1.69M
    kind = PyUnicode_KIND(self);
12340
1.69M
    data = PyUnicode_DATA(self);
12341
12342
    /* Shortcut for single character strings */
12343
1.69M
    if (length == 1) {
12344
1.69M
        const Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
12345
1.69M
        return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch));
12346
1.69M
    }
12347
12348
    /* Special case for empty strings */
12349
306
    if (length == 0)
12350
0
        Py_RETURN_FALSE;
12351
12352
1.09k
    for (i = 0; i < length; i++) {
12353
786
        if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i)))
12354
0
            Py_RETURN_FALSE;
12355
786
    }
12356
306
    Py_RETURN_TRUE;
12357
306
}
12358
12359
/*[clinic input]
12360
@permit_long_docstring_body
12361
str.isnumeric as unicode_isnumeric
12362
12363
Return True if the string is a numeric string, False otherwise.
12364
12365
A string is numeric if all characters in the string are numeric and there is at
12366
least one character in the string.
12367
[clinic start generated code]*/
12368
12369
static PyObject *
12370
unicode_isnumeric_impl(PyObject *self)
12371
/*[clinic end generated code: output=9172a32d9013051a input=e9f5b6b8b29b0ee6]*/
12372
0
{
12373
0
    Py_ssize_t i, length;
12374
0
    int kind;
12375
0
    const void *data;
12376
12377
0
    length = PyUnicode_GET_LENGTH(self);
12378
0
    kind = PyUnicode_KIND(self);
12379
0
    data = PyUnicode_DATA(self);
12380
12381
    /* Shortcut for single character strings */
12382
0
    if (length == 1)
12383
0
        return PyBool_FromLong(
12384
0
            Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0)));
12385
12386
    /* Special case for empty strings */
12387
0
    if (length == 0)
12388
0
        Py_RETURN_FALSE;
12389
12390
0
    for (i = 0; i < length; i++) {
12391
0
        if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i)))
12392
0
            Py_RETURN_FALSE;
12393
0
    }
12394
0
    Py_RETURN_TRUE;
12395
0
}
12396
12397
Py_ssize_t
12398
_PyUnicode_ScanIdentifier(PyObject *self)
12399
13.9k
{
12400
13.9k
    Py_ssize_t i;
12401
13.9k
    Py_ssize_t len = PyUnicode_GET_LENGTH(self);
12402
13.9k
    if (len == 0) {
12403
        /* an empty string is not a valid identifier */
12404
0
        return 0;
12405
0
    }
12406
12407
13.9k
    int kind = PyUnicode_KIND(self);
12408
13.9k
    const void *data = PyUnicode_DATA(self);
12409
13.9k
    Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
12410
    /* PEP 3131 says that the first character must be in
12411
       XID_Start and subsequent characters in XID_Continue,
12412
       and for the ASCII range, the 2.x rules apply (i.e
12413
       start with letters and underscore, continue with
12414
       letters, digits, underscore). However, given the current
12415
       definition of XID_Start and XID_Continue, it is sufficient
12416
       to check just for these, except that _ must be allowed
12417
       as starting an identifier.  */
12418
13.9k
    if (!_PyUnicode_IsXidStart(ch) && ch != 0x5F /* LOW LINE */) {
12419
493
        return 0;
12420
493
    }
12421
12422
53.1k
    for (i = 1; i < len; i++) {
12423
39.8k
        ch = PyUnicode_READ(kind, data, i);
12424
39.8k
        if (!_PyUnicode_IsXidContinue(ch)) {
12425
214
            return i;
12426
214
        }
12427
39.8k
    }
12428
13.2k
    return i;
12429
13.4k
}
12430
12431
int
12432
PyUnicode_IsIdentifier(PyObject *self)
12433
956
{
12434
956
    Py_ssize_t i = _PyUnicode_ScanIdentifier(self);
12435
956
    Py_ssize_t len = PyUnicode_GET_LENGTH(self);
12436
    /* an empty string is not a valid identifier */
12437
956
    return len && i == len;
12438
956
}
12439
12440
/*[clinic input]
12441
@permit_long_docstring_body
12442
str.isidentifier as unicode_isidentifier
12443
12444
Return True if the string is a valid Python identifier, False otherwise.
12445
12446
Call keyword.iskeyword(s) to test whether string s is a reserved identifier,
12447
such as "def" or "class".
12448
[clinic start generated code]*/
12449
12450
static PyObject *
12451
unicode_isidentifier_impl(PyObject *self)
12452
/*[clinic end generated code: output=fe585a9666572905 input=86315dd889d7bd04]*/
12453
494
{
12454
494
    return PyBool_FromLong(PyUnicode_IsIdentifier(self));
12455
494
}
12456
12457
/*[clinic input]
12458
@permit_long_summary
12459
str.isprintable as unicode_isprintable
12460
12461
Return True if all characters in the string are printable, False otherwise.
12462
12463
A character is printable if repr() may use it in its output.
12464
[clinic start generated code]*/
12465
12466
static PyObject *
12467
unicode_isprintable_impl(PyObject *self)
12468
/*[clinic end generated code: output=3ab9626cd32dd1a0 input=18345ba847084ec5]*/
12469
1.98M
{
12470
1.98M
    Py_ssize_t i, length;
12471
1.98M
    int kind;
12472
1.98M
    const void *data;
12473
12474
1.98M
    length = PyUnicode_GET_LENGTH(self);
12475
1.98M
    kind = PyUnicode_KIND(self);
12476
1.98M
    data = PyUnicode_DATA(self);
12477
12478
    /* Shortcut for single character strings */
12479
1.98M
    if (length == 1)
12480
1.98M
        return PyBool_FromLong(
12481
1.98M
            Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0)));
12482
12483
0
    for (i = 0; i < length; i++) {
12484
0
        if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) {
12485
0
            Py_RETURN_FALSE;
12486
0
        }
12487
0
    }
12488
0
    Py_RETURN_TRUE;
12489
0
}
12490
12491
/*[clinic input]
12492
@permit_long_docstring_body
12493
str.join as unicode_join
12494
12495
    iterable: object
12496
    /
12497
12498
Concatenate any number of strings.
12499
12500
The string whose method is called is inserted in between each given string.
12501
The result is returned as a new string.
12502
12503
Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
12504
[clinic start generated code]*/
12505
12506
static PyObject *
12507
unicode_join(PyObject *self, PyObject *iterable)
12508
/*[clinic end generated code: output=6857e7cecfe7bf98 input=bac724ed412ef3f8]*/
12509
21.1M
{
12510
21.1M
    return PyUnicode_Join(self, iterable);
12511
21.1M
}
12512
12513
static Py_ssize_t
12514
unicode_length(PyObject *self)
12515
40.4M
{
12516
40.4M
    return PyUnicode_GET_LENGTH(self);
12517
40.4M
}
12518
12519
/*[clinic input]
12520
str.ljust as unicode_ljust
12521
12522
    width: Py_ssize_t
12523
    fillchar: Py_UCS4 = ' '
12524
    /
12525
12526
Return a left-justified string of length width.
12527
12528
Padding is done using the specified fill character (default is a space).
12529
[clinic start generated code]*/
12530
12531
static PyObject *
12532
unicode_ljust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar)
12533
/*[clinic end generated code: output=1cce0e0e0a0b84b3 input=3ab599e335e60a32]*/
12534
0
{
12535
0
    if (PyUnicode_GET_LENGTH(self) >= width)
12536
0
        return unicode_result_unchanged(self);
12537
12538
0
    return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar);
12539
0
}
12540
12541
/*[clinic input]
12542
str.lower as unicode_lower
12543
12544
Return a copy of the string converted to lowercase.
12545
[clinic start generated code]*/
12546
12547
static PyObject *
12548
unicode_lower_impl(PyObject *self)
12549
/*[clinic end generated code: output=84ef9ed42efad663 input=60a2984b8beff23a]*/
12550
100M
{
12551
100M
    if (PyUnicode_IS_ASCII(self))
12552
74.0M
        return ascii_upper_or_lower(self, 1);
12553
26.5M
    return case_operation(self, do_lower);
12554
100M
}
12555
12556
62.8M
#define LEFTSTRIP 0
12557
82.5M
#define RIGHTSTRIP 1
12558
37.9M
#define BOTHSTRIP 2
12559
12560
/* Arrays indexed by above */
12561
static const char *stripfuncnames[] = {"lstrip", "rstrip", "strip"};
12562
12563
0
#define STRIPNAME(i) (stripfuncnames[i])
12564
12565
/* externally visible for str.strip(unicode) */
12566
PyObject *
12567
_PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj)
12568
8.95M
{
12569
8.95M
    const void *data;
12570
8.95M
    int kind;
12571
8.95M
    Py_ssize_t i, j, len;
12572
8.95M
    BLOOM_MASK sepmask;
12573
8.95M
    Py_ssize_t seplen;
12574
12575
8.95M
    kind = PyUnicode_KIND(self);
12576
8.95M
    data = PyUnicode_DATA(self);
12577
8.95M
    len = PyUnicode_GET_LENGTH(self);
12578
8.95M
    seplen = PyUnicode_GET_LENGTH(sepobj);
12579
8.95M
    sepmask = make_bloom_mask(PyUnicode_KIND(sepobj),
12580
8.95M
                              PyUnicode_DATA(sepobj),
12581
8.95M
                              seplen);
12582
12583
8.95M
    i = 0;
12584
8.95M
    if (striptype != RIGHTSTRIP) {
12585
485k
        while (i < len) {
12586
483k
            Py_UCS4 ch = PyUnicode_READ(kind, data, i);
12587
483k
            if (!BLOOM(sepmask, ch))
12588
448k
                break;
12589
34.8k
            if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0)
12590
1.79k
                break;
12591
33.0k
            i++;
12592
33.0k
        }
12593
452k
    }
12594
12595
8.95M
    j = len;
12596
8.95M
    if (striptype != LEFTSTRIP) {
12597
8.50M
        j--;
12598
8.95M
        while (j >= i) {
12599
3.34M
            Py_UCS4 ch = PyUnicode_READ(kind, data, j);
12600
3.34M
            if (!BLOOM(sepmask, ch))
12601
2.87M
                break;
12602
473k
            if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0)
12603
23.6k
                break;
12604
449k
            j--;
12605
449k
        }
12606
12607
8.50M
        j++;
12608
8.50M
    }
12609
12610
8.95M
    return PyUnicode_Substring(self, i, j);
12611
8.95M
}
12612
12613
PyObject*
12614
PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end)
12615
284M
{
12616
284M
    const unsigned char *data;
12617
284M
    int kind;
12618
284M
    Py_ssize_t length;
12619
12620
284M
    length = PyUnicode_GET_LENGTH(self);
12621
284M
    end = Py_MIN(end, length);
12622
12623
284M
    if (start == 0 && end == length)
12624
54.4M
        return unicode_result_unchanged(self);
12625
12626
230M
    if (start < 0 || end < 0) {
12627
0
        PyErr_SetString(PyExc_IndexError, "string index out of range");
12628
0
        return NULL;
12629
0
    }
12630
230M
    if (start >= length || end < start)
12631
208k
        _Py_RETURN_UNICODE_EMPTY();
12632
12633
230M
    length = end - start;
12634
230M
    if (PyUnicode_IS_ASCII(self)) {
12635
52.7M
        data = PyUnicode_1BYTE_DATA(self);
12636
52.7M
        return _PyUnicode_FromASCII((const char*)(data + start), length);
12637
52.7M
    }
12638
177M
    else {
12639
177M
        kind = PyUnicode_KIND(self);
12640
177M
        data = PyUnicode_1BYTE_DATA(self);
12641
177M
        return PyUnicode_FromKindAndData(kind,
12642
177M
                                         data + kind * start,
12643
177M
                                         length);
12644
177M
    }
12645
230M
}
12646
12647
static PyObject *
12648
do_strip(PyObject *self, int striptype)
12649
52.1M
{
12650
52.1M
    Py_ssize_t len, i, j;
12651
12652
52.1M
    len = PyUnicode_GET_LENGTH(self);
12653
12654
52.1M
    if (PyUnicode_IS_ASCII(self)) {
12655
43.5M
        const Py_UCS1 *data = PyUnicode_1BYTE_DATA(self);
12656
12657
43.5M
        i = 0;
12658
43.5M
        if (striptype != RIGHTSTRIP) {
12659
32.3M
            while (i < len) {
12660
19.1M
                Py_UCS1 ch = data[i];
12661
19.1M
                if (!_Py_ascii_whitespace[ch])
12662
18.6M
                    break;
12663
480k
                i++;
12664
480k
            }
12665
31.9M
        }
12666
12667
43.5M
        j = len;
12668
43.5M
        if (striptype != LEFTSTRIP) {
12669
43.1M
            j--;
12670
55.7M
            while (j >= i) {
12671
36.0M
                Py_UCS1 ch = data[j];
12672
36.0M
                if (!_Py_ascii_whitespace[ch])
12673
23.4M
                    break;
12674
12.6M
                j--;
12675
12.6M
            }
12676
43.1M
            j++;
12677
43.1M
        }
12678
43.5M
    }
12679
8.60M
    else {
12680
8.60M
        int kind = PyUnicode_KIND(self);
12681
8.60M
        const void *data = PyUnicode_DATA(self);
12682
12683
8.60M
        i = 0;
12684
8.60M
        if (striptype != RIGHTSTRIP) {
12685
8.78M
            while (i < len) {
12686
8.77M
                Py_UCS4 ch = PyUnicode_READ(kind, data, i);
12687
8.77M
                if (!Py_UNICODE_ISSPACE(ch))
12688
7.26M
                    break;
12689
1.51M
                i++;
12690
1.51M
            }
12691
7.27M
        }
12692
12693
8.60M
        j = len;
12694
8.60M
        if (striptype != LEFTSTRIP) {
12695
7.69M
            j--;
12696
8.61M
            while (j >= i) {
12697
8.57M
                Py_UCS4 ch = PyUnicode_READ(kind, data, j);
12698
8.57M
                if (!Py_UNICODE_ISSPACE(ch))
12699
7.65M
                    break;
12700
918k
                j--;
12701
918k
            }
12702
7.69M
            j++;
12703
7.69M
        }
12704
8.60M
    }
12705
12706
52.1M
    return PyUnicode_Substring(self, i, j);
12707
52.1M
}
12708
12709
12710
static PyObject *
12711
do_argstrip(PyObject *self, int striptype, PyObject *sep)
12712
61.1M
{
12713
61.1M
    if (sep != Py_None) {
12714
8.95M
        if (PyUnicode_Check(sep))
12715
8.95M
            return _PyUnicode_XStrip(self, striptype, sep);
12716
0
        else {
12717
0
            PyErr_Format(PyExc_TypeError,
12718
0
                         "%s arg must be None or str",
12719
0
                         STRIPNAME(striptype));
12720
0
            return NULL;
12721
0
        }
12722
8.95M
    }
12723
12724
52.1M
    return do_strip(self, striptype);
12725
61.1M
}
12726
12727
12728
/*[clinic input]
12729
@permit_long_summary
12730
str.strip as unicode_strip
12731
12732
    chars: object = None
12733
    /
12734
12735
Return a copy of the string with leading and trailing whitespace removed.
12736
12737
If chars is given and not None, remove characters in chars instead.
12738
[clinic start generated code]*/
12739
12740
static PyObject *
12741
unicode_strip_impl(PyObject *self, PyObject *chars)
12742
/*[clinic end generated code: output=ca19018454345d57 input=8bc6353450345fbd]*/
12743
37.9M
{
12744
37.9M
    return do_argstrip(self, BOTHSTRIP, chars);
12745
37.9M
}
12746
12747
12748
/*[clinic input]
12749
str.lstrip as unicode_lstrip
12750
12751
    chars: object = None
12752
    /
12753
12754
Return a copy of the string with leading whitespace removed.
12755
12756
If chars is given and not None, remove characters in chars instead.
12757
[clinic start generated code]*/
12758
12759
static PyObject *
12760
unicode_lstrip_impl(PyObject *self, PyObject *chars)
12761
/*[clinic end generated code: output=3b43683251f79ca7 input=529f9f3834448671]*/
12762
1.70M
{
12763
1.70M
    return do_argstrip(self, LEFTSTRIP, chars);
12764
1.70M
}
12765
12766
12767
/*[clinic input]
12768
str.rstrip as unicode_rstrip
12769
12770
    chars: object = None
12771
    /
12772
12773
Return a copy of the string with trailing whitespace removed.
12774
12775
If chars is given and not None, remove characters in chars instead.
12776
[clinic start generated code]*/
12777
12778
static PyObject *
12779
unicode_rstrip_impl(PyObject *self, PyObject *chars)
12780
/*[clinic end generated code: output=4a59230017cc3b7a input=62566c627916557f]*/
12781
21.4M
{
12782
21.4M
    return do_argstrip(self, RIGHTSTRIP, chars);
12783
21.4M
}
12784
12785
12786
static PyObject*
12787
unicode_repeat(PyObject *str, Py_ssize_t len)
12788
444k
{
12789
444k
    PyObject *u;
12790
444k
    Py_ssize_t nchars, n;
12791
12792
444k
    if (len < 1)
12793
37.9k
        _Py_RETURN_UNICODE_EMPTY();
12794
12795
    /* no repeat, return original string */
12796
406k
    if (len == 1)
12797
131k
        return unicode_result_unchanged(str);
12798
12799
274k
    if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) {
12800
0
        PyErr_SetString(PyExc_OverflowError,
12801
0
                        "repeated string is too long");
12802
0
        return NULL;
12803
0
    }
12804
274k
    nchars = len * PyUnicode_GET_LENGTH(str);
12805
12806
274k
    u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str));
12807
274k
    if (!u)
12808
0
        return NULL;
12809
274k
    assert(PyUnicode_KIND(u) == PyUnicode_KIND(str));
12810
12811
274k
    if (PyUnicode_GET_LENGTH(str) == 1) {
12812
271k
        int kind = PyUnicode_KIND(str);
12813
271k
        Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0);
12814
271k
        if (kind == PyUnicode_1BYTE_KIND) {
12815
271k
            void *to = PyUnicode_DATA(u);
12816
271k
            memset(to, (unsigned char)fill_char, len);
12817
271k
        }
12818
0
        else if (kind == PyUnicode_2BYTE_KIND) {
12819
0
            Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u);
12820
0
            for (n = 0; n < len; ++n)
12821
0
                ucs2[n] = fill_char;
12822
0
        } else {
12823
0
            Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u);
12824
0
            assert(kind == PyUnicode_4BYTE_KIND);
12825
0
            for (n = 0; n < len; ++n)
12826
0
                ucs4[n] = fill_char;
12827
0
        }
12828
271k
    }
12829
2.99k
    else {
12830
2.99k
        Py_ssize_t char_size = PyUnicode_KIND(str);
12831
2.99k
        char *to = (char *) PyUnicode_DATA(u);
12832
2.99k
        _PyBytes_Repeat(to, nchars * char_size, PyUnicode_DATA(str),
12833
2.99k
            PyUnicode_GET_LENGTH(str) * char_size);
12834
2.99k
    }
12835
12836
274k
    assert(_PyUnicode_CheckConsistency(u, 1));
12837
274k
    return u;
12838
274k
}
12839
12840
PyObject *
12841
PyUnicode_Replace(PyObject *str,
12842
                  PyObject *substr,
12843
                  PyObject *replstr,
12844
                  Py_ssize_t maxcount)
12845
2
{
12846
2
    if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0 ||
12847
2
            ensure_unicode(replstr) < 0)
12848
0
        return NULL;
12849
2
    return replace(str, substr, replstr, maxcount);
12850
2
}
12851
12852
/*[clinic input]
12853
@permit_long_docstring_body
12854
str.replace as unicode_replace
12855
12856
    old: unicode
12857
    new: unicode
12858
    /
12859
    count: Py_ssize_t = -1
12860
        Maximum number of occurrences to replace.
12861
        -1 (the default value) means replace all occurrences.
12862
12863
Return a copy with all occurrences of substring old replaced by new.
12864
12865
If the optional argument count is given, only the first count occurrences are
12866
replaced.
12867
[clinic start generated code]*/
12868
12869
static PyObject *
12870
unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
12871
                     Py_ssize_t count)
12872
/*[clinic end generated code: output=b63f1a8b5eebf448 input=f27ca92ac46b65a1]*/
12873
89.2M
{
12874
89.2M
    return replace(self, old, new, count);
12875
89.2M
}
12876
12877
/*[clinic input]
12878
@permit_long_docstring_body
12879
str.removeprefix as unicode_removeprefix
12880
12881
    prefix: unicode
12882
    /
12883
12884
Return a str with the given prefix string removed if present.
12885
12886
If the string starts with the prefix string, return string[len(prefix):].
12887
Otherwise, return a copy of the original string.
12888
[clinic start generated code]*/
12889
12890
static PyObject *
12891
unicode_removeprefix_impl(PyObject *self, PyObject *prefix)
12892
/*[clinic end generated code: output=f1e5945e9763bcb9 input=1989a856dbb813f1]*/
12893
0
{
12894
0
    int match = tailmatch(self, prefix, 0, PY_SSIZE_T_MAX, -1);
12895
0
    if (match == -1) {
12896
0
        return NULL;
12897
0
    }
12898
0
    if (match) {
12899
0
        return PyUnicode_Substring(self, PyUnicode_GET_LENGTH(prefix),
12900
0
                                   PyUnicode_GET_LENGTH(self));
12901
0
    }
12902
0
    return unicode_result_unchanged(self);
12903
0
}
12904
12905
/*[clinic input]
12906
str.removesuffix as unicode_removesuffix
12907
12908
    suffix: unicode
12909
    /
12910
12911
Return a str with the given suffix string removed if present.
12912
12913
If the string ends with the suffix string and that suffix is not empty,
12914
return string[:-len(suffix)]. Otherwise, return a copy of the original
12915
string.
12916
[clinic start generated code]*/
12917
12918
static PyObject *
12919
unicode_removesuffix_impl(PyObject *self, PyObject *suffix)
12920
/*[clinic end generated code: output=d36629e227636822 input=12cc32561e769be4]*/
12921
0
{
12922
0
    int match = tailmatch(self, suffix, 0, PY_SSIZE_T_MAX, +1);
12923
0
    if (match == -1) {
12924
0
        return NULL;
12925
0
    }
12926
0
    if (match) {
12927
0
        return PyUnicode_Substring(self, 0, PyUnicode_GET_LENGTH(self)
12928
0
                                            - PyUnicode_GET_LENGTH(suffix));
12929
0
    }
12930
0
    return unicode_result_unchanged(self);
12931
0
}
12932
12933
static PyObject *
12934
unicode_repr(PyObject *unicode)
12935
4.03M
{
12936
4.03M
    Py_ssize_t isize = PyUnicode_GET_LENGTH(unicode);
12937
4.03M
    const void *idata = PyUnicode_DATA(unicode);
12938
12939
    /* Compute length of output, quote characters, and
12940
       maximum character */
12941
4.03M
    Py_ssize_t osize = 0;
12942
4.03M
    Py_UCS4 maxch = 127;
12943
4.03M
    Py_ssize_t squote = 0;
12944
4.03M
    Py_ssize_t dquote = 0;
12945
4.03M
    int ikind = PyUnicode_KIND(unicode);
12946
145M
    for (Py_ssize_t i = 0; i < isize; i++) {
12947
141M
        Py_UCS4 ch = PyUnicode_READ(ikind, idata, i);
12948
141M
        Py_ssize_t incr = 1;
12949
141M
        switch (ch) {
12950
218k
        case '\'': squote++; break;
12951
580k
        case '"':  dquote++; break;
12952
129k
        case '\\': case '\t': case '\r': case '\n':
12953
129k
            incr = 2;
12954
129k
            break;
12955
140M
        default:
12956
            /* Fast-path ASCII */
12957
140M
            if (ch < ' ' || ch == 0x7f)
12958
82.5M
                incr = 4; /* \xHH */
12959
57.8M
            else if (ch < 0x7f)
12960
49.0M
                ;
12961
8.81M
            else if (Py_UNICODE_ISPRINTABLE(ch))
12962
8.72M
                maxch = (ch > maxch) ? ch : maxch;
12963
96.6k
            else if (ch < 0x100)
12964
26.4k
                incr = 4; /* \xHH */
12965
70.1k
            else if (ch < 0x10000)
12966
45.8k
                incr = 6; /* \uHHHH */
12967
24.3k
            else
12968
24.3k
                incr = 10; /* \uHHHHHHHH */
12969
141M
        }
12970
141M
        if (osize > PY_SSIZE_T_MAX - incr) {
12971
0
            PyErr_SetString(PyExc_OverflowError,
12972
0
                            "string is too long to generate repr");
12973
0
            return NULL;
12974
0
        }
12975
141M
        osize += incr;
12976
141M
    }
12977
12978
4.03M
    Py_UCS4 quote = '\'';
12979
4.03M
    int changed = (osize != isize);
12980
4.03M
    if (squote) {
12981
78.1k
        changed = 1;
12982
78.1k
        if (dquote)
12983
            /* Both squote and dquote present. Use squote,
12984
               and escape them */
12985
8.47k
            osize += squote;
12986
69.6k
        else
12987
69.6k
            quote = '"';
12988
78.1k
    }
12989
4.03M
    osize += 2;   /* quotes */
12990
12991
4.03M
    PyObject *repr = PyUnicode_New(osize, maxch);
12992
4.03M
    if (repr == NULL)
12993
0
        return NULL;
12994
4.03M
    int okind = PyUnicode_KIND(repr);
12995
4.03M
    void *odata = PyUnicode_DATA(repr);
12996
12997
4.03M
    if (!changed) {
12998
3.42M
        PyUnicode_WRITE(okind, odata, 0, quote);
12999
13000
3.42M
        _PyUnicode_FastCopyCharacters(repr, 1,
13001
3.42M
                                      unicode, 0,
13002
3.42M
                                      isize);
13003
13004
3.42M
        PyUnicode_WRITE(okind, odata, osize-1, quote);
13005
3.42M
    }
13006
613k
    else {
13007
613k
        switch (okind) {
13008
392k
        case PyUnicode_1BYTE_KIND:
13009
392k
            ucs1lib_repr(unicode, quote, odata);
13010
392k
            break;
13011
217k
        case PyUnicode_2BYTE_KIND:
13012
217k
            ucs2lib_repr(unicode, quote, odata);
13013
217k
            break;
13014
3.43k
        default:
13015
3.43k
            assert(okind == PyUnicode_4BYTE_KIND);
13016
3.43k
            ucs4lib_repr(unicode, quote, odata);
13017
613k
        }
13018
613k
    }
13019
13020
4.03M
    assert(_PyUnicode_CheckConsistency(repr, 1));
13021
4.03M
    return repr;
13022
4.03M
}
13023
13024
/*[clinic input]
13025
@permit_long_summary
13026
str.rfind as unicode_rfind = str.count
13027
13028
Return the highest index in S where substring sub is found, such that sub is contained within S[start:end].
13029
13030
Optional arguments start and end are interpreted as in slice notation.
13031
Return -1 on failure.
13032
[clinic start generated code]*/
13033
13034
static Py_ssize_t
13035
unicode_rfind_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
13036
                   Py_ssize_t end)
13037
/*[clinic end generated code: output=880b29f01dd014c8 input=7f7e97d5cd3299a2]*/
13038
10.3k
{
13039
10.3k
    Py_ssize_t result = any_find_slice(str, substr, start, end, -1);
13040
10.3k
    if (result < 0) {
13041
7.08k
        return -1;
13042
7.08k
    }
13043
3.28k
    return result;
13044
10.3k
}
13045
13046
/*[clinic input]
13047
@permit_long_summary
13048
str.rindex as unicode_rindex = str.count
13049
13050
Return the highest index in S where substring sub is found, such that sub is contained within S[start:end].
13051
13052
Optional arguments start and end are interpreted as in slice notation.
13053
Raises ValueError when the substring is not found.
13054
[clinic start generated code]*/
13055
13056
static Py_ssize_t
13057
unicode_rindex_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
13058
                    Py_ssize_t end)
13059
/*[clinic end generated code: output=5f3aef124c867fe1 input=0363a324740b3e62]*/
13060
150k
{
13061
150k
    Py_ssize_t result = any_find_slice(str, substr, start, end, -1);
13062
150k
    if (result == -1) {
13063
0
        PyErr_SetString(PyExc_ValueError, "substring not found");
13064
0
    }
13065
150k
    else if (result < 0) {
13066
0
        return -1;
13067
0
    }
13068
150k
    return result;
13069
150k
}
13070
13071
/*[clinic input]
13072
str.rjust as unicode_rjust
13073
13074
    width: Py_ssize_t
13075
    fillchar: Py_UCS4 = ' '
13076
    /
13077
13078
Return a right-justified string of length width.
13079
13080
Padding is done using the specified fill character (default is a space).
13081
[clinic start generated code]*/
13082
13083
static PyObject *
13084
unicode_rjust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar)
13085
/*[clinic end generated code: output=804a1a57fbe8d5cf input=d05f550b5beb1f72]*/
13086
0
{
13087
0
    if (PyUnicode_GET_LENGTH(self) >= width)
13088
0
        return unicode_result_unchanged(self);
13089
13090
0
    return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar);
13091
0
}
13092
13093
PyObject *
13094
PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
13095
0
{
13096
0
    if (ensure_unicode(s) < 0 || (sep != NULL && ensure_unicode(sep) < 0))
13097
0
        return NULL;
13098
13099
0
    return split(s, sep, maxsplit);
13100
0
}
13101
13102
/*[clinic input]
13103
@permit_long_summary
13104
str.split as unicode_split
13105
13106
    sep: object = None
13107
        The separator used to split the string.
13108
13109
        When set to None (the default value), will split on any whitespace
13110
        character (including \n \r \t \f and spaces) and will discard
13111
        empty strings from the result.
13112
    maxsplit: Py_ssize_t = -1
13113
        Maximum number of splits.
13114
        -1 (the default value) means no limit.
13115
13116
Return a list of the substrings in the string, using sep as the separator string.
13117
13118
Splitting starts at the front of the string and works to the end.
13119
13120
Note, str.split() is mainly useful for data that has been intentionally
13121
delimited.  With natural text that includes punctuation, consider using
13122
the regular expression module.
13123
13124
[clinic start generated code]*/
13125
13126
static PyObject *
13127
unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit)
13128
/*[clinic end generated code: output=3a65b1db356948dc input=2c1fd08a78e038b8]*/
13129
22.6M
{
13130
22.6M
    if (sep == Py_None)
13131
166k
        return split(self, NULL, maxsplit);
13132
22.5M
    if (PyUnicode_Check(sep))
13133
22.5M
        return split(self, sep, maxsplit);
13134
13135
0
    PyErr_Format(PyExc_TypeError,
13136
0
                 "must be str or None, not %.100s",
13137
0
                 Py_TYPE(sep)->tp_name);
13138
0
    return NULL;
13139
22.5M
}
13140
13141
PyObject *
13142
PyUnicode_Partition(PyObject *str_obj, PyObject *sep_obj)
13143
6.47M
{
13144
6.47M
    PyObject* out;
13145
6.47M
    int kind1, kind2;
13146
6.47M
    const void *buf1, *buf2;
13147
6.47M
    Py_ssize_t len1, len2;
13148
13149
6.47M
    if (ensure_unicode(str_obj) < 0 || ensure_unicode(sep_obj) < 0)
13150
0
        return NULL;
13151
13152
6.47M
    kind1 = PyUnicode_KIND(str_obj);
13153
6.47M
    kind2 = PyUnicode_KIND(sep_obj);
13154
6.47M
    len1 = PyUnicode_GET_LENGTH(str_obj);
13155
6.47M
    len2 = PyUnicode_GET_LENGTH(sep_obj);
13156
6.47M
    if (kind1 < kind2 || len1 < len2) {
13157
1.02k
        PyObject *empty = unicode_get_empty();  // Borrowed reference
13158
1.02k
        return PyTuple_Pack(3, str_obj, empty, empty);
13159
1.02k
    }
13160
6.47M
    buf1 = PyUnicode_DATA(str_obj);
13161
6.47M
    buf2 = PyUnicode_DATA(sep_obj);
13162
6.47M
    if (kind2 != kind1) {
13163
84.4k
        buf2 = unicode_askind(kind2, buf2, len2, kind1);
13164
84.4k
        if (!buf2)
13165
0
            return NULL;
13166
84.4k
    }
13167
13168
6.47M
    switch (kind1) {
13169
6.39M
    case PyUnicode_1BYTE_KIND:
13170
6.39M
        if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj))
13171
2.21M
            out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
13172
4.17M
        else
13173
4.17M
            out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
13174
6.39M
        break;
13175
74.7k
    case PyUnicode_2BYTE_KIND:
13176
74.7k
        out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
13177
74.7k
        break;
13178
9.74k
    case PyUnicode_4BYTE_KIND:
13179
9.74k
        out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
13180
9.74k
        break;
13181
0
    default:
13182
0
        Py_UNREACHABLE();
13183
6.47M
    }
13184
13185
6.47M
    assert((kind2 == kind1) == (buf2 == PyUnicode_DATA(sep_obj)));
13186
6.47M
    if (kind2 != kind1)
13187
84.4k
        PyMem_Free((void *)buf2);
13188
13189
6.47M
    return out;
13190
6.47M
}
13191
13192
13193
PyObject *
13194
PyUnicode_RPartition(PyObject *str_obj, PyObject *sep_obj)
13195
9.77k
{
13196
9.77k
    PyObject* out;
13197
9.77k
    int kind1, kind2;
13198
9.77k
    const void *buf1, *buf2;
13199
9.77k
    Py_ssize_t len1, len2;
13200
13201
9.77k
    if (ensure_unicode(str_obj) < 0 || ensure_unicode(sep_obj) < 0)
13202
0
        return NULL;
13203
13204
9.77k
    kind1 = PyUnicode_KIND(str_obj);
13205
9.77k
    kind2 = PyUnicode_KIND(sep_obj);
13206
9.77k
    len1 = PyUnicode_GET_LENGTH(str_obj);
13207
9.77k
    len2 = PyUnicode_GET_LENGTH(sep_obj);
13208
9.77k
    if (kind1 < kind2 || len1 < len2) {
13209
0
        PyObject *empty = unicode_get_empty();  // Borrowed reference
13210
0
        return PyTuple_Pack(3, empty, empty, str_obj);
13211
0
    }
13212
9.77k
    buf1 = PyUnicode_DATA(str_obj);
13213
9.77k
    buf2 = PyUnicode_DATA(sep_obj);
13214
9.77k
    if (kind2 != kind1) {
13215
0
        buf2 = unicode_askind(kind2, buf2, len2, kind1);
13216
0
        if (!buf2)
13217
0
            return NULL;
13218
0
    }
13219
13220
9.77k
    switch (kind1) {
13221
9.77k
    case PyUnicode_1BYTE_KIND:
13222
9.77k
        if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj))
13223
9.77k
            out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
13224
0
        else
13225
0
            out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
13226
9.77k
        break;
13227
0
    case PyUnicode_2BYTE_KIND:
13228
0
        out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
13229
0
        break;
13230
0
    case PyUnicode_4BYTE_KIND:
13231
0
        out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
13232
0
        break;
13233
0
    default:
13234
0
        Py_UNREACHABLE();
13235
9.77k
    }
13236
13237
9.77k
    assert((kind2 == kind1) == (buf2 == PyUnicode_DATA(sep_obj)));
13238
9.77k
    if (kind2 != kind1)
13239
0
        PyMem_Free((void *)buf2);
13240
13241
9.77k
    return out;
13242
9.77k
}
13243
13244
/*[clinic input]
13245
@permit_long_docstring_body
13246
str.partition as unicode_partition
13247
13248
    sep: object
13249
    /
13250
13251
Partition the string into three parts using the given separator.
13252
13253
This will search for the separator in the string.  If the separator is found,
13254
returns a 3-tuple containing the part before the separator, the separator
13255
itself, and the part after it.
13256
13257
If the separator is not found, returns a 3-tuple containing the original string
13258
and two empty strings.
13259
[clinic start generated code]*/
13260
13261
static PyObject *
13262
unicode_partition(PyObject *self, PyObject *sep)
13263
/*[clinic end generated code: output=e4ced7bd253ca3c4 input=4d854b520d7b0e97]*/
13264
6.47M
{
13265
6.47M
    return PyUnicode_Partition(self, sep);
13266
6.47M
}
13267
13268
/*[clinic input]
13269
@permit_long_docstring_body
13270
str.rpartition as unicode_rpartition = str.partition
13271
13272
Partition the string into three parts using the given separator.
13273
13274
This will search for the separator in the string, starting at the end. If
13275
the separator is found, returns a 3-tuple containing the part before the
13276
separator, the separator itself, and the part after it.
13277
13278
If the separator is not found, returns a 3-tuple containing two empty strings
13279
and the original string.
13280
[clinic start generated code]*/
13281
13282
static PyObject *
13283
unicode_rpartition(PyObject *self, PyObject *sep)
13284
/*[clinic end generated code: output=1aa13cf1156572aa input=a6adabe91e75b486]*/
13285
9.77k
{
13286
9.77k
    return PyUnicode_RPartition(self, sep);
13287
9.77k
}
13288
13289
PyObject *
13290
PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
13291
0
{
13292
0
    if (ensure_unicode(s) < 0 || (sep != NULL && ensure_unicode(sep) < 0))
13293
0
        return NULL;
13294
13295
0
    return rsplit(s, sep, maxsplit);
13296
0
}
13297
13298
/*[clinic input]
13299
@permit_long_summary
13300
str.rsplit as unicode_rsplit = str.split
13301
13302
Return a list of the substrings in the string, using sep as the separator string.
13303
13304
Splitting starts at the end of the string and works to the front.
13305
[clinic start generated code]*/
13306
13307
static PyObject *
13308
unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit)
13309
/*[clinic end generated code: output=c2b815c63bcabffc input=0f762e30d267fa83]*/
13310
50
{
13311
50
    if (sep == Py_None)
13312
0
        return rsplit(self, NULL, maxsplit);
13313
50
    if (PyUnicode_Check(sep))
13314
50
        return rsplit(self, sep, maxsplit);
13315
13316
0
    PyErr_Format(PyExc_TypeError,
13317
0
                 "must be str or None, not %.100s",
13318
0
                 Py_TYPE(sep)->tp_name);
13319
0
    return NULL;
13320
50
}
13321
13322
/*[clinic input]
13323
@permit_long_docstring_body
13324
str.splitlines as unicode_splitlines
13325
13326
    keepends: bool = False
13327
13328
Return a list of the lines in the string, breaking at line boundaries.
13329
13330
Line breaks are not included in the resulting list unless keepends is given and
13331
true.
13332
[clinic start generated code]*/
13333
13334
static PyObject *
13335
unicode_splitlines_impl(PyObject *self, int keepends)
13336
/*[clinic end generated code: output=f664dcdad153ec40 input=39eeafbfef61c827]*/
13337
13.8k
{
13338
13.8k
    return PyUnicode_Splitlines(self, keepends);
13339
13.8k
}
13340
13341
static
13342
PyObject *unicode_str(PyObject *self)
13343
3.17M
{
13344
3.17M
    return unicode_result_unchanged(self);
13345
3.17M
}
13346
13347
/*[clinic input]
13348
@permit_long_summary
13349
str.swapcase as unicode_swapcase
13350
13351
Convert uppercase characters to lowercase and lowercase characters to uppercase.
13352
[clinic start generated code]*/
13353
13354
static PyObject *
13355
unicode_swapcase_impl(PyObject *self)
13356
/*[clinic end generated code: output=5d28966bf6d7b2af input=85bc39a9b4e8ee91]*/
13357
0
{
13358
0
    return case_operation(self, do_swapcase);
13359
0
}
13360
13361
/*[clinic input]
13362
13363
@staticmethod
13364
str.maketrans as unicode_maketrans
13365
13366
  x: object
13367
13368
  y: unicode=NULL
13369
13370
  z: unicode=NULL
13371
13372
  /
13373
13374
Return a translation table usable for str.translate().
13375
13376
If there is only one argument, it must be a dictionary mapping Unicode
13377
ordinals (integers) or characters to Unicode ordinals, strings or None.
13378
Character keys will be then converted to ordinals.
13379
If there are two arguments, they must be strings of equal length, and
13380
in the resulting dictionary, each character in x will be mapped to the
13381
character at the same position in y. If there is a third argument, it
13382
must be a string, whose characters will be mapped to None in the result.
13383
[clinic start generated code]*/
13384
13385
static PyObject *
13386
unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z)
13387
/*[clinic end generated code: output=a925c89452bd5881 input=7bfbf529a293c6c5]*/
13388
0
{
13389
0
    PyObject *new = NULL, *key, *value;
13390
0
    Py_ssize_t i = 0;
13391
0
    int res;
13392
13393
0
    new = PyDict_New();
13394
0
    if (!new)
13395
0
        return NULL;
13396
0
    if (y != NULL) {
13397
0
        int x_kind, y_kind, z_kind;
13398
0
        const void *x_data, *y_data, *z_data;
13399
13400
        /* x must be a string too, of equal length */
13401
0
        if (!PyUnicode_Check(x)) {
13402
0
            PyErr_SetString(PyExc_TypeError, "first maketrans argument must "
13403
0
                            "be a string if there is a second argument");
13404
0
            goto err;
13405
0
        }
13406
0
        if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) {
13407
0
            PyErr_SetString(PyExc_ValueError, "the first two maketrans "
13408
0
                            "arguments must have equal length");
13409
0
            goto err;
13410
0
        }
13411
        /* create entries for translating chars in x to those in y */
13412
0
        x_kind = PyUnicode_KIND(x);
13413
0
        y_kind = PyUnicode_KIND(y);
13414
0
        x_data = PyUnicode_DATA(x);
13415
0
        y_data = PyUnicode_DATA(y);
13416
0
        for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) {
13417
0
            key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i));
13418
0
            if (!key)
13419
0
                goto err;
13420
0
            value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i));
13421
0
            if (!value) {
13422
0
                Py_DECREF(key);
13423
0
                goto err;
13424
0
            }
13425
0
            res = PyDict_SetItem(new, key, value);
13426
0
            Py_DECREF(key);
13427
0
            Py_DECREF(value);
13428
0
            if (res < 0)
13429
0
                goto err;
13430
0
        }
13431
        /* create entries for deleting chars in z */
13432
0
        if (z != NULL) {
13433
0
            z_kind = PyUnicode_KIND(z);
13434
0
            z_data = PyUnicode_DATA(z);
13435
0
            for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) {
13436
0
                key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i));
13437
0
                if (!key)
13438
0
                    goto err;
13439
0
                res = PyDict_SetItem(new, key, Py_None);
13440
0
                Py_DECREF(key);
13441
0
                if (res < 0)
13442
0
                    goto err;
13443
0
            }
13444
0
        }
13445
0
    } else {
13446
0
        int kind;
13447
0
        const void *data;
13448
13449
        /* x must be a dict */
13450
0
        if (!PyDict_CheckExact(x)) {
13451
0
            PyErr_SetString(PyExc_TypeError, "if you give only one argument "
13452
0
                            "to maketrans it must be a dict");
13453
0
            goto err;
13454
0
        }
13455
        /* copy entries into the new dict, converting string keys to int keys */
13456
0
        while (PyDict_Next(x, &i, &key, &value)) {
13457
0
            if (PyUnicode_Check(key)) {
13458
                /* convert string keys to integer keys */
13459
0
                PyObject *newkey;
13460
0
                if (PyUnicode_GET_LENGTH(key) != 1) {
13461
0
                    PyErr_SetString(PyExc_ValueError, "string keys in translate "
13462
0
                                    "table must be of length 1");
13463
0
                    goto err;
13464
0
                }
13465
0
                kind = PyUnicode_KIND(key);
13466
0
                data = PyUnicode_DATA(key);
13467
0
                newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0));
13468
0
                if (!newkey)
13469
0
                    goto err;
13470
0
                res = PyDict_SetItem(new, newkey, value);
13471
0
                Py_DECREF(newkey);
13472
0
                if (res < 0)
13473
0
                    goto err;
13474
0
            } else if (PyLong_Check(key)) {
13475
                /* just keep integer keys */
13476
0
                if (PyDict_SetItem(new, key, value) < 0)
13477
0
                    goto err;
13478
0
            } else {
13479
0
                PyErr_SetString(PyExc_TypeError, "keys in translate table must "
13480
0
                                "be strings or integers");
13481
0
                goto err;
13482
0
            }
13483
0
        }
13484
0
    }
13485
0
    return new;
13486
0
  err:
13487
0
    Py_DECREF(new);
13488
0
    return NULL;
13489
0
}
13490
13491
/*[clinic input]
13492
@permit_long_docstring_body
13493
str.translate as unicode_translate
13494
13495
    table: object
13496
        Translation table, which must be a mapping of Unicode ordinals to
13497
        Unicode ordinals, strings, or None.
13498
    /
13499
13500
Replace each character in the string using the given translation table.
13501
13502
The table must implement lookup/indexing via __getitem__, for instance a
13503
dictionary or list.  If this operation raises LookupError, the character is
13504
left untouched.  Characters mapped to None are deleted.
13505
[clinic start generated code]*/
13506
13507
static PyObject *
13508
unicode_translate(PyObject *self, PyObject *table)
13509
/*[clinic end generated code: output=3cb448ff2fd96bf3 input=699e5fa0ebf9f5e9]*/
13510
104
{
13511
104
    return _PyUnicode_TranslateCharmap(self, table, "ignore");
13512
104
}
13513
13514
/*[clinic input]
13515
str.upper as unicode_upper
13516
13517
Return a copy of the string converted to uppercase.
13518
[clinic start generated code]*/
13519
13520
static PyObject *
13521
unicode_upper_impl(PyObject *self)
13522
/*[clinic end generated code: output=1b7ddd16bbcdc092 input=db3d55682dfe2e6c]*/
13523
102
{
13524
102
    if (PyUnicode_IS_ASCII(self))
13525
102
        return ascii_upper_or_lower(self, 0);
13526
0
    return case_operation(self, do_upper);
13527
102
}
13528
13529
/*[clinic input]
13530
@permit_long_summary
13531
str.zfill as unicode_zfill
13532
13533
    width: Py_ssize_t
13534
    /
13535
13536
Pad a numeric string with zeros on the left, to fill a field of the given width.
13537
13538
The string is never truncated.
13539
[clinic start generated code]*/
13540
13541
static PyObject *
13542
unicode_zfill_impl(PyObject *self, Py_ssize_t width)
13543
/*[clinic end generated code: output=e13fb6bdf8e3b9df input=25a4ee0ea3e58ce0]*/
13544
0
{
13545
0
    Py_ssize_t fill;
13546
0
    PyObject *u;
13547
0
    int kind;
13548
0
    const void *data;
13549
0
    Py_UCS4 chr;
13550
13551
0
    if (PyUnicode_GET_LENGTH(self) >= width)
13552
0
        return unicode_result_unchanged(self);
13553
13554
0
    fill = width - PyUnicode_GET_LENGTH(self);
13555
13556
0
    u = pad(self, fill, 0, '0');
13557
13558
0
    if (u == NULL)
13559
0
        return NULL;
13560
13561
0
    kind = PyUnicode_KIND(u);
13562
0
    data = PyUnicode_DATA(u);
13563
0
    chr = PyUnicode_READ(kind, data, fill);
13564
13565
0
    if (chr == '+' || chr == '-') {
13566
        /* move sign to beginning of string */
13567
0
        PyUnicode_WRITE(kind, data, 0, chr);
13568
0
        PyUnicode_WRITE(kind, data, fill, '0');
13569
0
    }
13570
13571
0
    assert(_PyUnicode_CheckConsistency(u, 1));
13572
0
    return u;
13573
0
}
13574
13575
/*[clinic input]
13576
@permit_long_summary
13577
@text_signature "($self, prefix[, start[, end]], /)"
13578
str.startswith as unicode_startswith
13579
13580
    prefix as subobj: object
13581
        A string or a tuple of strings to try.
13582
    start: slice_index(accept={int, NoneType}, c_default='0') = None
13583
        Optional start position. Default: start of the string.
13584
    end: slice_index(accept={int, NoneType}, c_default='PY_SSIZE_T_MAX') = None
13585
        Optional stop position. Default: end of the string.
13586
    /
13587
13588
Return True if the string starts with the specified prefix, False otherwise.
13589
[clinic start generated code]*/
13590
13591
static PyObject *
13592
unicode_startswith_impl(PyObject *self, PyObject *subobj, Py_ssize_t start,
13593
                        Py_ssize_t end)
13594
/*[clinic end generated code: output=4bd7cfd0803051d4 input=766bdbd33df251dc]*/
13595
76.9M
{
13596
76.9M
    if (PyTuple_Check(subobj)) {
13597
9.92M
        Py_ssize_t i;
13598
36.0M
        for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
13599
26.1M
            PyObject *substring = PyTuple_GET_ITEM(subobj, i);
13600
26.1M
            if (!PyUnicode_Check(substring)) {
13601
0
                PyErr_Format(PyExc_TypeError,
13602
0
                             "tuple for startswith must only contain str, "
13603
0
                             "not %.100s",
13604
0
                             Py_TYPE(substring)->tp_name);
13605
0
                return NULL;
13606
0
            }
13607
26.1M
            int result = tailmatch(self, substring, start, end, -1);
13608
26.1M
            if (result < 0) {
13609
0
                return NULL;
13610
0
            }
13611
26.1M
            if (result) {
13612
37.8k
                Py_RETURN_TRUE;
13613
37.8k
            }
13614
26.1M
        }
13615
        /* nothing matched */
13616
9.92M
        Py_RETURN_FALSE;
13617
9.92M
    }
13618
67.0M
    if (!PyUnicode_Check(subobj)) {
13619
0
        PyErr_Format(PyExc_TypeError,
13620
0
                     "startswith first arg must be str or "
13621
0
                     "a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name);
13622
0
        return NULL;
13623
0
    }
13624
67.0M
    int result = tailmatch(self, subobj, start, end, -1);
13625
67.0M
    if (result < 0) {
13626
0
        return NULL;
13627
0
    }
13628
67.0M
    return PyBool_FromLong(result);
13629
67.0M
}
13630
13631
13632
/*[clinic input]
13633
@permit_long_summary
13634
@text_signature "($self, suffix[, start[, end]], /)"
13635
str.endswith as unicode_endswith
13636
13637
    suffix as subobj: object
13638
        A string or a tuple of strings to try.
13639
    start: slice_index(accept={int, NoneType}, c_default='0') = None
13640
        Optional start position. Default: start of the string.
13641
    end: slice_index(accept={int, NoneType}, c_default='PY_SSIZE_T_MAX') = None
13642
        Optional stop position. Default: end of the string.
13643
    /
13644
13645
Return True if the string ends with the specified suffix, False otherwise.
13646
[clinic start generated code]*/
13647
13648
static PyObject *
13649
unicode_endswith_impl(PyObject *self, PyObject *subobj, Py_ssize_t start,
13650
                      Py_ssize_t end)
13651
/*[clinic end generated code: output=cce6f8ceb0102ca9 input=b66bf6d5547ba1aa]*/
13652
15.9M
{
13653
15.9M
    if (PyTuple_Check(subobj)) {
13654
222k
        Py_ssize_t i;
13655
414k
        for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
13656
389k
            PyObject *substring = PyTuple_GET_ITEM(subobj, i);
13657
389k
            if (!PyUnicode_Check(substring)) {
13658
0
                PyErr_Format(PyExc_TypeError,
13659
0
                             "tuple for endswith must only contain str, "
13660
0
                             "not %.100s",
13661
0
                             Py_TYPE(substring)->tp_name);
13662
0
                return NULL;
13663
0
            }
13664
389k
            int result = tailmatch(self, substring, start, end, +1);
13665
389k
            if (result < 0) {
13666
0
                return NULL;
13667
0
            }
13668
389k
            if (result) {
13669
197k
                Py_RETURN_TRUE;
13670
197k
            }
13671
389k
        }
13672
222k
        Py_RETURN_FALSE;
13673
222k
    }
13674
15.7M
    if (!PyUnicode_Check(subobj)) {
13675
0
        PyErr_Format(PyExc_TypeError,
13676
0
                     "endswith first arg must be str or "
13677
0
                     "a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name);
13678
0
        return NULL;
13679
0
    }
13680
15.7M
    int result = tailmatch(self, subobj, start, end, +1);
13681
15.7M
    if (result < 0) {
13682
0
        return NULL;
13683
0
    }
13684
15.7M
    return PyBool_FromLong(result);
13685
15.7M
}
13686
13687
13688
static inline void
13689
_PyUnicodeWriter_Update(_PyUnicodeWriter *writer)
13690
70.2M
{
13691
70.2M
    writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer);
13692
70.2M
    writer->data = PyUnicode_DATA(writer->buffer);
13693
13694
70.2M
    if (!writer->readonly) {
13695
70.2M
        writer->kind = PyUnicode_KIND(writer->buffer);
13696
70.2M
        writer->size = PyUnicode_GET_LENGTH(writer->buffer);
13697
70.2M
    }
13698
15.0k
    else {
13699
        /* use a value smaller than PyUnicode_1BYTE_KIND() so
13700
           _PyUnicodeWriter_PrepareKind() will copy the buffer. */
13701
15.0k
        writer->kind = 0;
13702
15.0k
        assert(writer->kind <= PyUnicode_1BYTE_KIND);
13703
13704
        /* Copy-on-write mode: set buffer size to 0 so
13705
         * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on
13706
         * next write. */
13707
15.0k
        writer->size = 0;
13708
15.0k
    }
13709
70.2M
}
13710
13711
13712
void
13713
_PyUnicodeWriter_Init(_PyUnicodeWriter *writer)
13714
56.3M
{
13715
56.3M
    memset(writer, 0, sizeof(*writer));
13716
13717
    /* ASCII is the bare minimum */
13718
56.3M
    writer->min_char = 127;
13719
13720
    /* use a kind value smaller than PyUnicode_1BYTE_KIND so
13721
       _PyUnicodeWriter_PrepareKind() will copy the buffer. */
13722
56.3M
    assert(writer->kind == 0);
13723
56.3M
    assert(writer->kind < PyUnicode_1BYTE_KIND);
13724
56.3M
}
13725
13726
13727
PyUnicodeWriter*
13728
PyUnicodeWriter_Create(Py_ssize_t length)
13729
4.81M
{
13730
4.81M
    if (length < 0) {
13731
0
        PyErr_SetString(PyExc_ValueError,
13732
0
                        "length must be positive");
13733
0
        return NULL;
13734
0
    }
13735
13736
4.81M
    const size_t size = sizeof(_PyUnicodeWriter);
13737
4.81M
    PyUnicodeWriter *pub_writer;
13738
4.81M
    pub_writer = _Py_FREELIST_POP_MEM(unicode_writers);
13739
4.81M
    if (pub_writer == NULL) {
13740
2.67M
        pub_writer = (PyUnicodeWriter *)PyMem_Malloc(size);
13741
2.67M
        if (pub_writer == NULL) {
13742
0
            return (PyUnicodeWriter *)PyErr_NoMemory();
13743
0
        }
13744
2.67M
    }
13745
4.81M
    _PyUnicodeWriter *writer = (_PyUnicodeWriter *)pub_writer;
13746
13747
4.81M
    _PyUnicodeWriter_Init(writer);
13748
4.81M
    if (_PyUnicodeWriter_Prepare(writer, length, 127) < 0) {
13749
0
        PyUnicodeWriter_Discard(pub_writer);
13750
0
        return NULL;
13751
0
    }
13752
4.81M
    writer->overallocate = 1;
13753
13754
4.81M
    return pub_writer;
13755
4.81M
}
13756
13757
13758
void PyUnicodeWriter_Discard(PyUnicodeWriter *writer)
13759
65.8k
{
13760
65.8k
    if (writer == NULL) {
13761
65.2k
        return;
13762
65.2k
    }
13763
516
    _PyUnicodeWriter_Dealloc((_PyUnicodeWriter*)writer);
13764
516
    _Py_FREELIST_FREE(unicode_writers, writer, PyMem_Free);
13765
516
}
13766
13767
13768
// Initialize _PyUnicodeWriter with initial buffer
13769
static inline void
13770
_PyUnicodeWriter_InitWithBuffer(_PyUnicodeWriter *writer, PyObject *buffer)
13771
542k
{
13772
542k
    memset(writer, 0, sizeof(*writer));
13773
542k
    writer->buffer = buffer;
13774
542k
    _PyUnicodeWriter_Update(writer);
13775
542k
    writer->min_length = writer->size;
13776
542k
}
13777
13778
13779
int
13780
_PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
13781
                                 Py_ssize_t length, Py_UCS4 maxchar)
13782
69.6M
{
13783
69.6M
    Py_ssize_t newlen;
13784
69.6M
    PyObject *newbuffer;
13785
13786
69.6M
    assert(length >= 0);
13787
69.6M
    assert(maxchar <= MAX_UNICODE);
13788
13789
    /* ensure that the _PyUnicodeWriter_Prepare macro was used */
13790
69.6M
    assert((maxchar > writer->maxchar && length >= 0)
13791
69.6M
           || length > 0);
13792
13793
69.6M
    if (length > PY_SSIZE_T_MAX - writer->pos) {
13794
0
        PyErr_NoMemory();
13795
0
        return -1;
13796
0
    }
13797
69.6M
    newlen = writer->pos + length;
13798
13799
69.6M
    maxchar = Py_MAX(maxchar, writer->min_char);
13800
13801
69.6M
    if (writer->buffer == NULL) {
13802
51.1M
        assert(!writer->readonly);
13803
51.1M
        if (writer->overallocate
13804
51.1M
            && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) {
13805
            /* overallocate to limit the number of realloc() */
13806
39.2M
            newlen += newlen / OVERALLOCATE_FACTOR;
13807
39.2M
        }
13808
51.1M
        if (newlen < writer->min_length)
13809
46.2M
            newlen = writer->min_length;
13810
13811
51.1M
        writer->buffer = PyUnicode_New(newlen, maxchar);
13812
51.1M
        if (writer->buffer == NULL)
13813
0
            return -1;
13814
51.1M
    }
13815
18.5M
    else if (newlen > writer->size) {
13816
15.7M
        if (writer->overallocate
13817
15.7M
            && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) {
13818
            /* overallocate to limit the number of realloc() */
13819
15.3M
            newlen += newlen / OVERALLOCATE_FACTOR;
13820
15.3M
        }
13821
15.7M
        if (newlen < writer->min_length)
13822
1.19k
            newlen = writer->min_length;
13823
13824
15.7M
        if (maxchar > writer->maxchar || writer->readonly) {
13825
            /* resize + widen */
13826
3.82M
            maxchar = Py_MAX(maxchar, writer->maxchar);
13827
3.82M
            newbuffer = PyUnicode_New(newlen, maxchar);
13828
3.82M
            if (newbuffer == NULL)
13829
0
                return -1;
13830
3.82M
            _PyUnicode_FastCopyCharacters(newbuffer, 0,
13831
3.82M
                                          writer->buffer, 0, writer->pos);
13832
3.82M
            Py_DECREF(writer->buffer);
13833
3.82M
            writer->readonly = 0;
13834
3.82M
        }
13835
11.9M
        else {
13836
11.9M
            newbuffer = resize_compact(writer->buffer, newlen);
13837
11.9M
            if (newbuffer == NULL)
13838
0
                return -1;
13839
11.9M
        }
13840
15.7M
        writer->buffer = newbuffer;
13841
15.7M
    }
13842
2.78M
    else if (maxchar > writer->maxchar) {
13843
2.78M
        assert(!writer->readonly);
13844
2.78M
        newbuffer = PyUnicode_New(writer->size, maxchar);
13845
2.78M
        if (newbuffer == NULL)
13846
0
            return -1;
13847
2.78M
        _PyUnicode_FastCopyCharacters(newbuffer, 0,
13848
2.78M
                                      writer->buffer, 0, writer->pos);
13849
2.78M
        Py_SETREF(writer->buffer, newbuffer);
13850
2.78M
    }
13851
69.6M
    _PyUnicodeWriter_Update(writer);
13852
69.6M
    return 0;
13853
13854
69.6M
#undef OVERALLOCATE_FACTOR
13855
69.6M
}
13856
13857
int
13858
_PyUnicodeWriter_PrepareKindInternal(_PyUnicodeWriter *writer,
13859
                                     int kind)
13860
164k
{
13861
164k
    Py_UCS4 maxchar;
13862
13863
    /* ensure that the _PyUnicodeWriter_PrepareKind macro was used */
13864
164k
    assert(writer->kind < kind);
13865
13866
164k
    switch (kind)
13867
164k
    {
13868
0
    case PyUnicode_1BYTE_KIND: maxchar = 0xff; break;
13869
164k
    case PyUnicode_2BYTE_KIND: maxchar = 0xffff; break;
13870
0
    case PyUnicode_4BYTE_KIND: maxchar = MAX_UNICODE; break;
13871
0
    default:
13872
0
        Py_UNREACHABLE();
13873
164k
    }
13874
13875
164k
    return _PyUnicodeWriter_PrepareInternal(writer, 0, maxchar);
13876
164k
}
13877
13878
static inline int
13879
_PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch)
13880
299M
{
13881
299M
    assert(ch <= MAX_UNICODE);
13882
299M
    if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0)
13883
0
        return -1;
13884
299M
    PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch);
13885
299M
    writer->pos++;
13886
299M
    return 0;
13887
299M
}
13888
13889
int
13890
_PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch)
13891
107M
{
13892
107M
    return _PyUnicodeWriter_WriteCharInline(writer, ch);
13893
107M
}
13894
13895
int
13896
PyUnicodeWriter_WriteChar(PyUnicodeWriter *writer, Py_UCS4 ch)
13897
78.2M
{
13898
78.2M
    if (ch > MAX_UNICODE) {
13899
0
        PyErr_SetString(PyExc_ValueError,
13900
0
                        "character must be in range(0x110000)");
13901
0
        return -1;
13902
0
    }
13903
13904
78.2M
    return _PyUnicodeWriter_WriteChar((_PyUnicodeWriter*)writer, ch);
13905
78.2M
}
13906
13907
int
13908
_PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str)
13909
69.9M
{
13910
69.9M
    assert(PyUnicode_Check(str));
13911
13912
69.9M
    Py_UCS4 maxchar;
13913
69.9M
    Py_ssize_t len;
13914
13915
69.9M
    len = PyUnicode_GET_LENGTH(str);
13916
69.9M
    if (len == 0)
13917
25.7M
        return 0;
13918
44.2M
    maxchar = PyUnicode_MAX_CHAR_VALUE(str);
13919
44.2M
    if (maxchar > writer->maxchar || len > writer->size - writer->pos) {
13920
24.1M
        if (writer->buffer == NULL && !writer->overallocate) {
13921
6.41k
            assert(_PyUnicode_CheckConsistency(str, 1));
13922
6.41k
            writer->readonly = 1;
13923
6.41k
            writer->buffer = Py_NewRef(str);
13924
6.41k
            _PyUnicodeWriter_Update(writer);
13925
6.41k
            writer->pos += len;
13926
6.41k
            return 0;
13927
6.41k
        }
13928
24.1M
        if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1)
13929
0
            return -1;
13930
24.1M
    }
13931
44.1M
    _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
13932
44.1M
                                  str, 0, len);
13933
44.1M
    writer->pos += len;
13934
44.1M
    return 0;
13935
44.2M
}
13936
13937
int
13938
PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *obj)
13939
4.95M
{
13940
4.95M
    PyTypeObject *type = Py_TYPE(obj);
13941
4.95M
    if (type == &PyUnicode_Type) {
13942
4.95M
        return _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, obj);
13943
4.95M
    }
13944
13945
0
    if (type == &PyLong_Type) {
13946
0
        return _PyLong_FormatWriter((_PyUnicodeWriter*)writer, obj, 10, 0);
13947
0
    }
13948
13949
0
    PyObject *str = PyObject_Str(obj);
13950
0
    if (str == NULL) {
13951
0
        return -1;
13952
0
    }
13953
13954
0
    int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, str);
13955
0
    Py_DECREF(str);
13956
0
    return res;
13957
0
}
13958
13959
13960
int
13961
PyUnicodeWriter_WriteRepr(PyUnicodeWriter *writer, PyObject *obj)
13962
8.93M
{
13963
8.93M
    if (Py_TYPE(obj) == &PyLong_Type) {
13964
1.32M
        return _PyLong_FormatWriter((_PyUnicodeWriter*)writer, obj, 10, 0);
13965
1.32M
    }
13966
13967
7.61M
    PyObject *repr = PyObject_Repr(obj);
13968
7.61M
    if (repr == NULL) {
13969
0
        return -1;
13970
0
    }
13971
13972
7.61M
    int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, repr);
13973
7.61M
    Py_DECREF(repr);
13974
7.61M
    return res;
13975
7.61M
}
13976
13977
13978
int
13979
_PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str,
13980
                                Py_ssize_t start, Py_ssize_t end)
13981
72.2M
{
13982
72.2M
    assert(0 <= start);
13983
72.2M
    assert(end <= PyUnicode_GET_LENGTH(str));
13984
72.2M
    assert(start <= end);
13985
13986
72.2M
    if (start == 0 && end == PyUnicode_GET_LENGTH(str))
13987
107
        return _PyUnicodeWriter_WriteStr(writer, str);
13988
13989
72.2M
    Py_ssize_t len = end - start;
13990
72.2M
    if (len == 0) {
13991
0
        return 0;
13992
0
    }
13993
13994
72.2M
    Py_UCS4 maxchar;
13995
72.2M
    if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) {
13996
15.3M
        maxchar = _PyUnicode_FindMaxChar(str, start, end);
13997
15.3M
    }
13998
56.8M
    else {
13999
56.8M
        maxchar = writer->maxchar;
14000
56.8M
    }
14001
72.2M
    if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) {
14002
0
        return -1;
14003
0
    }
14004
14005
72.2M
    _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
14006
72.2M
                                  str, start, len);
14007
72.2M
    writer->pos += len;
14008
72.2M
    return 0;
14009
72.2M
}
14010
14011
14012
int
14013
PyUnicodeWriter_WriteSubstring(PyUnicodeWriter *writer, PyObject *str,
14014
                               Py_ssize_t start, Py_ssize_t end)
14015
673k
{
14016
673k
    if (!PyUnicode_Check(str)) {
14017
0
        PyErr_Format(PyExc_TypeError, "expect str, not %T", str);
14018
0
        return -1;
14019
0
    }
14020
673k
    if (start < 0 || start > end) {
14021
0
        PyErr_Format(PyExc_ValueError, "invalid start argument");
14022
0
        return -1;
14023
0
    }
14024
673k
    if (end > PyUnicode_GET_LENGTH(str)) {
14025
0
        PyErr_Format(PyExc_ValueError, "invalid end argument");
14026
0
        return -1;
14027
0
    }
14028
14029
673k
    return _PyUnicodeWriter_WriteSubstring((_PyUnicodeWriter*)writer, str,
14030
673k
                                           start, end);
14031
673k
}
14032
14033
14034
int
14035
_PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer,
14036
                                  const char *ascii, Py_ssize_t len)
14037
55.1M
{
14038
55.1M
    if (len == -1)
14039
0
        len = strlen(ascii);
14040
14041
55.1M
    assert(ucs1lib_find_max_char((const Py_UCS1*)ascii, (const Py_UCS1*)ascii + len) < 128);
14042
14043
55.1M
    if (writer->buffer == NULL && !writer->overallocate) {
14044
8.61k
        PyObject *str;
14045
14046
8.61k
        str = _PyUnicode_FromASCII(ascii, len);
14047
8.61k
        if (str == NULL)
14048
0
            return -1;
14049
14050
8.61k
        writer->readonly = 1;
14051
8.61k
        writer->buffer = str;
14052
8.61k
        _PyUnicodeWriter_Update(writer);
14053
8.61k
        writer->pos += len;
14054
8.61k
        return 0;
14055
8.61k
    }
14056
14057
55.1M
    if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1)
14058
0
        return -1;
14059
14060
55.1M
    switch (writer->kind)
14061
55.1M
    {
14062
55.1M
    case PyUnicode_1BYTE_KIND:
14063
55.1M
    {
14064
55.1M
        const Py_UCS1 *str = (const Py_UCS1 *)ascii;
14065
55.1M
        Py_UCS1 *data = writer->data;
14066
14067
55.1M
        memcpy(data + writer->pos, str, len);
14068
55.1M
        break;
14069
0
    }
14070
11.3k
    case PyUnicode_2BYTE_KIND:
14071
11.3k
    {
14072
11.3k
        _PyUnicode_CONVERT_BYTES(
14073
11.3k
            Py_UCS1, Py_UCS2,
14074
11.3k
            ascii, ascii + len,
14075
11.3k
            (Py_UCS2 *)writer->data + writer->pos);
14076
11.3k
        break;
14077
0
    }
14078
3.03k
    case PyUnicode_4BYTE_KIND:
14079
3.03k
    {
14080
3.03k
        _PyUnicode_CONVERT_BYTES(
14081
3.03k
            Py_UCS1, Py_UCS4,
14082
3.03k
            ascii, ascii + len,
14083
3.03k
            (Py_UCS4 *)writer->data + writer->pos);
14084
3.03k
        break;
14085
0
    }
14086
0
    default:
14087
0
        Py_UNREACHABLE();
14088
55.1M
    }
14089
14090
55.1M
    writer->pos += len;
14091
55.1M
    return 0;
14092
55.1M
}
14093
14094
14095
int
14096
PyUnicodeWriter_WriteASCII(PyUnicodeWriter *writer,
14097
                           const char *str,
14098
                           Py_ssize_t size)
14099
600k
{
14100
600k
    assert(writer != NULL);
14101
600k
    _Py_AssertHoldsTstate();
14102
14103
600k
    _PyUnicodeWriter *priv_writer = (_PyUnicodeWriter*)writer;
14104
600k
    return _PyUnicodeWriter_WriteASCIIString(priv_writer, str, size);
14105
600k
}
14106
14107
14108
int
14109
PyUnicodeWriter_WriteUTF8(PyUnicodeWriter *writer,
14110
                          const char *str,
14111
                          Py_ssize_t size)
14112
0
{
14113
0
    if (size < 0) {
14114
0
        size = strlen(str);
14115
0
    }
14116
14117
0
    _PyUnicodeWriter *_writer = (_PyUnicodeWriter*)writer;
14118
0
    Py_ssize_t old_pos = _writer->pos;
14119
0
    int res = unicode_decode_utf8_writer(_writer, str, size,
14120
0
                                         _Py_ERROR_STRICT, NULL, NULL);
14121
0
    if (res < 0) {
14122
0
        _writer->pos = old_pos;
14123
0
    }
14124
0
    return res;
14125
0
}
14126
14127
14128
int
14129
PyUnicodeWriter_DecodeUTF8Stateful(PyUnicodeWriter *writer,
14130
                                   const char *string,
14131
                                   Py_ssize_t length,
14132
                                   const char *errors,
14133
                                   Py_ssize_t *consumed)
14134
0
{
14135
0
    if (length < 0) {
14136
0
        length = strlen(string);
14137
0
    }
14138
14139
0
    _PyUnicodeWriter *_writer = (_PyUnicodeWriter*)writer;
14140
0
    Py_ssize_t old_pos = _writer->pos;
14141
0
    int res = unicode_decode_utf8_writer(_writer, string, length,
14142
0
                                         _Py_ERROR_UNKNOWN, errors, consumed);
14143
0
    if (res < 0) {
14144
0
        _writer->pos = old_pos;
14145
0
        if (consumed) {
14146
0
            *consumed = 0;
14147
0
        }
14148
0
    }
14149
0
    return res;
14150
0
}
14151
14152
14153
int
14154
_PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer,
14155
                                   const char *str, Py_ssize_t len)
14156
0
{
14157
0
    Py_UCS4 maxchar;
14158
14159
0
    maxchar = ucs1lib_find_max_char((const Py_UCS1*)str, (const Py_UCS1*)str + len);
14160
0
    if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1)
14161
0
        return -1;
14162
0
    unicode_write_cstr(writer->buffer, writer->pos, str, len);
14163
0
    writer->pos += len;
14164
0
    return 0;
14165
0
}
14166
14167
PyObject *
14168
_PyUnicodeWriter_Finish(_PyUnicodeWriter *writer)
14169
51.6M
{
14170
51.6M
    PyObject *str;
14171
14172
51.6M
    if (writer->pos == 0) {
14173
880
        Py_CLEAR(writer->buffer);
14174
880
        _Py_RETURN_UNICODE_EMPTY();
14175
880
    }
14176
14177
51.6M
    str = writer->buffer;
14178
51.6M
    writer->buffer = NULL;
14179
14180
51.6M
    if (writer->readonly) {
14181
13.8k
        assert(PyUnicode_GET_LENGTH(str) == writer->pos);
14182
13.8k
        return str;
14183
13.8k
    }
14184
14185
51.6M
    if (PyUnicode_GET_LENGTH(str) != writer->pos) {
14186
50.6M
        PyObject *str2;
14187
50.6M
        str2 = resize_compact(str, writer->pos);
14188
50.6M
        if (str2 == NULL) {
14189
0
            Py_DECREF(str);
14190
0
            return NULL;
14191
0
        }
14192
50.6M
        str = str2;
14193
50.6M
    }
14194
14195
51.6M
    assert(_PyUnicode_CheckConsistency(str, 1));
14196
51.6M
    return unicode_result(str);
14197
51.6M
}
14198
14199
14200
PyObject*
14201
PyUnicodeWriter_Finish(PyUnicodeWriter *writer)
14202
4.81M
{
14203
4.81M
    PyObject *str = _PyUnicodeWriter_Finish((_PyUnicodeWriter*)writer);
14204
4.81M
    assert(((_PyUnicodeWriter*)writer)->buffer == NULL);
14205
4.81M
    _Py_FREELIST_FREE(unicode_writers, writer, PyMem_Free);
14206
4.81M
    return str;
14207
4.81M
}
14208
14209
14210
void
14211
_PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer)
14212
5.21M
{
14213
5.21M
    Py_CLEAR(writer->buffer);
14214
5.21M
}
14215
14216
#include "stringlib/unicode_format.h"
14217
14218
PyDoc_STRVAR(format__doc__,
14219
             "format($self, /, *args, **kwargs)\n\
14220
--\n\
14221
\n\
14222
Return a formatted version of the string, using substitutions from args and kwargs.\n\
14223
The substitutions are identified by braces ('{' and '}').");
14224
14225
PyDoc_STRVAR(format_map__doc__,
14226
             "format_map($self, mapping, /)\n\
14227
--\n\
14228
\n\
14229
Return a formatted version of the string, using substitutions from mapping.\n\
14230
The substitutions are identified by braces ('{' and '}').");
14231
14232
/*[clinic input]
14233
str.__format__ as unicode___format__
14234
14235
    format_spec: unicode
14236
    /
14237
14238
Return a formatted version of the string as described by format_spec.
14239
[clinic start generated code]*/
14240
14241
static PyObject *
14242
unicode___format___impl(PyObject *self, PyObject *format_spec)
14243
/*[clinic end generated code: output=45fceaca6d2ba4c8 input=5e135645d167a214]*/
14244
0
{
14245
0
    _PyUnicodeWriter writer;
14246
0
    int ret;
14247
14248
0
    _PyUnicodeWriter_Init(&writer);
14249
0
    ret = _PyUnicode_FormatAdvancedWriter(&writer,
14250
0
                                          self, format_spec, 0,
14251
0
                                          PyUnicode_GET_LENGTH(format_spec));
14252
0
    if (ret == -1) {
14253
0
        _PyUnicodeWriter_Dealloc(&writer);
14254
0
        return NULL;
14255
0
    }
14256
0
    return _PyUnicodeWriter_Finish(&writer);
14257
0
}
14258
14259
/*[clinic input]
14260
str.__sizeof__ as unicode_sizeof
14261
14262
Return the size of the string in memory, in bytes.
14263
[clinic start generated code]*/
14264
14265
static PyObject *
14266
unicode_sizeof_impl(PyObject *self)
14267
/*[clinic end generated code: output=6dbc2f5a408b6d4f input=6dd011c108e33fb0]*/
14268
0
{
14269
0
    Py_ssize_t size;
14270
14271
    /* If it's a compact object, account for base structure +
14272
       character data. */
14273
0
    if (PyUnicode_IS_COMPACT_ASCII(self)) {
14274
0
        size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(self) + 1;
14275
0
    }
14276
0
    else if (PyUnicode_IS_COMPACT(self)) {
14277
0
        size = sizeof(PyCompactUnicodeObject) +
14278
0
            (PyUnicode_GET_LENGTH(self) + 1) * PyUnicode_KIND(self);
14279
0
    }
14280
0
    else {
14281
        /* If it is a two-block object, account for base object, and
14282
           for character block if present. */
14283
0
        size = sizeof(PyUnicodeObject);
14284
0
        if (_PyUnicode_DATA_ANY(self))
14285
0
            size += (PyUnicode_GET_LENGTH(self) + 1) *
14286
0
                PyUnicode_KIND(self);
14287
0
    }
14288
0
    if (_PyUnicode_HAS_UTF8_MEMORY(self))
14289
0
        size += PyUnicode_UTF8_LENGTH(self) + 1;
14290
14291
0
    return PyLong_FromSsize_t(size);
14292
0
}
14293
14294
static PyObject *
14295
unicode_getnewargs(PyObject *v, PyObject *Py_UNUSED(ignored))
14296
0
{
14297
0
    PyObject *copy = _PyUnicode_Copy(v);
14298
0
    if (!copy)
14299
0
        return NULL;
14300
0
    return Py_BuildValue("(N)", copy);
14301
0
}
14302
14303
/*
14304
This function searchs the longest common leading whitespace
14305
of all lines in the [src, end).
14306
It returns the length of the common leading whitespace and sets `output` to
14307
point to the beginning of the common leading whitespace if length > 0.
14308
*/
14309
static Py_ssize_t
14310
search_longest_common_leading_whitespace(
14311
    const char *const src,
14312
    const char *const end,
14313
    const char **output)
14314
0
{
14315
    // [_start, _start + _len)
14316
    // describes the current longest common leading whitespace
14317
0
    const char *_start = NULL;
14318
0
    Py_ssize_t _len = 0;
14319
14320
0
    for (const char *iter = src; iter < end; ++iter) {
14321
0
        const char *line_start = iter;
14322
0
        const char *leading_whitespace_end = NULL;
14323
14324
        // scan the whole line
14325
0
        while (iter < end && *iter != '\n') {
14326
0
            if (!leading_whitespace_end && *iter != ' ' && *iter != '\t') {
14327
                /* `iter` points to the first non-whitespace character
14328
                   in this line */
14329
0
                if (iter == line_start) {
14330
                    // some line has no indent, fast exit!
14331
0
                    return 0;
14332
0
                }
14333
0
                leading_whitespace_end = iter;
14334
0
            }
14335
0
            ++iter;
14336
0
        }
14337
14338
        // if this line has all white space, skip it
14339
0
        if (!leading_whitespace_end) {
14340
0
            continue;
14341
0
        }
14342
14343
0
        if (!_start) {
14344
            // update the first leading whitespace
14345
0
            _start = line_start;
14346
0
            _len = leading_whitespace_end - line_start;
14347
0
            assert(_len > 0);
14348
0
        }
14349
0
        else {
14350
            /* We then compare with the current longest leading whitespace.
14351
14352
               [line_start, leading_whitespace_end) is the leading
14353
               whitespace of this line,
14354
14355
               [_start, _start + _len) is the leading whitespace of the
14356
               current longest leading whitespace. */
14357
0
            Py_ssize_t new_len = 0;
14358
0
            const char *_iter = _start, *line_iter = line_start;
14359
14360
0
            while (_iter < _start + _len && line_iter < leading_whitespace_end
14361
0
                   && *_iter == *line_iter)
14362
0
            {
14363
0
                ++_iter;
14364
0
                ++line_iter;
14365
0
                ++new_len;
14366
0
            }
14367
14368
0
            _len = new_len;
14369
0
            if (_len == 0) {
14370
                // No common things now, fast exit!
14371
0
                return 0;
14372
0
            }
14373
0
        }
14374
0
    }
14375
14376
0
    assert(_len >= 0);
14377
0
    if (_len > 0) {
14378
0
        *output = _start;
14379
0
    }
14380
0
    return _len;
14381
0
}
14382
14383
/* Dedent a string.
14384
   Behaviour is expected to be an exact match of `textwrap.dedent`.
14385
   Return a new reference on success, NULL with exception set on error.
14386
   */
14387
PyObject *
14388
_PyUnicode_Dedent(PyObject *unicode)
14389
0
{
14390
0
    Py_ssize_t src_len = 0;
14391
0
    const char *src = PyUnicode_AsUTF8AndSize(unicode, &src_len);
14392
0
    if (!src) {
14393
0
        return NULL;
14394
0
    }
14395
0
    assert(src_len >= 0);
14396
0
    if (src_len == 0) {
14397
0
        return Py_NewRef(unicode);
14398
0
    }
14399
14400
0
    const char *const end = src + src_len;
14401
14402
    // [whitespace_start, whitespace_start + whitespace_len)
14403
    // describes the current longest common leading whitespace
14404
0
    const char *whitespace_start = NULL;
14405
0
    Py_ssize_t whitespace_len = search_longest_common_leading_whitespace(
14406
0
        src, end, &whitespace_start);
14407
14408
0
    if (whitespace_len == 0) {
14409
0
        return Py_NewRef(unicode);
14410
0
    }
14411
14412
    // now we should trigger a dedent
14413
0
    char *dest = PyMem_Malloc(src_len);
14414
0
    if (!dest) {
14415
0
        PyErr_NoMemory();
14416
0
        return NULL;
14417
0
    }
14418
0
    char *dest_iter = dest;
14419
14420
0
    for (const char *iter = src; iter < end; ++iter) {
14421
0
        const char *line_start = iter;
14422
0
        bool in_leading_space = true;
14423
14424
        // iterate over a line to find the end of a line
14425
0
        while (iter < end && *iter != '\n') {
14426
0
            if (in_leading_space && *iter != ' ' && *iter != '\t') {
14427
0
                in_leading_space = false;
14428
0
            }
14429
0
            ++iter;
14430
0
        }
14431
14432
        // invariant: *iter == '\n' or iter == end
14433
0
        bool append_newline = iter < end;
14434
14435
        // if this line has all white space, write '\n' and continue
14436
0
        if (in_leading_space && append_newline) {
14437
0
            *dest_iter++ = '\n';
14438
0
            continue;
14439
0
        }
14440
14441
        /* copy [new_line_start + whitespace_len, iter) to buffer, then
14442
            conditionally append '\n' */
14443
14444
0
        Py_ssize_t new_line_len = iter - line_start - whitespace_len;
14445
0
        assert(new_line_len >= 0);
14446
0
        memcpy(dest_iter, line_start + whitespace_len, new_line_len);
14447
14448
0
        dest_iter += new_line_len;
14449
14450
0
        if (append_newline) {
14451
0
            *dest_iter++ = '\n';
14452
0
        }
14453
0
    }
14454
14455
0
    PyObject *res = PyUnicode_FromStringAndSize(dest, dest_iter - dest);
14456
0
    PyMem_Free(dest);
14457
0
    return res;
14458
0
}
14459
14460
static PyMethodDef unicode_methods[] = {
14461
    UNICODE_ENCODE_METHODDEF
14462
    UNICODE_REPLACE_METHODDEF
14463
    UNICODE_SPLIT_METHODDEF
14464
    UNICODE_RSPLIT_METHODDEF
14465
    UNICODE_JOIN_METHODDEF
14466
    UNICODE_CAPITALIZE_METHODDEF
14467
    UNICODE_CASEFOLD_METHODDEF
14468
    UNICODE_TITLE_METHODDEF
14469
    UNICODE_CENTER_METHODDEF
14470
    UNICODE_COUNT_METHODDEF
14471
    UNICODE_EXPANDTABS_METHODDEF
14472
    UNICODE_FIND_METHODDEF
14473
    UNICODE_PARTITION_METHODDEF
14474
    UNICODE_INDEX_METHODDEF
14475
    UNICODE_LJUST_METHODDEF
14476
    UNICODE_LOWER_METHODDEF
14477
    UNICODE_LSTRIP_METHODDEF
14478
    UNICODE_RFIND_METHODDEF
14479
    UNICODE_RINDEX_METHODDEF
14480
    UNICODE_RJUST_METHODDEF
14481
    UNICODE_RSTRIP_METHODDEF
14482
    UNICODE_RPARTITION_METHODDEF
14483
    UNICODE_SPLITLINES_METHODDEF
14484
    UNICODE_STRIP_METHODDEF
14485
    UNICODE_SWAPCASE_METHODDEF
14486
    UNICODE_TRANSLATE_METHODDEF
14487
    UNICODE_UPPER_METHODDEF
14488
    UNICODE_STARTSWITH_METHODDEF
14489
    UNICODE_ENDSWITH_METHODDEF
14490
    UNICODE_REMOVEPREFIX_METHODDEF
14491
    UNICODE_REMOVESUFFIX_METHODDEF
14492
    UNICODE_ISASCII_METHODDEF
14493
    UNICODE_ISLOWER_METHODDEF
14494
    UNICODE_ISUPPER_METHODDEF
14495
    UNICODE_ISTITLE_METHODDEF
14496
    UNICODE_ISSPACE_METHODDEF
14497
    UNICODE_ISDECIMAL_METHODDEF
14498
    UNICODE_ISDIGIT_METHODDEF
14499
    UNICODE_ISNUMERIC_METHODDEF
14500
    UNICODE_ISALPHA_METHODDEF
14501
    UNICODE_ISALNUM_METHODDEF
14502
    UNICODE_ISIDENTIFIER_METHODDEF
14503
    UNICODE_ISPRINTABLE_METHODDEF
14504
    UNICODE_ZFILL_METHODDEF
14505
    {"format", _PyCFunction_CAST(do_string_format), METH_VARARGS | METH_KEYWORDS, format__doc__},
14506
    {"format_map", do_string_format_map, METH_O, format_map__doc__},
14507
    UNICODE___FORMAT___METHODDEF
14508
    UNICODE_MAKETRANS_METHODDEF
14509
    UNICODE_SIZEOF_METHODDEF
14510
    {"__getnewargs__",  unicode_getnewargs, METH_NOARGS},
14511
    {NULL, NULL}
14512
};
14513
14514
static PyObject *
14515
unicode_mod(PyObject *v, PyObject *w)
14516
26.6M
{
14517
26.6M
    if (!PyUnicode_Check(v))
14518
0
        Py_RETURN_NOTIMPLEMENTED;
14519
26.6M
    return PyUnicode_Format(v, w);
14520
26.6M
}
14521
14522
static PyNumberMethods unicode_as_number = {
14523
    0,              /*nb_add*/
14524
    0,              /*nb_subtract*/
14525
    0,              /*nb_multiply*/
14526
    unicode_mod,            /*nb_remainder*/
14527
};
14528
14529
static PySequenceMethods unicode_as_sequence = {
14530
    unicode_length,     /* sq_length */
14531
    PyUnicode_Concat,   /* sq_concat */
14532
    unicode_repeat,     /* sq_repeat */
14533
    unicode_getitem,    /* sq_item */
14534
    0,                  /* sq_slice */
14535
    0,                  /* sq_ass_item */
14536
    0,                  /* sq_ass_slice */
14537
    PyUnicode_Contains, /* sq_contains */
14538
};
14539
14540
static PyObject*
14541
unicode_subscript(PyObject* self, PyObject* item)
14542
149M
{
14543
149M
    if (_PyIndex_Check(item)) {
14544
57.8M
        Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
14545
57.8M
        if (i == -1 && PyErr_Occurred())
14546
0
            return NULL;
14547
57.8M
        if (i < 0)
14548
56.9k
            i += PyUnicode_GET_LENGTH(self);
14549
57.8M
        return unicode_getitem(self, i);
14550
91.8M
    } else if (PySlice_Check(item)) {
14551
91.8M
        Py_ssize_t start, stop, step, slicelength, i;
14552
91.8M
        size_t cur;
14553
91.8M
        PyObject *result;
14554
91.8M
        const void *src_data;
14555
91.8M
        void *dest_data;
14556
91.8M
        int src_kind, dest_kind;
14557
91.8M
        Py_UCS4 ch, max_char, kind_limit;
14558
14559
91.8M
        if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
14560
0
            return NULL;
14561
0
        }
14562
91.8M
        slicelength = PySlice_AdjustIndices(PyUnicode_GET_LENGTH(self),
14563
91.8M
                                            &start, &stop, step);
14564
14565
91.8M
        if (slicelength <= 0) {
14566
16.9M
            _Py_RETURN_UNICODE_EMPTY();
14567
74.9M
        } else if (start == 0 && step == 1 &&
14568
74.9M
                   slicelength == PyUnicode_GET_LENGTH(self)) {
14569
7.46M
            return unicode_result_unchanged(self);
14570
67.4M
        } else if (step == 1) {
14571
67.4M
            return PyUnicode_Substring(self,
14572
67.4M
                                       start, start + slicelength);
14573
67.4M
        }
14574
        /* General case */
14575
0
        src_kind = PyUnicode_KIND(self);
14576
0
        src_data = PyUnicode_DATA(self);
14577
0
        if (!PyUnicode_IS_ASCII(self)) {
14578
0
            kind_limit = kind_maxchar_limit(src_kind);
14579
0
            max_char = 0;
14580
0
            for (cur = start, i = 0; i < slicelength; cur += step, i++) {
14581
0
                ch = PyUnicode_READ(src_kind, src_data, cur);
14582
0
                if (ch > max_char) {
14583
0
                    max_char = ch;
14584
0
                    if (max_char >= kind_limit)
14585
0
                        break;
14586
0
                }
14587
0
            }
14588
0
        }
14589
0
        else
14590
0
            max_char = 127;
14591
0
        result = PyUnicode_New(slicelength, max_char);
14592
0
        if (result == NULL)
14593
0
            return NULL;
14594
0
        dest_kind = PyUnicode_KIND(result);
14595
0
        dest_data = PyUnicode_DATA(result);
14596
14597
0
        for (cur = start, i = 0; i < slicelength; cur += step, i++) {
14598
0
            Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur);
14599
0
            PyUnicode_WRITE(dest_kind, dest_data, i, ch);
14600
0
        }
14601
0
        assert(_PyUnicode_CheckConsistency(result, 1));
14602
0
        return result;
14603
0
    } else {
14604
0
        PyErr_Format(PyExc_TypeError, "string indices must be integers, not '%.200s'",
14605
0
                     Py_TYPE(item)->tp_name);
14606
0
        return NULL;
14607
0
    }
14608
149M
}
14609
14610
static PyMappingMethods unicode_as_mapping = {
14611
    unicode_length,     /* mp_length */
14612
    unicode_subscript,  /* mp_subscript */
14613
    0,                  /* mp_ass_subscript */
14614
};
14615
14616
14617
/* Helpers for PyUnicode_Format() */
14618
14619
struct unicode_formatter_t {
14620
    PyObject *args;
14621
    int args_owned;
14622
    Py_ssize_t arglen, argidx;
14623
    PyObject *dict;
14624
14625
    int fmtkind;
14626
    Py_ssize_t fmtcnt, fmtpos;
14627
    const void *fmtdata;
14628
    PyObject *fmtstr;
14629
14630
    _PyUnicodeWriter writer;
14631
};
14632
14633
struct unicode_format_arg_t {
14634
    Py_UCS4 ch;
14635
    int flags;
14636
    Py_ssize_t width;
14637
    int prec;
14638
    int sign;
14639
};
14640
14641
static PyObject *
14642
unicode_format_getnextarg(struct unicode_formatter_t *ctx)
14643
52.0M
{
14644
52.0M
    Py_ssize_t argidx = ctx->argidx;
14645
14646
52.0M
    if (argidx < ctx->arglen) {
14647
52.0M
        ctx->argidx++;
14648
52.0M
        if (ctx->arglen < 0)
14649
20.2M
            return ctx->args;
14650
31.8M
        else
14651
31.8M
            return PyTuple_GetItem(ctx->args, argidx);
14652
52.0M
    }
14653
0
    PyErr_SetString(PyExc_TypeError,
14654
0
                    "not enough arguments for format string");
14655
0
    return NULL;
14656
52.0M
}
14657
14658
/* Returns a new reference to a PyUnicode object, or NULL on failure. */
14659
14660
/* Format a float into the writer if the writer is not NULL, or into *p_output
14661
   otherwise.
14662
14663
   Return 0 on success, raise an exception and return -1 on error. */
14664
static int
14665
formatfloat(PyObject *v, struct unicode_format_arg_t *arg,
14666
            PyObject **p_output,
14667
            _PyUnicodeWriter *writer)
14668
0
{
14669
0
    char *p;
14670
0
    double x;
14671
0
    Py_ssize_t len;
14672
0
    int prec;
14673
0
    int dtoa_flags = 0;
14674
14675
0
    x = PyFloat_AsDouble(v);
14676
0
    if (x == -1.0 && PyErr_Occurred())
14677
0
        return -1;
14678
14679
0
    prec = arg->prec;
14680
0
    if (prec < 0)
14681
0
        prec = 6;
14682
14683
0
    if (arg->flags & F_ALT)
14684
0
        dtoa_flags |= Py_DTSF_ALT;
14685
0
    p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL);
14686
0
    if (p == NULL)
14687
0
        return -1;
14688
0
    len = strlen(p);
14689
0
    if (writer) {
14690
0
        if (_PyUnicodeWriter_WriteASCIIString(writer, p, len) < 0) {
14691
0
            PyMem_Free(p);
14692
0
            return -1;
14693
0
        }
14694
0
    }
14695
0
    else
14696
0
        *p_output = _PyUnicode_FromASCII(p, len);
14697
0
    PyMem_Free(p);
14698
0
    return 0;
14699
0
}
14700
14701
/* formatlong() emulates the format codes d, u, o, x and X, and
14702
 * the F_ALT flag, for Python's long (unbounded) ints.  It's not used for
14703
 * Python's regular ints.
14704
 * Return value:  a new PyUnicodeObject*, or NULL if error.
14705
 *     The output string is of the form
14706
 *         "-"? ("0x" | "0X")? digit+
14707
 *     "0x"/"0X" are present only for x and X conversions, with F_ALT
14708
 *         set in flags.  The case of hex digits will be correct,
14709
 *     There will be at least prec digits, zero-filled on the left if
14710
 *         necessary to get that many.
14711
 * val          object to be converted
14712
 * flags        bitmask of format flags; only F_ALT is looked at
14713
 * prec         minimum number of digits; 0-fill on left if needed
14714
 * type         a character in [duoxX]; u acts the same as d
14715
 *
14716
 * CAUTION:  o, x and X conversions on regular ints can never
14717
 * produce a '-' sign, but can for Python's unbounded ints.
14718
 */
14719
PyObject *
14720
_PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type)
14721
1.53k
{
14722
1.53k
    PyObject *result = NULL;
14723
1.53k
    char *buf;
14724
1.53k
    Py_ssize_t i;
14725
1.53k
    int sign;           /* 1 if '-', else 0 */
14726
1.53k
    int len;            /* number of characters */
14727
1.53k
    Py_ssize_t llen;
14728
1.53k
    int numdigits;      /* len == numnondigits + numdigits */
14729
1.53k
    int numnondigits = 0;
14730
14731
    /* Avoid exceeding SSIZE_T_MAX */
14732
1.53k
    if (prec > INT_MAX-3) {
14733
0
        PyErr_SetString(PyExc_OverflowError,
14734
0
                        "precision too large");
14735
0
        return NULL;
14736
0
    }
14737
14738
1.53k
    assert(PyLong_Check(val));
14739
14740
1.53k
    switch (type) {
14741
0
    default:
14742
0
        Py_UNREACHABLE();
14743
0
    case 'd':
14744
0
    case 'i':
14745
0
    case 'u':
14746
        /* int and int subclasses should print numerically when a numeric */
14747
        /* format code is used (see issue18780) */
14748
0
        result = PyNumber_ToBase(val, 10);
14749
0
        break;
14750
0
    case 'o':
14751
0
        numnondigits = 2;
14752
0
        result = PyNumber_ToBase(val, 8);
14753
0
        break;
14754
0
    case 'x':
14755
1.53k
    case 'X':
14756
1.53k
        numnondigits = 2;
14757
1.53k
        result = PyNumber_ToBase(val, 16);
14758
1.53k
        break;
14759
1.53k
    }
14760
1.53k
    if (!result)
14761
0
        return NULL;
14762
14763
1.53k
    assert(unicode_modifiable(result));
14764
1.53k
    assert(PyUnicode_IS_ASCII(result));
14765
14766
    /* To modify the string in-place, there can only be one reference. */
14767
1.53k
    if (!_PyObject_IsUniquelyReferenced(result)) {
14768
0
        Py_DECREF(result);
14769
0
        PyErr_BadInternalCall();
14770
0
        return NULL;
14771
0
    }
14772
1.53k
    buf = PyUnicode_DATA(result);
14773
1.53k
    llen = PyUnicode_GET_LENGTH(result);
14774
1.53k
    if (llen > INT_MAX) {
14775
0
        Py_DECREF(result);
14776
0
        PyErr_SetString(PyExc_ValueError,
14777
0
                        "string too large in _PyUnicode_FormatLong");
14778
0
        return NULL;
14779
0
    }
14780
1.53k
    len = (int)llen;
14781
1.53k
    sign = buf[0] == '-';
14782
1.53k
    numnondigits += sign;
14783
1.53k
    numdigits = len - numnondigits;
14784
1.53k
    assert(numdigits > 0);
14785
14786
    /* Get rid of base marker unless F_ALT */
14787
1.53k
    if (((alt) == 0 &&
14788
1.53k
        (type == 'o' || type == 'x' || type == 'X'))) {
14789
1.53k
        assert(buf[sign] == '0');
14790
1.53k
        assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' ||
14791
1.53k
               buf[sign+1] == 'o');
14792
1.53k
        numnondigits -= 2;
14793
1.53k
        buf += 2;
14794
1.53k
        len -= 2;
14795
1.53k
        if (sign)
14796
0
            buf[0] = '-';
14797
1.53k
        assert(len == numnondigits + numdigits);
14798
1.53k
        assert(numdigits > 0);
14799
1.53k
    }
14800
14801
    /* Fill with leading zeroes to meet minimum width. */
14802
1.53k
    if (prec > numdigits) {
14803
0
        PyObject *r1 = PyBytes_FromStringAndSize(NULL,
14804
0
                                numnondigits + prec);
14805
0
        char *b1;
14806
0
        if (!r1) {
14807
0
            Py_DECREF(result);
14808
0
            return NULL;
14809
0
        }
14810
0
        b1 = PyBytes_AS_STRING(r1);
14811
0
        for (i = 0; i < numnondigits; ++i)
14812
0
            *b1++ = *buf++;
14813
0
        for (i = 0; i < prec - numdigits; i++)
14814
0
            *b1++ = '0';
14815
0
        for (i = 0; i < numdigits; i++)
14816
0
            *b1++ = *buf++;
14817
0
        *b1 = '\0';
14818
0
        Py_SETREF(result, r1);
14819
0
        buf = PyBytes_AS_STRING(result);
14820
0
        len = numnondigits + prec;
14821
0
    }
14822
14823
    /* Fix up case for hex conversions. */
14824
1.53k
    if (type == 'X') {
14825
        /* Need to convert all lower case letters to upper case.
14826
           and need to convert 0x to 0X (and -0x to -0X). */
14827
4.51k
        for (i = 0; i < len; i++)
14828
2.97k
            if (buf[i] >= 'a' && buf[i] <= 'x')
14829
1.15k
                buf[i] -= 'a'-'A';
14830
1.53k
    }
14831
1.53k
    if (!PyUnicode_Check(result)
14832
1.53k
        || buf != PyUnicode_DATA(result)) {
14833
1.53k
        PyObject *unicode;
14834
1.53k
        unicode = _PyUnicode_FromASCII(buf, len);
14835
1.53k
        Py_SETREF(result, unicode);
14836
1.53k
    }
14837
0
    else if (len != PyUnicode_GET_LENGTH(result)) {
14838
0
        if (PyUnicode_Resize(&result, len) < 0)
14839
0
            Py_CLEAR(result);
14840
0
    }
14841
1.53k
    return result;
14842
1.53k
}
14843
14844
/* Format an integer or a float as an integer.
14845
 * Return 1 if the number has been formatted into the writer,
14846
 *        0 if the number has been formatted into *p_output
14847
 *       -1 and raise an exception on error */
14848
static int
14849
mainformatlong(PyObject *v,
14850
               struct unicode_format_arg_t *arg,
14851
               PyObject **p_output,
14852
               _PyUnicodeWriter *writer)
14853
12.7M
{
14854
12.7M
    PyObject *iobj, *res;
14855
12.7M
    char type = (char)arg->ch;
14856
14857
12.7M
    if (!PyNumber_Check(v))
14858
5.15M
        goto wrongtype;
14859
14860
    /* make sure number is a type of integer for o, x, and X */
14861
7.62M
    if (!PyLong_Check(v)) {
14862
0
        if (type == 'o' || type == 'x' || type == 'X') {
14863
0
            iobj = _PyNumber_Index(v);
14864
0
        }
14865
0
        else {
14866
0
            iobj = PyNumber_Long(v);
14867
0
        }
14868
0
        if (iobj == NULL ) {
14869
0
            if (PyErr_ExceptionMatches(PyExc_TypeError))
14870
0
                goto wrongtype;
14871
0
            return -1;
14872
0
        }
14873
0
        assert(PyLong_Check(iobj));
14874
0
    }
14875
7.62M
    else {
14876
7.62M
        iobj = Py_NewRef(v);
14877
7.62M
    }
14878
14879
7.62M
    if (PyLong_CheckExact(v)
14880
7.62M
        && arg->width == -1 && arg->prec == -1
14881
7.62M
        && !(arg->flags & (F_SIGN | F_BLANK))
14882
7.62M
        && type != 'X')
14883
7.62M
    {
14884
        /* Fast path */
14885
7.62M
        int alternate = arg->flags & F_ALT;
14886
7.62M
        int base;
14887
14888
7.62M
        switch(type)
14889
7.62M
        {
14890
0
            default:
14891
0
                Py_UNREACHABLE();
14892
7.62M
            case 'd':
14893
7.62M
            case 'i':
14894
7.62M
            case 'u':
14895
7.62M
                base = 10;
14896
7.62M
                break;
14897
0
            case 'o':
14898
0
                base = 8;
14899
0
                break;
14900
0
            case 'x':
14901
0
            case 'X':
14902
0
                base = 16;
14903
0
                break;
14904
7.62M
        }
14905
14906
7.62M
        if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) {
14907
0
            Py_DECREF(iobj);
14908
0
            return -1;
14909
0
        }
14910
7.62M
        Py_DECREF(iobj);
14911
7.62M
        return 1;
14912
7.62M
    }
14913
14914
1.53k
    res = _PyUnicode_FormatLong(iobj, arg->flags & F_ALT, arg->prec, type);
14915
1.53k
    Py_DECREF(iobj);
14916
1.53k
    if (res == NULL)
14917
0
        return -1;
14918
1.53k
    *p_output = res;
14919
1.53k
    return 0;
14920
14921
5.15M
wrongtype:
14922
5.15M
    switch(type)
14923
5.15M
    {
14924
0
        case 'o':
14925
0
        case 'x':
14926
0
        case 'X':
14927
0
            PyErr_Format(PyExc_TypeError,
14928
0
                    "%%%c format: an integer is required, "
14929
0
                    "not %.200s",
14930
0
                    type, Py_TYPE(v)->tp_name);
14931
0
            break;
14932
5.15M
        default:
14933
5.15M
            PyErr_Format(PyExc_TypeError,
14934
5.15M
                    "%%%c format: a real number is required, "
14935
5.15M
                    "not %.200s",
14936
5.15M
                    type, Py_TYPE(v)->tp_name);
14937
5.15M
            break;
14938
5.15M
    }
14939
5.15M
    return -1;
14940
5.15M
}
14941
14942
static Py_UCS4
14943
formatchar(PyObject *v)
14944
0
{
14945
    /* presume that the buffer is at least 3 characters long */
14946
0
    if (PyUnicode_Check(v)) {
14947
0
        if (PyUnicode_GET_LENGTH(v) == 1) {
14948
0
            return PyUnicode_READ_CHAR(v, 0);
14949
0
        }
14950
0
        PyErr_Format(PyExc_TypeError,
14951
0
                     "%%c requires an int or a unicode character, "
14952
0
                     "not a string of length %zd",
14953
0
                     PyUnicode_GET_LENGTH(v));
14954
0
        return (Py_UCS4) -1;
14955
0
    }
14956
0
    else {
14957
0
        int overflow;
14958
0
        long x = PyLong_AsLongAndOverflow(v, &overflow);
14959
0
        if (x == -1 && PyErr_Occurred()) {
14960
0
            if (PyErr_ExceptionMatches(PyExc_TypeError)) {
14961
0
                PyErr_Format(PyExc_TypeError,
14962
0
                             "%%c requires an int or a unicode character, not %T",
14963
0
                             v);
14964
0
                return (Py_UCS4) -1;
14965
0
            }
14966
0
            return (Py_UCS4) -1;
14967
0
        }
14968
14969
0
        if (x < 0 || x > MAX_UNICODE) {
14970
            /* this includes an overflow in converting to C long */
14971
0
            PyErr_SetString(PyExc_OverflowError,
14972
0
                            "%c arg not in range(0x110000)");
14973
0
            return (Py_UCS4) -1;
14974
0
        }
14975
14976
0
        return (Py_UCS4) x;
14977
0
    }
14978
0
}
14979
14980
/* Parse options of an argument: flags, width, precision.
14981
   Handle also "%(name)" syntax.
14982
14983
   Return 0 if the argument has been formatted into arg->str.
14984
   Return 1 if the argument has been written into ctx->writer,
14985
   Raise an exception and return -1 on error. */
14986
static int
14987
unicode_format_arg_parse(struct unicode_formatter_t *ctx,
14988
                         struct unicode_format_arg_t *arg)
14989
52.0M
{
14990
52.0M
#define FORMAT_READ(ctx) \
14991
52.3M
        PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos)
14992
14993
52.0M
    PyObject *v;
14994
14995
52.0M
    if (arg->ch == '(') {
14996
        /* Get argument value from a dictionary. Example: "%(name)s". */
14997
38.6k
        Py_ssize_t keystart;
14998
38.6k
        Py_ssize_t keylen;
14999
38.6k
        PyObject *key;
15000
38.6k
        int pcount = 1;
15001
15002
38.6k
        if (ctx->dict == NULL) {
15003
0
            PyErr_SetString(PyExc_TypeError,
15004
0
                            "format requires a mapping");
15005
0
            return -1;
15006
0
        }
15007
38.6k
        ++ctx->fmtpos;
15008
38.6k
        --ctx->fmtcnt;
15009
38.6k
        keystart = ctx->fmtpos;
15010
        /* Skip over balanced parentheses */
15011
347k
        while (pcount > 0 && --ctx->fmtcnt >= 0) {
15012
309k
            arg->ch = FORMAT_READ(ctx);
15013
309k
            if (arg->ch == ')')
15014
38.6k
                --pcount;
15015
270k
            else if (arg->ch == '(')
15016
0
                ++pcount;
15017
309k
            ctx->fmtpos++;
15018
309k
        }
15019
38.6k
        keylen = ctx->fmtpos - keystart - 1;
15020
38.6k
        if (ctx->fmtcnt < 0 || pcount > 0) {
15021
0
            PyErr_SetString(PyExc_ValueError,
15022
0
                            "incomplete format key");
15023
0
            return -1;
15024
0
        }
15025
38.6k
        key = PyUnicode_Substring(ctx->fmtstr,
15026
38.6k
                                  keystart, keystart + keylen);
15027
38.6k
        if (key == NULL)
15028
0
            return -1;
15029
38.6k
        if (ctx->args_owned) {
15030
27.6k
            ctx->args_owned = 0;
15031
27.6k
            Py_DECREF(ctx->args);
15032
27.6k
        }
15033
38.6k
        ctx->args = PyObject_GetItem(ctx->dict, key);
15034
38.6k
        Py_DECREF(key);
15035
38.6k
        if (ctx->args == NULL)
15036
0
            return -1;
15037
38.6k
        ctx->args_owned = 1;
15038
38.6k
        ctx->arglen = -1;
15039
38.6k
        ctx->argidx = -2;
15040
38.6k
    }
15041
15042
    /* Parse flags. Example: "%+i" => flags=F_SIGN. */
15043
52.0M
    while (--ctx->fmtcnt >= 0) {
15044
52.0M
        arg->ch = FORMAT_READ(ctx);
15045
52.0M
        ctx->fmtpos++;
15046
52.0M
        switch (arg->ch) {
15047
0
        case '-': arg->flags |= F_LJUST; continue;
15048
0
        case '+': arg->flags |= F_SIGN; continue;
15049
0
        case ' ': arg->flags |= F_BLANK; continue;
15050
0
        case '#': arg->flags |= F_ALT; continue;
15051
1.53k
        case '0': arg->flags |= F_ZERO; continue;
15052
52.0M
        }
15053
52.0M
        break;
15054
52.0M
    }
15055
15056
    /* Parse width. Example: "%10s" => width=10 */
15057
52.0M
    if (arg->ch == '*') {
15058
0
        v = unicode_format_getnextarg(ctx);
15059
0
        if (v == NULL)
15060
0
            return -1;
15061
0
        if (!PyLong_Check(v)) {
15062
0
            PyErr_SetString(PyExc_TypeError,
15063
0
                            "* wants int");
15064
0
            return -1;
15065
0
        }
15066
0
        arg->width = PyLong_AsSsize_t(v);
15067
0
        if (arg->width == -1 && PyErr_Occurred())
15068
0
            return -1;
15069
0
        if (arg->width < 0) {
15070
0
            arg->flags |= F_LJUST;
15071
0
            arg->width = -arg->width;
15072
0
        }
15073
0
        if (--ctx->fmtcnt >= 0) {
15074
0
            arg->ch = FORMAT_READ(ctx);
15075
0
            ctx->fmtpos++;
15076
0
        }
15077
0
    }
15078
52.0M
    else if (arg->ch >= '0' && arg->ch <= '9') {
15079
1.53k
        arg->width = arg->ch - '0';
15080
1.53k
        while (--ctx->fmtcnt >= 0) {
15081
1.53k
            arg->ch = FORMAT_READ(ctx);
15082
1.53k
            ctx->fmtpos++;
15083
1.53k
            if (arg->ch < '0' || arg->ch > '9')
15084
1.53k
                break;
15085
            /* Since arg->ch is unsigned, the RHS would end up as unsigned,
15086
               mixing signed and unsigned comparison. Since arg->ch is between
15087
               '0' and '9', casting to int is safe. */
15088
0
            if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) {
15089
0
                PyErr_SetString(PyExc_ValueError,
15090
0
                                "width too big");
15091
0
                return -1;
15092
0
            }
15093
0
            arg->width = arg->width*10 + (arg->ch - '0');
15094
0
        }
15095
1.53k
    }
15096
15097
    /* Parse precision. Example: "%.3f" => prec=3 */
15098
52.0M
    if (arg->ch == '.') {
15099
0
        arg->prec = 0;
15100
0
        if (--ctx->fmtcnt >= 0) {
15101
0
            arg->ch = FORMAT_READ(ctx);
15102
0
            ctx->fmtpos++;
15103
0
        }
15104
0
        if (arg->ch == '*') {
15105
0
            v = unicode_format_getnextarg(ctx);
15106
0
            if (v == NULL)
15107
0
                return -1;
15108
0
            if (!PyLong_Check(v)) {
15109
0
                PyErr_SetString(PyExc_TypeError,
15110
0
                                "* wants int");
15111
0
                return -1;
15112
0
            }
15113
0
            arg->prec = PyLong_AsInt(v);
15114
0
            if (arg->prec == -1 && PyErr_Occurred())
15115
0
                return -1;
15116
0
            if (arg->prec < 0)
15117
0
                arg->prec = 0;
15118
0
            if (--ctx->fmtcnt >= 0) {
15119
0
                arg->ch = FORMAT_READ(ctx);
15120
0
                ctx->fmtpos++;
15121
0
            }
15122
0
        }
15123
0
        else if (arg->ch >= '0' && arg->ch <= '9') {
15124
0
            arg->prec = arg->ch - '0';
15125
0
            while (--ctx->fmtcnt >= 0) {
15126
0
                arg->ch = FORMAT_READ(ctx);
15127
0
                ctx->fmtpos++;
15128
0
                if (arg->ch < '0' || arg->ch > '9')
15129
0
                    break;
15130
0
                if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) {
15131
0
                    PyErr_SetString(PyExc_ValueError,
15132
0
                                    "precision too big");
15133
0
                    return -1;
15134
0
                }
15135
0
                arg->prec = arg->prec*10 + (arg->ch - '0');
15136
0
            }
15137
0
        }
15138
0
    }
15139
15140
    /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */
15141
52.0M
    if (ctx->fmtcnt >= 0) {
15142
52.0M
        if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') {
15143
0
            if (--ctx->fmtcnt >= 0) {
15144
0
                arg->ch = FORMAT_READ(ctx);
15145
0
                ctx->fmtpos++;
15146
0
            }
15147
0
        }
15148
52.0M
    }
15149
52.0M
    if (ctx->fmtcnt < 0) {
15150
0
        PyErr_SetString(PyExc_ValueError,
15151
0
                        "incomplete format");
15152
0
        return -1;
15153
0
    }
15154
52.0M
    return 0;
15155
15156
52.0M
#undef FORMAT_READ
15157
52.0M
}
15158
15159
/* Format one argument. Supported conversion specifiers:
15160
15161
   - "s", "r", "a": any type
15162
   - "i", "d", "u": int or float
15163
   - "o", "x", "X": int
15164
   - "e", "E", "f", "F", "g", "G": float
15165
   - "c": int or str (1 character)
15166
15167
   When possible, the output is written directly into the Unicode writer
15168
   (ctx->writer). A string is created when padding is required.
15169
15170
   Return 0 if the argument has been formatted into *p_str,
15171
          1 if the argument has been written into ctx->writer,
15172
         -1 on error. */
15173
static int
15174
unicode_format_arg_format(struct unicode_formatter_t *ctx,
15175
                          struct unicode_format_arg_t *arg,
15176
                          PyObject **p_str)
15177
52.0M
{
15178
52.0M
    PyObject *v;
15179
52.0M
    _PyUnicodeWriter *writer = &ctx->writer;
15180
15181
52.0M
    if (ctx->fmtcnt == 0)
15182
12.6M
        ctx->writer.overallocate = 0;
15183
15184
52.0M
    v = unicode_format_getnextarg(ctx);
15185
52.0M
    if (v == NULL)
15186
0
        return -1;
15187
15188
15189
52.0M
    switch (arg->ch) {
15190
39.2M
    case 's':
15191
39.2M
    case 'r':
15192
39.2M
    case 'a':
15193
39.2M
        if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) {
15194
            /* Fast path */
15195
0
            if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1)
15196
0
                return -1;
15197
0
            return 1;
15198
0
        }
15199
15200
39.2M
        if (PyUnicode_CheckExact(v) && arg->ch == 's') {
15201
39.2M
            *p_str = Py_NewRef(v);
15202
39.2M
        }
15203
0
        else {
15204
0
            if (arg->ch == 's')
15205
0
                *p_str = PyObject_Str(v);
15206
0
            else if (arg->ch == 'r')
15207
0
                *p_str = PyObject_Repr(v);
15208
0
            else
15209
0
                *p_str = PyObject_ASCII(v);
15210
0
        }
15211
39.2M
        break;
15212
15213
0
    case 'i':
15214
12.7M
    case 'd':
15215
12.7M
    case 'u':
15216
12.7M
    case 'o':
15217
12.7M
    case 'x':
15218
12.7M
    case 'X':
15219
12.7M
    {
15220
12.7M
        int ret = mainformatlong(v, arg, p_str, writer);
15221
12.7M
        if (ret != 0)
15222
12.7M
            return ret;
15223
1.53k
        arg->sign = 1;
15224
1.53k
        break;
15225
12.7M
    }
15226
15227
0
    case 'e':
15228
0
    case 'E':
15229
0
    case 'f':
15230
0
    case 'F':
15231
0
    case 'g':
15232
0
    case 'G':
15233
0
        if (arg->width == -1 && arg->prec == -1
15234
0
            && !(arg->flags & (F_SIGN | F_BLANK)))
15235
0
        {
15236
            /* Fast path */
15237
0
            if (formatfloat(v, arg, NULL, writer) == -1)
15238
0
                return -1;
15239
0
            return 1;
15240
0
        }
15241
15242
0
        arg->sign = 1;
15243
0
        if (formatfloat(v, arg, p_str, NULL) == -1)
15244
0
            return -1;
15245
0
        break;
15246
15247
0
    case 'c':
15248
0
    {
15249
0
        Py_UCS4 ch = formatchar(v);
15250
0
        if (ch == (Py_UCS4) -1)
15251
0
            return -1;
15252
0
        if (arg->width == -1 && arg->prec == -1) {
15253
            /* Fast path */
15254
0
            if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0)
15255
0
                return -1;
15256
0
            return 1;
15257
0
        }
15258
0
        *p_str = PyUnicode_FromOrdinal(ch);
15259
0
        break;
15260
0
    }
15261
15262
0
    default:
15263
0
        PyErr_Format(PyExc_ValueError,
15264
0
                     "unsupported format character '%c' (0x%x) "
15265
0
                     "at index %zd",
15266
0
                     (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?',
15267
0
                     (int)arg->ch,
15268
0
                     ctx->fmtpos - 1);
15269
0
        return -1;
15270
52.0M
    }
15271
39.2M
    if (*p_str == NULL)
15272
0
        return -1;
15273
39.2M
    assert (PyUnicode_Check(*p_str));
15274
39.2M
    return 0;
15275
39.2M
}
15276
15277
static int
15278
unicode_format_arg_output(struct unicode_formatter_t *ctx,
15279
                          struct unicode_format_arg_t *arg,
15280
                          PyObject *str)
15281
39.2M
{
15282
39.2M
    Py_ssize_t len;
15283
39.2M
    int kind;
15284
39.2M
    const void *pbuf;
15285
39.2M
    Py_ssize_t pindex;
15286
39.2M
    Py_UCS4 signchar;
15287
39.2M
    Py_ssize_t buflen;
15288
39.2M
    Py_UCS4 maxchar;
15289
39.2M
    Py_ssize_t sublen;
15290
39.2M
    _PyUnicodeWriter *writer = &ctx->writer;
15291
39.2M
    Py_UCS4 fill;
15292
15293
39.2M
    fill = ' ';
15294
39.2M
    if (arg->sign && arg->flags & F_ZERO)
15295
1.53k
        fill = '0';
15296
15297
39.2M
    len = PyUnicode_GET_LENGTH(str);
15298
39.2M
    if ((arg->width == -1 || arg->width <= len)
15299
39.2M
        && (arg->prec == -1 || arg->prec >= len)
15300
39.2M
        && !(arg->flags & (F_SIGN | F_BLANK)))
15301
39.2M
    {
15302
        /* Fast path */
15303
39.2M
        if (_PyUnicodeWriter_WriteStr(writer, str) == -1)
15304
0
            return -1;
15305
39.2M
        return 0;
15306
39.2M
    }
15307
15308
    /* Truncate the string for "s", "r" and "a" formats
15309
       if the precision is set */
15310
96
    if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') {
15311
0
        if (arg->prec >= 0 && len > arg->prec)
15312
0
            len = arg->prec;
15313
0
    }
15314
15315
    /* Adjust sign and width */
15316
96
    kind = PyUnicode_KIND(str);
15317
96
    pbuf = PyUnicode_DATA(str);
15318
96
    pindex = 0;
15319
96
    signchar = '\0';
15320
96
    if (arg->sign) {
15321
96
        Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex);
15322
96
        if (ch == '-' || ch == '+') {
15323
0
            signchar = ch;
15324
0
            len--;
15325
0
            pindex++;
15326
0
        }
15327
96
        else if (arg->flags & F_SIGN)
15328
0
            signchar = '+';
15329
96
        else if (arg->flags & F_BLANK)
15330
0
            signchar = ' ';
15331
96
        else
15332
96
            arg->sign = 0;
15333
96
    }
15334
96
    if (arg->width < len)
15335
0
        arg->width = len;
15336
15337
    /* Prepare the writer */
15338
96
    maxchar = writer->maxchar;
15339
96
    if (!(arg->flags & F_LJUST)) {
15340
96
        if (arg->sign) {
15341
0
            if ((arg->width-1) > len)
15342
0
                maxchar = Py_MAX(maxchar, fill);
15343
0
        }
15344
96
        else {
15345
96
            if (arg->width > len)
15346
96
                maxchar = Py_MAX(maxchar, fill);
15347
96
        }
15348
96
    }
15349
96
    if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) {
15350
0
        Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len);
15351
0
        maxchar = Py_MAX(maxchar, strmaxchar);
15352
0
    }
15353
15354
96
    buflen = arg->width;
15355
96
    if (arg->sign && len == arg->width)
15356
0
        buflen++;
15357
96
    if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1)
15358
0
        return -1;
15359
15360
    /* Write the sign if needed */
15361
96
    if (arg->sign) {
15362
0
        if (fill != ' ') {
15363
0
            PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar);
15364
0
            writer->pos += 1;
15365
0
        }
15366
0
        if (arg->width > len)
15367
0
            arg->width--;
15368
0
    }
15369
15370
    /* Write the numeric prefix for "x", "X" and "o" formats
15371
       if the alternate form is used.
15372
       For example, write "0x" for the "%#x" format. */
15373
96
    if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) {
15374
0
        assert(PyUnicode_READ(kind, pbuf, pindex) == '0');
15375
0
        assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch);
15376
0
        if (fill != ' ') {
15377
0
            PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0');
15378
0
            PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch);
15379
0
            writer->pos += 2;
15380
0
            pindex += 2;
15381
0
        }
15382
0
        arg->width -= 2;
15383
0
        if (arg->width < 0)
15384
0
            arg->width = 0;
15385
0
        len -= 2;
15386
0
    }
15387
15388
    /* Pad left with the fill character if needed */
15389
96
    if (arg->width > len && !(arg->flags & F_LJUST)) {
15390
96
        sublen = arg->width - len;
15391
96
        unicode_fill(writer->kind, writer->data, fill, writer->pos, sublen);
15392
96
        writer->pos += sublen;
15393
96
        arg->width = len;
15394
96
    }
15395
15396
    /* If padding with spaces: write sign if needed and/or numeric prefix if
15397
       the alternate form is used */
15398
96
    if (fill == ' ') {
15399
0
        if (arg->sign) {
15400
0
            PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar);
15401
0
            writer->pos += 1;
15402
0
        }
15403
0
        if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) {
15404
0
            assert(PyUnicode_READ(kind, pbuf, pindex) == '0');
15405
0
            assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch);
15406
0
            PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0');
15407
0
            PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch);
15408
0
            writer->pos += 2;
15409
0
            pindex += 2;
15410
0
        }
15411
0
    }
15412
15413
    /* Write characters */
15414
96
    if (len) {
15415
96
        _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
15416
96
                                      str, pindex, len);
15417
96
        writer->pos += len;
15418
96
    }
15419
15420
    /* Pad right with the fill character if needed */
15421
96
    if (arg->width > len) {
15422
0
        sublen = arg->width - len;
15423
0
        unicode_fill(writer->kind, writer->data, ' ', writer->pos, sublen);
15424
0
        writer->pos += sublen;
15425
0
    }
15426
96
    return 0;
15427
96
}
15428
15429
/* Helper of PyUnicode_Format(): format one arg.
15430
   Return 0 on success, raise an exception and return -1 on error. */
15431
static int
15432
unicode_format_arg(struct unicode_formatter_t *ctx)
15433
52.0M
{
15434
52.0M
    struct unicode_format_arg_t arg;
15435
52.0M
    PyObject *str;
15436
52.0M
    int ret;
15437
15438
52.0M
    arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos);
15439
52.0M
    if (arg.ch == '%') {
15440
0
        ctx->fmtpos++;
15441
0
        ctx->fmtcnt--;
15442
0
        if (_PyUnicodeWriter_WriteCharInline(&ctx->writer, '%') < 0)
15443
0
            return -1;
15444
0
        return 0;
15445
0
    }
15446
52.0M
    arg.flags = 0;
15447
52.0M
    arg.width = -1;
15448
52.0M
    arg.prec = -1;
15449
52.0M
    arg.sign = 0;
15450
52.0M
    str = NULL;
15451
15452
52.0M
    ret = unicode_format_arg_parse(ctx, &arg);
15453
52.0M
    if (ret == -1)
15454
0
        return -1;
15455
15456
52.0M
    ret = unicode_format_arg_format(ctx, &arg, &str);
15457
52.0M
    if (ret == -1)
15458
5.15M
        return -1;
15459
15460
46.9M
    if (ret != 1) {
15461
39.2M
        ret = unicode_format_arg_output(ctx, &arg, str);
15462
39.2M
        Py_DECREF(str);
15463
39.2M
        if (ret == -1)
15464
0
            return -1;
15465
39.2M
    }
15466
15467
46.9M
    if (ctx->dict && (ctx->argidx < ctx->arglen)) {
15468
0
        PyErr_SetString(PyExc_TypeError,
15469
0
                        "not all arguments converted during string formatting");
15470
0
        return -1;
15471
0
    }
15472
46.9M
    return 0;
15473
46.9M
}
15474
15475
PyObject *
15476
PyUnicode_Format(PyObject *format, PyObject *args)
15477
26.6M
{
15478
26.6M
    struct unicode_formatter_t ctx;
15479
15480
26.6M
    if (format == NULL || args == NULL) {
15481
0
        PyErr_BadInternalCall();
15482
0
        return NULL;
15483
0
    }
15484
15485
26.6M
    if (ensure_unicode(format) < 0)
15486
0
        return NULL;
15487
15488
26.6M
    ctx.fmtstr = format;
15489
26.6M
    ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr);
15490
26.6M
    ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr);
15491
26.6M
    ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr);
15492
26.6M
    ctx.fmtpos = 0;
15493
15494
26.6M
    _PyUnicodeWriter_Init(&ctx.writer);
15495
26.6M
    ctx.writer.min_length = ctx.fmtcnt + 100;
15496
26.6M
    ctx.writer.overallocate = 1;
15497
15498
26.6M
    if (PyTuple_Check(args)) {
15499
6.42M
        ctx.arglen = PyTuple_Size(args);
15500
6.42M
        ctx.argidx = 0;
15501
6.42M
    }
15502
20.1M
    else {
15503
20.1M
        ctx.arglen = -1;
15504
20.1M
        ctx.argidx = -2;
15505
20.1M
    }
15506
26.6M
    ctx.args_owned = 0;
15507
26.6M
    if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args))
15508
11.0k
        ctx.dict = args;
15509
26.6M
    else
15510
26.6M
        ctx.dict = NULL;
15511
26.6M
    ctx.args = args;
15512
15513
126M
    while (--ctx.fmtcnt >= 0) {
15514
105M
        if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') {
15515
53.3M
            Py_ssize_t nonfmtpos;
15516
15517
53.3M
            nonfmtpos = ctx.fmtpos++;
15518
524M
            while (ctx.fmtcnt >= 0 &&
15519
524M
                   PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') {
15520
470M
                ctx.fmtpos++;
15521
470M
                ctx.fmtcnt--;
15522
470M
            }
15523
53.3M
            if (ctx.fmtcnt < 0) {
15524
13.9M
                ctx.fmtpos--;
15525
13.9M
                ctx.writer.overallocate = 0;
15526
13.9M
            }
15527
15528
53.3M
            if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr,
15529
53.3M
                                                nonfmtpos, ctx.fmtpos) < 0)
15530
0
                goto onError;
15531
53.3M
        }
15532
52.0M
        else {
15533
52.0M
            ctx.fmtpos++;
15534
52.0M
            if (unicode_format_arg(&ctx) == -1)
15535
5.15M
                goto onError;
15536
52.0M
        }
15537
105M
    }
15538
15539
21.4M
    if (ctx.argidx < ctx.arglen && !ctx.dict) {
15540
0
        PyErr_SetString(PyExc_TypeError,
15541
0
                        "not all arguments converted during string formatting");
15542
0
        goto onError;
15543
0
    }
15544
15545
21.4M
    if (ctx.args_owned) {
15546
11.0k
        Py_DECREF(ctx.args);
15547
11.0k
    }
15548
21.4M
    return _PyUnicodeWriter_Finish(&ctx.writer);
15549
15550
5.15M
  onError:
15551
5.15M
    _PyUnicodeWriter_Dealloc(&ctx.writer);
15552
5.15M
    if (ctx.args_owned) {
15553
0
        Py_DECREF(ctx.args);
15554
0
    }
15555
5.15M
    return NULL;
15556
21.4M
}
15557
15558
static PyObject *
15559
unicode_subtype_new(PyTypeObject *type, PyObject *unicode);
15560
15561
/*[clinic input]
15562
@classmethod
15563
str.__new__ as unicode_new
15564
15565
    object as x: object = NULL
15566
    encoding: str = NULL
15567
    errors: str = NULL
15568
15569
[clinic start generated code]*/
15570
15571
static PyObject *
15572
unicode_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
15573
                 const char *errors)
15574
/*[clinic end generated code: output=fc72d4878b0b57e9 input=e81255e5676d174e]*/
15575
10.9M
{
15576
10.9M
    PyObject *unicode;
15577
10.9M
    if (x == NULL) {
15578
0
        unicode = unicode_get_empty();
15579
0
    }
15580
10.9M
    else if (encoding == NULL && errors == NULL) {
15581
10.9M
        unicode = PyObject_Str(x);
15582
10.9M
    }
15583
0
    else {
15584
0
        unicode = PyUnicode_FromEncodedObject(x, encoding, errors);
15585
0
    }
15586
15587
10.9M
    if (unicode != NULL && type != &PyUnicode_Type) {
15588
10.9M
        Py_SETREF(unicode, unicode_subtype_new(type, unicode));
15589
10.9M
    }
15590
10.9M
    return unicode;
15591
10.9M
}
15592
15593
static const char *
15594
arg_as_utf8(PyObject *obj, const char *name)
15595
954k
{
15596
954k
    if (!PyUnicode_Check(obj)) {
15597
0
        PyErr_Format(PyExc_TypeError,
15598
0
                     "str() argument '%s' must be str, not %T",
15599
0
                     name, obj);
15600
0
        return NULL;
15601
0
    }
15602
954k
    return _PyUnicode_AsUTF8NoNUL(obj);
15603
954k
}
15604
15605
static PyObject *
15606
unicode_vectorcall(PyObject *type, PyObject *const *args,
15607
                   size_t nargsf, PyObject *kwnames)
15608
736k
{
15609
736k
    assert(Py_Is(_PyType_CAST(type), &PyUnicode_Type));
15610
15611
736k
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
15612
736k
    if (kwnames != NULL && PyTuple_GET_SIZE(kwnames) != 0) {
15613
        // Fallback to unicode_new()
15614
0
        PyObject *tuple = _PyTuple_FromArray(args, nargs);
15615
0
        if (tuple == NULL) {
15616
0
            return NULL;
15617
0
        }
15618
0
        PyObject *dict = _PyStack_AsDict(args + nargs, kwnames);
15619
0
        if (dict == NULL) {
15620
0
            Py_DECREF(tuple);
15621
0
            return NULL;
15622
0
        }
15623
0
        PyObject *ret = unicode_new(_PyType_CAST(type), tuple, dict);
15624
0
        Py_DECREF(tuple);
15625
0
        Py_DECREF(dict);
15626
0
        return ret;
15627
0
    }
15628
736k
    if (!_PyArg_CheckPositional("str", nargs, 0, 3)) {
15629
0
        return NULL;
15630
0
    }
15631
736k
    if (nargs == 0) {
15632
0
        return unicode_get_empty();
15633
0
    }
15634
736k
    PyObject *object = args[0];
15635
736k
    if (nargs == 1) {
15636
426
        return PyObject_Str(object);
15637
426
    }
15638
736k
    const char *encoding = arg_as_utf8(args[1], "encoding");
15639
736k
    if (encoding == NULL) {
15640
174
        return NULL;
15641
174
    }
15642
735k
    const char *errors = NULL;
15643
735k
    if (nargs == 3) {
15644
218k
        errors = arg_as_utf8(args[2], "errors");
15645
218k
        if (errors == NULL) {
15646
0
            return NULL;
15647
0
        }
15648
218k
    }
15649
735k
    return PyUnicode_FromEncodedObject(object, encoding, errors);
15650
735k
}
15651
15652
static PyObject *
15653
unicode_subtype_new(PyTypeObject *type, PyObject *unicode)
15654
10.9M
{
15655
10.9M
    PyObject *self;
15656
10.9M
    Py_ssize_t length, char_size;
15657
10.9M
    int share_utf8;
15658
10.9M
    int kind;
15659
10.9M
    void *data;
15660
15661
10.9M
    assert(PyType_IsSubtype(type, &PyUnicode_Type));
15662
10.9M
    assert(_PyUnicode_CHECK(unicode));
15663
15664
10.9M
    self = type->tp_alloc(type, 0);
15665
10.9M
    if (self == NULL) {
15666
0
        return NULL;
15667
0
    }
15668
10.9M
    kind = PyUnicode_KIND(unicode);
15669
10.9M
    length = PyUnicode_GET_LENGTH(unicode);
15670
15671
10.9M
    _PyUnicode_LENGTH(self) = length;
15672
#ifdef Py_DEBUG
15673
    _PyUnicode_HASH(self) = -1;
15674
#else
15675
10.9M
    _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode);
15676
10.9M
#endif
15677
10.9M
    _PyUnicode_STATE(self).interned = 0;
15678
10.9M
    _PyUnicode_STATE(self).kind = kind;
15679
10.9M
    _PyUnicode_STATE(self).compact = 0;
15680
10.9M
    _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii;
15681
10.9M
    _PyUnicode_STATE(self).statically_allocated = 0;
15682
10.9M
    PyUnicode_SET_UTF8_LENGTH(self, 0);
15683
10.9M
    PyUnicode_SET_UTF8(self, NULL);
15684
10.9M
    _PyUnicode_DATA_ANY(self) = NULL;
15685
15686
10.9M
    share_utf8 = 0;
15687
10.9M
    if (kind == PyUnicode_1BYTE_KIND) {
15688
9.76M
        char_size = 1;
15689
9.76M
        if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128)
15690
9.72M
            share_utf8 = 1;
15691
9.76M
    }
15692
1.22M
    else if (kind == PyUnicode_2BYTE_KIND) {
15693
1.17M
        char_size = 2;
15694
1.17M
    }
15695
53.1k
    else {
15696
53.1k
        assert(kind == PyUnicode_4BYTE_KIND);
15697
53.1k
        char_size = 4;
15698
53.1k
    }
15699
15700
    /* Ensure we won't overflow the length. */
15701
10.9M
    if (length > (PY_SSIZE_T_MAX / char_size - 1)) {
15702
0
        PyErr_NoMemory();
15703
0
        goto onError;
15704
0
    }
15705
10.9M
    data = PyMem_Malloc((length + 1) * char_size);
15706
10.9M
    if (data == NULL) {
15707
0
        PyErr_NoMemory();
15708
0
        goto onError;
15709
0
    }
15710
15711
10.9M
    _PyUnicode_DATA_ANY(self) = data;
15712
10.9M
    if (share_utf8) {
15713
9.72M
        PyUnicode_SET_UTF8_LENGTH(self, length);
15714
9.72M
        PyUnicode_SET_UTF8(self, data);
15715
9.72M
    }
15716
15717
10.9M
    memcpy(data, PyUnicode_DATA(unicode), kind * (length + 1));
15718
10.9M
    assert(_PyUnicode_CheckConsistency(self, 1));
15719
#ifdef Py_DEBUG
15720
    _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode);
15721
#endif
15722
10.9M
    return self;
15723
15724
0
onError:
15725
0
    Py_DECREF(self);
15726
0
    return NULL;
15727
10.9M
}
15728
15729
void
15730
_PyUnicode_ExactDealloc(PyObject *op)
15731
57.3M
{
15732
57.3M
    assert(PyUnicode_CheckExact(op));
15733
57.3M
    unicode_dealloc(op);
15734
57.3M
}
15735
15736
PyDoc_STRVAR(unicode_doc,
15737
"str(object='') -> str\n\
15738
str(bytes_or_buffer[, encoding[, errors]]) -> str\n\
15739
\n\
15740
Create a new string object from the given object. If encoding or\n\
15741
errors is specified, then the object must expose a data buffer\n\
15742
that will be decoded using the given encoding and error handler.\n\
15743
Otherwise, returns the result of object.__str__() (if defined)\n\
15744
or repr(object).\n\
15745
encoding defaults to 'utf-8'.\n\
15746
errors defaults to 'strict'.");
15747
15748
static PyObject *unicode_iter(PyObject *seq);
15749
15750
PyTypeObject PyUnicode_Type = {
15751
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
15752
    "str",                        /* tp_name */
15753
    sizeof(PyUnicodeObject),      /* tp_basicsize */
15754
    0,                            /* tp_itemsize */
15755
    /* Slots */
15756
    unicode_dealloc,              /* tp_dealloc */
15757
    0,                            /* tp_vectorcall_offset */
15758
    0,                            /* tp_getattr */
15759
    0,                            /* tp_setattr */
15760
    0,                            /* tp_as_async */
15761
    unicode_repr,                 /* tp_repr */
15762
    &unicode_as_number,           /* tp_as_number */
15763
    &unicode_as_sequence,         /* tp_as_sequence */
15764
    &unicode_as_mapping,          /* tp_as_mapping */
15765
    unicode_hash,                 /* tp_hash*/
15766
    0,                            /* tp_call*/
15767
    unicode_str,                  /* tp_str */
15768
    PyObject_GenericGetAttr,      /* tp_getattro */
15769
    0,                            /* tp_setattro */
15770
    0,                            /* tp_as_buffer */
15771
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
15772
        Py_TPFLAGS_UNICODE_SUBCLASS |
15773
        _Py_TPFLAGS_MATCH_SELF, /* tp_flags */
15774
    unicode_doc,                  /* tp_doc */
15775
    0,                            /* tp_traverse */
15776
    0,                            /* tp_clear */
15777
    PyUnicode_RichCompare,        /* tp_richcompare */
15778
    0,                            /* tp_weaklistoffset */
15779
    unicode_iter,                 /* tp_iter */
15780
    0,                            /* tp_iternext */
15781
    unicode_methods,              /* tp_methods */
15782
    0,                            /* tp_members */
15783
    0,                            /* tp_getset */
15784
    0,                            /* tp_base */
15785
    0,                            /* tp_dict */
15786
    0,                            /* tp_descr_get */
15787
    0,                            /* tp_descr_set */
15788
    0,                            /* tp_dictoffset */
15789
    0,                            /* tp_init */
15790
    0,                            /* tp_alloc */
15791
    unicode_new,                  /* tp_new */
15792
    PyObject_Free,                /* tp_free */
15793
    .tp_vectorcall = unicode_vectorcall,
15794
};
15795
15796
/* Initialize the Unicode implementation */
15797
15798
static void
15799
_init_global_state(void)
15800
16
{
15801
16
    static int initialized = 0;
15802
16
    if (initialized) {
15803
0
        return;
15804
0
    }
15805
16
    initialized = 1;
15806
15807
    /* initialize the linebreak bloom filter */
15808
16
    const Py_UCS2 linebreak[] = {
15809
16
        0x000A, /* LINE FEED */
15810
16
        0x000D, /* CARRIAGE RETURN */
15811
16
        0x001C, /* FILE SEPARATOR */
15812
16
        0x001D, /* GROUP SEPARATOR */
15813
16
        0x001E, /* RECORD SEPARATOR */
15814
16
        0x0085, /* NEXT LINE */
15815
16
        0x2028, /* LINE SEPARATOR */
15816
16
        0x2029, /* PARAGRAPH SEPARATOR */
15817
16
    };
15818
16
    bloom_linebreak = make_bloom_mask(
15819
16
        PyUnicode_2BYTE_KIND, linebreak,
15820
16
        Py_ARRAY_LENGTH(linebreak));
15821
16
}
15822
15823
void
15824
_PyUnicode_InitState(PyInterpreterState *interp)
15825
16
{
15826
16
    if (!_Py_IsMainInterpreter(interp)) {
15827
0
        return;
15828
0
    }
15829
16
    _init_global_state();
15830
16
}
15831
15832
15833
PyStatus
15834
_PyUnicode_InitGlobalObjects(PyInterpreterState *interp)
15835
16
{
15836
16
    if (_Py_IsMainInterpreter(interp)) {
15837
16
        PyStatus status = init_global_interned_strings(interp);
15838
16
        if (_PyStatus_EXCEPTION(status)) {
15839
0
            return status;
15840
0
        }
15841
16
    }
15842
16
    assert(INTERNED_STRINGS);
15843
15844
16
    if (init_interned_dict(interp)) {
15845
0
        PyErr_Clear();
15846
0
        return _PyStatus_ERR("failed to create interned dict");
15847
0
    }
15848
15849
16
    return _PyStatus_OK();
15850
16
}
15851
15852
15853
PyStatus
15854
_PyUnicode_InitTypes(PyInterpreterState *interp)
15855
16
{
15856
16
    if (_PyStaticType_InitBuiltin(interp, &EncodingMapType) < 0) {
15857
0
        goto error;
15858
0
    }
15859
16
    if (_PyStaticType_InitBuiltin(interp, &PyFieldNameIter_Type) < 0) {
15860
0
        goto error;
15861
0
    }
15862
16
    if (_PyStaticType_InitBuiltin(interp, &PyFormatterIter_Type) < 0) {
15863
0
        goto error;
15864
0
    }
15865
16
    return _PyStatus_OK();
15866
15867
0
error:
15868
0
    return _PyStatus_ERR("Can't initialize unicode types");
15869
16
}
15870
15871
static /* non-null */ PyObject*
15872
intern_static(PyInterpreterState *interp, PyObject *s /* stolen */)
15873
16.9k
{
15874
    // Note that this steals a reference to `s`, but in many cases that
15875
    // stolen ref is returned, requiring no decref/incref.
15876
15877
16.9k
    assert(s != NULL);
15878
16.9k
    assert(_PyUnicode_CHECK(s));
15879
16.9k
    assert(_PyUnicode_STATE(s).statically_allocated);
15880
16.9k
    assert(!PyUnicode_CHECK_INTERNED(s));
15881
15882
#ifdef Py_DEBUG
15883
    /* We must not add process-global interned string if there's already a
15884
     * per-interpreter interned_dict, which might contain duplicates.
15885
     */
15886
    PyObject *interned = get_interned_dict(interp);
15887
    assert(interned == NULL);
15888
#endif
15889
15890
    /* Look in the global cache first. */
15891
16.9k
    PyObject *r = (PyObject *)_Py_hashtable_get(INTERNED_STRINGS, s);
15892
    /* We should only init each string once */
15893
16.9k
    assert(r == NULL);
15894
    /* but just in case (for the non-debug build), handle this */
15895
16.9k
    if (r != NULL && r != s) {
15896
0
        assert(_PyUnicode_STATE(r).interned == SSTATE_INTERNED_IMMORTAL_STATIC);
15897
0
        assert(_PyUnicode_CHECK(r));
15898
0
        Py_DECREF(s);
15899
0
        return Py_NewRef(r);
15900
0
    }
15901
15902
16.9k
    if (_Py_hashtable_set(INTERNED_STRINGS, s, s) < -1) {
15903
0
        Py_FatalError("failed to intern static string");
15904
0
    }
15905
15906
16.9k
    _PyUnicode_STATE(s).interned = SSTATE_INTERNED_IMMORTAL_STATIC;
15907
16.9k
    return s;
15908
16.9k
}
15909
15910
void
15911
_PyUnicode_InternStatic(PyInterpreterState *interp, PyObject **p)
15912
16.9k
{
15913
    // This should only be called as part of runtime initialization
15914
16.9k
    assert(!Py_IsInitialized());
15915
15916
16.9k
    *p = intern_static(interp, *p);
15917
16.9k
    assert(*p);
15918
16.9k
}
15919
15920
static void
15921
immortalize_interned(PyObject *s)
15922
95.3k
{
15923
95.3k
    assert(PyUnicode_CHECK_INTERNED(s) == SSTATE_INTERNED_MORTAL);
15924
95.3k
    assert(!_Py_IsImmortal(s));
15925
#ifdef Py_REF_DEBUG
15926
    /* The reference count value should be excluded from the RefTotal.
15927
       The decrements to these objects will not be registered so they
15928
       need to be accounted for in here. */
15929
    for (Py_ssize_t i = 0; i < Py_REFCNT(s); i++) {
15930
        _Py_DecRefTotal(_PyThreadState_GET());
15931
    }
15932
#endif
15933
95.3k
    FT_ATOMIC_STORE_UINT8_RELAXED(_PyUnicode_STATE(s).interned, SSTATE_INTERNED_IMMORTAL);
15934
95.3k
    _Py_SetImmortal(s);
15935
95.3k
}
15936
15937
static /* non-null */ PyObject*
15938
intern_common(PyInterpreterState *interp, PyObject *s /* stolen */,
15939
              bool immortalize)
15940
38.9M
{
15941
    // Note that this steals a reference to `s`, but in many cases that
15942
    // stolen ref is returned, requiring no decref/incref.
15943
15944
#ifdef Py_DEBUG
15945
    assert(s != NULL);
15946
    assert(_PyUnicode_CHECK(s));
15947
#else
15948
38.9M
    if (s == NULL || !PyUnicode_Check(s)) {
15949
0
        return s;
15950
0
    }
15951
38.9M
#endif
15952
15953
    /* If it's a subclass, we don't really know what putting
15954
       it in the interned dict might do. */
15955
38.9M
    if (!PyUnicode_CheckExact(s)) {
15956
0
        return s;
15957
0
    }
15958
15959
    /* Is it already interned? */
15960
38.9M
    switch (PyUnicode_CHECK_INTERNED(s)) {
15961
3.33M
        case SSTATE_NOT_INTERNED:
15962
            // no, go on
15963
3.33M
            break;
15964
13.6k
        case SSTATE_INTERNED_MORTAL:
15965
            // yes but we might need to make it immortal
15966
13.6k
            if (immortalize) {
15967
35
                immortalize_interned(s);
15968
35
            }
15969
13.6k
            return s;
15970
35.5M
        default:
15971
            // all done
15972
35.5M
            return s;
15973
38.9M
    }
15974
15975
    /* Statically allocated strings must be already interned. */
15976
3.33M
    assert(!_PyUnicode_STATE(s).statically_allocated);
15977
15978
#if Py_GIL_DISABLED
15979
    /* In the free-threaded build, all interned strings are immortal */
15980
    immortalize = 1;
15981
#endif
15982
15983
    /* If it's already immortal, intern it as such */
15984
3.33M
    if (_Py_IsImmortal(s)) {
15985
0
        immortalize = 1;
15986
0
    }
15987
15988
    /* if it's a short string, get the singleton */
15989
3.33M
    if (PyUnicode_GET_LENGTH(s) == 1 &&
15990
3.33M
                PyUnicode_KIND(s) == PyUnicode_1BYTE_KIND) {
15991
0
        PyObject *r = LATIN1(*(unsigned char*)PyUnicode_DATA(s));
15992
0
        assert(PyUnicode_CHECK_INTERNED(r));
15993
0
        Py_DECREF(s);
15994
0
        return r;
15995
0
    }
15996
#ifdef Py_DEBUG
15997
    assert(!unicode_is_singleton(s));
15998
#endif
15999
16000
    /* Look in the global cache now. */
16001
3.33M
    {
16002
3.33M
        PyObject *r = (PyObject *)_Py_hashtable_get(INTERNED_STRINGS, s);
16003
3.33M
        if (r != NULL) {
16004
273k
            assert(_PyUnicode_STATE(r).statically_allocated);
16005
273k
            assert(r != s);  // r must be statically_allocated; s is not
16006
273k
            Py_DECREF(s);
16007
273k
            return Py_NewRef(r);
16008
273k
        }
16009
3.33M
    }
16010
16011
    /* Do a setdefault on the per-interpreter cache. */
16012
3.05M
    PyObject *interned = get_interned_dict(interp);
16013
3.05M
    assert(interned != NULL);
16014
#ifdef Py_GIL_DISABLED
16015
#  define INTERN_MUTEX &_Py_INTERP_CACHED_OBJECT(interp, interned_mutex)
16016
#endif
16017
3.05M
    FT_MUTEX_LOCK(INTERN_MUTEX);
16018
3.05M
    PyObject *t;
16019
3.05M
    {
16020
3.05M
        int res = PyDict_SetDefaultRef(interned, s, s, &t);
16021
3.05M
        if (res < 0) {
16022
0
            PyErr_Clear();
16023
0
            FT_MUTEX_UNLOCK(INTERN_MUTEX);
16024
0
            return s;
16025
0
        }
16026
3.05M
        else if (res == 1) {
16027
            // value was already present (not inserted)
16028
2.41M
            Py_DECREF(s);
16029
2.41M
            if (immortalize &&
16030
2.41M
                    PyUnicode_CHECK_INTERNED(t) == SSTATE_INTERNED_MORTAL) {
16031
4.18k
                immortalize_interned(t);
16032
4.18k
            }
16033
2.41M
            FT_MUTEX_UNLOCK(INTERN_MUTEX);
16034
2.41M
            return t;
16035
2.41M
        }
16036
642k
        else {
16037
            // value was newly inserted
16038
642k
            assert (s == t);
16039
642k
            Py_DECREF(t);
16040
642k
        }
16041
3.05M
    }
16042
16043
    /* NOT_INTERNED -> INTERNED_MORTAL */
16044
16045
642k
    assert(_PyUnicode_STATE(s).interned == SSTATE_NOT_INTERNED);
16046
16047
642k
    if (!_Py_IsImmortal(s)) {
16048
        /* The two references in interned dict (key and value) are not counted.
16049
        unicode_dealloc() and _PyUnicode_ClearInterned() take care of this. */
16050
642k
        Py_DECREF(s);
16051
642k
        Py_DECREF(s);
16052
642k
    }
16053
642k
    FT_ATOMIC_STORE_UINT8_RELAXED(_PyUnicode_STATE(s).interned, SSTATE_INTERNED_MORTAL);
16054
16055
    /* INTERNED_MORTAL -> INTERNED_IMMORTAL (if needed) */
16056
16057
#ifdef Py_DEBUG
16058
    if (_Py_IsImmortal(s)) {
16059
        assert(immortalize);
16060
    }
16061
#endif
16062
642k
    if (immortalize) {
16063
91.1k
        immortalize_interned(s);
16064
91.1k
    }
16065
16066
642k
    FT_MUTEX_UNLOCK(INTERN_MUTEX);
16067
642k
    return s;
16068
3.05M
}
16069
16070
void
16071
_PyUnicode_InternImmortal(PyInterpreterState *interp, PyObject **p)
16072
2.78M
{
16073
2.78M
    *p = intern_common(interp, *p, 1);
16074
2.78M
    assert(*p);
16075
2.78M
}
16076
16077
void
16078
_PyUnicode_InternMortal(PyInterpreterState *interp, PyObject **p)
16079
36.1M
{
16080
36.1M
    *p = intern_common(interp, *p, 0);
16081
36.1M
    assert(*p);
16082
36.1M
}
16083
16084
16085
void
16086
_PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p)
16087
0
{
16088
0
    _PyUnicode_InternImmortal(interp, p);
16089
0
    return;
16090
0
}
16091
16092
void
16093
PyUnicode_InternInPlace(PyObject **p)
16094
0
{
16095
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
16096
0
    _PyUnicode_InternMortal(interp, p);
16097
0
}
16098
16099
// Public-looking name kept for the stable ABI; user should not call this:
16100
PyAPI_FUNC(void) PyUnicode_InternImmortal(PyObject **);
16101
void
16102
PyUnicode_InternImmortal(PyObject **p)
16103
0
{
16104
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
16105
0
    _PyUnicode_InternImmortal(interp, p);
16106
0
}
16107
16108
PyObject *
16109
PyUnicode_InternFromString(const char *cp)
16110
1.12M
{
16111
1.12M
    PyObject *s = PyUnicode_FromString(cp);
16112
1.12M
    if (s == NULL) {
16113
0
        return NULL;
16114
0
    }
16115
1.12M
    PyInterpreterState *interp = _PyInterpreterState_GET();
16116
1.12M
    _PyUnicode_InternMortal(interp, &s);
16117
1.12M
    return s;
16118
1.12M
}
16119
16120
16121
void
16122
_PyUnicode_ClearInterned(PyInterpreterState *interp)
16123
0
{
16124
0
    PyObject *interned = get_interned_dict(interp);
16125
0
    if (interned == NULL) {
16126
0
        return;
16127
0
    }
16128
0
    assert(PyDict_CheckExact(interned));
16129
16130
0
    if (has_shared_intern_dict(interp)) {
16131
        // the dict doesn't belong to this interpreter, skip the debug
16132
        // checks on it and just clear the pointer to it
16133
0
        clear_interned_dict(interp);
16134
0
        return;
16135
0
    }
16136
16137
#ifdef INTERNED_STATS
16138
    fprintf(stderr, "releasing %zd interned strings\n",
16139
            PyDict_GET_SIZE(interned));
16140
16141
    Py_ssize_t total_length = 0;
16142
#endif
16143
0
    Py_ssize_t pos = 0;
16144
0
    PyObject *s, *ignored_value;
16145
0
    while (PyDict_Next(interned, &pos, &s, &ignored_value)) {
16146
0
        int shared = 0;
16147
0
        switch (PyUnicode_CHECK_INTERNED(s)) {
16148
0
        case SSTATE_INTERNED_IMMORTAL:
16149
            /* Make immortal interned strings mortal again. */
16150
            // Skip the Immortal Instance check and restore
16151
            // the two references (key and value) ignored
16152
            // by PyUnicode_InternInPlace().
16153
0
            _Py_SetMortal(s, 2);
16154
#ifdef Py_REF_DEBUG
16155
            /* let's be pedantic with the ref total */
16156
            _Py_IncRefTotal(_PyThreadState_GET());
16157
            _Py_IncRefTotal(_PyThreadState_GET());
16158
#endif
16159
#ifdef INTERNED_STATS
16160
            total_length += PyUnicode_GET_LENGTH(s);
16161
#endif
16162
0
            break;
16163
0
        case SSTATE_INTERNED_IMMORTAL_STATIC:
16164
            /* It is shared between interpreters, so we should unmark it
16165
               only when this is the last interpreter in which it's
16166
               interned.  We immortalize all the statically initialized
16167
               strings during startup, so we can rely on the
16168
               main interpreter to be the last one. */
16169
0
            if (!_Py_IsMainInterpreter(interp)) {
16170
0
                shared = 1;
16171
0
            }
16172
0
            break;
16173
0
        case SSTATE_INTERNED_MORTAL:
16174
            // Restore 2 references held by the interned dict; these will
16175
            // be decref'd by clear_interned_dict's PyDict_Clear.
16176
0
            _Py_RefcntAdd(s, 2);
16177
#ifdef Py_REF_DEBUG
16178
            /* let's be pedantic with the ref total */
16179
            _Py_IncRefTotal(_PyThreadState_GET());
16180
            _Py_IncRefTotal(_PyThreadState_GET());
16181
#endif
16182
0
            break;
16183
0
        case SSTATE_NOT_INTERNED:
16184
0
            _Py_FALLTHROUGH;
16185
0
        default:
16186
0
            Py_UNREACHABLE();
16187
0
        }
16188
0
        if (!shared) {
16189
0
            FT_ATOMIC_STORE_UINT8_RELAXED(_PyUnicode_STATE(s).interned, SSTATE_NOT_INTERNED);
16190
0
        }
16191
0
    }
16192
#ifdef INTERNED_STATS
16193
    fprintf(stderr,
16194
            "total length of all interned strings: %zd characters\n",
16195
            total_length);
16196
#endif
16197
16198
0
    struct _Py_unicode_state *state = &interp->unicode;
16199
0
    struct _Py_unicode_ids *ids = &state->ids;
16200
0
    for (Py_ssize_t i=0; i < ids->size; i++) {
16201
0
        Py_XINCREF(ids->array[i]);
16202
0
    }
16203
0
    clear_interned_dict(interp);
16204
0
    if (_Py_IsMainInterpreter(interp)) {
16205
0
        clear_global_interned_strings();
16206
0
    }
16207
0
}
16208
16209
16210
/********************* Unicode Iterator **************************/
16211
16212
typedef struct {
16213
    PyObject_HEAD
16214
    Py_ssize_t it_index;
16215
    PyObject *it_seq;    /* Set to NULL when iterator is exhausted */
16216
} unicodeiterobject;
16217
16218
static void
16219
unicodeiter_dealloc(PyObject *op)
16220
1.72M
{
16221
1.72M
    unicodeiterobject *it = (unicodeiterobject *)op;
16222
1.72M
    _PyObject_GC_UNTRACK(it);
16223
1.72M
    Py_XDECREF(it->it_seq);
16224
1.72M
    PyObject_GC_Del(it);
16225
1.72M
}
16226
16227
static int
16228
unicodeiter_traverse(PyObject *op, visitproc visit, void *arg)
16229
12
{
16230
12
    unicodeiterobject *it = (unicodeiterobject *)op;
16231
12
    Py_VISIT(it->it_seq);
16232
12
    return 0;
16233
12
}
16234
16235
static PyObject *
16236
unicodeiter_next(PyObject *op)
16237
151M
{
16238
151M
    unicodeiterobject *it = (unicodeiterobject *)op;
16239
151M
    PyObject *seq;
16240
16241
151M
    assert(it != NULL);
16242
151M
    seq = it->it_seq;
16243
151M
    if (seq == NULL)
16244
0
        return NULL;
16245
151M
    assert(_PyUnicode_CHECK(seq));
16246
16247
151M
    if (it->it_index < PyUnicode_GET_LENGTH(seq)) {
16248
150M
        int kind = PyUnicode_KIND(seq);
16249
150M
        const void *data = PyUnicode_DATA(seq);
16250
150M
        Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index);
16251
150M
        it->it_index++;
16252
150M
        return unicode_char(chr);
16253
150M
    }
16254
16255
850k
    it->it_seq = NULL;
16256
850k
    Py_DECREF(seq);
16257
850k
    return NULL;
16258
151M
}
16259
16260
static PyObject *
16261
unicode_ascii_iter_next(PyObject *op)
16262
120M
{
16263
120M
    unicodeiterobject *it = (unicodeiterobject *)op;
16264
120M
    assert(it != NULL);
16265
120M
    PyObject *seq = it->it_seq;
16266
120M
    if (seq == NULL) {
16267
0
        return NULL;
16268
0
    }
16269
120M
    assert(_PyUnicode_CHECK(seq));
16270
120M
    assert(PyUnicode_IS_COMPACT_ASCII(seq));
16271
120M
    if (it->it_index < PyUnicode_GET_LENGTH(seq)) {
16272
119M
        const void *data = ((void*)(_PyASCIIObject_CAST(seq) + 1));
16273
119M
        Py_UCS1 chr = (Py_UCS1)PyUnicode_READ(PyUnicode_1BYTE_KIND,
16274
119M
                                              data, it->it_index);
16275
119M
        it->it_index++;
16276
119M
        return (PyObject*)&_Py_SINGLETON(strings).ascii[chr];
16277
119M
    }
16278
785k
    it->it_seq = NULL;
16279
785k
    Py_DECREF(seq);
16280
785k
    return NULL;
16281
120M
}
16282
16283
static PyObject *
16284
unicodeiter_len(PyObject *op, PyObject *Py_UNUSED(ignored))
16285
0
{
16286
0
    unicodeiterobject *it = (unicodeiterobject *)op;
16287
0
    Py_ssize_t len = 0;
16288
0
    if (it->it_seq)
16289
0
        len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index;
16290
0
    return PyLong_FromSsize_t(len);
16291
0
}
16292
16293
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
16294
16295
static PyObject *
16296
unicodeiter_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
16297
0
{
16298
0
    unicodeiterobject *it = (unicodeiterobject *)op;
16299
0
    PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
16300
16301
    /* _PyEval_GetBuiltin can invoke arbitrary code,
16302
     * call must be before access of iterator pointers.
16303
     * see issue #101765 */
16304
16305
0
    if (it->it_seq != NULL) {
16306
0
        return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
16307
0
    } else {
16308
0
        PyObject *u = unicode_get_empty();
16309
0
        if (u == NULL) {
16310
0
            Py_XDECREF(iter);
16311
0
            return NULL;
16312
0
        }
16313
0
        return Py_BuildValue("N(N)", iter, u);
16314
0
    }
16315
0
}
16316
16317
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
16318
16319
static PyObject *
16320
unicodeiter_setstate(PyObject *op, PyObject *state)
16321
0
{
16322
0
    unicodeiterobject *it = (unicodeiterobject *)op;
16323
0
    Py_ssize_t index = PyLong_AsSsize_t(state);
16324
0
    if (index == -1 && PyErr_Occurred())
16325
0
        return NULL;
16326
0
    if (it->it_seq != NULL) {
16327
0
        if (index < 0)
16328
0
            index = 0;
16329
0
        else if (index > PyUnicode_GET_LENGTH(it->it_seq))
16330
0
            index = PyUnicode_GET_LENGTH(it->it_seq); /* iterator truncated */
16331
0
        it->it_index = index;
16332
0
    }
16333
0
    Py_RETURN_NONE;
16334
0
}
16335
16336
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
16337
16338
static PyMethodDef unicodeiter_methods[] = {
16339
    {"__length_hint__", unicodeiter_len, METH_NOARGS, length_hint_doc},
16340
    {"__reduce__",      unicodeiter_reduce, METH_NOARGS, reduce_doc},
16341
    {"__setstate__",    unicodeiter_setstate, METH_O, setstate_doc},
16342
    {NULL,      NULL}       /* sentinel */
16343
};
16344
16345
PyTypeObject PyUnicodeIter_Type = {
16346
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
16347
    "str_iterator",         /* tp_name */
16348
    sizeof(unicodeiterobject),      /* tp_basicsize */
16349
    0,                  /* tp_itemsize */
16350
    /* methods */
16351
    unicodeiter_dealloc,/* tp_dealloc */
16352
    0,                  /* tp_vectorcall_offset */
16353
    0,                  /* tp_getattr */
16354
    0,                  /* tp_setattr */
16355
    0,                  /* tp_as_async */
16356
    0,                  /* tp_repr */
16357
    0,                  /* tp_as_number */
16358
    0,                  /* tp_as_sequence */
16359
    0,                  /* tp_as_mapping */
16360
    0,                  /* tp_hash */
16361
    0,                  /* tp_call */
16362
    0,                  /* tp_str */
16363
    PyObject_GenericGetAttr,        /* tp_getattro */
16364
    0,                  /* tp_setattro */
16365
    0,                  /* tp_as_buffer */
16366
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
16367
    0,                  /* tp_doc */
16368
    unicodeiter_traverse, /* tp_traverse */
16369
    0,                  /* tp_clear */
16370
    0,                  /* tp_richcompare */
16371
    0,                  /* tp_weaklistoffset */
16372
    PyObject_SelfIter,          /* tp_iter */
16373
    unicodeiter_next,   /* tp_iternext */
16374
    unicodeiter_methods,            /* tp_methods */
16375
    0,
16376
};
16377
16378
PyTypeObject _PyUnicodeASCIIIter_Type = {
16379
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
16380
    .tp_name = "str_ascii_iterator",
16381
    .tp_basicsize = sizeof(unicodeiterobject),
16382
    .tp_dealloc = unicodeiter_dealloc,
16383
    .tp_getattro = PyObject_GenericGetAttr,
16384
    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
16385
    .tp_traverse = unicodeiter_traverse,
16386
    .tp_iter = PyObject_SelfIter,
16387
    .tp_iternext = unicode_ascii_iter_next,
16388
    .tp_methods = unicodeiter_methods,
16389
};
16390
16391
static PyObject *
16392
unicode_iter(PyObject *seq)
16393
1.72M
{
16394
1.72M
    unicodeiterobject *it;
16395
16396
1.72M
    if (!PyUnicode_Check(seq)) {
16397
0
        PyErr_BadInternalCall();
16398
0
        return NULL;
16399
0
    }
16400
1.72M
    if (PyUnicode_IS_COMPACT_ASCII(seq)) {
16401
877k
        it = PyObject_GC_New(unicodeiterobject, &_PyUnicodeASCIIIter_Type);
16402
877k
    }
16403
851k
    else {
16404
851k
        it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type);
16405
851k
    }
16406
1.72M
    if (it == NULL)
16407
0
        return NULL;
16408
1.72M
    it->it_index = 0;
16409
1.72M
    it->it_seq = Py_NewRef(seq);
16410
1.72M
    _PyObject_GC_TRACK(it);
16411
1.72M
    return (PyObject *)it;
16412
1.72M
}
16413
16414
static int
16415
encode_wstr_utf8(wchar_t *wstr, char **str, const char *name)
16416
64
{
16417
64
    int res;
16418
64
    res = _Py_EncodeUTF8Ex(wstr, str, NULL, NULL, 1, _Py_ERROR_STRICT);
16419
64
    if (res == -2) {
16420
0
        PyErr_Format(PyExc_RuntimeError, "cannot encode %s", name);
16421
0
        return -1;
16422
0
    }
16423
64
    if (res < 0) {
16424
0
        PyErr_NoMemory();
16425
0
        return -1;
16426
0
    }
16427
64
    return 0;
16428
64
}
16429
16430
16431
static int
16432
config_get_codec_name(wchar_t **config_encoding)
16433
32
{
16434
32
    char *encoding;
16435
32
    if (encode_wstr_utf8(*config_encoding, &encoding, "stdio_encoding") < 0) {
16436
0
        return -1;
16437
0
    }
16438
16439
32
    PyObject *name_obj = NULL;
16440
32
    PyObject *codec = _PyCodec_Lookup(encoding);
16441
32
    PyMem_RawFree(encoding);
16442
16443
32
    if (!codec)
16444
0
        goto error;
16445
16446
32
    name_obj = PyObject_GetAttrString(codec, "name");
16447
32
    Py_CLEAR(codec);
16448
32
    if (!name_obj) {
16449
0
        goto error;
16450
0
    }
16451
16452
32
    wchar_t *wname = PyUnicode_AsWideCharString(name_obj, NULL);
16453
32
    Py_DECREF(name_obj);
16454
32
    if (wname == NULL) {
16455
0
        goto error;
16456
0
    }
16457
16458
32
    wchar_t *raw_wname = _PyMem_RawWcsdup(wname);
16459
32
    if (raw_wname == NULL) {
16460
0
        PyMem_Free(wname);
16461
0
        PyErr_NoMemory();
16462
0
        goto error;
16463
0
    }
16464
16465
32
    PyMem_RawFree(*config_encoding);
16466
32
    *config_encoding = raw_wname;
16467
16468
32
    PyMem_Free(wname);
16469
32
    return 0;
16470
16471
0
error:
16472
0
    Py_XDECREF(codec);
16473
0
    Py_XDECREF(name_obj);
16474
0
    return -1;
16475
32
}
16476
16477
16478
static PyStatus
16479
init_stdio_encoding(PyInterpreterState *interp)
16480
16
{
16481
    /* Update the stdio encoding to the normalized Python codec name. */
16482
16
    PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
16483
16
    if (config_get_codec_name(&config->stdio_encoding) < 0) {
16484
0
        return _PyStatus_ERR("failed to get the Python codec name "
16485
0
                             "of the stdio encoding");
16486
0
    }
16487
16
    return _PyStatus_OK();
16488
16
}
16489
16490
16491
static int
16492
init_fs_codec(PyInterpreterState *interp)
16493
16
{
16494
16
    const PyConfig *config = _PyInterpreterState_GetConfig(interp);
16495
16496
16
    _Py_error_handler error_handler;
16497
16
    error_handler = get_error_handler_wide(config->filesystem_errors);
16498
16
    if (error_handler == _Py_ERROR_UNKNOWN) {
16499
0
        PyErr_SetString(PyExc_RuntimeError, "unknown filesystem error handler");
16500
0
        return -1;
16501
0
    }
16502
16503
16
    char *encoding, *errors;
16504
16
    if (encode_wstr_utf8(config->filesystem_encoding,
16505
16
                         &encoding,
16506
16
                         "filesystem_encoding") < 0) {
16507
0
        return -1;
16508
0
    }
16509
16510
16
    if (encode_wstr_utf8(config->filesystem_errors,
16511
16
                         &errors,
16512
16
                         "filesystem_errors") < 0) {
16513
0
        PyMem_RawFree(encoding);
16514
0
        return -1;
16515
0
    }
16516
16517
16
    struct _Py_unicode_fs_codec *fs_codec = &interp->unicode.fs_codec;
16518
16
    PyMem_RawFree(fs_codec->encoding);
16519
16
    fs_codec->encoding = encoding;
16520
    /* encoding has been normalized by init_fs_encoding() */
16521
16
    fs_codec->utf8 = (strcmp(encoding, "utf-8") == 0);
16522
16
    PyMem_RawFree(fs_codec->errors);
16523
16
    fs_codec->errors = errors;
16524
16
    fs_codec->error_handler = error_handler;
16525
16526
#ifdef _Py_FORCE_UTF8_FS_ENCODING
16527
    assert(fs_codec->utf8 == 1);
16528
#endif
16529
16530
    /* At this point, PyUnicode_EncodeFSDefault() and
16531
       PyUnicode_DecodeFSDefault() can now use the Python codec rather than
16532
       the C implementation of the filesystem encoding. */
16533
16534
    /* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors
16535
       global configuration variables. */
16536
16
    if (_Py_IsMainInterpreter(interp)) {
16537
16538
16
        if (_Py_SetFileSystemEncoding(fs_codec->encoding,
16539
16
                                      fs_codec->errors) < 0) {
16540
0
            PyErr_NoMemory();
16541
0
            return -1;
16542
0
        }
16543
16
    }
16544
16
    return 0;
16545
16
}
16546
16547
16548
static PyStatus
16549
init_fs_encoding(PyThreadState *tstate)
16550
16
{
16551
16
    PyInterpreterState *interp = tstate->interp;
16552
16553
    /* Update the filesystem encoding to the normalized Python codec name.
16554
       For example, replace "ANSI_X3.4-1968" (locale encoding) with "ascii"
16555
       (Python codec name). */
16556
16
    PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
16557
16
    if (config_get_codec_name(&config->filesystem_encoding) < 0) {
16558
0
        _Py_DumpPathConfig(tstate);
16559
0
        return _PyStatus_ERR("failed to get the Python codec "
16560
0
                             "of the filesystem encoding");
16561
0
    }
16562
16563
16
    if (init_fs_codec(interp) < 0) {
16564
0
        return _PyStatus_ERR("cannot initialize filesystem codec");
16565
0
    }
16566
16
    return _PyStatus_OK();
16567
16
}
16568
16569
16570
PyStatus
16571
_PyUnicode_InitEncodings(PyThreadState *tstate)
16572
16
{
16573
16
    PyStatus status = _PyCodec_InitRegistry(tstate->interp);
16574
16
    if (_PyStatus_EXCEPTION(status)) {
16575
0
        return status;
16576
0
    }
16577
16
    status = init_fs_encoding(tstate);
16578
16
    if (_PyStatus_EXCEPTION(status)) {
16579
0
        return status;
16580
0
    }
16581
16582
16
    return init_stdio_encoding(tstate->interp);
16583
16
}
16584
16585
16586
static void
16587
_PyUnicode_FiniEncodings(struct _Py_unicode_fs_codec *fs_codec)
16588
0
{
16589
0
    PyMem_RawFree(fs_codec->encoding);
16590
0
    fs_codec->encoding = NULL;
16591
0
    fs_codec->utf8 = 0;
16592
0
    PyMem_RawFree(fs_codec->errors);
16593
0
    fs_codec->errors = NULL;
16594
0
    fs_codec->error_handler = _Py_ERROR_UNKNOWN;
16595
0
}
16596
16597
16598
#ifdef MS_WINDOWS
16599
int
16600
_PyUnicode_EnableLegacyWindowsFSEncoding(void)
16601
{
16602
    PyInterpreterState *interp = _PyInterpreterState_GET();
16603
    PyConfig *config = (PyConfig *)_PyInterpreterState_GetConfig(interp);
16604
16605
    /* Set the filesystem encoding to mbcs/replace (PEP 529) */
16606
    wchar_t *encoding = _PyMem_RawWcsdup(L"mbcs");
16607
    wchar_t *errors = _PyMem_RawWcsdup(L"replace");
16608
    if (encoding == NULL || errors == NULL) {
16609
        PyMem_RawFree(encoding);
16610
        PyMem_RawFree(errors);
16611
        PyErr_NoMemory();
16612
        return -1;
16613
    }
16614
16615
    PyMem_RawFree(config->filesystem_encoding);
16616
    config->filesystem_encoding = encoding;
16617
    PyMem_RawFree(config->filesystem_errors);
16618
    config->filesystem_errors = errors;
16619
16620
    return init_fs_codec(interp);
16621
}
16622
#endif
16623
16624
16625
#ifdef Py_DEBUG
16626
static inline int
16627
unicode_is_finalizing(void)
16628
{
16629
    return (get_interned_dict(_PyInterpreterState_Main()) == NULL);
16630
}
16631
#endif
16632
16633
16634
void
16635
_PyUnicode_FiniTypes(PyInterpreterState *interp)
16636
0
{
16637
0
    _PyStaticType_FiniBuiltin(interp, &EncodingMapType);
16638
0
    _PyStaticType_FiniBuiltin(interp, &PyFieldNameIter_Type);
16639
0
    _PyStaticType_FiniBuiltin(interp, &PyFormatterIter_Type);
16640
0
}
16641
16642
16643
void
16644
_PyUnicode_Fini(PyInterpreterState *interp)
16645
0
{
16646
0
    struct _Py_unicode_state *state = &interp->unicode;
16647
16648
0
    if (!has_shared_intern_dict(interp)) {
16649
        // _PyUnicode_ClearInterned() must be called before _PyUnicode_Fini()
16650
0
        assert(get_interned_dict(interp) == NULL);
16651
0
    }
16652
16653
0
    _PyUnicode_FiniEncodings(&state->fs_codec);
16654
16655
    // bpo-47182: force a unicodedata CAPI capsule re-import on
16656
    // subsequent initialization of interpreter.
16657
0
    interp->unicode.ucnhash_capi = NULL;
16658
16659
0
    unicode_clear_identifiers(state);
16660
0
}
16661
16662
/* A _string module, to export formatter_parser and formatter_field_name_split
16663
   to the string.Formatter class implemented in Python. */
16664
16665
static PyMethodDef _string_methods[] = {
16666
    {"formatter_field_name_split", formatter_field_name_split,
16667
     METH_O, PyDoc_STR("split the argument as a field name")},
16668
    {"formatter_parser", formatter_parser,
16669
     METH_O, PyDoc_STR("parse the argument as a format string")},
16670
    {NULL, NULL}
16671
};
16672
16673
static PyModuleDef_Slot module_slots[] = {
16674
    {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
16675
    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
16676
    {0, NULL}
16677
};
16678
16679
static struct PyModuleDef _string_module = {
16680
    PyModuleDef_HEAD_INIT,
16681
    .m_name = "_string",
16682
    .m_doc = PyDoc_STR("string helper module"),
16683
    .m_size = 0,
16684
    .m_methods = _string_methods,
16685
    .m_slots = module_slots,
16686
};
16687
16688
PyMODINIT_FUNC
16689
PyInit__string(void)
16690
6
{
16691
6
    return PyModuleDef_Init(&_string_module);
16692
6
}
16693
16694
16695
#undef PyUnicode_KIND
16696
int PyUnicode_KIND(PyObject *op)
16697
0
{
16698
0
    if (!PyUnicode_Check(op)) {
16699
0
        PyErr_Format(PyExc_TypeError, "expect str, got %T", op);
16700
0
        return -1;
16701
0
    }
16702
0
    return _PyASCIIObject_CAST(op)->state.kind;
16703
0
}
16704
16705
#undef PyUnicode_DATA
16706
void* PyUnicode_DATA(PyObject *op)
16707
0
{
16708
0
    if (!PyUnicode_Check(op)) {
16709
0
        PyErr_Format(PyExc_TypeError, "expect str, got %T", op);
16710
0
        return NULL;
16711
0
    }
16712
0
    return _PyUnicode_DATA(op);
16713
0
}