blob: 2d81730d687cddb1ded1175513cfb13bbbd31bec [file] [log] [blame]
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
INADA Naoki3ae20562017-01-16 20:41:20 +09005PyDoc_STRVAR(unicode_title__doc__,
6"title($self, /)\n"
7"--\n"
8"\n"
INADA Naoki15f94592017-01-16 21:49:13 +09009"Return a version of the string where each word is titlecased.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +090010"\n"
INADA Naoki15f94592017-01-16 21:49:13 +090011"More specifically, words start with uppercased characters and all remaining\n"
INADA Naoki3ae20562017-01-16 20:41:20 +090012"cased characters have lower case.");
13
14#define UNICODE_TITLE_METHODDEF \
15 {"title", (PyCFunction)unicode_title, METH_NOARGS, unicode_title__doc__},
16
17static PyObject *
18unicode_title_impl(PyObject *self);
19
20static PyObject *
21unicode_title(PyObject *self, PyObject *Py_UNUSED(ignored))
22{
23 return unicode_title_impl(self);
24}
25
26PyDoc_STRVAR(unicode_capitalize__doc__,
27"capitalize($self, /)\n"
28"--\n"
29"\n"
30"Return a capitalized version of the string.\n"
31"\n"
32"More specifically, make the first character have upper case and the rest lower\n"
33"case.");
34
35#define UNICODE_CAPITALIZE_METHODDEF \
36 {"capitalize", (PyCFunction)unicode_capitalize, METH_NOARGS, unicode_capitalize__doc__},
37
38static PyObject *
39unicode_capitalize_impl(PyObject *self);
40
41static PyObject *
42unicode_capitalize(PyObject *self, PyObject *Py_UNUSED(ignored))
43{
44 return unicode_capitalize_impl(self);
45}
46
47PyDoc_STRVAR(unicode_casefold__doc__,
48"casefold($self, /)\n"
49"--\n"
50"\n"
INADA Naoki15f94592017-01-16 21:49:13 +090051"Return a version of the string suitable for caseless comparisons.");
INADA Naoki3ae20562017-01-16 20:41:20 +090052
53#define UNICODE_CASEFOLD_METHODDEF \
54 {"casefold", (PyCFunction)unicode_casefold, METH_NOARGS, unicode_casefold__doc__},
55
56static PyObject *
57unicode_casefold_impl(PyObject *self);
58
59static PyObject *
60unicode_casefold(PyObject *self, PyObject *Py_UNUSED(ignored))
61{
62 return unicode_casefold_impl(self);
63}
64
65PyDoc_STRVAR(unicode_center__doc__,
66"center($self, width, fillchar=\' \', /)\n"
67"--\n"
68"\n"
69"Return a centered string of length width.\n"
70"\n"
71"Padding is done using the specified fill character (default is a space).");
72
73#define UNICODE_CENTER_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020074 {"center", (PyCFunction)(void(*)(void))unicode_center, METH_FASTCALL, unicode_center__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +090075
76static PyObject *
77unicode_center_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
78
79static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020080unicode_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +090081{
82 PyObject *return_value = NULL;
83 Py_ssize_t width;
84 Py_UCS4 fillchar = ' ';
85
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020086 if (!_PyArg_CheckPositional("center", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +010087 goto exit;
88 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020089 {
90 Py_ssize_t ival = -1;
91 PyObject *iobj = PyNumber_Index(args[0]);
92 if (iobj != NULL) {
93 ival = PyLong_AsSsize_t(iobj);
94 Py_DECREF(iobj);
95 }
96 if (ival == -1 && PyErr_Occurred()) {
97 goto exit;
98 }
99 width = ival;
100 }
101 if (nargs < 2) {
102 goto skip_optional;
103 }
104 if (!convert_uc(args[1], &fillchar)) {
105 goto exit;
106 }
107skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900108 return_value = unicode_center_impl(self, width, fillchar);
109
110exit:
111 return return_value;
112}
113
114PyDoc_STRVAR(unicode_encode__doc__,
115"encode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
116"--\n"
117"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900118"Encode the string using the codec registered for encoding.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900119"\n"
120" encoding\n"
121" The encoding in which to encode the string.\n"
122" errors\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900123" The error handling scheme to use for encoding errors.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900124" The default is \'strict\' meaning that encoding errors raise a\n"
125" UnicodeEncodeError. Other possible values are \'ignore\', \'replace\' and\n"
126" \'xmlcharrefreplace\' as well as any other name registered with\n"
127" codecs.register_error that can handle UnicodeEncodeErrors.");
128
129#define UNICODE_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200130 {"encode", (PyCFunction)(void(*)(void))unicode_encode, METH_FASTCALL|METH_KEYWORDS, unicode_encode__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900131
132static PyObject *
133unicode_encode_impl(PyObject *self, const char *encoding, const char *errors);
134
135static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200136unicode_encode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +0900137{
138 PyObject *return_value = NULL;
139 static const char * const _keywords[] = {"encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200140 static _PyArg_Parser _parser = {NULL, _keywords, "encode", 0};
141 PyObject *argsbuf[2];
142 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
INADA Naoki3ae20562017-01-16 20:41:20 +0900143 const char *encoding = NULL;
144 const char *errors = NULL;
145
Serhiy Storchaka31913912019-03-14 10:32:22 +0200146 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
147 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +0900148 goto exit;
149 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200150 if (!noptargs) {
151 goto skip_optional_pos;
152 }
153 if (args[0]) {
154 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200155 _PyArg_BadArgument("encode", "argument 'encoding'", "str", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200156 goto exit;
157 }
158 Py_ssize_t encoding_length;
159 encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
160 if (encoding == NULL) {
161 goto exit;
162 }
163 if (strlen(encoding) != (size_t)encoding_length) {
164 PyErr_SetString(PyExc_ValueError, "embedded null character");
165 goto exit;
166 }
167 if (!--noptargs) {
168 goto skip_optional_pos;
169 }
170 }
171 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200172 _PyArg_BadArgument("encode", "argument 'errors'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200173 goto exit;
174 }
175 Py_ssize_t errors_length;
176 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
177 if (errors == NULL) {
178 goto exit;
179 }
180 if (strlen(errors) != (size_t)errors_length) {
181 PyErr_SetString(PyExc_ValueError, "embedded null character");
182 goto exit;
183 }
184skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +0900185 return_value = unicode_encode_impl(self, encoding, errors);
186
187exit:
188 return return_value;
189}
190
191PyDoc_STRVAR(unicode_expandtabs__doc__,
192"expandtabs($self, /, tabsize=8)\n"
193"--\n"
194"\n"
195"Return a copy where all tab characters are expanded using spaces.\n"
196"\n"
197"If tabsize is not given, a tab size of 8 characters is assumed.");
198
199#define UNICODE_EXPANDTABS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200200 {"expandtabs", (PyCFunction)(void(*)(void))unicode_expandtabs, METH_FASTCALL|METH_KEYWORDS, unicode_expandtabs__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900201
202static PyObject *
203unicode_expandtabs_impl(PyObject *self, int tabsize);
204
205static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200206unicode_expandtabs(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +0900207{
208 PyObject *return_value = NULL;
209 static const char * const _keywords[] = {"tabsize", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200210 static _PyArg_Parser _parser = {NULL, _keywords, "expandtabs", 0};
211 PyObject *argsbuf[1];
212 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
INADA Naoki3ae20562017-01-16 20:41:20 +0900213 int tabsize = 8;
214
Serhiy Storchaka31913912019-03-14 10:32:22 +0200215 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
216 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +0900217 goto exit;
218 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200219 if (!noptargs) {
220 goto skip_optional_pos;
221 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200222 tabsize = _PyLong_AsInt(args[0]);
223 if (tabsize == -1 && PyErr_Occurred()) {
224 goto exit;
225 }
226skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +0900227 return_value = unicode_expandtabs_impl(self, tabsize);
228
229exit:
230 return return_value;
231}
232
INADA Naokia49ac992018-01-27 14:06:21 +0900233PyDoc_STRVAR(unicode_isascii__doc__,
234"isascii($self, /)\n"
235"--\n"
236"\n"
237"Return True if all characters in the string are ASCII, False otherwise.\n"
238"\n"
239"ASCII characters have code points in the range U+0000-U+007F.\n"
240"Empty string is ASCII too.");
241
242#define UNICODE_ISASCII_METHODDEF \
243 {"isascii", (PyCFunction)unicode_isascii, METH_NOARGS, unicode_isascii__doc__},
244
245static PyObject *
246unicode_isascii_impl(PyObject *self);
247
248static PyObject *
249unicode_isascii(PyObject *self, PyObject *Py_UNUSED(ignored))
250{
251 return unicode_isascii_impl(self);
252}
253
INADA Naoki3ae20562017-01-16 20:41:20 +0900254PyDoc_STRVAR(unicode_islower__doc__,
255"islower($self, /)\n"
256"--\n"
257"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900258"Return True if the string is a lowercase string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900259"\n"
260"A string is lowercase if all cased characters in the string are lowercase and\n"
261"there is at least one cased character in the string.");
262
263#define UNICODE_ISLOWER_METHODDEF \
264 {"islower", (PyCFunction)unicode_islower, METH_NOARGS, unicode_islower__doc__},
265
266static PyObject *
267unicode_islower_impl(PyObject *self);
268
269static PyObject *
270unicode_islower(PyObject *self, PyObject *Py_UNUSED(ignored))
271{
272 return unicode_islower_impl(self);
273}
274
275PyDoc_STRVAR(unicode_isupper__doc__,
276"isupper($self, /)\n"
277"--\n"
278"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900279"Return True if the string is an uppercase string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900280"\n"
281"A string is uppercase if all cased characters in the string are uppercase and\n"
282"there is at least one cased character in the string.");
283
284#define UNICODE_ISUPPER_METHODDEF \
285 {"isupper", (PyCFunction)unicode_isupper, METH_NOARGS, unicode_isupper__doc__},
286
287static PyObject *
288unicode_isupper_impl(PyObject *self);
289
290static PyObject *
291unicode_isupper(PyObject *self, PyObject *Py_UNUSED(ignored))
292{
293 return unicode_isupper_impl(self);
294}
295
296PyDoc_STRVAR(unicode_istitle__doc__,
297"istitle($self, /)\n"
298"--\n"
299"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900300"Return True if the string is a title-cased string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900301"\n"
302"In a title-cased string, upper- and title-case characters may only\n"
303"follow uncased characters and lowercase characters only cased ones.");
304
305#define UNICODE_ISTITLE_METHODDEF \
306 {"istitle", (PyCFunction)unicode_istitle, METH_NOARGS, unicode_istitle__doc__},
307
308static PyObject *
309unicode_istitle_impl(PyObject *self);
310
311static PyObject *
312unicode_istitle(PyObject *self, PyObject *Py_UNUSED(ignored))
313{
314 return unicode_istitle_impl(self);
315}
316
317PyDoc_STRVAR(unicode_isspace__doc__,
318"isspace($self, /)\n"
319"--\n"
320"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900321"Return True if the string is a whitespace string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900322"\n"
323"A string is whitespace if all characters in the string are whitespace and there\n"
324"is at least one character in the string.");
325
326#define UNICODE_ISSPACE_METHODDEF \
327 {"isspace", (PyCFunction)unicode_isspace, METH_NOARGS, unicode_isspace__doc__},
328
329static PyObject *
330unicode_isspace_impl(PyObject *self);
331
332static PyObject *
333unicode_isspace(PyObject *self, PyObject *Py_UNUSED(ignored))
334{
335 return unicode_isspace_impl(self);
336}
337
338PyDoc_STRVAR(unicode_isalpha__doc__,
339"isalpha($self, /)\n"
340"--\n"
341"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900342"Return True if the string is an alphabetic string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900343"\n"
344"A string is alphabetic if all characters in the string are alphabetic and there\n"
345"is at least one character in the string.");
346
347#define UNICODE_ISALPHA_METHODDEF \
348 {"isalpha", (PyCFunction)unicode_isalpha, METH_NOARGS, unicode_isalpha__doc__},
349
350static PyObject *
351unicode_isalpha_impl(PyObject *self);
352
353static PyObject *
354unicode_isalpha(PyObject *self, PyObject *Py_UNUSED(ignored))
355{
356 return unicode_isalpha_impl(self);
357}
358
359PyDoc_STRVAR(unicode_isalnum__doc__,
360"isalnum($self, /)\n"
361"--\n"
362"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900363"Return True if the string is an alpha-numeric string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900364"\n"
365"A string is alpha-numeric if all characters in the string are alpha-numeric and\n"
366"there is at least one character in the string.");
367
368#define UNICODE_ISALNUM_METHODDEF \
369 {"isalnum", (PyCFunction)unicode_isalnum, METH_NOARGS, unicode_isalnum__doc__},
370
371static PyObject *
372unicode_isalnum_impl(PyObject *self);
373
374static PyObject *
375unicode_isalnum(PyObject *self, PyObject *Py_UNUSED(ignored))
376{
377 return unicode_isalnum_impl(self);
378}
379
380PyDoc_STRVAR(unicode_isdecimal__doc__,
381"isdecimal($self, /)\n"
382"--\n"
383"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900384"Return True if the string is a decimal string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900385"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900386"A string is a decimal string if all characters in the string are decimal and\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900387"there is at least one character in the string.");
388
389#define UNICODE_ISDECIMAL_METHODDEF \
390 {"isdecimal", (PyCFunction)unicode_isdecimal, METH_NOARGS, unicode_isdecimal__doc__},
391
392static PyObject *
393unicode_isdecimal_impl(PyObject *self);
394
395static PyObject *
396unicode_isdecimal(PyObject *self, PyObject *Py_UNUSED(ignored))
397{
398 return unicode_isdecimal_impl(self);
399}
400
401PyDoc_STRVAR(unicode_isdigit__doc__,
402"isdigit($self, /)\n"
403"--\n"
404"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900405"Return True if the string is a digit string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900406"\n"
407"A string is a digit string if all characters in the string are digits and there\n"
408"is at least one character in the string.");
409
410#define UNICODE_ISDIGIT_METHODDEF \
411 {"isdigit", (PyCFunction)unicode_isdigit, METH_NOARGS, unicode_isdigit__doc__},
412
413static PyObject *
414unicode_isdigit_impl(PyObject *self);
415
416static PyObject *
417unicode_isdigit(PyObject *self, PyObject *Py_UNUSED(ignored))
418{
419 return unicode_isdigit_impl(self);
420}
421
422PyDoc_STRVAR(unicode_isnumeric__doc__,
423"isnumeric($self, /)\n"
424"--\n"
425"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900426"Return True if the string is a numeric string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900427"\n"
428"A string is numeric if all characters in the string are numeric and there is at\n"
429"least one character in the string.");
430
431#define UNICODE_ISNUMERIC_METHODDEF \
432 {"isnumeric", (PyCFunction)unicode_isnumeric, METH_NOARGS, unicode_isnumeric__doc__},
433
434static PyObject *
435unicode_isnumeric_impl(PyObject *self);
436
437static PyObject *
438unicode_isnumeric(PyObject *self, PyObject *Py_UNUSED(ignored))
439{
440 return unicode_isnumeric_impl(self);
441}
442
443PyDoc_STRVAR(unicode_isidentifier__doc__,
444"isidentifier($self, /)\n"
445"--\n"
446"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900447"Return True if the string is a valid Python identifier, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900448"\n"
Sanyam Khuranaffc5a142018-10-08 12:23:32 +0530449"Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n"
Emanuele Gaifasfc8205c2018-10-08 12:44:47 +0200450"such as \"def\" or \"class\".");
INADA Naoki3ae20562017-01-16 20:41:20 +0900451
452#define UNICODE_ISIDENTIFIER_METHODDEF \
453 {"isidentifier", (PyCFunction)unicode_isidentifier, METH_NOARGS, unicode_isidentifier__doc__},
454
455static PyObject *
456unicode_isidentifier_impl(PyObject *self);
457
458static PyObject *
459unicode_isidentifier(PyObject *self, PyObject *Py_UNUSED(ignored))
460{
461 return unicode_isidentifier_impl(self);
462}
463
464PyDoc_STRVAR(unicode_isprintable__doc__,
465"isprintable($self, /)\n"
466"--\n"
467"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900468"Return True if the string is printable, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900469"\n"
470"A string is printable if all of its characters are considered printable in\n"
471"repr() or if it is empty.");
472
473#define UNICODE_ISPRINTABLE_METHODDEF \
474 {"isprintable", (PyCFunction)unicode_isprintable, METH_NOARGS, unicode_isprintable__doc__},
475
476static PyObject *
477unicode_isprintable_impl(PyObject *self);
478
479static PyObject *
480unicode_isprintable(PyObject *self, PyObject *Py_UNUSED(ignored))
481{
482 return unicode_isprintable_impl(self);
483}
484
485PyDoc_STRVAR(unicode_join__doc__,
486"join($self, iterable, /)\n"
487"--\n"
488"\n"
489"Concatenate any number of strings.\n"
490"\n"
Martin Panter91a88662017-01-24 00:30:06 +0000491"The string whose method is called is inserted in between each given string.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900492"The result is returned as a new string.\n"
493"\n"
494"Example: \'.\'.join([\'ab\', \'pq\', \'rs\']) -> \'ab.pq.rs\'");
495
496#define UNICODE_JOIN_METHODDEF \
497 {"join", (PyCFunction)unicode_join, METH_O, unicode_join__doc__},
498
499PyDoc_STRVAR(unicode_ljust__doc__,
500"ljust($self, width, fillchar=\' \', /)\n"
501"--\n"
502"\n"
503"Return a left-justified string of length width.\n"
504"\n"
505"Padding is done using the specified fill character (default is a space).");
506
507#define UNICODE_LJUST_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200508 {"ljust", (PyCFunction)(void(*)(void))unicode_ljust, METH_FASTCALL, unicode_ljust__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900509
510static PyObject *
511unicode_ljust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
512
513static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200514unicode_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900515{
516 PyObject *return_value = NULL;
517 Py_ssize_t width;
518 Py_UCS4 fillchar = ' ';
519
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200520 if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100521 goto exit;
522 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200523 {
524 Py_ssize_t ival = -1;
525 PyObject *iobj = PyNumber_Index(args[0]);
526 if (iobj != NULL) {
527 ival = PyLong_AsSsize_t(iobj);
528 Py_DECREF(iobj);
529 }
530 if (ival == -1 && PyErr_Occurred()) {
531 goto exit;
532 }
533 width = ival;
534 }
535 if (nargs < 2) {
536 goto skip_optional;
537 }
538 if (!convert_uc(args[1], &fillchar)) {
539 goto exit;
540 }
541skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900542 return_value = unicode_ljust_impl(self, width, fillchar);
543
544exit:
545 return return_value;
546}
547
548PyDoc_STRVAR(unicode_lower__doc__,
549"lower($self, /)\n"
550"--\n"
551"\n"
552"Return a copy of the string converted to lowercase.");
553
554#define UNICODE_LOWER_METHODDEF \
555 {"lower", (PyCFunction)unicode_lower, METH_NOARGS, unicode_lower__doc__},
556
557static PyObject *
558unicode_lower_impl(PyObject *self);
559
560static PyObject *
561unicode_lower(PyObject *self, PyObject *Py_UNUSED(ignored))
562{
563 return unicode_lower_impl(self);
564}
565
566PyDoc_STRVAR(unicode_strip__doc__,
567"strip($self, chars=None, /)\n"
568"--\n"
569"\n"
Zachary Ware09895c22019-10-09 16:09:00 -0500570"Return a copy of the string with leading and trailing whitespace removed.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900571"\n"
572"If chars is given and not None, remove characters in chars instead.");
573
574#define UNICODE_STRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200575 {"strip", (PyCFunction)(void(*)(void))unicode_strip, METH_FASTCALL, unicode_strip__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900576
577static PyObject *
578unicode_strip_impl(PyObject *self, PyObject *chars);
579
580static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200581unicode_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900582{
583 PyObject *return_value = NULL;
584 PyObject *chars = Py_None;
585
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200586 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100587 goto exit;
588 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200589 if (nargs < 1) {
590 goto skip_optional;
591 }
592 chars = args[0];
593skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900594 return_value = unicode_strip_impl(self, chars);
595
596exit:
597 return return_value;
598}
599
600PyDoc_STRVAR(unicode_lstrip__doc__,
601"lstrip($self, chars=None, /)\n"
602"--\n"
603"\n"
604"Return a copy of the string with leading whitespace removed.\n"
605"\n"
606"If chars is given and not None, remove characters in chars instead.");
607
608#define UNICODE_LSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200609 {"lstrip", (PyCFunction)(void(*)(void))unicode_lstrip, METH_FASTCALL, unicode_lstrip__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900610
611static PyObject *
612unicode_lstrip_impl(PyObject *self, PyObject *chars);
613
614static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200615unicode_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900616{
617 PyObject *return_value = NULL;
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300618 PyObject *chars = Py_None;
INADA Naoki3ae20562017-01-16 20:41:20 +0900619
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200620 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100621 goto exit;
622 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200623 if (nargs < 1) {
624 goto skip_optional;
625 }
626 chars = args[0];
627skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900628 return_value = unicode_lstrip_impl(self, chars);
629
630exit:
631 return return_value;
632}
633
634PyDoc_STRVAR(unicode_rstrip__doc__,
635"rstrip($self, chars=None, /)\n"
636"--\n"
637"\n"
638"Return a copy of the string with trailing whitespace removed.\n"
639"\n"
640"If chars is given and not None, remove characters in chars instead.");
641
642#define UNICODE_RSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200643 {"rstrip", (PyCFunction)(void(*)(void))unicode_rstrip, METH_FASTCALL, unicode_rstrip__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900644
645static PyObject *
646unicode_rstrip_impl(PyObject *self, PyObject *chars);
647
648static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200649unicode_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900650{
651 PyObject *return_value = NULL;
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300652 PyObject *chars = Py_None;
INADA Naoki3ae20562017-01-16 20:41:20 +0900653
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200654 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100655 goto exit;
656 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200657 if (nargs < 1) {
658 goto skip_optional;
659 }
660 chars = args[0];
661skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900662 return_value = unicode_rstrip_impl(self, chars);
663
664exit:
665 return return_value;
666}
667
668PyDoc_STRVAR(unicode_replace__doc__,
669"replace($self, old, new, count=-1, /)\n"
670"--\n"
671"\n"
672"Return a copy with all occurrences of substring old replaced by new.\n"
673"\n"
674" count\n"
675" Maximum number of occurrences to replace.\n"
676" -1 (the default value) means replace all occurrences.\n"
677"\n"
678"If the optional argument count is given, only the first count occurrences are\n"
679"replaced.");
680
681#define UNICODE_REPLACE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200682 {"replace", (PyCFunction)(void(*)(void))unicode_replace, METH_FASTCALL, unicode_replace__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900683
684static PyObject *
685unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
686 Py_ssize_t count);
687
688static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200689unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900690{
691 PyObject *return_value = NULL;
692 PyObject *old;
693 PyObject *new;
694 Py_ssize_t count = -1;
695
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200696 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100697 goto exit;
698 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200699 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200700 _PyArg_BadArgument("replace", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200701 goto exit;
702 }
703 if (PyUnicode_READY(args[0]) == -1) {
704 goto exit;
705 }
706 old = args[0];
707 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200708 _PyArg_BadArgument("replace", "argument 2", "str", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200709 goto exit;
710 }
711 if (PyUnicode_READY(args[1]) == -1) {
712 goto exit;
713 }
714 new = args[1];
715 if (nargs < 3) {
716 goto skip_optional;
717 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200718 {
719 Py_ssize_t ival = -1;
720 PyObject *iobj = PyNumber_Index(args[2]);
721 if (iobj != NULL) {
722 ival = PyLong_AsSsize_t(iobj);
723 Py_DECREF(iobj);
724 }
725 if (ival == -1 && PyErr_Occurred()) {
726 goto exit;
727 }
728 count = ival;
729 }
730skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900731 return_value = unicode_replace_impl(self, old, new, count);
732
733exit:
734 return return_value;
735}
736
sweeneydea81849b2020-04-22 17:05:48 -0400737PyDoc_STRVAR(unicode_removeprefix__doc__,
738"removeprefix($self, prefix, /)\n"
739"--\n"
740"\n"
741"Return a str with the given prefix string removed if present.\n"
742"\n"
743"If the string starts with the prefix string, return string[len(prefix):].\n"
744"Otherwise, return a copy of the original string.");
745
746#define UNICODE_REMOVEPREFIX_METHODDEF \
747 {"removeprefix", (PyCFunction)unicode_removeprefix, METH_O, unicode_removeprefix__doc__},
748
749static PyObject *
750unicode_removeprefix_impl(PyObject *self, PyObject *prefix);
751
752static PyObject *
753unicode_removeprefix(PyObject *self, PyObject *arg)
754{
755 PyObject *return_value = NULL;
756 PyObject *prefix;
757
758 if (!PyUnicode_Check(arg)) {
759 _PyArg_BadArgument("removeprefix", "argument", "str", arg);
760 goto exit;
761 }
762 if (PyUnicode_READY(arg) == -1) {
763 goto exit;
764 }
765 prefix = arg;
766 return_value = unicode_removeprefix_impl(self, prefix);
767
768exit:
769 return return_value;
770}
771
772PyDoc_STRVAR(unicode_removesuffix__doc__,
773"removesuffix($self, suffix, /)\n"
774"--\n"
775"\n"
776"Return a str with the given suffix string removed if present.\n"
777"\n"
778"If the string ends with the suffix string and that suffix is not empty,\n"
779"return string[:-len(suffix)]. Otherwise, return a copy of the original\n"
780"string.");
781
782#define UNICODE_REMOVESUFFIX_METHODDEF \
783 {"removesuffix", (PyCFunction)unicode_removesuffix, METH_O, unicode_removesuffix__doc__},
784
785static PyObject *
786unicode_removesuffix_impl(PyObject *self, PyObject *suffix);
787
788static PyObject *
789unicode_removesuffix(PyObject *self, PyObject *arg)
790{
791 PyObject *return_value = NULL;
792 PyObject *suffix;
793
794 if (!PyUnicode_Check(arg)) {
795 _PyArg_BadArgument("removesuffix", "argument", "str", arg);
796 goto exit;
797 }
798 if (PyUnicode_READY(arg) == -1) {
799 goto exit;
800 }
801 suffix = arg;
802 return_value = unicode_removesuffix_impl(self, suffix);
803
804exit:
805 return return_value;
806}
807
INADA Naoki3ae20562017-01-16 20:41:20 +0900808PyDoc_STRVAR(unicode_rjust__doc__,
809"rjust($self, width, fillchar=\' \', /)\n"
810"--\n"
811"\n"
812"Return a right-justified string of length width.\n"
813"\n"
814"Padding is done using the specified fill character (default is a space).");
815
816#define UNICODE_RJUST_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200817 {"rjust", (PyCFunction)(void(*)(void))unicode_rjust, METH_FASTCALL, unicode_rjust__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900818
819static PyObject *
820unicode_rjust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
821
822static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200823unicode_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900824{
825 PyObject *return_value = NULL;
826 Py_ssize_t width;
827 Py_UCS4 fillchar = ' ';
828
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200829 if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100830 goto exit;
831 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200832 {
833 Py_ssize_t ival = -1;
834 PyObject *iobj = PyNumber_Index(args[0]);
835 if (iobj != NULL) {
836 ival = PyLong_AsSsize_t(iobj);
837 Py_DECREF(iobj);
838 }
839 if (ival == -1 && PyErr_Occurred()) {
840 goto exit;
841 }
842 width = ival;
843 }
844 if (nargs < 2) {
845 goto skip_optional;
846 }
847 if (!convert_uc(args[1], &fillchar)) {
848 goto exit;
849 }
850skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900851 return_value = unicode_rjust_impl(self, width, fillchar);
852
853exit:
854 return return_value;
855}
856
857PyDoc_STRVAR(unicode_split__doc__,
858"split($self, /, sep=None, maxsplit=-1)\n"
859"--\n"
860"\n"
861"Return a list of the words in the string, using sep as the delimiter string.\n"
862"\n"
863" sep\n"
864" The delimiter according which to split the string.\n"
865" None (the default value) means split according to any whitespace,\n"
866" and discard empty strings from the result.\n"
867" maxsplit\n"
868" Maximum number of splits to do.\n"
869" -1 (the default value) means no limit.");
870
871#define UNICODE_SPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200872 {"split", (PyCFunction)(void(*)(void))unicode_split, METH_FASTCALL|METH_KEYWORDS, unicode_split__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900873
874static PyObject *
875unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
876
877static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200878unicode_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +0900879{
880 PyObject *return_value = NULL;
881 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200882 static _PyArg_Parser _parser = {NULL, _keywords, "split", 0};
883 PyObject *argsbuf[2];
884 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
INADA Naoki3ae20562017-01-16 20:41:20 +0900885 PyObject *sep = Py_None;
886 Py_ssize_t maxsplit = -1;
887
Serhiy Storchaka31913912019-03-14 10:32:22 +0200888 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
889 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +0900890 goto exit;
891 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200892 if (!noptargs) {
893 goto skip_optional_pos;
894 }
895 if (args[0]) {
896 sep = args[0];
897 if (!--noptargs) {
898 goto skip_optional_pos;
899 }
900 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200901 {
902 Py_ssize_t ival = -1;
903 PyObject *iobj = PyNumber_Index(args[1]);
904 if (iobj != NULL) {
905 ival = PyLong_AsSsize_t(iobj);
906 Py_DECREF(iobj);
907 }
908 if (ival == -1 && PyErr_Occurred()) {
909 goto exit;
910 }
911 maxsplit = ival;
912 }
913skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +0900914 return_value = unicode_split_impl(self, sep, maxsplit);
915
916exit:
917 return return_value;
918}
919
920PyDoc_STRVAR(unicode_partition__doc__,
921"partition($self, sep, /)\n"
922"--\n"
923"\n"
924"Partition the string into three parts using the given separator.\n"
925"\n"
926"This will search for the separator in the string. If the separator is found,\n"
927"returns a 3-tuple containing the part before the separator, the separator\n"
928"itself, and the part after it.\n"
929"\n"
930"If the separator is not found, returns a 3-tuple containing the original string\n"
931"and two empty strings.");
932
933#define UNICODE_PARTITION_METHODDEF \
934 {"partition", (PyCFunction)unicode_partition, METH_O, unicode_partition__doc__},
935
936PyDoc_STRVAR(unicode_rpartition__doc__,
937"rpartition($self, sep, /)\n"
938"--\n"
939"\n"
940"Partition the string into three parts using the given separator.\n"
941"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300942"This will search for the separator in the string, starting at the end. If\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900943"the separator is found, returns a 3-tuple containing the part before the\n"
944"separator, the separator itself, and the part after it.\n"
945"\n"
946"If the separator is not found, returns a 3-tuple containing two empty strings\n"
947"and the original string.");
948
949#define UNICODE_RPARTITION_METHODDEF \
950 {"rpartition", (PyCFunction)unicode_rpartition, METH_O, unicode_rpartition__doc__},
951
952PyDoc_STRVAR(unicode_rsplit__doc__,
953"rsplit($self, /, sep=None, maxsplit=-1)\n"
954"--\n"
955"\n"
956"Return a list of the words in the string, using sep as the delimiter string.\n"
957"\n"
958" sep\n"
959" The delimiter according which to split the string.\n"
960" None (the default value) means split according to any whitespace,\n"
961" and discard empty strings from the result.\n"
962" maxsplit\n"
963" Maximum number of splits to do.\n"
964" -1 (the default value) means no limit.\n"
965"\n"
966"Splits are done starting at the end of the string and working to the front.");
967
968#define UNICODE_RSPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200969 {"rsplit", (PyCFunction)(void(*)(void))unicode_rsplit, METH_FASTCALL|METH_KEYWORDS, unicode_rsplit__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900970
971static PyObject *
972unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
973
974static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200975unicode_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +0900976{
977 PyObject *return_value = NULL;
978 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200979 static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0};
980 PyObject *argsbuf[2];
981 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
INADA Naoki3ae20562017-01-16 20:41:20 +0900982 PyObject *sep = Py_None;
983 Py_ssize_t maxsplit = -1;
984
Serhiy Storchaka31913912019-03-14 10:32:22 +0200985 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
986 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +0900987 goto exit;
988 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200989 if (!noptargs) {
990 goto skip_optional_pos;
991 }
992 if (args[0]) {
993 sep = args[0];
994 if (!--noptargs) {
995 goto skip_optional_pos;
996 }
997 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200998 {
999 Py_ssize_t ival = -1;
1000 PyObject *iobj = PyNumber_Index(args[1]);
1001 if (iobj != NULL) {
1002 ival = PyLong_AsSsize_t(iobj);
1003 Py_DECREF(iobj);
1004 }
1005 if (ival == -1 && PyErr_Occurred()) {
1006 goto exit;
1007 }
1008 maxsplit = ival;
1009 }
1010skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +09001011 return_value = unicode_rsplit_impl(self, sep, maxsplit);
1012
1013exit:
1014 return return_value;
1015}
1016
1017PyDoc_STRVAR(unicode_splitlines__doc__,
1018"splitlines($self, /, keepends=False)\n"
1019"--\n"
1020"\n"
1021"Return a list of the lines in the string, breaking at line boundaries.\n"
1022"\n"
1023"Line breaks are not included in the resulting list unless keepends is given and\n"
1024"true.");
1025
1026#define UNICODE_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001027 {"splitlines", (PyCFunction)(void(*)(void))unicode_splitlines, METH_FASTCALL|METH_KEYWORDS, unicode_splitlines__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +09001028
1029static PyObject *
1030unicode_splitlines_impl(PyObject *self, int keepends);
1031
1032static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001033unicode_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +09001034{
1035 PyObject *return_value = NULL;
1036 static const char * const _keywords[] = {"keepends", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001037 static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0};
1038 PyObject *argsbuf[1];
1039 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
INADA Naoki3ae20562017-01-16 20:41:20 +09001040 int keepends = 0;
1041
Serhiy Storchaka31913912019-03-14 10:32:22 +02001042 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
1043 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +09001044 goto exit;
1045 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001046 if (!noptargs) {
1047 goto skip_optional_pos;
1048 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001049 keepends = _PyLong_AsInt(args[0]);
1050 if (keepends == -1 && PyErr_Occurred()) {
1051 goto exit;
1052 }
1053skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +09001054 return_value = unicode_splitlines_impl(self, keepends);
1055
1056exit:
1057 return return_value;
1058}
1059
1060PyDoc_STRVAR(unicode_swapcase__doc__,
1061"swapcase($self, /)\n"
1062"--\n"
1063"\n"
1064"Convert uppercase characters to lowercase and lowercase characters to uppercase.");
1065
1066#define UNICODE_SWAPCASE_METHODDEF \
1067 {"swapcase", (PyCFunction)unicode_swapcase, METH_NOARGS, unicode_swapcase__doc__},
1068
1069static PyObject *
1070unicode_swapcase_impl(PyObject *self);
1071
1072static PyObject *
1073unicode_swapcase(PyObject *self, PyObject *Py_UNUSED(ignored))
1074{
1075 return unicode_swapcase_impl(self);
1076}
1077
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001078PyDoc_STRVAR(unicode_maketrans__doc__,
Serhiy Storchaka279f4462019-09-14 12:24:05 +03001079"maketrans(x, y=<unrepresentable>, z=<unrepresentable>, /)\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001080"--\n"
1081"\n"
1082"Return a translation table usable for str.translate().\n"
1083"\n"
1084"If there is only one argument, it must be a dictionary mapping Unicode\n"
1085"ordinals (integers) or characters to Unicode ordinals, strings or None.\n"
1086"Character keys will be then converted to ordinals.\n"
1087"If there are two arguments, they must be strings of equal length, and\n"
1088"in the resulting dictionary, each character in x will be mapped to the\n"
1089"character at the same position in y. If there is a third argument, it\n"
1090"must be a string, whose characters will be mapped to None in the result.");
1091
1092#define UNICODE_MAKETRANS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001093 {"maketrans", (PyCFunction)(void(*)(void))unicode_maketrans, METH_FASTCALL|METH_STATIC, unicode_maketrans__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001094
1095static PyObject *
1096unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z);
1097
1098static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001099unicode_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001100{
1101 PyObject *return_value = NULL;
1102 PyObject *x;
1103 PyObject *y = NULL;
1104 PyObject *z = NULL;
1105
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001106 if (!_PyArg_CheckPositional("maketrans", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001107 goto exit;
1108 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001109 x = args[0];
1110 if (nargs < 2) {
1111 goto skip_optional;
1112 }
1113 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001114 _PyArg_BadArgument("maketrans", "argument 2", "str", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001115 goto exit;
1116 }
1117 if (PyUnicode_READY(args[1]) == -1) {
1118 goto exit;
1119 }
1120 y = args[1];
1121 if (nargs < 3) {
1122 goto skip_optional;
1123 }
1124 if (!PyUnicode_Check(args[2])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001125 _PyArg_BadArgument("maketrans", "argument 3", "str", args[2]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001126 goto exit;
1127 }
1128 if (PyUnicode_READY(args[2]) == -1) {
1129 goto exit;
1130 }
1131 z = args[2];
1132skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001133 return_value = unicode_maketrans_impl(x, y, z);
1134
1135exit:
1136 return return_value;
1137}
INADA Naoki3ae20562017-01-16 20:41:20 +09001138
1139PyDoc_STRVAR(unicode_translate__doc__,
1140"translate($self, table, /)\n"
1141"--\n"
1142"\n"
1143"Replace each character in the string using the given translation table.\n"
1144"\n"
1145" table\n"
1146" Translation table, which must be a mapping of Unicode ordinals to\n"
1147" Unicode ordinals, strings, or None.\n"
1148"\n"
1149"The table must implement lookup/indexing via __getitem__, for instance a\n"
1150"dictionary or list. If this operation raises LookupError, the character is\n"
1151"left untouched. Characters mapped to None are deleted.");
1152
1153#define UNICODE_TRANSLATE_METHODDEF \
1154 {"translate", (PyCFunction)unicode_translate, METH_O, unicode_translate__doc__},
1155
1156PyDoc_STRVAR(unicode_upper__doc__,
1157"upper($self, /)\n"
1158"--\n"
1159"\n"
1160"Return a copy of the string converted to uppercase.");
1161
1162#define UNICODE_UPPER_METHODDEF \
1163 {"upper", (PyCFunction)unicode_upper, METH_NOARGS, unicode_upper__doc__},
1164
1165static PyObject *
1166unicode_upper_impl(PyObject *self);
1167
1168static PyObject *
1169unicode_upper(PyObject *self, PyObject *Py_UNUSED(ignored))
1170{
1171 return unicode_upper_impl(self);
1172}
1173
1174PyDoc_STRVAR(unicode_zfill__doc__,
1175"zfill($self, width, /)\n"
1176"--\n"
1177"\n"
1178"Pad a numeric string with zeros on the left, to fill a field of the given width.\n"
1179"\n"
INADA Naoki15f94592017-01-16 21:49:13 +09001180"The string is never truncated.");
INADA Naoki3ae20562017-01-16 20:41:20 +09001181
1182#define UNICODE_ZFILL_METHODDEF \
1183 {"zfill", (PyCFunction)unicode_zfill, METH_O, unicode_zfill__doc__},
1184
1185static PyObject *
1186unicode_zfill_impl(PyObject *self, Py_ssize_t width);
1187
1188static PyObject *
1189unicode_zfill(PyObject *self, PyObject *arg)
1190{
1191 PyObject *return_value = NULL;
1192 Py_ssize_t width;
1193
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001194 {
1195 Py_ssize_t ival = -1;
1196 PyObject *iobj = PyNumber_Index(arg);
1197 if (iobj != NULL) {
1198 ival = PyLong_AsSsize_t(iobj);
1199 Py_DECREF(iobj);
1200 }
1201 if (ival == -1 && PyErr_Occurred()) {
1202 goto exit;
1203 }
1204 width = ival;
1205 }
INADA Naoki3ae20562017-01-16 20:41:20 +09001206 return_value = unicode_zfill_impl(self, width);
1207
1208exit:
1209 return return_value;
1210}
1211
1212PyDoc_STRVAR(unicode___format____doc__,
1213"__format__($self, format_spec, /)\n"
1214"--\n"
1215"\n"
INADA Naoki15f94592017-01-16 21:49:13 +09001216"Return a formatted version of the string as described by format_spec.");
INADA Naoki3ae20562017-01-16 20:41:20 +09001217
1218#define UNICODE___FORMAT___METHODDEF \
1219 {"__format__", (PyCFunction)unicode___format__, METH_O, unicode___format____doc__},
1220
1221static PyObject *
1222unicode___format___impl(PyObject *self, PyObject *format_spec);
1223
1224static PyObject *
1225unicode___format__(PyObject *self, PyObject *arg)
1226{
1227 PyObject *return_value = NULL;
1228 PyObject *format_spec;
1229
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001230 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001231 _PyArg_BadArgument("__format__", "argument", "str", arg);
INADA Naoki3ae20562017-01-16 20:41:20 +09001232 goto exit;
1233 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001234 if (PyUnicode_READY(arg) == -1) {
1235 goto exit;
1236 }
1237 format_spec = arg;
INADA Naoki3ae20562017-01-16 20:41:20 +09001238 return_value = unicode___format___impl(self, format_spec);
1239
1240exit:
1241 return return_value;
1242}
1243
1244PyDoc_STRVAR(unicode_sizeof__doc__,
1245"__sizeof__($self, /)\n"
1246"--\n"
1247"\n"
1248"Return the size of the string in memory, in bytes.");
1249
1250#define UNICODE_SIZEOF_METHODDEF \
1251 {"__sizeof__", (PyCFunction)unicode_sizeof, METH_NOARGS, unicode_sizeof__doc__},
1252
1253static PyObject *
1254unicode_sizeof_impl(PyObject *self);
1255
1256static PyObject *
1257unicode_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored))
1258{
1259 return unicode_sizeof_impl(self);
1260}
Serhiy Storchaka578c3952020-05-26 18:43:38 +03001261/*[clinic end generated code: output=ea1aff10c743be14 input=a9049054013a1b77]*/