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