Coverage Report

Created: 2026-05-16 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/clinic/unicodeobject.c.h
Line
Count
Source
1
/*[clinic input]
2
preserve
3
[clinic start generated code]*/
4
5
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6
#  include "pycore_gc.h"          // PyGC_Head
7
#  include "pycore_runtime.h"     // _Py_ID()
8
#endif
9
#include "pycore_abstract.h"      // _PyNumber_Index()
10
#include "pycore_modsupport.h"    // _PyArg_CheckPositional()
11
12
PyDoc_STRVAR(EncodingMap_size__doc__,
13
"size($self, /)\n"
14
"--\n"
15
"\n"
16
"Return the size (in bytes) of this object.");
17
18
#define ENCODINGMAP_SIZE_METHODDEF    \
19
    {"size", (PyCFunction)EncodingMap_size, METH_NOARGS, EncodingMap_size__doc__},
20
21
static PyObject *
22
EncodingMap_size_impl(struct encoding_map *self);
23
24
static PyObject *
25
EncodingMap_size(PyObject *self, PyObject *Py_UNUSED(ignored))
26
0
{
27
0
    return EncodingMap_size_impl((struct encoding_map *)self);
28
0
}
29
30
PyDoc_STRVAR(unicode_title__doc__,
31
"title($self, /)\n"
32
"--\n"
33
"\n"
34
"Return a version of the string where each word is titlecased.\n"
35
"\n"
36
"More specifically, words start with uppercased characters and all remaining\n"
37
"cased characters have lower case.");
38
39
#define UNICODE_TITLE_METHODDEF    \
40
    {"title", (PyCFunction)unicode_title, METH_NOARGS, unicode_title__doc__},
41
42
static PyObject *
43
unicode_title_impl(PyObject *self);
44
45
static PyObject *
46
unicode_title(PyObject *self, PyObject *Py_UNUSED(ignored))
47
0
{
48
0
    return unicode_title_impl(self);
49
0
}
50
51
PyDoc_STRVAR(unicode_capitalize__doc__,
52
"capitalize($self, /)\n"
53
"--\n"
54
"\n"
55
"Return a capitalized version of the string.\n"
56
"\n"
57
"More specifically, make the first character have upper case and the rest lower\n"
58
"case.");
59
60
#define UNICODE_CAPITALIZE_METHODDEF    \
61
    {"capitalize", (PyCFunction)unicode_capitalize, METH_NOARGS, unicode_capitalize__doc__},
62
63
static PyObject *
64
unicode_capitalize_impl(PyObject *self);
65
66
static PyObject *
67
unicode_capitalize(PyObject *self, PyObject *Py_UNUSED(ignored))
68
0
{
69
0
    return unicode_capitalize_impl(self);
70
0
}
71
72
PyDoc_STRVAR(unicode_casefold__doc__,
73
"casefold($self, /)\n"
74
"--\n"
75
"\n"
76
"Return a version of the string suitable for caseless comparisons.");
77
78
#define UNICODE_CASEFOLD_METHODDEF    \
79
    {"casefold", (PyCFunction)unicode_casefold, METH_NOARGS, unicode_casefold__doc__},
80
81
static PyObject *
82
unicode_casefold_impl(PyObject *self);
83
84
static PyObject *
85
unicode_casefold(PyObject *self, PyObject *Py_UNUSED(ignored))
86
0
{
87
0
    return unicode_casefold_impl(self);
88
0
}
89
90
PyDoc_STRVAR(unicode_center__doc__,
91
"center($self, width, fillchar=\' \', /)\n"
92
"--\n"
93
"\n"
94
"Return a centered string of length width.\n"
95
"\n"
96
"Padding is done using the specified fill character (default is a space).");
97
98
#define UNICODE_CENTER_METHODDEF    \
99
    {"center", _PyCFunction_CAST(unicode_center), METH_FASTCALL, unicode_center__doc__},
100
101
static PyObject *
102
unicode_center_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
103
104
static PyObject *
105
unicode_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
106
0
{
107
0
    PyObject *return_value = NULL;
108
0
    Py_ssize_t width;
109
0
    Py_UCS4 fillchar = ' ';
110
111
0
    if (!_PyArg_CheckPositional("center", nargs, 1, 2)) {
112
0
        goto exit;
113
0
    }
114
0
    {
115
0
        Py_ssize_t ival = -1;
116
0
        PyObject *iobj = _PyNumber_Index(args[0]);
117
0
        if (iobj != NULL) {
118
0
            ival = PyLong_AsSsize_t(iobj);
119
0
            Py_DECREF(iobj);
120
0
        }
121
0
        if (ival == -1 && PyErr_Occurred()) {
122
0
            goto exit;
123
0
        }
124
0
        width = ival;
125
0
    }
126
0
    if (nargs < 2) {
127
0
        goto skip_optional;
128
0
    }
129
0
    if (!convert_uc(args[1], &fillchar)) {
130
0
        goto exit;
131
0
    }
132
0
skip_optional:
133
0
    return_value = unicode_center_impl(self, width, fillchar);
134
135
0
exit:
136
0
    return return_value;
137
0
}
138
139
PyDoc_STRVAR(unicode_count__doc__,
140
"count($self, sub[, start[, end]], /)\n"
141
"--\n"
142
"\n"
143
"Return the number of non-overlapping occurrences of substring sub in string S[start:end].\n"
144
"\n"
145
"Optional arguments start and end are interpreted as in slice notation.");
146
147
#define UNICODE_COUNT_METHODDEF    \
148
    {"count", _PyCFunction_CAST(unicode_count), METH_FASTCALL, unicode_count__doc__},
149
150
static Py_ssize_t
151
unicode_count_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
152
                   Py_ssize_t end);
153
154
static PyObject *
155
unicode_count(PyObject *str, PyObject *const *args, Py_ssize_t nargs)
156
26.0M
{
157
26.0M
    PyObject *return_value = NULL;
158
26.0M
    PyObject *substr;
159
26.0M
    Py_ssize_t start = 0;
160
26.0M
    Py_ssize_t end = PY_SSIZE_T_MAX;
161
26.0M
    Py_ssize_t _return_value;
162
163
26.0M
    if (!_PyArg_CheckPositional("count", nargs, 1, 3)) {
164
0
        goto exit;
165
0
    }
166
26.0M
    if (!PyUnicode_Check(args[0])) {
167
0
        _PyArg_BadArgument("count", "argument 1", "str", args[0]);
168
0
        goto exit;
169
0
    }
170
26.0M
    substr = args[0];
171
26.0M
    if (nargs < 2) {
172
8.24M
        goto skip_optional;
173
8.24M
    }
174
17.7M
    if (!_PyEval_SliceIndex(args[1], &start)) {
175
0
        goto exit;
176
0
    }
177
17.7M
    if (nargs < 3) {
178
0
        goto skip_optional;
179
0
    }
180
17.7M
    if (!_PyEval_SliceIndex(args[2], &end)) {
181
0
        goto exit;
182
0
    }
183
26.0M
skip_optional:
184
26.0M
    _return_value = unicode_count_impl(str, substr, start, end);
185
26.0M
    if ((_return_value == -1) && PyErr_Occurred()) {
186
0
        goto exit;
187
0
    }
188
26.0M
    return_value = PyLong_FromSsize_t(_return_value);
189
190
26.0M
exit:
191
26.0M
    return return_value;
192
26.0M
}
193
194
PyDoc_STRVAR(unicode_encode__doc__,
195
"encode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
196
"--\n"
197
"\n"
198
"Encode the string using the codec registered for encoding.\n"
199
"\n"
200
"  encoding\n"
201
"    The encoding in which to encode the string.\n"
202
"  errors\n"
203
"    The error handling scheme to use for encoding errors.\n"
204
"    The default is \'strict\' meaning that encoding errors raise a\n"
205
"    UnicodeEncodeError.  Other possible values are \'ignore\', \'replace\' and\n"
206
"    \'xmlcharrefreplace\' as well as any other name registered with\n"
207
"    codecs.register_error that can handle UnicodeEncodeErrors.");
208
209
#define UNICODE_ENCODE_METHODDEF    \
210
    {"encode", _PyCFunction_CAST(unicode_encode), METH_FASTCALL|METH_KEYWORDS, unicode_encode__doc__},
211
212
static PyObject *
213
unicode_encode_impl(PyObject *self, const char *encoding, const char *errors);
214
215
static PyObject *
216
unicode_encode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
217
18.2M
{
218
18.2M
    PyObject *return_value = NULL;
219
18.2M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
220
221
18.2M
    #define NUM_KEYWORDS 2
222
18.2M
    static struct {
223
18.2M
        PyGC_Head _this_is_not_used;
224
18.2M
        PyObject_VAR_HEAD
225
18.2M
        Py_hash_t ob_hash;
226
18.2M
        PyObject *ob_item[NUM_KEYWORDS];
227
18.2M
    } _kwtuple = {
228
18.2M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
229
18.2M
        .ob_hash = -1,
230
18.2M
        .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), },
231
18.2M
    };
232
18.2M
    #undef NUM_KEYWORDS
233
18.2M
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
234
235
    #else  // !Py_BUILD_CORE
236
    #  define KWTUPLE NULL
237
    #endif  // !Py_BUILD_CORE
238
239
18.2M
    static const char * const _keywords[] = {"encoding", "errors", NULL};
240
18.2M
    static _PyArg_Parser _parser = {
241
18.2M
        .keywords = _keywords,
242
18.2M
        .fname = "encode",
243
18.2M
        .kwtuple = KWTUPLE,
244
18.2M
    };
245
18.2M
    #undef KWTUPLE
246
18.2M
    PyObject *argsbuf[2];
247
18.2M
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
248
18.2M
    const char *encoding = NULL;
249
18.2M
    const char *errors = NULL;
250
251
18.2M
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
252
18.2M
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
253
18.2M
    if (!args) {
254
0
        goto exit;
255
0
    }
256
18.2M
    if (!noptargs) {
257
11.8M
        goto skip_optional_pos;
258
11.8M
    }
259
6.40M
    if (args[0]) {
260
6.35M
        if (!PyUnicode_Check(args[0])) {
261
0
            _PyArg_BadArgument("encode", "argument 'encoding'", "str", args[0]);
262
0
            goto exit;
263
0
        }
264
6.35M
        Py_ssize_t encoding_length;
265
6.35M
        encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
266
6.35M
        if (encoding == NULL) {
267
0
            goto exit;
268
0
        }
269
6.35M
        if (strlen(encoding) != (size_t)encoding_length) {
270
0
            PyErr_SetString(PyExc_ValueError, "embedded null character");
271
0
            goto exit;
272
0
        }
273
6.35M
        if (!--noptargs) {
274
5.39M
            goto skip_optional_pos;
275
5.39M
        }
276
6.35M
    }
277
1.01M
    if (!PyUnicode_Check(args[1])) {
278
0
        _PyArg_BadArgument("encode", "argument 'errors'", "str", args[1]);
279
0
        goto exit;
280
0
    }
281
1.01M
    Py_ssize_t errors_length;
282
1.01M
    errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
283
1.01M
    if (errors == NULL) {
284
0
        goto exit;
285
0
    }
286
1.01M
    if (strlen(errors) != (size_t)errors_length) {
287
0
        PyErr_SetString(PyExc_ValueError, "embedded null character");
288
0
        goto exit;
289
0
    }
290
18.2M
skip_optional_pos:
291
18.2M
    return_value = unicode_encode_impl(self, encoding, errors);
292
293
18.2M
exit:
294
18.2M
    return return_value;
295
18.2M
}
296
297
PyDoc_STRVAR(unicode_expandtabs__doc__,
298
"expandtabs($self, /, tabsize=8)\n"
299
"--\n"
300
"\n"
301
"Return a copy where all tab characters are expanded using spaces.\n"
302
"\n"
303
"If tabsize is not given, a tab size of 8 characters is assumed.");
304
305
#define UNICODE_EXPANDTABS_METHODDEF    \
306
    {"expandtabs", _PyCFunction_CAST(unicode_expandtabs), METH_FASTCALL|METH_KEYWORDS, unicode_expandtabs__doc__},
307
308
static PyObject *
309
unicode_expandtabs_impl(PyObject *self, int tabsize);
310
311
static PyObject *
312
unicode_expandtabs(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
313
1.37M
{
314
1.37M
    PyObject *return_value = NULL;
315
1.37M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
316
317
1.37M
    #define NUM_KEYWORDS 1
318
1.37M
    static struct {
319
1.37M
        PyGC_Head _this_is_not_used;
320
1.37M
        PyObject_VAR_HEAD
321
1.37M
        Py_hash_t ob_hash;
322
1.37M
        PyObject *ob_item[NUM_KEYWORDS];
323
1.37M
    } _kwtuple = {
324
1.37M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
325
1.37M
        .ob_hash = -1,
326
1.37M
        .ob_item = { &_Py_ID(tabsize), },
327
1.37M
    };
328
1.37M
    #undef NUM_KEYWORDS
329
1.37M
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
330
331
    #else  // !Py_BUILD_CORE
332
    #  define KWTUPLE NULL
333
    #endif  // !Py_BUILD_CORE
334
335
1.37M
    static const char * const _keywords[] = {"tabsize", NULL};
336
1.37M
    static _PyArg_Parser _parser = {
337
1.37M
        .keywords = _keywords,
338
1.37M
        .fname = "expandtabs",
339
1.37M
        .kwtuple = KWTUPLE,
340
1.37M
    };
341
1.37M
    #undef KWTUPLE
342
1.37M
    PyObject *argsbuf[1];
343
1.37M
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
344
1.37M
    int tabsize = 8;
345
346
1.37M
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
347
1.37M
            /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
348
1.37M
    if (!args) {
349
0
        goto exit;
350
0
    }
351
1.37M
    if (!noptargs) {
352
831
        goto skip_optional_pos;
353
831
    }
354
1.37M
    tabsize = PyLong_AsInt(args[0]);
355
1.37M
    if (tabsize == -1 && PyErr_Occurred()) {
356
0
        goto exit;
357
0
    }
358
1.37M
skip_optional_pos:
359
1.37M
    return_value = unicode_expandtabs_impl(self, tabsize);
360
361
1.37M
exit:
362
1.37M
    return return_value;
363
1.37M
}
364
365
PyDoc_STRVAR(unicode_find__doc__,
366
"find($self, sub[, start[, end]], /)\n"
367
"--\n"
368
"\n"
369
"Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end].\n"
370
"\n"
371
"Optional arguments start and end are interpreted as in slice notation.\n"
372
"Return -1 on failure.");
373
374
#define UNICODE_FIND_METHODDEF    \
375
    {"find", _PyCFunction_CAST(unicode_find), METH_FASTCALL, unicode_find__doc__},
376
377
static Py_ssize_t
378
unicode_find_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
379
                  Py_ssize_t end);
380
381
static PyObject *
382
unicode_find(PyObject *str, PyObject *const *args, Py_ssize_t nargs)
383
24.7M
{
384
24.7M
    PyObject *return_value = NULL;
385
24.7M
    PyObject *substr;
386
24.7M
    Py_ssize_t start = 0;
387
24.7M
    Py_ssize_t end = PY_SSIZE_T_MAX;
388
24.7M
    Py_ssize_t _return_value;
389
390
24.7M
    if (!_PyArg_CheckPositional("find", nargs, 1, 3)) {
391
0
        goto exit;
392
0
    }
393
24.7M
    if (!PyUnicode_Check(args[0])) {
394
0
        _PyArg_BadArgument("find", "argument 1", "str", args[0]);
395
0
        goto exit;
396
0
    }
397
24.7M
    substr = args[0];
398
24.7M
    if (nargs < 2) {
399
2.10M
        goto skip_optional;
400
2.10M
    }
401
22.6M
    if (!_PyEval_SliceIndex(args[1], &start)) {
402
0
        goto exit;
403
0
    }
404
22.6M
    if (nargs < 3) {
405
17.8M
        goto skip_optional;
406
17.8M
    }
407
4.75M
    if (!_PyEval_SliceIndex(args[2], &end)) {
408
0
        goto exit;
409
0
    }
410
24.7M
skip_optional:
411
24.7M
    _return_value = unicode_find_impl(str, substr, start, end);
412
24.7M
    if ((_return_value == -1) && PyErr_Occurred()) {
413
0
        goto exit;
414
0
    }
415
24.7M
    return_value = PyLong_FromSsize_t(_return_value);
416
417
24.7M
exit:
418
24.7M
    return return_value;
419
24.7M
}
420
421
PyDoc_STRVAR(unicode_index__doc__,
422
"index($self, sub[, start[, end]], /)\n"
423
"--\n"
424
"\n"
425
"Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end].\n"
426
"\n"
427
"Optional arguments start and end are interpreted as in slice notation.\n"
428
"Raises ValueError when the substring is not found.");
429
430
#define UNICODE_INDEX_METHODDEF    \
431
    {"index", _PyCFunction_CAST(unicode_index), METH_FASTCALL, unicode_index__doc__},
432
433
static Py_ssize_t
434
unicode_index_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
435
                   Py_ssize_t end);
436
437
static PyObject *
438
unicode_index(PyObject *str, PyObject *const *args, Py_ssize_t nargs)
439
45.0k
{
440
45.0k
    PyObject *return_value = NULL;
441
45.0k
    PyObject *substr;
442
45.0k
    Py_ssize_t start = 0;
443
45.0k
    Py_ssize_t end = PY_SSIZE_T_MAX;
444
45.0k
    Py_ssize_t _return_value;
445
446
45.0k
    if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
447
0
        goto exit;
448
0
    }
449
45.0k
    if (!PyUnicode_Check(args[0])) {
450
0
        _PyArg_BadArgument("index", "argument 1", "str", args[0]);
451
0
        goto exit;
452
0
    }
453
45.0k
    substr = args[0];
454
45.0k
    if (nargs < 2) {
455
0
        goto skip_optional;
456
0
    }
457
45.0k
    if (!_PyEval_SliceIndex(args[1], &start)) {
458
0
        goto exit;
459
0
    }
460
45.0k
    if (nargs < 3) {
461
45.0k
        goto skip_optional;
462
45.0k
    }
463
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
464
0
        goto exit;
465
0
    }
466
45.0k
skip_optional:
467
45.0k
    _return_value = unicode_index_impl(str, substr, start, end);
468
45.0k
    if ((_return_value == -1) && PyErr_Occurred()) {
469
712
        goto exit;
470
712
    }
471
44.3k
    return_value = PyLong_FromSsize_t(_return_value);
472
473
45.0k
exit:
474
45.0k
    return return_value;
475
44.3k
}
476
477
PyDoc_STRVAR(unicode_isascii__doc__,
478
"isascii($self, /)\n"
479
"--\n"
480
"\n"
481
"Return True if all characters in the string are ASCII, False otherwise.\n"
482
"\n"
483
"ASCII characters have code points in the range U+0000-U+007F.\n"
484
"Empty string is ASCII too.");
485
486
#define UNICODE_ISASCII_METHODDEF    \
487
    {"isascii", (PyCFunction)unicode_isascii, METH_NOARGS, unicode_isascii__doc__},
488
489
static PyObject *
490
unicode_isascii_impl(PyObject *self);
491
492
static PyObject *
493
unicode_isascii(PyObject *self, PyObject *Py_UNUSED(ignored))
494
5.29k
{
495
5.29k
    return unicode_isascii_impl(self);
496
5.29k
}
497
498
PyDoc_STRVAR(unicode_islower__doc__,
499
"islower($self, /)\n"
500
"--\n"
501
"\n"
502
"Return True if the string is a lowercase string, False otherwise.\n"
503
"\n"
504
"A string is lowercase if all cased characters in the string are lowercase and\n"
505
"there is at least one cased character in the string.");
506
507
#define UNICODE_ISLOWER_METHODDEF    \
508
    {"islower", (PyCFunction)unicode_islower, METH_NOARGS, unicode_islower__doc__},
509
510
static PyObject *
511
unicode_islower_impl(PyObject *self);
512
513
static PyObject *
514
unicode_islower(PyObject *self, PyObject *Py_UNUSED(ignored))
515
0
{
516
0
    return unicode_islower_impl(self);
517
0
}
518
519
PyDoc_STRVAR(unicode_isupper__doc__,
520
"isupper($self, /)\n"
521
"--\n"
522
"\n"
523
"Return True if the string is an uppercase string, False otherwise.\n"
524
"\n"
525
"A string is uppercase if all cased characters in the string are uppercase and\n"
526
"there is at least one cased character in the string.");
527
528
#define UNICODE_ISUPPER_METHODDEF    \
529
    {"isupper", (PyCFunction)unicode_isupper, METH_NOARGS, unicode_isupper__doc__},
530
531
static PyObject *
532
unicode_isupper_impl(PyObject *self);
533
534
static PyObject *
535
unicode_isupper(PyObject *self, PyObject *Py_UNUSED(ignored))
536
10.7k
{
537
10.7k
    return unicode_isupper_impl(self);
538
10.7k
}
539
540
PyDoc_STRVAR(unicode_istitle__doc__,
541
"istitle($self, /)\n"
542
"--\n"
543
"\n"
544
"Return True if the string is a title-cased string, False otherwise.\n"
545
"\n"
546
"In a title-cased string, upper- and title-case characters may only\n"
547
"follow uncased characters and lowercase characters only cased ones.");
548
549
#define UNICODE_ISTITLE_METHODDEF    \
550
    {"istitle", (PyCFunction)unicode_istitle, METH_NOARGS, unicode_istitle__doc__},
551
552
static PyObject *
553
unicode_istitle_impl(PyObject *self);
554
555
static PyObject *
556
unicode_istitle(PyObject *self, PyObject *Py_UNUSED(ignored))
557
0
{
558
0
    return unicode_istitle_impl(self);
559
0
}
560
561
PyDoc_STRVAR(unicode_isspace__doc__,
562
"isspace($self, /)\n"
563
"--\n"
564
"\n"
565
"Return True if the string is a whitespace string, False otherwise.\n"
566
"\n"
567
"A string is whitespace if all characters in the string are whitespace and there\n"
568
"is at least one character in the string.");
569
570
#define UNICODE_ISSPACE_METHODDEF    \
571
    {"isspace", (PyCFunction)unicode_isspace, METH_NOARGS, unicode_isspace__doc__},
572
573
static PyObject *
574
unicode_isspace_impl(PyObject *self);
575
576
static PyObject *
577
unicode_isspace(PyObject *self, PyObject *Py_UNUSED(ignored))
578
1.34M
{
579
1.34M
    return unicode_isspace_impl(self);
580
1.34M
}
581
582
PyDoc_STRVAR(unicode_isalpha__doc__,
583
"isalpha($self, /)\n"
584
"--\n"
585
"\n"
586
"Return True if the string is an alphabetic string, False otherwise.\n"
587
"\n"
588
"A string is alphabetic if all characters in the string are alphabetic and there\n"
589
"is at least one character in the string.");
590
591
#define UNICODE_ISALPHA_METHODDEF    \
592
    {"isalpha", (PyCFunction)unicode_isalpha, METH_NOARGS, unicode_isalpha__doc__},
593
594
static PyObject *
595
unicode_isalpha_impl(PyObject *self);
596
597
static PyObject *
598
unicode_isalpha(PyObject *self, PyObject *Py_UNUSED(ignored))
599
19
{
600
19
    return unicode_isalpha_impl(self);
601
19
}
602
603
PyDoc_STRVAR(unicode_isalnum__doc__,
604
"isalnum($self, /)\n"
605
"--\n"
606
"\n"
607
"Return True if the string is an alpha-numeric string, False otherwise.\n"
608
"\n"
609
"A string is alpha-numeric if all characters in the string are alpha-numeric and\n"
610
"there is at least one character in the string.");
611
612
#define UNICODE_ISALNUM_METHODDEF    \
613
    {"isalnum", (PyCFunction)unicode_isalnum, METH_NOARGS, unicode_isalnum__doc__},
614
615
static PyObject *
616
unicode_isalnum_impl(PyObject *self);
617
618
static PyObject *
619
unicode_isalnum(PyObject *self, PyObject *Py_UNUSED(ignored))
620
0
{
621
0
    return unicode_isalnum_impl(self);
622
0
}
623
624
PyDoc_STRVAR(unicode_isdecimal__doc__,
625
"isdecimal($self, /)\n"
626
"--\n"
627
"\n"
628
"Return True if the string is a decimal string, False otherwise.\n"
629
"\n"
630
"A string is a decimal string if all characters in the string are decimal and\n"
631
"there is at least one character in the string.");
632
633
#define UNICODE_ISDECIMAL_METHODDEF    \
634
    {"isdecimal", (PyCFunction)unicode_isdecimal, METH_NOARGS, unicode_isdecimal__doc__},
635
636
static PyObject *
637
unicode_isdecimal_impl(PyObject *self);
638
639
static PyObject *
640
unicode_isdecimal(PyObject *self, PyObject *Py_UNUSED(ignored))
641
1.43k
{
642
1.43k
    return unicode_isdecimal_impl(self);
643
1.43k
}
644
645
PyDoc_STRVAR(unicode_isdigit__doc__,
646
"isdigit($self, /)\n"
647
"--\n"
648
"\n"
649
"Return True if the string is a digit string, False otherwise.\n"
650
"\n"
651
"A string is a digit string if all characters in the string are digits and there\n"
652
"is at least one character in the string.");
653
654
#define UNICODE_ISDIGIT_METHODDEF    \
655
    {"isdigit", (PyCFunction)unicode_isdigit, METH_NOARGS, unicode_isdigit__doc__},
656
657
static PyObject *
658
unicode_isdigit_impl(PyObject *self);
659
660
static PyObject *
661
unicode_isdigit(PyObject *self, PyObject *Py_UNUSED(ignored))
662
1.08M
{
663
1.08M
    return unicode_isdigit_impl(self);
664
1.08M
}
665
666
PyDoc_STRVAR(unicode_isnumeric__doc__,
667
"isnumeric($self, /)\n"
668
"--\n"
669
"\n"
670
"Return True if the string is a numeric string, False otherwise.\n"
671
"\n"
672
"A string is numeric if all characters in the string are numeric and there is at\n"
673
"least one character in the string.");
674
675
#define UNICODE_ISNUMERIC_METHODDEF    \
676
    {"isnumeric", (PyCFunction)unicode_isnumeric, METH_NOARGS, unicode_isnumeric__doc__},
677
678
static PyObject *
679
unicode_isnumeric_impl(PyObject *self);
680
681
static PyObject *
682
unicode_isnumeric(PyObject *self, PyObject *Py_UNUSED(ignored))
683
0
{
684
0
    return unicode_isnumeric_impl(self);
685
0
}
686
687
PyDoc_STRVAR(unicode_isidentifier__doc__,
688
"isidentifier($self, /)\n"
689
"--\n"
690
"\n"
691
"Return True if the string is a valid Python identifier, False otherwise.\n"
692
"\n"
693
"Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n"
694
"such as \"def\" or \"class\".");
695
696
#define UNICODE_ISIDENTIFIER_METHODDEF    \
697
    {"isidentifier", (PyCFunction)unicode_isidentifier, METH_NOARGS, unicode_isidentifier__doc__},
698
699
static PyObject *
700
unicode_isidentifier_impl(PyObject *self);
701
702
static PyObject *
703
unicode_isidentifier(PyObject *self, PyObject *Py_UNUSED(ignored))
704
49.0k
{
705
49.0k
    return unicode_isidentifier_impl(self);
706
49.0k
}
707
708
PyDoc_STRVAR(unicode_isprintable__doc__,
709
"isprintable($self, /)\n"
710
"--\n"
711
"\n"
712
"Return True if all characters in the string are printable, False otherwise.\n"
713
"\n"
714
"A character is printable if repr() may use it in its output.");
715
716
#define UNICODE_ISPRINTABLE_METHODDEF    \
717
    {"isprintable", (PyCFunction)unicode_isprintable, METH_NOARGS, unicode_isprintable__doc__},
718
719
static PyObject *
720
unicode_isprintable_impl(PyObject *self);
721
722
static PyObject *
723
unicode_isprintable(PyObject *self, PyObject *Py_UNUSED(ignored))
724
1.81M
{
725
1.81M
    return unicode_isprintable_impl(self);
726
1.81M
}
727
728
PyDoc_STRVAR(unicode_join__doc__,
729
"join($self, iterable, /)\n"
730
"--\n"
731
"\n"
732
"Concatenate any number of strings.\n"
733
"\n"
734
"The string whose method is called is inserted in between each given string.\n"
735
"The result is returned as a new string.\n"
736
"\n"
737
"Example: \'.\'.join([\'ab\', \'pq\', \'rs\']) -> \'ab.pq.rs\'");
738
739
#define UNICODE_JOIN_METHODDEF    \
740
    {"join", (PyCFunction)unicode_join, METH_O, unicode_join__doc__},
741
742
PyDoc_STRVAR(unicode_ljust__doc__,
743
"ljust($self, width, fillchar=\' \', /)\n"
744
"--\n"
745
"\n"
746
"Return a left-justified string of length width.\n"
747
"\n"
748
"Padding is done using the specified fill character (default is a space).");
749
750
#define UNICODE_LJUST_METHODDEF    \
751
    {"ljust", _PyCFunction_CAST(unicode_ljust), METH_FASTCALL, unicode_ljust__doc__},
752
753
static PyObject *
754
unicode_ljust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
755
756
static PyObject *
757
unicode_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
758
130
{
759
130
    PyObject *return_value = NULL;
760
130
    Py_ssize_t width;
761
130
    Py_UCS4 fillchar = ' ';
762
763
130
    if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) {
764
0
        goto exit;
765
0
    }
766
130
    {
767
130
        Py_ssize_t ival = -1;
768
130
        PyObject *iobj = _PyNumber_Index(args[0]);
769
130
        if (iobj != NULL) {
770
130
            ival = PyLong_AsSsize_t(iobj);
771
130
            Py_DECREF(iobj);
772
130
        }
773
130
        if (ival == -1 && PyErr_Occurred()) {
774
0
            goto exit;
775
0
        }
776
130
        width = ival;
777
130
    }
778
130
    if (nargs < 2) {
779
0
        goto skip_optional;
780
0
    }
781
130
    if (!convert_uc(args[1], &fillchar)) {
782
0
        goto exit;
783
0
    }
784
130
skip_optional:
785
130
    return_value = unicode_ljust_impl(self, width, fillchar);
786
787
130
exit:
788
130
    return return_value;
789
130
}
790
791
PyDoc_STRVAR(unicode_lower__doc__,
792
"lower($self, /)\n"
793
"--\n"
794
"\n"
795
"Return a copy of the string converted to lowercase.");
796
797
#define UNICODE_LOWER_METHODDEF    \
798
    {"lower", (PyCFunction)unicode_lower, METH_NOARGS, unicode_lower__doc__},
799
800
static PyObject *
801
unicode_lower_impl(PyObject *self);
802
803
static PyObject *
804
unicode_lower(PyObject *self, PyObject *Py_UNUSED(ignored))
805
72.6M
{
806
72.6M
    return unicode_lower_impl(self);
807
72.6M
}
808
809
PyDoc_STRVAR(unicode_strip__doc__,
810
"strip($self, chars=None, /)\n"
811
"--\n"
812
"\n"
813
"Return a copy of the string with leading and trailing whitespace removed.\n"
814
"\n"
815
"If chars is given and not None, remove characters in chars instead.");
816
817
#define UNICODE_STRIP_METHODDEF    \
818
    {"strip", _PyCFunction_CAST(unicode_strip), METH_FASTCALL, unicode_strip__doc__},
819
820
static PyObject *
821
unicode_strip_impl(PyObject *self, PyObject *chars);
822
823
static PyObject *
824
unicode_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
825
44.1M
{
826
44.1M
    PyObject *return_value = NULL;
827
44.1M
    PyObject *chars = Py_None;
828
829
44.1M
    if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
830
0
        goto exit;
831
0
    }
832
44.1M
    if (nargs < 1) {
833
44.1M
        goto skip_optional;
834
44.1M
    }
835
36
    chars = args[0];
836
44.1M
skip_optional:
837
44.1M
    return_value = unicode_strip_impl(self, chars);
838
839
44.1M
exit:
840
44.1M
    return return_value;
841
44.1M
}
842
843
PyDoc_STRVAR(unicode_lstrip__doc__,
844
"lstrip($self, chars=None, /)\n"
845
"--\n"
846
"\n"
847
"Return a copy of the string with leading whitespace removed.\n"
848
"\n"
849
"If chars is given and not None, remove characters in chars instead.");
850
851
#define UNICODE_LSTRIP_METHODDEF    \
852
    {"lstrip", _PyCFunction_CAST(unicode_lstrip), METH_FASTCALL, unicode_lstrip__doc__},
853
854
static PyObject *
855
unicode_lstrip_impl(PyObject *self, PyObject *chars);
856
857
static PyObject *
858
unicode_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
859
2.01M
{
860
2.01M
    PyObject *return_value = NULL;
861
2.01M
    PyObject *chars = Py_None;
862
863
2.01M
    if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
864
0
        goto exit;
865
0
    }
866
2.01M
    if (nargs < 1) {
867
1.58M
        goto skip_optional;
868
1.58M
    }
869
421k
    chars = args[0];
870
2.01M
skip_optional:
871
2.01M
    return_value = unicode_lstrip_impl(self, chars);
872
873
2.01M
exit:
874
2.01M
    return return_value;
875
2.01M
}
876
877
PyDoc_STRVAR(unicode_rstrip__doc__,
878
"rstrip($self, chars=None, /)\n"
879
"--\n"
880
"\n"
881
"Return a copy of the string with trailing whitespace removed.\n"
882
"\n"
883
"If chars is given and not None, remove characters in chars instead.");
884
885
#define UNICODE_RSTRIP_METHODDEF    \
886
    {"rstrip", _PyCFunction_CAST(unicode_rstrip), METH_FASTCALL, unicode_rstrip__doc__},
887
888
static PyObject *
889
unicode_rstrip_impl(PyObject *self, PyObject *chars);
890
891
static PyObject *
892
unicode_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
893
16.9M
{
894
16.9M
    PyObject *return_value = NULL;
895
16.9M
    PyObject *chars = Py_None;
896
897
16.9M
    if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
898
0
        goto exit;
899
0
    }
900
16.9M
    if (nargs < 1) {
901
12.9M
        goto skip_optional;
902
12.9M
    }
903
4.03M
    chars = args[0];
904
16.9M
skip_optional:
905
16.9M
    return_value = unicode_rstrip_impl(self, chars);
906
907
16.9M
exit:
908
16.9M
    return return_value;
909
16.9M
}
910
911
PyDoc_STRVAR(unicode_replace__doc__,
912
"replace($self, old, new, /, count=-1)\n"
913
"--\n"
914
"\n"
915
"Return a copy with all occurrences of substring old replaced by new.\n"
916
"\n"
917
"  count\n"
918
"    Maximum number of occurrences to replace.\n"
919
"    -1 (the default value) means replace all occurrences.\n"
920
"\n"
921
"If count is given, only the first count occurrences are replaced.\n"
922
"If count is not specified or -1, then all occurrences are replaced.");
923
924
#define UNICODE_REPLACE_METHODDEF    \
925
    {"replace", _PyCFunction_CAST(unicode_replace), METH_FASTCALL|METH_KEYWORDS, unicode_replace__doc__},
926
927
static PyObject *
928
unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
929
                     Py_ssize_t count);
930
931
static PyObject *
932
unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
933
20.2M
{
934
20.2M
    PyObject *return_value = NULL;
935
20.2M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
936
937
20.2M
    #define NUM_KEYWORDS 1
938
20.2M
    static struct {
939
20.2M
        PyGC_Head _this_is_not_used;
940
20.2M
        PyObject_VAR_HEAD
941
20.2M
        Py_hash_t ob_hash;
942
20.2M
        PyObject *ob_item[NUM_KEYWORDS];
943
20.2M
    } _kwtuple = {
944
20.2M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
945
20.2M
        .ob_hash = -1,
946
20.2M
        .ob_item = { &_Py_ID(count), },
947
20.2M
    };
948
20.2M
    #undef NUM_KEYWORDS
949
20.2M
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
950
951
    #else  // !Py_BUILD_CORE
952
    #  define KWTUPLE NULL
953
    #endif  // !Py_BUILD_CORE
954
955
20.2M
    static const char * const _keywords[] = {"", "", "count", NULL};
956
20.2M
    static _PyArg_Parser _parser = {
957
20.2M
        .keywords = _keywords,
958
20.2M
        .fname = "replace",
959
20.2M
        .kwtuple = KWTUPLE,
960
20.2M
    };
961
20.2M
    #undef KWTUPLE
962
20.2M
    PyObject *argsbuf[3];
963
20.2M
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
964
20.2M
    PyObject *old;
965
20.2M
    PyObject *new;
966
20.2M
    Py_ssize_t count = -1;
967
968
20.2M
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
969
20.2M
            /*minpos*/ 2, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
970
20.2M
    if (!args) {
971
0
        goto exit;
972
0
    }
973
20.2M
    if (!PyUnicode_Check(args[0])) {
974
0
        _PyArg_BadArgument("replace", "argument 1", "str", args[0]);
975
0
        goto exit;
976
0
    }
977
20.2M
    old = args[0];
978
20.2M
    if (!PyUnicode_Check(args[1])) {
979
0
        _PyArg_BadArgument("replace", "argument 2", "str", args[1]);
980
0
        goto exit;
981
0
    }
982
20.2M
    new = args[1];
983
20.2M
    if (!noptargs) {
984
20.2M
        goto skip_optional_pos;
985
20.2M
    }
986
0
    {
987
0
        Py_ssize_t ival = -1;
988
0
        PyObject *iobj = _PyNumber_Index(args[2]);
989
0
        if (iobj != NULL) {
990
0
            ival = PyLong_AsSsize_t(iobj);
991
0
            Py_DECREF(iobj);
992
0
        }
993
0
        if (ival == -1 && PyErr_Occurred()) {
994
0
            goto exit;
995
0
        }
996
0
        count = ival;
997
0
    }
998
20.2M
skip_optional_pos:
999
20.2M
    return_value = unicode_replace_impl(self, old, new, count);
1000
1001
20.2M
exit:
1002
20.2M
    return return_value;
1003
20.2M
}
1004
1005
PyDoc_STRVAR(unicode_removeprefix__doc__,
1006
"removeprefix($self, prefix, /)\n"
1007
"--\n"
1008
"\n"
1009
"Return a str with the given prefix string removed if present.\n"
1010
"\n"
1011
"If the string starts with the prefix string, return string[len(prefix):].\n"
1012
"Otherwise, return a copy of the original string.");
1013
1014
#define UNICODE_REMOVEPREFIX_METHODDEF    \
1015
    {"removeprefix", (PyCFunction)unicode_removeprefix, METH_O, unicode_removeprefix__doc__},
1016
1017
static PyObject *
1018
unicode_removeprefix_impl(PyObject *self, PyObject *prefix);
1019
1020
static PyObject *
1021
unicode_removeprefix(PyObject *self, PyObject *arg)
1022
28
{
1023
28
    PyObject *return_value = NULL;
1024
28
    PyObject *prefix;
1025
1026
28
    if (!PyUnicode_Check(arg)) {
1027
0
        _PyArg_BadArgument("removeprefix", "argument", "str", arg);
1028
0
        goto exit;
1029
0
    }
1030
28
    prefix = arg;
1031
28
    return_value = unicode_removeprefix_impl(self, prefix);
1032
1033
28
exit:
1034
28
    return return_value;
1035
28
}
1036
1037
PyDoc_STRVAR(unicode_removesuffix__doc__,
1038
"removesuffix($self, suffix, /)\n"
1039
"--\n"
1040
"\n"
1041
"Return a str with the given suffix string removed if present.\n"
1042
"\n"
1043
"If the string ends with the suffix string and that suffix is not empty,\n"
1044
"return string[:-len(suffix)]. Otherwise, return a copy of the original\n"
1045
"string.");
1046
1047
#define UNICODE_REMOVESUFFIX_METHODDEF    \
1048
    {"removesuffix", (PyCFunction)unicode_removesuffix, METH_O, unicode_removesuffix__doc__},
1049
1050
static PyObject *
1051
unicode_removesuffix_impl(PyObject *self, PyObject *suffix);
1052
1053
static PyObject *
1054
unicode_removesuffix(PyObject *self, PyObject *arg)
1055
0
{
1056
0
    PyObject *return_value = NULL;
1057
0
    PyObject *suffix;
1058
1059
0
    if (!PyUnicode_Check(arg)) {
1060
0
        _PyArg_BadArgument("removesuffix", "argument", "str", arg);
1061
0
        goto exit;
1062
0
    }
1063
0
    suffix = arg;
1064
0
    return_value = unicode_removesuffix_impl(self, suffix);
1065
1066
0
exit:
1067
0
    return return_value;
1068
0
}
1069
1070
PyDoc_STRVAR(unicode_rfind__doc__,
1071
"rfind($self, sub[, start[, end]], /)\n"
1072
"--\n"
1073
"\n"
1074
"Return the highest index in S where substring sub is found, such that sub is contained within S[start:end].\n"
1075
"\n"
1076
"Optional arguments start and end are interpreted as in slice notation.\n"
1077
"Return -1 on failure.");
1078
1079
#define UNICODE_RFIND_METHODDEF    \
1080
    {"rfind", _PyCFunction_CAST(unicode_rfind), METH_FASTCALL, unicode_rfind__doc__},
1081
1082
static Py_ssize_t
1083
unicode_rfind_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
1084
                   Py_ssize_t end);
1085
1086
static PyObject *
1087
unicode_rfind(PyObject *str, PyObject *const *args, Py_ssize_t nargs)
1088
267k
{
1089
267k
    PyObject *return_value = NULL;
1090
267k
    PyObject *substr;
1091
267k
    Py_ssize_t start = 0;
1092
267k
    Py_ssize_t end = PY_SSIZE_T_MAX;
1093
267k
    Py_ssize_t _return_value;
1094
1095
267k
    if (!_PyArg_CheckPositional("rfind", nargs, 1, 3)) {
1096
0
        goto exit;
1097
0
    }
1098
267k
    if (!PyUnicode_Check(args[0])) {
1099
0
        _PyArg_BadArgument("rfind", "argument 1", "str", args[0]);
1100
0
        goto exit;
1101
0
    }
1102
267k
    substr = args[0];
1103
267k
    if (nargs < 2) {
1104
255k
        goto skip_optional;
1105
255k
    }
1106
11.9k
    if (!_PyEval_SliceIndex(args[1], &start)) {
1107
0
        goto exit;
1108
0
    }
1109
11.9k
    if (nargs < 3) {
1110
3.93k
        goto skip_optional;
1111
3.93k
    }
1112
8.05k
    if (!_PyEval_SliceIndex(args[2], &end)) {
1113
0
        goto exit;
1114
0
    }
1115
267k
skip_optional:
1116
267k
    _return_value = unicode_rfind_impl(str, substr, start, end);
1117
267k
    if ((_return_value == -1) && PyErr_Occurred()) {
1118
0
        goto exit;
1119
0
    }
1120
267k
    return_value = PyLong_FromSsize_t(_return_value);
1121
1122
267k
exit:
1123
267k
    return return_value;
1124
267k
}
1125
1126
PyDoc_STRVAR(unicode_rindex__doc__,
1127
"rindex($self, sub[, start[, end]], /)\n"
1128
"--\n"
1129
"\n"
1130
"Return the highest index in S where substring sub is found, such that sub is contained within S[start:end].\n"
1131
"\n"
1132
"Optional arguments start and end are interpreted as in slice notation.\n"
1133
"Raises ValueError when the substring is not found.");
1134
1135
#define UNICODE_RINDEX_METHODDEF    \
1136
    {"rindex", _PyCFunction_CAST(unicode_rindex), METH_FASTCALL, unicode_rindex__doc__},
1137
1138
static Py_ssize_t
1139
unicode_rindex_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
1140
                    Py_ssize_t end);
1141
1142
static PyObject *
1143
unicode_rindex(PyObject *str, PyObject *const *args, Py_ssize_t nargs)
1144
165k
{
1145
165k
    PyObject *return_value = NULL;
1146
165k
    PyObject *substr;
1147
165k
    Py_ssize_t start = 0;
1148
165k
    Py_ssize_t end = PY_SSIZE_T_MAX;
1149
165k
    Py_ssize_t _return_value;
1150
1151
165k
    if (!_PyArg_CheckPositional("rindex", nargs, 1, 3)) {
1152
0
        goto exit;
1153
0
    }
1154
165k
    if (!PyUnicode_Check(args[0])) {
1155
0
        _PyArg_BadArgument("rindex", "argument 1", "str", args[0]);
1156
0
        goto exit;
1157
0
    }
1158
165k
    substr = args[0];
1159
165k
    if (nargs < 2) {
1160
0
        goto skip_optional;
1161
0
    }
1162
165k
    if (!_PyEval_SliceIndex(args[1], &start)) {
1163
0
        goto exit;
1164
0
    }
1165
165k
    if (nargs < 3) {
1166
0
        goto skip_optional;
1167
0
    }
1168
165k
    if (!_PyEval_SliceIndex(args[2], &end)) {
1169
0
        goto exit;
1170
0
    }
1171
165k
skip_optional:
1172
165k
    _return_value = unicode_rindex_impl(str, substr, start, end);
1173
165k
    if ((_return_value == -1) && PyErr_Occurred()) {
1174
0
        goto exit;
1175
0
    }
1176
165k
    return_value = PyLong_FromSsize_t(_return_value);
1177
1178
165k
exit:
1179
165k
    return return_value;
1180
165k
}
1181
1182
PyDoc_STRVAR(unicode_rjust__doc__,
1183
"rjust($self, width, fillchar=\' \', /)\n"
1184
"--\n"
1185
"\n"
1186
"Return a right-justified string of length width.\n"
1187
"\n"
1188
"Padding is done using the specified fill character (default is a space).");
1189
1190
#define UNICODE_RJUST_METHODDEF    \
1191
    {"rjust", _PyCFunction_CAST(unicode_rjust), METH_FASTCALL, unicode_rjust__doc__},
1192
1193
static PyObject *
1194
unicode_rjust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
1195
1196
static PyObject *
1197
unicode_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1198
0
{
1199
0
    PyObject *return_value = NULL;
1200
0
    Py_ssize_t width;
1201
0
    Py_UCS4 fillchar = ' ';
1202
1203
0
    if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) {
1204
0
        goto exit;
1205
0
    }
1206
0
    {
1207
0
        Py_ssize_t ival = -1;
1208
0
        PyObject *iobj = _PyNumber_Index(args[0]);
1209
0
        if (iobj != NULL) {
1210
0
            ival = PyLong_AsSsize_t(iobj);
1211
0
            Py_DECREF(iobj);
1212
0
        }
1213
0
        if (ival == -1 && PyErr_Occurred()) {
1214
0
            goto exit;
1215
0
        }
1216
0
        width = ival;
1217
0
    }
1218
0
    if (nargs < 2) {
1219
0
        goto skip_optional;
1220
0
    }
1221
0
    if (!convert_uc(args[1], &fillchar)) {
1222
0
        goto exit;
1223
0
    }
1224
0
skip_optional:
1225
0
    return_value = unicode_rjust_impl(self, width, fillchar);
1226
1227
0
exit:
1228
0
    return return_value;
1229
0
}
1230
1231
PyDoc_STRVAR(unicode_split__doc__,
1232
"split($self, /, sep=None, maxsplit=-1)\n"
1233
"--\n"
1234
"\n"
1235
"Return a list of the substrings in the string, using sep as the separator string.\n"
1236
"\n"
1237
"  sep\n"
1238
"    The separator used to split the string.\n"
1239
"\n"
1240
"    When set to None (the default value), will split on any whitespace\n"
1241
"    character (including \\n \\r \\t \\f and spaces) and will discard\n"
1242
"    empty strings from the result.\n"
1243
"  maxsplit\n"
1244
"    Maximum number of splits.\n"
1245
"    -1 (the default value) means no limit.\n"
1246
"\n"
1247
"Splitting starts at the front of the string and works to the end.\n"
1248
"\n"
1249
"Note, str.split() is mainly useful for data that has been intentionally\n"
1250
"delimited.  With natural text that includes punctuation, consider using\n"
1251
"the regular expression module.");
1252
1253
#define UNICODE_SPLIT_METHODDEF    \
1254
    {"split", _PyCFunction_CAST(unicode_split), METH_FASTCALL|METH_KEYWORDS, unicode_split__doc__},
1255
1256
static PyObject *
1257
unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
1258
1259
static PyObject *
1260
unicode_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1261
20.8M
{
1262
20.8M
    PyObject *return_value = NULL;
1263
20.8M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1264
1265
20.8M
    #define NUM_KEYWORDS 2
1266
20.8M
    static struct {
1267
20.8M
        PyGC_Head _this_is_not_used;
1268
20.8M
        PyObject_VAR_HEAD
1269
20.8M
        Py_hash_t ob_hash;
1270
20.8M
        PyObject *ob_item[NUM_KEYWORDS];
1271
20.8M
    } _kwtuple = {
1272
20.8M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1273
20.8M
        .ob_hash = -1,
1274
20.8M
        .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
1275
20.8M
    };
1276
20.8M
    #undef NUM_KEYWORDS
1277
20.8M
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1278
1279
    #else  // !Py_BUILD_CORE
1280
    #  define KWTUPLE NULL
1281
    #endif  // !Py_BUILD_CORE
1282
1283
20.8M
    static const char * const _keywords[] = {"sep", "maxsplit", NULL};
1284
20.8M
    static _PyArg_Parser _parser = {
1285
20.8M
        .keywords = _keywords,
1286
20.8M
        .fname = "split",
1287
20.8M
        .kwtuple = KWTUPLE,
1288
20.8M
    };
1289
20.8M
    #undef KWTUPLE
1290
20.8M
    PyObject *argsbuf[2];
1291
20.8M
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1292
20.8M
    PyObject *sep = Py_None;
1293
20.8M
    Py_ssize_t maxsplit = -1;
1294
1295
20.8M
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1296
20.8M
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1297
20.8M
    if (!args) {
1298
0
        goto exit;
1299
0
    }
1300
20.8M
    if (!noptargs) {
1301
141k
        goto skip_optional_pos;
1302
141k
    }
1303
20.6M
    if (args[0]) {
1304
20.6M
        sep = args[0];
1305
20.6M
        if (!--noptargs) {
1306
15.0M
            goto skip_optional_pos;
1307
15.0M
        }
1308
20.6M
    }
1309
5.66M
    {
1310
5.66M
        Py_ssize_t ival = -1;
1311
5.66M
        PyObject *iobj = _PyNumber_Index(args[1]);
1312
5.66M
        if (iobj != NULL) {
1313
5.66M
            ival = PyLong_AsSsize_t(iobj);
1314
5.66M
            Py_DECREF(iobj);
1315
5.66M
        }
1316
5.66M
        if (ival == -1 && PyErr_Occurred()) {
1317
0
            goto exit;
1318
0
        }
1319
5.66M
        maxsplit = ival;
1320
5.66M
    }
1321
20.8M
skip_optional_pos:
1322
20.8M
    return_value = unicode_split_impl(self, sep, maxsplit);
1323
1324
20.8M
exit:
1325
20.8M
    return return_value;
1326
20.8M
}
1327
1328
PyDoc_STRVAR(unicode_partition__doc__,
1329
"partition($self, sep, /)\n"
1330
"--\n"
1331
"\n"
1332
"Partition the string into three parts using the given separator.\n"
1333
"\n"
1334
"This will search for the separator in the string.  If the separator is found,\n"
1335
"returns a 3-tuple containing the part before the separator, the separator\n"
1336
"itself, and the part after it.\n"
1337
"\n"
1338
"If the separator is not found, returns a 3-tuple containing the original string\n"
1339
"and two empty strings.");
1340
1341
#define UNICODE_PARTITION_METHODDEF    \
1342
    {"partition", (PyCFunction)unicode_partition, METH_O, unicode_partition__doc__},
1343
1344
PyDoc_STRVAR(unicode_rpartition__doc__,
1345
"rpartition($self, sep, /)\n"
1346
"--\n"
1347
"\n"
1348
"Partition the string into three parts using the given separator.\n"
1349
"\n"
1350
"This will search for the separator in the string, starting at the end. If\n"
1351
"the separator is found, returns a 3-tuple containing the part before the\n"
1352
"separator, the separator itself, and the part after it.\n"
1353
"\n"
1354
"If the separator is not found, returns a 3-tuple containing two empty strings\n"
1355
"and the original string.");
1356
1357
#define UNICODE_RPARTITION_METHODDEF    \
1358
    {"rpartition", (PyCFunction)unicode_rpartition, METH_O, unicode_rpartition__doc__},
1359
1360
PyDoc_STRVAR(unicode_rsplit__doc__,
1361
"rsplit($self, /, sep=None, maxsplit=-1)\n"
1362
"--\n"
1363
"\n"
1364
"Return a list of the substrings in the string, using sep as the separator string.\n"
1365
"\n"
1366
"  sep\n"
1367
"    The separator used to split the string.\n"
1368
"\n"
1369
"    When set to None (the default value), will split on any whitespace\n"
1370
"    character (including \\n \\r \\t \\f and spaces) and will discard\n"
1371
"    empty strings from the result.\n"
1372
"  maxsplit\n"
1373
"    Maximum number of splits.\n"
1374
"    -1 (the default value) means no limit.\n"
1375
"\n"
1376
"Splitting starts at the end of the string and works to the front.");
1377
1378
#define UNICODE_RSPLIT_METHODDEF    \
1379
    {"rsplit", _PyCFunction_CAST(unicode_rsplit), METH_FASTCALL|METH_KEYWORDS, unicode_rsplit__doc__},
1380
1381
static PyObject *
1382
unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
1383
1384
static PyObject *
1385
unicode_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1386
66
{
1387
66
    PyObject *return_value = NULL;
1388
66
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1389
1390
66
    #define NUM_KEYWORDS 2
1391
66
    static struct {
1392
66
        PyGC_Head _this_is_not_used;
1393
66
        PyObject_VAR_HEAD
1394
66
        Py_hash_t ob_hash;
1395
66
        PyObject *ob_item[NUM_KEYWORDS];
1396
66
    } _kwtuple = {
1397
66
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1398
66
        .ob_hash = -1,
1399
66
        .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
1400
66
    };
1401
66
    #undef NUM_KEYWORDS
1402
66
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1403
1404
    #else  // !Py_BUILD_CORE
1405
    #  define KWTUPLE NULL
1406
    #endif  // !Py_BUILD_CORE
1407
1408
66
    static const char * const _keywords[] = {"sep", "maxsplit", NULL};
1409
66
    static _PyArg_Parser _parser = {
1410
66
        .keywords = _keywords,
1411
66
        .fname = "rsplit",
1412
66
        .kwtuple = KWTUPLE,
1413
66
    };
1414
66
    #undef KWTUPLE
1415
66
    PyObject *argsbuf[2];
1416
66
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1417
66
    PyObject *sep = Py_None;
1418
66
    Py_ssize_t maxsplit = -1;
1419
1420
66
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1421
66
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1422
66
    if (!args) {
1423
0
        goto exit;
1424
0
    }
1425
66
    if (!noptargs) {
1426
0
        goto skip_optional_pos;
1427
0
    }
1428
66
    if (args[0]) {
1429
66
        sep = args[0];
1430
66
        if (!--noptargs) {
1431
0
            goto skip_optional_pos;
1432
0
        }
1433
66
    }
1434
66
    {
1435
66
        Py_ssize_t ival = -1;
1436
66
        PyObject *iobj = _PyNumber_Index(args[1]);
1437
66
        if (iobj != NULL) {
1438
66
            ival = PyLong_AsSsize_t(iobj);
1439
66
            Py_DECREF(iobj);
1440
66
        }
1441
66
        if (ival == -1 && PyErr_Occurred()) {
1442
0
            goto exit;
1443
0
        }
1444
66
        maxsplit = ival;
1445
66
    }
1446
66
skip_optional_pos:
1447
66
    return_value = unicode_rsplit_impl(self, sep, maxsplit);
1448
1449
66
exit:
1450
66
    return return_value;
1451
66
}
1452
1453
PyDoc_STRVAR(unicode_splitlines__doc__,
1454
"splitlines($self, /, keepends=False)\n"
1455
"--\n"
1456
"\n"
1457
"Return a list of the lines in the string, breaking at line boundaries.\n"
1458
"\n"
1459
"Line breaks are not included in the resulting list unless keepends is given and\n"
1460
"true.");
1461
1462
#define UNICODE_SPLITLINES_METHODDEF    \
1463
    {"splitlines", _PyCFunction_CAST(unicode_splitlines), METH_FASTCALL|METH_KEYWORDS, unicode_splitlines__doc__},
1464
1465
static PyObject *
1466
unicode_splitlines_impl(PyObject *self, int keepends);
1467
1468
static PyObject *
1469
unicode_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1470
18.1k
{
1471
18.1k
    PyObject *return_value = NULL;
1472
18.1k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1473
1474
18.1k
    #define NUM_KEYWORDS 1
1475
18.1k
    static struct {
1476
18.1k
        PyGC_Head _this_is_not_used;
1477
18.1k
        PyObject_VAR_HEAD
1478
18.1k
        Py_hash_t ob_hash;
1479
18.1k
        PyObject *ob_item[NUM_KEYWORDS];
1480
18.1k
    } _kwtuple = {
1481
18.1k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1482
18.1k
        .ob_hash = -1,
1483
18.1k
        .ob_item = { &_Py_ID(keepends), },
1484
18.1k
    };
1485
18.1k
    #undef NUM_KEYWORDS
1486
18.1k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1487
1488
    #else  // !Py_BUILD_CORE
1489
    #  define KWTUPLE NULL
1490
    #endif  // !Py_BUILD_CORE
1491
1492
18.1k
    static const char * const _keywords[] = {"keepends", NULL};
1493
18.1k
    static _PyArg_Parser _parser = {
1494
18.1k
        .keywords = _keywords,
1495
18.1k
        .fname = "splitlines",
1496
18.1k
        .kwtuple = KWTUPLE,
1497
18.1k
    };
1498
18.1k
    #undef KWTUPLE
1499
18.1k
    PyObject *argsbuf[1];
1500
18.1k
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1501
18.1k
    int keepends = 0;
1502
1503
18.1k
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1504
18.1k
            /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1505
18.1k
    if (!args) {
1506
0
        goto exit;
1507
0
    }
1508
18.1k
    if (!noptargs) {
1509
18.1k
        goto skip_optional_pos;
1510
18.1k
    }
1511
0
    keepends = PyObject_IsTrue(args[0]);
1512
0
    if (keepends < 0) {
1513
0
        goto exit;
1514
0
    }
1515
18.1k
skip_optional_pos:
1516
18.1k
    return_value = unicode_splitlines_impl(self, keepends);
1517
1518
18.1k
exit:
1519
18.1k
    return return_value;
1520
18.1k
}
1521
1522
PyDoc_STRVAR(unicode_swapcase__doc__,
1523
"swapcase($self, /)\n"
1524
"--\n"
1525
"\n"
1526
"Convert uppercase characters to lowercase and lowercase characters to uppercase.");
1527
1528
#define UNICODE_SWAPCASE_METHODDEF    \
1529
    {"swapcase", (PyCFunction)unicode_swapcase, METH_NOARGS, unicode_swapcase__doc__},
1530
1531
static PyObject *
1532
unicode_swapcase_impl(PyObject *self);
1533
1534
static PyObject *
1535
unicode_swapcase(PyObject *self, PyObject *Py_UNUSED(ignored))
1536
0
{
1537
0
    return unicode_swapcase_impl(self);
1538
0
}
1539
1540
PyDoc_STRVAR(unicode_maketrans__doc__,
1541
"maketrans(x, y=<unrepresentable>, z=<unrepresentable>, /)\n"
1542
"--\n"
1543
"\n"
1544
"Return a translation table usable for str.translate().\n"
1545
"\n"
1546
"If there is only one argument, it must be a dictionary mapping Unicode\n"
1547
"ordinals (integers) or characters to Unicode ordinals, strings or None.\n"
1548
"Character keys will be then converted to ordinals.\n"
1549
"If there are two arguments, they must be strings of equal length, and\n"
1550
"in the resulting dictionary, each character in x will be mapped to the\n"
1551
"character at the same position in y. If there is a third argument, it\n"
1552
"must be a string, whose characters will be mapped to None in the result.");
1553
1554
#define UNICODE_MAKETRANS_METHODDEF    \
1555
    {"maketrans", _PyCFunction_CAST(unicode_maketrans), METH_FASTCALL|METH_STATIC, unicode_maketrans__doc__},
1556
1557
static PyObject *
1558
unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z);
1559
1560
static PyObject *
1561
unicode_maketrans(PyObject *null, PyObject *const *args, Py_ssize_t nargs)
1562
4
{
1563
4
    PyObject *return_value = NULL;
1564
4
    PyObject *x;
1565
4
    PyObject *y = NULL;
1566
4
    PyObject *z = NULL;
1567
1568
4
    if (!_PyArg_CheckPositional("maketrans", nargs, 1, 3)) {
1569
0
        goto exit;
1570
0
    }
1571
4
    x = args[0];
1572
4
    if (nargs < 2) {
1573
0
        goto skip_optional;
1574
0
    }
1575
4
    if (!PyUnicode_Check(args[1])) {
1576
0
        _PyArg_BadArgument("maketrans", "argument 2", "str", args[1]);
1577
0
        goto exit;
1578
0
    }
1579
4
    y = args[1];
1580
4
    if (nargs < 3) {
1581
4
        goto skip_optional;
1582
4
    }
1583
0
    if (!PyUnicode_Check(args[2])) {
1584
0
        _PyArg_BadArgument("maketrans", "argument 3", "str", args[2]);
1585
0
        goto exit;
1586
0
    }
1587
0
    z = args[2];
1588
4
skip_optional:
1589
4
    return_value = unicode_maketrans_impl(x, y, z);
1590
1591
4
exit:
1592
4
    return return_value;
1593
4
}
1594
1595
PyDoc_STRVAR(unicode_translate__doc__,
1596
"translate($self, table, /)\n"
1597
"--\n"
1598
"\n"
1599
"Replace each character in the string using the given translation table.\n"
1600
"\n"
1601
"  table\n"
1602
"    Translation table, which must be a mapping of Unicode ordinals to\n"
1603
"    Unicode ordinals, strings, or None.\n"
1604
"\n"
1605
"The table must implement lookup/indexing via __getitem__, for instance a\n"
1606
"dictionary or list.  If this operation raises LookupError, the character is\n"
1607
"left untouched.  Characters mapped to None are deleted.");
1608
1609
#define UNICODE_TRANSLATE_METHODDEF    \
1610
    {"translate", (PyCFunction)unicode_translate, METH_O, unicode_translate__doc__},
1611
1612
PyDoc_STRVAR(unicode_upper__doc__,
1613
"upper($self, /)\n"
1614
"--\n"
1615
"\n"
1616
"Return a copy of the string converted to uppercase.");
1617
1618
#define UNICODE_UPPER_METHODDEF    \
1619
    {"upper", (PyCFunction)unicode_upper, METH_NOARGS, unicode_upper__doc__},
1620
1621
static PyObject *
1622
unicode_upper_impl(PyObject *self);
1623
1624
static PyObject *
1625
unicode_upper(PyObject *self, PyObject *Py_UNUSED(ignored))
1626
306
{
1627
306
    return unicode_upper_impl(self);
1628
306
}
1629
1630
PyDoc_STRVAR(unicode_zfill__doc__,
1631
"zfill($self, width, /)\n"
1632
"--\n"
1633
"\n"
1634
"Pad a numeric string with zeros on the left, to fill a field of the given width.\n"
1635
"\n"
1636
"The string is never truncated.");
1637
1638
#define UNICODE_ZFILL_METHODDEF    \
1639
    {"zfill", (PyCFunction)unicode_zfill, METH_O, unicode_zfill__doc__},
1640
1641
static PyObject *
1642
unicode_zfill_impl(PyObject *self, Py_ssize_t width);
1643
1644
static PyObject *
1645
unicode_zfill(PyObject *self, PyObject *arg)
1646
0
{
1647
0
    PyObject *return_value = NULL;
1648
0
    Py_ssize_t width;
1649
1650
0
    {
1651
0
        Py_ssize_t ival = -1;
1652
0
        PyObject *iobj = _PyNumber_Index(arg);
1653
0
        if (iobj != NULL) {
1654
0
            ival = PyLong_AsSsize_t(iobj);
1655
0
            Py_DECREF(iobj);
1656
0
        }
1657
0
        if (ival == -1 && PyErr_Occurred()) {
1658
0
            goto exit;
1659
0
        }
1660
0
        width = ival;
1661
0
    }
1662
0
    return_value = unicode_zfill_impl(self, width);
1663
1664
0
exit:
1665
0
    return return_value;
1666
0
}
1667
1668
PyDoc_STRVAR(unicode_startswith__doc__,
1669
"startswith($self, prefix[, start[, end]], /)\n"
1670
"--\n"
1671
"\n"
1672
"Return True if the string starts with the specified prefix, False otherwise.\n"
1673
"\n"
1674
"  prefix\n"
1675
"    A string or a tuple of strings to try.\n"
1676
"  start\n"
1677
"    Optional start position. Default: start of the string.\n"
1678
"  end\n"
1679
"    Optional stop position. Default: end of the string.");
1680
1681
#define UNICODE_STARTSWITH_METHODDEF    \
1682
    {"startswith", _PyCFunction_CAST(unicode_startswith), METH_FASTCALL, unicode_startswith__doc__},
1683
1684
static PyObject *
1685
unicode_startswith_impl(PyObject *self, PyObject *subobj, Py_ssize_t start,
1686
                        Py_ssize_t end);
1687
1688
static PyObject *
1689
unicode_startswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1690
43.0M
{
1691
43.0M
    PyObject *return_value = NULL;
1692
43.0M
    PyObject *subobj;
1693
43.0M
    Py_ssize_t start = 0;
1694
43.0M
    Py_ssize_t end = PY_SSIZE_T_MAX;
1695
1696
43.0M
    if (!_PyArg_CheckPositional("startswith", nargs, 1, 3)) {
1697
0
        goto exit;
1698
0
    }
1699
43.0M
    subobj = args[0];
1700
43.0M
    if (nargs < 2) {
1701
20.8M
        goto skip_optional;
1702
20.8M
    }
1703
22.1M
    if (!_PyEval_SliceIndex(args[1], &start)) {
1704
0
        goto exit;
1705
0
    }
1706
22.1M
    if (nargs < 3) {
1707
22.1M
        goto skip_optional;
1708
22.1M
    }
1709
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
1710
0
        goto exit;
1711
0
    }
1712
43.0M
skip_optional:
1713
43.0M
    return_value = unicode_startswith_impl(self, subobj, start, end);
1714
1715
43.0M
exit:
1716
43.0M
    return return_value;
1717
43.0M
}
1718
1719
PyDoc_STRVAR(unicode_endswith__doc__,
1720
"endswith($self, suffix[, start[, end]], /)\n"
1721
"--\n"
1722
"\n"
1723
"Return True if the string ends with the specified suffix, False otherwise.\n"
1724
"\n"
1725
"  suffix\n"
1726
"    A string or a tuple of strings to try.\n"
1727
"  start\n"
1728
"    Optional start position. Default: start of the string.\n"
1729
"  end\n"
1730
"    Optional stop position. Default: end of the string.");
1731
1732
#define UNICODE_ENDSWITH_METHODDEF    \
1733
    {"endswith", _PyCFunction_CAST(unicode_endswith), METH_FASTCALL, unicode_endswith__doc__},
1734
1735
static PyObject *
1736
unicode_endswith_impl(PyObject *self, PyObject *subobj, Py_ssize_t start,
1737
                      Py_ssize_t end);
1738
1739
static PyObject *
1740
unicode_endswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1741
10.0M
{
1742
10.0M
    PyObject *return_value = NULL;
1743
10.0M
    PyObject *subobj;
1744
10.0M
    Py_ssize_t start = 0;
1745
10.0M
    Py_ssize_t end = PY_SSIZE_T_MAX;
1746
1747
10.0M
    if (!_PyArg_CheckPositional("endswith", nargs, 1, 3)) {
1748
0
        goto exit;
1749
0
    }
1750
10.0M
    subobj = args[0];
1751
10.0M
    if (nargs < 2) {
1752
10.0M
        goto skip_optional;
1753
10.0M
    }
1754
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
1755
0
        goto exit;
1756
0
    }
1757
0
    if (nargs < 3) {
1758
0
        goto skip_optional;
1759
0
    }
1760
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
1761
0
        goto exit;
1762
0
    }
1763
10.0M
skip_optional:
1764
10.0M
    return_value = unicode_endswith_impl(self, subobj, start, end);
1765
1766
10.0M
exit:
1767
10.0M
    return return_value;
1768
10.0M
}
1769
1770
PyDoc_STRVAR(unicode___format____doc__,
1771
"__format__($self, format_spec, /)\n"
1772
"--\n"
1773
"\n"
1774
"Return a formatted version of the string as described by format_spec.");
1775
1776
#define UNICODE___FORMAT___METHODDEF    \
1777
    {"__format__", (PyCFunction)unicode___format__, METH_O, unicode___format____doc__},
1778
1779
static PyObject *
1780
unicode___format___impl(PyObject *self, PyObject *format_spec);
1781
1782
static PyObject *
1783
unicode___format__(PyObject *self, PyObject *arg)
1784
0
{
1785
0
    PyObject *return_value = NULL;
1786
0
    PyObject *format_spec;
1787
1788
0
    if (!PyUnicode_Check(arg)) {
1789
0
        _PyArg_BadArgument("__format__", "argument", "str", arg);
1790
0
        goto exit;
1791
0
    }
1792
0
    format_spec = arg;
1793
0
    return_value = unicode___format___impl(self, format_spec);
1794
1795
0
exit:
1796
0
    return return_value;
1797
0
}
1798
1799
PyDoc_STRVAR(unicode_sizeof__doc__,
1800
"__sizeof__($self, /)\n"
1801
"--\n"
1802
"\n"
1803
"Return the size of the string in memory, in bytes.");
1804
1805
#define UNICODE_SIZEOF_METHODDEF    \
1806
    {"__sizeof__", (PyCFunction)unicode_sizeof, METH_NOARGS, unicode_sizeof__doc__},
1807
1808
static PyObject *
1809
unicode_sizeof_impl(PyObject *self);
1810
1811
static PyObject *
1812
unicode_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored))
1813
0
{
1814
0
    return unicode_sizeof_impl(self);
1815
0
}
1816
1817
static PyObject *
1818
unicode_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
1819
                 const char *errors);
1820
1821
static PyObject *
1822
unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1823
15.5M
{
1824
15.5M
    PyObject *return_value = NULL;
1825
15.5M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1826
1827
15.5M
    #define NUM_KEYWORDS 3
1828
15.5M
    static struct {
1829
15.5M
        PyGC_Head _this_is_not_used;
1830
15.5M
        PyObject_VAR_HEAD
1831
15.5M
        Py_hash_t ob_hash;
1832
15.5M
        PyObject *ob_item[NUM_KEYWORDS];
1833
15.5M
    } _kwtuple = {
1834
15.5M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1835
15.5M
        .ob_hash = -1,
1836
15.5M
        .ob_item = { &_Py_ID(object), &_Py_ID(encoding), &_Py_ID(errors), },
1837
15.5M
    };
1838
15.5M
    #undef NUM_KEYWORDS
1839
15.5M
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1840
1841
    #else  // !Py_BUILD_CORE
1842
    #  define KWTUPLE NULL
1843
    #endif  // !Py_BUILD_CORE
1844
1845
15.5M
    static const char * const _keywords[] = {"object", "encoding", "errors", NULL};
1846
15.5M
    static _PyArg_Parser _parser = {
1847
15.5M
        .keywords = _keywords,
1848
15.5M
        .fname = "str",
1849
15.5M
        .kwtuple = KWTUPLE,
1850
15.5M
    };
1851
15.5M
    #undef KWTUPLE
1852
15.5M
    PyObject *argsbuf[3];
1853
15.5M
    PyObject * const *fastargs;
1854
15.5M
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1855
15.5M
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
1856
15.5M
    PyObject *x = NULL;
1857
15.5M
    const char *encoding = NULL;
1858
15.5M
    const char *errors = NULL;
1859
1860
15.5M
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
1861
15.5M
            /*minpos*/ 0, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1862
15.5M
    if (!fastargs) {
1863
0
        goto exit;
1864
0
    }
1865
15.5M
    if (!noptargs) {
1866
0
        goto skip_optional_pos;
1867
0
    }
1868
15.5M
    if (fastargs[0]) {
1869
15.5M
        x = fastargs[0];
1870
15.5M
        if (!--noptargs) {
1871
15.5M
            goto skip_optional_pos;
1872
15.5M
        }
1873
15.5M
    }
1874
0
    if (fastargs[1]) {
1875
0
        if (!PyUnicode_Check(fastargs[1])) {
1876
0
            _PyArg_BadArgument("str", "argument 'encoding'", "str", fastargs[1]);
1877
0
            goto exit;
1878
0
        }
1879
0
        Py_ssize_t encoding_length;
1880
0
        encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
1881
0
        if (encoding == NULL) {
1882
0
            goto exit;
1883
0
        }
1884
0
        if (strlen(encoding) != (size_t)encoding_length) {
1885
0
            PyErr_SetString(PyExc_ValueError, "embedded null character");
1886
0
            goto exit;
1887
0
        }
1888
0
        if (!--noptargs) {
1889
0
            goto skip_optional_pos;
1890
0
        }
1891
0
    }
1892
0
    if (!PyUnicode_Check(fastargs[2])) {
1893
0
        _PyArg_BadArgument("str", "argument 'errors'", "str", fastargs[2]);
1894
0
        goto exit;
1895
0
    }
1896
0
    Py_ssize_t errors_length;
1897
0
    errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
1898
0
    if (errors == NULL) {
1899
0
        goto exit;
1900
0
    }
1901
0
    if (strlen(errors) != (size_t)errors_length) {
1902
0
        PyErr_SetString(PyExc_ValueError, "embedded null character");
1903
0
        goto exit;
1904
0
    }
1905
15.5M
skip_optional_pos:
1906
15.5M
    return_value = unicode_new_impl(type, x, encoding, errors);
1907
1908
15.5M
exit:
1909
15.5M
    return return_value;
1910
15.5M
}
1911
/*[clinic end generated code: output=13eaf65699ea9fc9 input=a9049054013a1b77]*/