1/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(unicode_title__doc__,
6"title($self, /)\n"
7"--\n"
8"\n"
9"Return a version of the string where each word is titlecased.\n"
10"\n"
11"More specifically, words start with uppercased characters and all remaining\n"
12"cased characters have lower case.");
13
14#define UNICODE_TITLE_METHODDEF \
15 {"title", (PyCFunction)unicode_title, METH_NOARGS, unicode_title__doc__},
16
17static PyObject *
18unicode_title_impl(PyObject *self);
19
20static PyObject *
21unicode_title(PyObject *self, PyObject *Py_UNUSED(ignored))
22{
23 return unicode_title_impl(self);
24}
25
26PyDoc_STRVAR(unicode_capitalize__doc__,
27"capitalize($self, /)\n"
28"--\n"
29"\n"
30"Return a capitalized version of the string.\n"
31"\n"
32"More specifically, make the first character have upper case and the rest lower\n"
33"case.");
34
35#define UNICODE_CAPITALIZE_METHODDEF \
36 {"capitalize", (PyCFunction)unicode_capitalize, METH_NOARGS, unicode_capitalize__doc__},
37
38static PyObject *
39unicode_capitalize_impl(PyObject *self);
40
41static PyObject *
42unicode_capitalize(PyObject *self, PyObject *Py_UNUSED(ignored))
43{
44 return unicode_capitalize_impl(self);
45}
46
47PyDoc_STRVAR(unicode_casefold__doc__,
48"casefold($self, /)\n"
49"--\n"
50"\n"
51"Return a version of the string suitable for caseless comparisons.");
52
53#define UNICODE_CASEFOLD_METHODDEF \
54 {"casefold", (PyCFunction)unicode_casefold, METH_NOARGS, unicode_casefold__doc__},
55
56static PyObject *
57unicode_casefold_impl(PyObject *self);
58
59static PyObject *
60unicode_casefold(PyObject *self, PyObject *Py_UNUSED(ignored))
61{
62 return unicode_casefold_impl(self);
63}
64
65PyDoc_STRVAR(unicode_center__doc__,
66"center($self, width, fillchar=\' \', /)\n"
67"--\n"
68"\n"
69"Return a centered string of length width.\n"
70"\n"
71"Padding is done using the specified fill character (default is a space).");
72
73#define UNICODE_CENTER_METHODDEF \
74 {"center", (PyCFunction)(void(*)(void))unicode_center, METH_FASTCALL, unicode_center__doc__},
75
76static PyObject *
77unicode_center_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
78
79static PyObject *
80unicode_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
81{
82 PyObject *return_value = NULL;
83 Py_ssize_t width;
84 Py_UCS4 fillchar = ' ';
85
86 if (!_PyArg_CheckPositional("center", nargs, 1, 2)) {
87 goto exit;
88 }
89 {
90 Py_ssize_t ival = -1;
91 PyObject *iobj = _PyNumber_Index(args[0]);
92 if (iobj != NULL) {
93 ival = PyLong_AsSsize_t(iobj);
94 Py_DECREF(iobj);
95 }
96 if (ival == -1 && PyErr_Occurred()) {
97 goto exit;
98 }
99 width = ival;
100 }
101 if (nargs < 2) {
102 goto skip_optional;
103 }
104 if (!convert_uc(args[1], &fillchar)) {
105 goto exit;
106 }
107skip_optional:
108 return_value = unicode_center_impl(self, width, fillchar);
109
110exit:
111 return return_value;
112}
113
114PyDoc_STRVAR(unicode_encode__doc__,
115"encode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
116"--\n"
117"\n"
118"Encode the string using the codec registered for encoding.\n"
119"\n"
120" encoding\n"
121" The encoding in which to encode the string.\n"
122" errors\n"
123" The error handling scheme to use for encoding errors.\n"
124" The default is \'strict\' meaning that encoding errors raise a\n"
125" UnicodeEncodeError. Other possible values are \'ignore\', \'replace\' and\n"
126" \'xmlcharrefreplace\' as well as any other name registered with\n"
127" codecs.register_error that can handle UnicodeEncodeErrors.");
128
129#define UNICODE_ENCODE_METHODDEF \
130 {"encode", (PyCFunction)(void(*)(void))unicode_encode, METH_FASTCALL|METH_KEYWORDS, unicode_encode__doc__},
131
132static PyObject *
133unicode_encode_impl(PyObject *self, const char *encoding, const char *errors);
134
135static PyObject *
136unicode_encode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
137{
138 PyObject *return_value = NULL;
139 static const char * const _keywords[] = {"encoding", "errors", NULL};
140 static _PyArg_Parser _parser = {NULL, _keywords, "encode", 0};
141 PyObject *argsbuf[2];
142 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
143 const char *encoding = NULL;
144 const char *errors = NULL;
145
146 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
147 if (!args) {
148 goto exit;
149 }
150 if (!noptargs) {
151 goto skip_optional_pos;
152 }
153 if (args[0]) {
154 if (!PyUnicode_Check(args[0])) {
155 _PyArg_BadArgument("encode", "argument 'encoding'", "str", args[0]);
156 goto exit;
157 }
158 Py_ssize_t encoding_length;
159 encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
160 if (encoding == NULL) {
161 goto exit;
162 }
163 if (strlen(encoding) != (size_t)encoding_length) {
164 PyErr_SetString(PyExc_ValueError, "embedded null character");
165 goto exit;
166 }
167 if (!--noptargs) {
168 goto skip_optional_pos;
169 }
170 }
171 if (!PyUnicode_Check(args[1])) {
172 _PyArg_BadArgument("encode", "argument 'errors'", "str", args[1]);
173 goto exit;
174 }
175 Py_ssize_t errors_length;
176 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
177 if (errors == NULL) {
178 goto exit;
179 }
180 if (strlen(errors) != (size_t)errors_length) {
181 PyErr_SetString(PyExc_ValueError, "embedded null character");
182 goto exit;
183 }
184skip_optional_pos:
185 return_value = unicode_encode_impl(self, encoding, errors);
186
187exit:
188 return return_value;
189}
190
191PyDoc_STRVAR(unicode_expandtabs__doc__,
192"expandtabs($self, /, tabsize=8)\n"
193"--\n"
194"\n"
195"Return a copy where all tab characters are expanded using spaces.\n"
196"\n"
197"If tabsize is not given, a tab size of 8 characters is assumed.");
198
199#define UNICODE_EXPANDTABS_METHODDEF \
200 {"expandtabs", (PyCFunction)(void(*)(void))unicode_expandtabs, METH_FASTCALL|METH_KEYWORDS, unicode_expandtabs__doc__},
201
202static PyObject *
203unicode_expandtabs_impl(PyObject *self, int tabsize);
204
205static PyObject *
206unicode_expandtabs(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
207{
208 PyObject *return_value = NULL;
209 static const char * const _keywords[] = {"tabsize", NULL};
210 static _PyArg_Parser _parser = {NULL, _keywords, "expandtabs", 0};
211 PyObject *argsbuf[1];
212 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
213 int tabsize = 8;
214
215 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
216 if (!args) {
217 goto exit;
218 }
219 if (!noptargs) {
220 goto skip_optional_pos;
221 }
222 tabsize = _PyLong_AsInt(args[0]);
223 if (tabsize == -1 && PyErr_Occurred()) {
224 goto exit;
225 }
226skip_optional_pos:
227 return_value = unicode_expandtabs_impl(self, tabsize);
228
229exit:
230 return return_value;
231}
232
233PyDoc_STRVAR(unicode_isascii__doc__,
234"isascii($self, /)\n"
235"--\n"
236"\n"
237"Return True if all characters in the string are ASCII, False otherwise.\n"
238"\n"
239"ASCII characters have code points in the range U+0000-U+007F.\n"
240"Empty string is ASCII too.");
241
242#define UNICODE_ISASCII_METHODDEF \
243 {"isascii", (PyCFunction)unicode_isascii, METH_NOARGS, unicode_isascii__doc__},
244
245static PyObject *
246unicode_isascii_impl(PyObject *self);
247
248static PyObject *
249unicode_isascii(PyObject *self, PyObject *Py_UNUSED(ignored))
250{
251 return unicode_isascii_impl(self);
252}
253
254PyDoc_STRVAR(unicode_islower__doc__,
255"islower($self, /)\n"
256"--\n"
257"\n"
258"Return True if the string is a lowercase string, False otherwise.\n"
259"\n"
260"A string is lowercase if all cased characters in the string are lowercase and\n"
261"there is at least one cased character in the string.");
262
263#define UNICODE_ISLOWER_METHODDEF \
264 {"islower", (PyCFunction)unicode_islower, METH_NOARGS, unicode_islower__doc__},
265
266static PyObject *
267unicode_islower_impl(PyObject *self);
268
269static PyObject *
270unicode_islower(PyObject *self, PyObject *Py_UNUSED(ignored))
271{
272 return unicode_islower_impl(self);
273}
274
275PyDoc_STRVAR(unicode_isupper__doc__,
276"isupper($self, /)\n"
277"--\n"
278"\n"
279"Return True if the string is an uppercase string, False otherwise.\n"
280"\n"
281"A string is uppercase if all cased characters in the string are uppercase and\n"
282"there is at least one cased character in the string.");
283
284#define UNICODE_ISUPPER_METHODDEF \
285 {"isupper", (PyCFunction)unicode_isupper, METH_NOARGS, unicode_isupper__doc__},
286
287static PyObject *
288unicode_isupper_impl(PyObject *self);
289
290static PyObject *
291unicode_isupper(PyObject *self, PyObject *Py_UNUSED(ignored))
292{
293 return unicode_isupper_impl(self);
294}
295
296PyDoc_STRVAR(unicode_istitle__doc__,
297"istitle($self, /)\n"
298"--\n"
299"\n"
300"Return True if the string is a title-cased string, False otherwise.\n"
301"\n"
302"In a title-cased string, upper- and title-case characters may only\n"
303"follow uncased characters and lowercase characters only cased ones.");
304
305#define UNICODE_ISTITLE_METHODDEF \
306 {"istitle", (PyCFunction)unicode_istitle, METH_NOARGS, unicode_istitle__doc__},
307
308static PyObject *
309unicode_istitle_impl(PyObject *self);
310
311static PyObject *
312unicode_istitle(PyObject *self, PyObject *Py_UNUSED(ignored))
313{
314 return unicode_istitle_impl(self);
315}
316
317PyDoc_STRVAR(unicode_isspace__doc__,
318"isspace($self, /)\n"
319"--\n"
320"\n"
321"Return True if the string is a whitespace string, False otherwise.\n"
322"\n"
323"A string is whitespace if all characters in the string are whitespace and there\n"
324"is at least one character in the string.");
325
326#define UNICODE_ISSPACE_METHODDEF \
327 {"isspace", (PyCFunction)unicode_isspace, METH_NOARGS, unicode_isspace__doc__},
328
329static PyObject *
330unicode_isspace_impl(PyObject *self);
331
332static PyObject *
333unicode_isspace(PyObject *self, PyObject *Py_UNUSED(ignored))
334{
335 return unicode_isspace_impl(self);
336}
337
338PyDoc_STRVAR(unicode_isalpha__doc__,
339"isalpha($self, /)\n"
340"--\n"
341"\n"
342"Return True if the string is an alphabetic string, False otherwise.\n"
343"\n"
344"A string is alphabetic if all characters in the string are alphabetic and there\n"
345"is at least one character in the string.");
346
347#define UNICODE_ISALPHA_METHODDEF \
348 {"isalpha", (PyCFunction)unicode_isalpha, METH_NOARGS, unicode_isalpha__doc__},
349
350static PyObject *
351unicode_isalpha_impl(PyObject *self);
352
353static PyObject *
354unicode_isalpha(PyObject *self, PyObject *Py_UNUSED(ignored))
355{
356 return unicode_isalpha_impl(self);
357}
358
359PyDoc_STRVAR(unicode_isalnum__doc__,
360"isalnum($self, /)\n"
361"--\n"
362"\n"
363"Return True if the string is an alpha-numeric string, False otherwise.\n"
364"\n"
365"A string is alpha-numeric if all characters in the string are alpha-numeric and\n"
366"there is at least one character in the string.");
367
368#define UNICODE_ISALNUM_METHODDEF \
369 {"isalnum", (PyCFunction)unicode_isalnum, METH_NOARGS, unicode_isalnum__doc__},
370
371static PyObject *
372unicode_isalnum_impl(PyObject *self);
373
374static PyObject *
375unicode_isalnum(PyObject *self, PyObject *Py_UNUSED(ignored))
376{
377 return unicode_isalnum_impl(self);
378}
379
380PyDoc_STRVAR(unicode_isdecimal__doc__,
381"isdecimal($self, /)\n"
382"--\n"
383"\n"
384"Return True if the string is a decimal string, False otherwise.\n"
385"\n"
386"A string is a decimal string if all characters in the string are decimal and\n"
387"there is at least one character in the string.");
388
389#define UNICODE_ISDECIMAL_METHODDEF \
390 {"isdecimal", (PyCFunction)unicode_isdecimal, METH_NOARGS, unicode_isdecimal__doc__},
391
392static PyObject *
393unicode_isdecimal_impl(PyObject *self);
394
395static PyObject *
396unicode_isdecimal(PyObject *self, PyObject *Py_UNUSED(ignored))
397{
398 return unicode_isdecimal_impl(self);
399}
400
401PyDoc_STRVAR(unicode_isdigit__doc__,
402"isdigit($self, /)\n"
403"--\n"
404"\n"
405"Return True if the string is a digit string, False otherwise.\n"
406"\n"
407"A string is a digit string if all characters in the string are digits and there\n"
408"is at least one character in the string.");
409
410#define UNICODE_ISDIGIT_METHODDEF \
411 {"isdigit", (PyCFunction)unicode_isdigit, METH_NOARGS, unicode_isdigit__doc__},
412
413static PyObject *
414unicode_isdigit_impl(PyObject *self);
415
416static PyObject *
417unicode_isdigit(PyObject *self, PyObject *Py_UNUSED(ignored))
418{
419 return unicode_isdigit_impl(self);
420}
421
422PyDoc_STRVAR(unicode_isnumeric__doc__,
423"isnumeric($self, /)\n"
424"--\n"
425"\n"
426"Return True if the string is a numeric string, False otherwise.\n"
427"\n"
428"A string is numeric if all characters in the string are numeric and there is at\n"
429"least one character in the string.");
430
431#define UNICODE_ISNUMERIC_METHODDEF \
432 {"isnumeric", (PyCFunction)unicode_isnumeric, METH_NOARGS, unicode_isnumeric__doc__},
433
434static PyObject *
435unicode_isnumeric_impl(PyObject *self);
436
437static PyObject *
438unicode_isnumeric(PyObject *self, PyObject *Py_UNUSED(ignored))
439{
440 return unicode_isnumeric_impl(self);
441}
442
443PyDoc_STRVAR(unicode_isidentifier__doc__,
444"isidentifier($self, /)\n"
445"--\n"
446"\n"
447"Return True if the string is a valid Python identifier, False otherwise.\n"
448"\n"
449"Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n"
450"such as \"def\" or \"class\".");
451
452#define UNICODE_ISIDENTIFIER_METHODDEF \
453 {"isidentifier", (PyCFunction)unicode_isidentifier, METH_NOARGS, unicode_isidentifier__doc__},
454
455static PyObject *
456unicode_isidentifier_impl(PyObject *self);
457
458static PyObject *
459unicode_isidentifier(PyObject *self, PyObject *Py_UNUSED(ignored))
460{
461 return unicode_isidentifier_impl(self);
462}
463
464PyDoc_STRVAR(unicode_isprintable__doc__,
465"isprintable($self, /)\n"
466"--\n"
467"\n"
468"Return True if the string is printable, False otherwise.\n"
469"\n"
470"A string is printable if all of its characters are considered printable in\n"
471"repr() or if it is empty.");
472
473#define UNICODE_ISPRINTABLE_METHODDEF \
474 {"isprintable", (PyCFunction)unicode_isprintable, METH_NOARGS, unicode_isprintable__doc__},
475
476static PyObject *
477unicode_isprintable_impl(PyObject *self);
478
479static PyObject *
480unicode_isprintable(PyObject *self, PyObject *Py_UNUSED(ignored))
481{
482 return unicode_isprintable_impl(self);
483}
484
485PyDoc_STRVAR(unicode_join__doc__,
486"join($self, iterable, /)\n"
487"--\n"
488"\n"
489"Concatenate any number of strings.\n"
490"\n"
491"The string whose method is called is inserted in between each given string.\n"
492"The result is returned as a new string.\n"
493"\n"
494"Example: \'.\'.join([\'ab\', \'pq\', \'rs\']) -> \'ab.pq.rs\'");
495
496#define UNICODE_JOIN_METHODDEF \
497 {"join", (PyCFunction)unicode_join, METH_O, unicode_join__doc__},
498
499PyDoc_STRVAR(unicode_ljust__doc__,
500"ljust($self, width, fillchar=\' \', /)\n"
501"--\n"
502"\n"
503"Return a left-justified string of length width.\n"
504"\n"
505"Padding is done using the specified fill character (default is a space).");
506
507#define UNICODE_LJUST_METHODDEF \
508 {"ljust", (PyCFunction)(void(*)(void))unicode_ljust, METH_FASTCALL, unicode_ljust__doc__},
509
510static PyObject *
511unicode_ljust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
512
513static PyObject *
514unicode_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
515{
516 PyObject *return_value = NULL;
517 Py_ssize_t width;
518 Py_UCS4 fillchar = ' ';
519
520 if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) {
521 goto exit;
522 }
523 {
524 Py_ssize_t ival = -1;
525 PyObject *iobj = _PyNumber_Index(args[0]);
526 if (iobj != NULL) {
527 ival = PyLong_AsSsize_t(iobj);
528 Py_DECREF(iobj);
529 }
530 if (ival == -1 && PyErr_Occurred()) {
531 goto exit;
532 }
533 width = ival;
534 }
535 if (nargs < 2) {
536 goto skip_optional;
537 }
538 if (!convert_uc(args[1], &fillchar)) {
539 goto exit;
540 }
541skip_optional:
542 return_value = unicode_ljust_impl(self, width, fillchar);
543
544exit:
545 return return_value;
546}
547
548PyDoc_STRVAR(unicode_lower__doc__,
549"lower($self, /)\n"
550"--\n"
551"\n"
552"Return a copy of the string converted to lowercase.");
553
554#define UNICODE_LOWER_METHODDEF \
555 {"lower", (PyCFunction)unicode_lower, METH_NOARGS, unicode_lower__doc__},
556
557static PyObject *
558unicode_lower_impl(PyObject *self);
559
560static PyObject *
561unicode_lower(PyObject *self, PyObject *Py_UNUSED(ignored))
562{
563 return unicode_lower_impl(self);
564}
565
566PyDoc_STRVAR(unicode_strip__doc__,
567"strip($self, chars=None, /)\n"
568"--\n"
569"\n"
570"Return a copy of the string with leading and trailing whitespace removed.\n"
571"\n"
572"If chars is given and not None, remove characters in chars instead.");
573
574#define UNICODE_STRIP_METHODDEF \
575 {"strip", (PyCFunction)(void(*)(void))unicode_strip, METH_FASTCALL, unicode_strip__doc__},
576
577static PyObject *
578unicode_strip_impl(PyObject *self, PyObject *chars);
579
580static PyObject *
581unicode_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
582{
583 PyObject *return_value = NULL;
584 PyObject *chars = Py_None;
585
586 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
587 goto exit;
588 }
589 if (nargs < 1) {
590 goto skip_optional;
591 }
592 chars = args[0];
593skip_optional:
594 return_value = unicode_strip_impl(self, chars);
595
596exit:
597 return return_value;
598}
599
600PyDoc_STRVAR(unicode_lstrip__doc__,
601"lstrip($self, chars=None, /)\n"
602"--\n"
603"\n"
604"Return a copy of the string with leading whitespace removed.\n"
605"\n"
606"If chars is given and not None, remove characters in chars instead.");
607
608#define UNICODE_LSTRIP_METHODDEF \
609 {"lstrip", (PyCFunction)(void(*)(void))unicode_lstrip, METH_FASTCALL, unicode_lstrip__doc__},
610
611static PyObject *
612unicode_lstrip_impl(PyObject *self, PyObject *chars);
613
614static PyObject *
615unicode_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
616{
617 PyObject *return_value = NULL;
618 PyObject *chars = Py_None;
619
620 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
621 goto exit;
622 }
623 if (nargs < 1) {
624 goto skip_optional;
625 }
626 chars = args[0];
627skip_optional:
628 return_value = unicode_lstrip_impl(self, chars);
629
630exit:
631 return return_value;
632}
633
634PyDoc_STRVAR(unicode_rstrip__doc__,
635"rstrip($self, chars=None, /)\n"
636"--\n"
637"\n"
638"Return a copy of the string with trailing whitespace removed.\n"
639"\n"
640"If chars is given and not None, remove characters in chars instead.");
641
642#define UNICODE_RSTRIP_METHODDEF \
643 {"rstrip", (PyCFunction)(void(*)(void))unicode_rstrip, METH_FASTCALL, unicode_rstrip__doc__},
644
645static PyObject *
646unicode_rstrip_impl(PyObject *self, PyObject *chars);
647
648static PyObject *
649unicode_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
650{
651 PyObject *return_value = NULL;
652 PyObject *chars = Py_None;
653
654 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
655 goto exit;
656 }
657 if (nargs < 1) {
658 goto skip_optional;
659 }
660 chars = args[0];
661skip_optional:
662 return_value = unicode_rstrip_impl(self, chars);
663
664exit:
665 return return_value;
666}
667
668PyDoc_STRVAR(unicode_replace__doc__,
669"replace($self, old, new, count=-1, /)\n"
670"--\n"
671"\n"
672"Return a copy with all occurrences of substring old replaced by new.\n"
673"\n"
674" count\n"
675" Maximum number of occurrences to replace.\n"
676" -1 (the default value) means replace all occurrences.\n"
677"\n"
678"If the optional argument count is given, only the first count occurrences are\n"
679"replaced.");
680
681#define UNICODE_REPLACE_METHODDEF \
682 {"replace", (PyCFunction)(void(*)(void))unicode_replace, METH_FASTCALL, unicode_replace__doc__},
683
684static PyObject *
685unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
686 Py_ssize_t count);
687
688static PyObject *
689unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
690{
691 PyObject *return_value = NULL;
692 PyObject *old;
693 PyObject *new;
694 Py_ssize_t count = -1;
695
696 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
697 goto exit;
698 }
699 if (!PyUnicode_Check(args[0])) {
700 _PyArg_BadArgument("replace", "argument 1", "str", args[0]);
701 goto exit;
702 }
703 if (PyUnicode_READY(args[0]) == -1) {
704 goto exit;
705 }
706 old = args[0];
707 if (!PyUnicode_Check(args[1])) {
708 _PyArg_BadArgument("replace", "argument 2", "str", args[1]);
709 goto exit;
710 }
711 if (PyUnicode_READY(args[1]) == -1) {
712 goto exit;
713 }
714 new = args[1];
715 if (nargs < 3) {
716 goto skip_optional;
717 }
718 {
719 Py_ssize_t ival = -1;
720 PyObject *iobj = _PyNumber_Index(args[2]);
721 if (iobj != NULL) {
722 ival = PyLong_AsSsize_t(iobj);
723 Py_DECREF(iobj);
724 }
725 if (ival == -1 && PyErr_Occurred()) {
726 goto exit;
727 }
728 count = ival;
729 }
730skip_optional:
731 return_value = unicode_replace_impl(self, old, new, count);
732
733exit:
734 return return_value;
735}
736
737PyDoc_STRVAR(unicode_removeprefix__doc__,
738"removeprefix($self, prefix, /)\n"
739"--\n"
740"\n"
741"Return a str with the given prefix string removed if present.\n"
742"\n"
743"If the string starts with the prefix string, return string[len(prefix):].\n"
744"Otherwise, return a copy of the original string.");
745
746#define UNICODE_REMOVEPREFIX_METHODDEF \
747 {"removeprefix", (PyCFunction)unicode_removeprefix, METH_O, unicode_removeprefix__doc__},
748
749static PyObject *
750unicode_removeprefix_impl(PyObject *self, PyObject *prefix);
751
752static PyObject *
753unicode_removeprefix(PyObject *self, PyObject *arg)
754{
755 PyObject *return_value = NULL;
756 PyObject *prefix;
757
758 if (!PyUnicode_Check(arg)) {
759 _PyArg_BadArgument("removeprefix", "argument", "str", arg);
760 goto exit;
761 }
762 if (PyUnicode_READY(arg) == -1) {
763 goto exit;
764 }
765 prefix = arg;
766 return_value = unicode_removeprefix_impl(self, prefix);
767
768exit:
769 return return_value;
770}
771
772PyDoc_STRVAR(unicode_removesuffix__doc__,
773"removesuffix($self, suffix, /)\n"
774"--\n"
775"\n"
776"Return a str with the given suffix string removed if present.\n"
777"\n"
778"If the string ends with the suffix string and that suffix is not empty,\n"
779"return string[:-len(suffix)]. Otherwise, return a copy of the original\n"
780"string.");
781
782#define UNICODE_REMOVESUFFIX_METHODDEF \
783 {"removesuffix", (PyCFunction)unicode_removesuffix, METH_O, unicode_removesuffix__doc__},
784
785static PyObject *
786unicode_removesuffix_impl(PyObject *self, PyObject *suffix);
787
788static PyObject *
789unicode_removesuffix(PyObject *self, PyObject *arg)
790{
791 PyObject *return_value = NULL;
792 PyObject *suffix;
793
794 if (!PyUnicode_Check(arg)) {
795 _PyArg_BadArgument("removesuffix", "argument", "str", arg);
796 goto exit;
797 }
798 if (PyUnicode_READY(arg) == -1) {
799 goto exit;
800 }
801 suffix = arg;
802 return_value = unicode_removesuffix_impl(self, suffix);
803
804exit:
805 return return_value;
806}
807
808PyDoc_STRVAR(unicode_rjust__doc__,
809"rjust($self, width, fillchar=\' \', /)\n"
810"--\n"
811"\n"
812"Return a right-justified string of length width.\n"
813"\n"
814"Padding is done using the specified fill character (default is a space).");
815
816#define UNICODE_RJUST_METHODDEF \
817 {"rjust", (PyCFunction)(void(*)(void))unicode_rjust, METH_FASTCALL, unicode_rjust__doc__},
818
819static PyObject *
820unicode_rjust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
821
822static PyObject *
823unicode_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
824{
825 PyObject *return_value = NULL;
826 Py_ssize_t width;
827 Py_UCS4 fillchar = ' ';
828
829 if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) {
830 goto exit;
831 }
832 {
833 Py_ssize_t ival = -1;
834 PyObject *iobj = _PyNumber_Index(args[0]);
835 if (iobj != NULL) {
836 ival = PyLong_AsSsize_t(iobj);
837 Py_DECREF(iobj);
838 }
839 if (ival == -1 && PyErr_Occurred()) {
840 goto exit;
841 }
842 width = ival;
843 }
844 if (nargs < 2) {
845 goto skip_optional;
846 }
847 if (!convert_uc(args[1], &fillchar)) {
848 goto exit;
849 }
850skip_optional:
851 return_value = unicode_rjust_impl(self, width, fillchar);
852
853exit:
854 return return_value;
855}
856
857PyDoc_STRVAR(unicode_split__doc__,
858"split($self, /, sep=None, maxsplit=-1)\n"
859"--\n"
860"\n"
861"Return a list of the substrings in the string, using sep as the separator string.\n"
862"\n"
863" sep\n"
864" The separator used to split the string.\n"
865"\n"
866" When set to None (the default value), will split on any whitespace\n"
867" character (including \\\\n \\\\r \\\\t \\\\f and spaces) and will discard\n"
868" empty strings from the result.\n"
869" maxsplit\n"
870" Maximum number of splits (starting from the left).\n"
871" -1 (the default value) means no limit.\n"
872"\n"
873"Note, str.split() is mainly useful for data that has been intentionally\n"
874"delimited. With natural text that includes punctuation, consider using\n"
875"the regular expression module.");
876
877#define UNICODE_SPLIT_METHODDEF \
878 {"split", (PyCFunction)(void(*)(void))unicode_split, METH_FASTCALL|METH_KEYWORDS, unicode_split__doc__},
879
880static PyObject *
881unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
882
883static PyObject *
884unicode_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
885{
886 PyObject *return_value = NULL;
887 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
888 static _PyArg_Parser _parser = {NULL, _keywords, "split", 0};
889 PyObject *argsbuf[2];
890 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
891 PyObject *sep = Py_None;
892 Py_ssize_t maxsplit = -1;
893
894 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
895 if (!args) {
896 goto exit;
897 }
898 if (!noptargs) {
899 goto skip_optional_pos;
900 }
901 if (args[0]) {
902 sep = args[0];
903 if (!--noptargs) {
904 goto skip_optional_pos;
905 }
906 }
907 {
908 Py_ssize_t ival = -1;
909 PyObject *iobj = _PyNumber_Index(args[1]);
910 if (iobj != NULL) {
911 ival = PyLong_AsSsize_t(iobj);
912 Py_DECREF(iobj);
913 }
914 if (ival == -1 && PyErr_Occurred()) {
915 goto exit;
916 }
917 maxsplit = ival;
918 }
919skip_optional_pos:
920 return_value = unicode_split_impl(self, sep, maxsplit);
921
922exit:
923 return return_value;
924}
925
926PyDoc_STRVAR(unicode_partition__doc__,
927"partition($self, sep, /)\n"
928"--\n"
929"\n"
930"Partition the string into three parts using the given separator.\n"
931"\n"
932"This will search for the separator in the string. If the separator is found,\n"
933"returns a 3-tuple containing the part before the separator, the separator\n"
934"itself, and the part after it.\n"
935"\n"
936"If the separator is not found, returns a 3-tuple containing the original string\n"
937"and two empty strings.");
938
939#define UNICODE_PARTITION_METHODDEF \
940 {"partition", (PyCFunction)unicode_partition, METH_O, unicode_partition__doc__},
941
942PyDoc_STRVAR(unicode_rpartition__doc__,
943"rpartition($self, sep, /)\n"
944"--\n"
945"\n"
946"Partition the string into three parts using the given separator.\n"
947"\n"
948"This will search for the separator in the string, starting at the end. If\n"
949"the separator is found, returns a 3-tuple containing the part before the\n"
950"separator, the separator itself, and the part after it.\n"
951"\n"
952"If the separator is not found, returns a 3-tuple containing two empty strings\n"
953"and the original string.");
954
955#define UNICODE_RPARTITION_METHODDEF \
956 {"rpartition", (PyCFunction)unicode_rpartition, METH_O, unicode_rpartition__doc__},
957
958PyDoc_STRVAR(unicode_rsplit__doc__,
959"rsplit($self, /, sep=None, maxsplit=-1)\n"
960"--\n"
961"\n"
962"Return a list of the substrings in the string, using sep as the separator string.\n"
963"\n"
964" sep\n"
965" The separator used to split the string.\n"
966"\n"
967" When set to None (the default value), will split on any whitespace\n"
968" character (including \\\\n \\\\r \\\\t \\\\f and spaces) and will discard\n"
969" empty strings from the result.\n"
970" maxsplit\n"
971" Maximum number of splits (starting from the left).\n"
972" -1 (the default value) means no limit.\n"
973"\n"
974"Splitting starts at the end of the string and works to the front.");
975
976#define UNICODE_RSPLIT_METHODDEF \
977 {"rsplit", (PyCFunction)(void(*)(void))unicode_rsplit, METH_FASTCALL|METH_KEYWORDS, unicode_rsplit__doc__},
978
979static PyObject *
980unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
981
982static PyObject *
983unicode_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
984{
985 PyObject *return_value = NULL;
986 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
987 static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0};
988 PyObject *argsbuf[2];
989 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
990 PyObject *sep = Py_None;
991 Py_ssize_t maxsplit = -1;
992
993 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
994 if (!args) {
995 goto exit;
996 }
997 if (!noptargs) {
998 goto skip_optional_pos;
999 }
1000 if (args[0]) {
1001 sep = args[0];
1002 if (!--noptargs) {
1003 goto skip_optional_pos;
1004 }
1005 }
1006 {
1007 Py_ssize_t ival = -1;
1008 PyObject *iobj = _PyNumber_Index(args[1]);
1009 if (iobj != NULL) {
1010 ival = PyLong_AsSsize_t(iobj);
1011 Py_DECREF(iobj);
1012 }
1013 if (ival == -1 && PyErr_Occurred()) {
1014 goto exit;
1015 }
1016 maxsplit = ival;
1017 }
1018skip_optional_pos:
1019 return_value = unicode_rsplit_impl(self, sep, maxsplit);
1020
1021exit:
1022 return return_value;
1023}
1024
1025PyDoc_STRVAR(unicode_splitlines__doc__,
1026"splitlines($self, /, keepends=False)\n"
1027"--\n"
1028"\n"
1029"Return a list of the lines in the string, breaking at line boundaries.\n"
1030"\n"
1031"Line breaks are not included in the resulting list unless keepends is given and\n"
1032"true.");
1033
1034#define UNICODE_SPLITLINES_METHODDEF \
1035 {"splitlines", (PyCFunction)(void(*)(void))unicode_splitlines, METH_FASTCALL|METH_KEYWORDS, unicode_splitlines__doc__},
1036
1037static PyObject *
1038unicode_splitlines_impl(PyObject *self, int keepends);
1039
1040static PyObject *
1041unicode_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1042{
1043 PyObject *return_value = NULL;
1044 static const char * const _keywords[] = {"keepends", NULL};
1045 static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0};
1046 PyObject *argsbuf[1];
1047 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1048 int keepends = 0;
1049
1050 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
1051 if (!args) {
1052 goto exit;
1053 }
1054 if (!noptargs) {
1055 goto skip_optional_pos;
1056 }
1057 keepends = _PyLong_AsInt(args[0]);
1058 if (keepends == -1 && PyErr_Occurred()) {
1059 goto exit;
1060 }
1061skip_optional_pos:
1062 return_value = unicode_splitlines_impl(self, keepends);
1063
1064exit:
1065 return return_value;
1066}
1067
1068PyDoc_STRVAR(unicode_swapcase__doc__,
1069"swapcase($self, /)\n"
1070"--\n"
1071"\n"
1072"Convert uppercase characters to lowercase and lowercase characters to uppercase.");
1073
1074#define UNICODE_SWAPCASE_METHODDEF \
1075 {"swapcase", (PyCFunction)unicode_swapcase, METH_NOARGS, unicode_swapcase__doc__},
1076
1077static PyObject *
1078unicode_swapcase_impl(PyObject *self);
1079
1080static PyObject *
1081unicode_swapcase(PyObject *self, PyObject *Py_UNUSED(ignored))
1082{
1083 return unicode_swapcase_impl(self);
1084}
1085
1086PyDoc_STRVAR(unicode_maketrans__doc__,
1087"maketrans(x, y=<unrepresentable>, z=<unrepresentable>, /)\n"
1088"--\n"
1089"\n"
1090"Return a translation table usable for str.translate().\n"
1091"\n"
1092"If there is only one argument, it must be a dictionary mapping Unicode\n"
1093"ordinals (integers) or characters to Unicode ordinals, strings or None.\n"
1094"Character keys will be then converted to ordinals.\n"
1095"If there are two arguments, they must be strings of equal length, and\n"
1096"in the resulting dictionary, each character in x will be mapped to the\n"
1097"character at the same position in y. If there is a third argument, it\n"
1098"must be a string, whose characters will be mapped to None in the result.");
1099
1100#define UNICODE_MAKETRANS_METHODDEF \
1101 {"maketrans", (PyCFunction)(void(*)(void))unicode_maketrans, METH_FASTCALL|METH_STATIC, unicode_maketrans__doc__},
1102
1103static PyObject *
1104unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z);
1105
1106static PyObject *
1107unicode_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
1108{
1109 PyObject *return_value = NULL;
1110 PyObject *x;
1111 PyObject *y = NULL;
1112 PyObject *z = NULL;
1113
1114 if (!_PyArg_CheckPositional("maketrans", nargs, 1, 3)) {
1115 goto exit;
1116 }
1117 x = args[0];
1118 if (nargs < 2) {
1119 goto skip_optional;
1120 }
1121 if (!PyUnicode_Check(args[1])) {
1122 _PyArg_BadArgument("maketrans", "argument 2", "str", args[1]);
1123 goto exit;
1124 }
1125 if (PyUnicode_READY(args[1]) == -1) {
1126 goto exit;
1127 }
1128 y = args[1];
1129 if (nargs < 3) {
1130 goto skip_optional;
1131 }
1132 if (!PyUnicode_Check(args[2])) {
1133 _PyArg_BadArgument("maketrans", "argument 3", "str", args[2]);
1134 goto exit;
1135 }
1136 if (PyUnicode_READY(args[2]) == -1) {
1137 goto exit;
1138 }
1139 z = args[2];
1140skip_optional:
1141 return_value = unicode_maketrans_impl(x, y, z);
1142
1143exit:
1144 return return_value;
1145}
1146
1147PyDoc_STRVAR(unicode_translate__doc__,
1148"translate($self, table, /)\n"
1149"--\n"
1150"\n"
1151"Replace each character in the string using the given translation table.\n"
1152"\n"
1153" table\n"
1154" Translation table, which must be a mapping of Unicode ordinals to\n"
1155" Unicode ordinals, strings, or None.\n"
1156"\n"
1157"The table must implement lookup/indexing via __getitem__, for instance a\n"
1158"dictionary or list. If this operation raises LookupError, the character is\n"
1159"left untouched. Characters mapped to None are deleted.");
1160
1161#define UNICODE_TRANSLATE_METHODDEF \
1162 {"translate", (PyCFunction)unicode_translate, METH_O, unicode_translate__doc__},
1163
1164PyDoc_STRVAR(unicode_upper__doc__,
1165"upper($self, /)\n"
1166"--\n"
1167"\n"
1168"Return a copy of the string converted to uppercase.");
1169
1170#define UNICODE_UPPER_METHODDEF \
1171 {"upper", (PyCFunction)unicode_upper, METH_NOARGS, unicode_upper__doc__},
1172
1173static PyObject *
1174unicode_upper_impl(PyObject *self);
1175
1176static PyObject *
1177unicode_upper(PyObject *self, PyObject *Py_UNUSED(ignored))
1178{
1179 return unicode_upper_impl(self);
1180}
1181
1182PyDoc_STRVAR(unicode_zfill__doc__,
1183"zfill($self, width, /)\n"
1184"--\n"
1185"\n"
1186"Pad a numeric string with zeros on the left, to fill a field of the given width.\n"
1187"\n"
1188"The string is never truncated.");
1189
1190#define UNICODE_ZFILL_METHODDEF \
1191 {"zfill", (PyCFunction)unicode_zfill, METH_O, unicode_zfill__doc__},
1192
1193static PyObject *
1194unicode_zfill_impl(PyObject *self, Py_ssize_t width);
1195
1196static PyObject *
1197unicode_zfill(PyObject *self, PyObject *arg)
1198{
1199 PyObject *return_value = NULL;
1200 Py_ssize_t width;
1201
1202 {
1203 Py_ssize_t ival = -1;
1204 PyObject *iobj = _PyNumber_Index(arg);
1205 if (iobj != NULL) {
1206 ival = PyLong_AsSsize_t(iobj);
1207 Py_DECREF(iobj);
1208 }
1209 if (ival == -1 && PyErr_Occurred()) {
1210 goto exit;
1211 }
1212 width = ival;
1213 }
1214 return_value = unicode_zfill_impl(self, width);
1215
1216exit:
1217 return return_value;
1218}
1219
1220PyDoc_STRVAR(unicode___format____doc__,
1221"__format__($self, format_spec, /)\n"
1222"--\n"
1223"\n"
1224"Return a formatted version of the string as described by format_spec.");
1225
1226#define UNICODE___FORMAT___METHODDEF \
1227 {"__format__", (PyCFunction)unicode___format__, METH_O, unicode___format____doc__},
1228
1229static PyObject *
1230unicode___format___impl(PyObject *self, PyObject *format_spec);
1231
1232static PyObject *
1233unicode___format__(PyObject *self, PyObject *arg)
1234{
1235 PyObject *return_value = NULL;
1236 PyObject *format_spec;
1237
1238 if (!PyUnicode_Check(arg)) {
1239 _PyArg_BadArgument("__format__", "argument", "str", arg);
1240 goto exit;
1241 }
1242 if (PyUnicode_READY(arg) == -1) {
1243 goto exit;
1244 }
1245 format_spec = arg;
1246 return_value = unicode___format___impl(self, format_spec);
1247
1248exit:
1249 return return_value;
1250}
1251
1252PyDoc_STRVAR(unicode_sizeof__doc__,
1253"__sizeof__($self, /)\n"
1254"--\n"
1255"\n"
1256"Return the size of the string in memory, in bytes.");
1257
1258#define UNICODE_SIZEOF_METHODDEF \
1259 {"__sizeof__", (PyCFunction)unicode_sizeof, METH_NOARGS, unicode_sizeof__doc__},
1260
1261static PyObject *
1262unicode_sizeof_impl(PyObject *self);
1263
1264static PyObject *
1265unicode_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored))
1266{
1267 return unicode_sizeof_impl(self);
1268}
1269
1270static PyObject *
1271unicode_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
1272 const char *errors);
1273
1274static PyObject *
1275unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1276{
1277 PyObject *return_value = NULL;
1278 static const char * const _keywords[] = {"object", "encoding", "errors", NULL};
1279 static _PyArg_Parser _parser = {NULL, _keywords, "str", 0};
1280 PyObject *argsbuf[3];
1281 PyObject * const *fastargs;
1282 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1283 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
1284 PyObject *x = NULL;
1285 const char *encoding = NULL;
1286 const char *errors = NULL;
1287
1288 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 3, 0, argsbuf);
1289 if (!fastargs) {
1290 goto exit;
1291 }
1292 if (!noptargs) {
1293 goto skip_optional_pos;
1294 }
1295 if (fastargs[0]) {
1296 x = fastargs[0];
1297 if (!--noptargs) {
1298 goto skip_optional_pos;
1299 }
1300 }
1301 if (fastargs[1]) {
1302 if (!PyUnicode_Check(fastargs[1])) {
1303 _PyArg_BadArgument("str", "argument 'encoding'", "str", fastargs[1]);
1304 goto exit;
1305 }
1306 Py_ssize_t encoding_length;
1307 encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
1308 if (encoding == NULL) {
1309 goto exit;
1310 }
1311 if (strlen(encoding) != (size_t)encoding_length) {
1312 PyErr_SetString(PyExc_ValueError, "embedded null character");
1313 goto exit;
1314 }
1315 if (!--noptargs) {
1316 goto skip_optional_pos;
1317 }
1318 }
1319 if (!PyUnicode_Check(fastargs[2])) {
1320 _PyArg_BadArgument("str", "argument 'errors'", "str", fastargs[2]);
1321 goto exit;
1322 }
1323 Py_ssize_t errors_length;
1324 errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
1325 if (errors == NULL) {
1326 goto exit;
1327 }
1328 if (strlen(errors) != (size_t)errors_length) {
1329 PyErr_SetString(PyExc_ValueError, "embedded null character");
1330 goto exit;
1331 }
1332skip_optional_pos:
1333 return_value = unicode_new_impl(type, x, encoding, errors);
1334
1335exit:
1336 return return_value;
1337}
1338/*[clinic end generated code: output=c494bed46209961d input=a9049054013a1b77]*/
1339