Debian Codegraph @

Objects/clinic/unicodeobject.c.h

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