Debian Codegraph @

Objects/clinic/unicodeobject.c.h

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