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