blob: cf81df4af67b2cc1b478ffbfb5c1ec861cf911ba [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 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 }
112skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900113 return_value = unicode_center_impl(self, width, fillchar);
114
115exit:
116 return return_value;
117}
118
119PyDoc_STRVAR(unicode_encode__doc__,
120"encode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
121"--\n"
122"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900123"Encode the string using the codec registered for encoding.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900124"\n"
125" encoding\n"
126" The encoding in which to encode the string.\n"
127" errors\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900128" The error handling scheme to use for encoding errors.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900129" 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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200135 {"encode", (PyCFunction)(void(*)(void))unicode_encode, METH_FASTCALL|METH_KEYWORDS, unicode_encode__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900136
137static PyObject *
138unicode_encode_impl(PyObject *self, const char *encoding, const char *errors);
139
140static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200141unicode_encode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +0900142{
143 PyObject *return_value = NULL;
144 static const char * const _keywords[] = {"encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200145 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;
INADA Naoki3ae20562017-01-16 20:41:20 +0900148 const char *encoding = NULL;
149 const char *errors = NULL;
150
Serhiy Storchaka31913912019-03-14 10:32:22 +0200151 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
152 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +0900153 goto exit;
154 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200155 if (!noptargs) {
156 goto skip_optional_pos;
157 }
158 if (args[0]) {
159 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200160 _PyArg_BadArgument("encode", "argument 'encoding'", "str", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200161 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])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200177 _PyArg_BadArgument("encode", "argument 'errors'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200178 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 }
189skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +0900190 return_value = unicode_encode_impl(self, encoding, errors);
191
192exit:
193 return return_value;
194}
195
196PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200205 {"expandtabs", (PyCFunction)(void(*)(void))unicode_expandtabs, METH_FASTCALL|METH_KEYWORDS, unicode_expandtabs__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900206
207static PyObject *
208unicode_expandtabs_impl(PyObject *self, int tabsize);
209
210static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200211unicode_expandtabs(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +0900212{
213 PyObject *return_value = NULL;
214 static const char * const _keywords[] = {"tabsize", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200215 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;
INADA Naoki3ae20562017-01-16 20:41:20 +0900218 int tabsize = 8;
219
Serhiy Storchaka31913912019-03-14 10:32:22 +0200220 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
221 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +0900222 goto exit;
223 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200224 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 }
236skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +0900237 return_value = unicode_expandtabs_impl(self, tabsize);
238
239exit:
240 return return_value;
241}
242
INADA Naokia49ac992018-01-27 14:06:21 +0900243PyDoc_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
255static PyObject *
256unicode_isascii_impl(PyObject *self);
257
258static PyObject *
259unicode_isascii(PyObject *self, PyObject *Py_UNUSED(ignored))
260{
261 return unicode_isascii_impl(self);
262}
263
INADA Naoki3ae20562017-01-16 20:41:20 +0900264PyDoc_STRVAR(unicode_islower__doc__,
265"islower($self, /)\n"
266"--\n"
267"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900268"Return True if the string is a lowercase string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900269"\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
276static PyObject *
277unicode_islower_impl(PyObject *self);
278
279static PyObject *
280unicode_islower(PyObject *self, PyObject *Py_UNUSED(ignored))
281{
282 return unicode_islower_impl(self);
283}
284
285PyDoc_STRVAR(unicode_isupper__doc__,
286"isupper($self, /)\n"
287"--\n"
288"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900289"Return True if the string is an uppercase string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900290"\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
297static PyObject *
298unicode_isupper_impl(PyObject *self);
299
300static PyObject *
301unicode_isupper(PyObject *self, PyObject *Py_UNUSED(ignored))
302{
303 return unicode_isupper_impl(self);
304}
305
306PyDoc_STRVAR(unicode_istitle__doc__,
307"istitle($self, /)\n"
308"--\n"
309"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900310"Return True if the string is a title-cased string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900311"\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
318static PyObject *
319unicode_istitle_impl(PyObject *self);
320
321static PyObject *
322unicode_istitle(PyObject *self, PyObject *Py_UNUSED(ignored))
323{
324 return unicode_istitle_impl(self);
325}
326
327PyDoc_STRVAR(unicode_isspace__doc__,
328"isspace($self, /)\n"
329"--\n"
330"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900331"Return True if the string is a whitespace string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900332"\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
339static PyObject *
340unicode_isspace_impl(PyObject *self);
341
342static PyObject *
343unicode_isspace(PyObject *self, PyObject *Py_UNUSED(ignored))
344{
345 return unicode_isspace_impl(self);
346}
347
348PyDoc_STRVAR(unicode_isalpha__doc__,
349"isalpha($self, /)\n"
350"--\n"
351"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900352"Return True if the string is an alphabetic string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900353"\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
360static PyObject *
361unicode_isalpha_impl(PyObject *self);
362
363static PyObject *
364unicode_isalpha(PyObject *self, PyObject *Py_UNUSED(ignored))
365{
366 return unicode_isalpha_impl(self);
367}
368
369PyDoc_STRVAR(unicode_isalnum__doc__,
370"isalnum($self, /)\n"
371"--\n"
372"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900373"Return True if the string is an alpha-numeric string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900374"\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
381static PyObject *
382unicode_isalnum_impl(PyObject *self);
383
384static PyObject *
385unicode_isalnum(PyObject *self, PyObject *Py_UNUSED(ignored))
386{
387 return unicode_isalnum_impl(self);
388}
389
390PyDoc_STRVAR(unicode_isdecimal__doc__,
391"isdecimal($self, /)\n"
392"--\n"
393"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900394"Return True if the string is a decimal string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900395"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900396"A string is a decimal string if all characters in the string are decimal and\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900397"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
402static PyObject *
403unicode_isdecimal_impl(PyObject *self);
404
405static PyObject *
406unicode_isdecimal(PyObject *self, PyObject *Py_UNUSED(ignored))
407{
408 return unicode_isdecimal_impl(self);
409}
410
411PyDoc_STRVAR(unicode_isdigit__doc__,
412"isdigit($self, /)\n"
413"--\n"
414"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900415"Return True if the string is a digit string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900416"\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
423static PyObject *
424unicode_isdigit_impl(PyObject *self);
425
426static PyObject *
427unicode_isdigit(PyObject *self, PyObject *Py_UNUSED(ignored))
428{
429 return unicode_isdigit_impl(self);
430}
431
432PyDoc_STRVAR(unicode_isnumeric__doc__,
433"isnumeric($self, /)\n"
434"--\n"
435"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900436"Return True if the string is a numeric string, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900437"\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
444static PyObject *
445unicode_isnumeric_impl(PyObject *self);
446
447static PyObject *
448unicode_isnumeric(PyObject *self, PyObject *Py_UNUSED(ignored))
449{
450 return unicode_isnumeric_impl(self);
451}
452
453PyDoc_STRVAR(unicode_isidentifier__doc__,
454"isidentifier($self, /)\n"
455"--\n"
456"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900457"Return True if the string is a valid Python identifier, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900458"\n"
Sanyam Khuranaffc5a142018-10-08 12:23:32 +0530459"Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n"
Emanuele Gaifasfc8205c2018-10-08 12:44:47 +0200460"such as \"def\" or \"class\".");
INADA Naoki3ae20562017-01-16 20:41:20 +0900461
462#define UNICODE_ISIDENTIFIER_METHODDEF \
463 {"isidentifier", (PyCFunction)unicode_isidentifier, METH_NOARGS, unicode_isidentifier__doc__},
464
465static PyObject *
466unicode_isidentifier_impl(PyObject *self);
467
468static PyObject *
469unicode_isidentifier(PyObject *self, PyObject *Py_UNUSED(ignored))
470{
471 return unicode_isidentifier_impl(self);
472}
473
474PyDoc_STRVAR(unicode_isprintable__doc__,
475"isprintable($self, /)\n"
476"--\n"
477"\n"
INADA Naoki15f94592017-01-16 21:49:13 +0900478"Return True if the string is printable, False otherwise.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900479"\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
486static PyObject *
487unicode_isprintable_impl(PyObject *self);
488
489static PyObject *
490unicode_isprintable(PyObject *self, PyObject *Py_UNUSED(ignored))
491{
492 return unicode_isprintable_impl(self);
493}
494
495PyDoc_STRVAR(unicode_join__doc__,
496"join($self, iterable, /)\n"
497"--\n"
498"\n"
499"Concatenate any number of strings.\n"
500"\n"
Martin Panter91a88662017-01-24 00:30:06 +0000501"The string whose method is called is inserted in between each given string.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900502"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
509PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200518 {"ljust", (PyCFunction)(void(*)(void))unicode_ljust, METH_FASTCALL, unicode_ljust__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900519
520static PyObject *
521unicode_ljust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
522
523static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200524unicode_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900525{
526 PyObject *return_value = NULL;
527 Py_ssize_t width;
528 Py_UCS4 fillchar = ' ';
529
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200530 if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100531 goto exit;
532 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200533 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 }
556skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900557 return_value = unicode_ljust_impl(self, width, fillchar);
558
559exit:
560 return return_value;
561}
562
563PyDoc_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
572static PyObject *
573unicode_lower_impl(PyObject *self);
574
575static PyObject *
576unicode_lower(PyObject *self, PyObject *Py_UNUSED(ignored))
577{
578 return unicode_lower_impl(self);
579}
580
581PyDoc_STRVAR(unicode_strip__doc__,
582"strip($self, chars=None, /)\n"
583"--\n"
584"\n"
Zachary Ware09895c22019-10-09 16:09:00 -0500585"Return a copy of the string with leading and trailing whitespace removed.\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900586"\n"
587"If chars is given and not None, remove characters in chars instead.");
588
589#define UNICODE_STRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200590 {"strip", (PyCFunction)(void(*)(void))unicode_strip, METH_FASTCALL, unicode_strip__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900591
592static PyObject *
593unicode_strip_impl(PyObject *self, PyObject *chars);
594
595static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200596unicode_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900597{
598 PyObject *return_value = NULL;
599 PyObject *chars = Py_None;
600
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200601 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100602 goto exit;
603 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200604 if (nargs < 1) {
605 goto skip_optional;
606 }
607 chars = args[0];
608skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900609 return_value = unicode_strip_impl(self, chars);
610
611exit:
612 return return_value;
613}
614
615PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200624 {"lstrip", (PyCFunction)(void(*)(void))unicode_lstrip, METH_FASTCALL, unicode_lstrip__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900625
626static PyObject *
627unicode_lstrip_impl(PyObject *self, PyObject *chars);
628
629static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200630unicode_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900631{
632 PyObject *return_value = NULL;
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300633 PyObject *chars = Py_None;
INADA Naoki3ae20562017-01-16 20:41:20 +0900634
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200635 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100636 goto exit;
637 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200638 if (nargs < 1) {
639 goto skip_optional;
640 }
641 chars = args[0];
642skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900643 return_value = unicode_lstrip_impl(self, chars);
644
645exit:
646 return return_value;
647}
648
649PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200658 {"rstrip", (PyCFunction)(void(*)(void))unicode_rstrip, METH_FASTCALL, unicode_rstrip__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900659
660static PyObject *
661unicode_rstrip_impl(PyObject *self, PyObject *chars);
662
663static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200664unicode_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900665{
666 PyObject *return_value = NULL;
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300667 PyObject *chars = Py_None;
INADA Naoki3ae20562017-01-16 20:41:20 +0900668
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200669 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100670 goto exit;
671 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200672 if (nargs < 1) {
673 goto skip_optional;
674 }
675 chars = args[0];
676skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900677 return_value = unicode_rstrip_impl(self, chars);
678
679exit:
680 return return_value;
681}
682
683PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200697 {"replace", (PyCFunction)(void(*)(void))unicode_replace, METH_FASTCALL, unicode_replace__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900698
699static PyObject *
700unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
701 Py_ssize_t count);
702
703static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200704unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900705{
706 PyObject *return_value = NULL;
707 PyObject *old;
708 PyObject *new;
709 Py_ssize_t count = -1;
710
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200711 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100712 goto exit;
713 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200714 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200715 _PyArg_BadArgument("replace", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200716 goto exit;
717 }
718 if (PyUnicode_READY(args[0]) == -1) {
719 goto exit;
720 }
721 old = args[0];
722 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200723 _PyArg_BadArgument("replace", "argument 2", "str", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200724 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 }
750skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900751 return_value = unicode_replace_impl(self, old, new, count);
752
753exit:
754 return return_value;
755}
756
sweeneydea81849b2020-04-22 17:05:48 -0400757PyDoc_STRVAR(unicode_removeprefix__doc__,
758"removeprefix($self, prefix, /)\n"
759"--\n"
760"\n"
761"Return a str with the given prefix string removed if present.\n"
762"\n"
763"If the string starts with the prefix string, return string[len(prefix):].\n"
764"Otherwise, return a copy of the original string.");
765
766#define UNICODE_REMOVEPREFIX_METHODDEF \
767 {"removeprefix", (PyCFunction)unicode_removeprefix, METH_O, unicode_removeprefix__doc__},
768
769static PyObject *
770unicode_removeprefix_impl(PyObject *self, PyObject *prefix);
771
772static PyObject *
773unicode_removeprefix(PyObject *self, PyObject *arg)
774{
775 PyObject *return_value = NULL;
776 PyObject *prefix;
777
778 if (!PyUnicode_Check(arg)) {
779 _PyArg_BadArgument("removeprefix", "argument", "str", arg);
780 goto exit;
781 }
782 if (PyUnicode_READY(arg) == -1) {
783 goto exit;
784 }
785 prefix = arg;
786 return_value = unicode_removeprefix_impl(self, prefix);
787
788exit:
789 return return_value;
790}
791
792PyDoc_STRVAR(unicode_removesuffix__doc__,
793"removesuffix($self, suffix, /)\n"
794"--\n"
795"\n"
796"Return a str with the given suffix string removed if present.\n"
797"\n"
798"If the string ends with the suffix string and that suffix is not empty,\n"
799"return string[:-len(suffix)]. Otherwise, return a copy of the original\n"
800"string.");
801
802#define UNICODE_REMOVESUFFIX_METHODDEF \
803 {"removesuffix", (PyCFunction)unicode_removesuffix, METH_O, unicode_removesuffix__doc__},
804
805static PyObject *
806unicode_removesuffix_impl(PyObject *self, PyObject *suffix);
807
808static PyObject *
809unicode_removesuffix(PyObject *self, PyObject *arg)
810{
811 PyObject *return_value = NULL;
812 PyObject *suffix;
813
814 if (!PyUnicode_Check(arg)) {
815 _PyArg_BadArgument("removesuffix", "argument", "str", arg);
816 goto exit;
817 }
818 if (PyUnicode_READY(arg) == -1) {
819 goto exit;
820 }
821 suffix = arg;
822 return_value = unicode_removesuffix_impl(self, suffix);
823
824exit:
825 return return_value;
826}
827
INADA Naoki3ae20562017-01-16 20:41:20 +0900828PyDoc_STRVAR(unicode_rjust__doc__,
829"rjust($self, width, fillchar=\' \', /)\n"
830"--\n"
831"\n"
832"Return a right-justified string of length width.\n"
833"\n"
834"Padding is done using the specified fill character (default is a space).");
835
836#define UNICODE_RJUST_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200837 {"rjust", (PyCFunction)(void(*)(void))unicode_rjust, METH_FASTCALL, unicode_rjust__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900838
839static PyObject *
840unicode_rjust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
841
842static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200843unicode_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900844{
845 PyObject *return_value = NULL;
846 Py_ssize_t width;
847 Py_UCS4 fillchar = ' ';
848
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200849 if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100850 goto exit;
851 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200852 if (PyFloat_Check(args[0])) {
853 PyErr_SetString(PyExc_TypeError,
854 "integer argument expected, got float" );
855 goto exit;
856 }
857 {
858 Py_ssize_t ival = -1;
859 PyObject *iobj = PyNumber_Index(args[0]);
860 if (iobj != NULL) {
861 ival = PyLong_AsSsize_t(iobj);
862 Py_DECREF(iobj);
863 }
864 if (ival == -1 && PyErr_Occurred()) {
865 goto exit;
866 }
867 width = ival;
868 }
869 if (nargs < 2) {
870 goto skip_optional;
871 }
872 if (!convert_uc(args[1], &fillchar)) {
873 goto exit;
874 }
875skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900876 return_value = unicode_rjust_impl(self, width, fillchar);
877
878exit:
879 return return_value;
880}
881
882PyDoc_STRVAR(unicode_split__doc__,
883"split($self, /, sep=None, maxsplit=-1)\n"
884"--\n"
885"\n"
886"Return a list of the words in the string, using sep as the delimiter string.\n"
887"\n"
888" sep\n"
889" The delimiter according which to split the string.\n"
890" None (the default value) means split according to any whitespace,\n"
891" and discard empty strings from the result.\n"
892" maxsplit\n"
893" Maximum number of splits to do.\n"
894" -1 (the default value) means no limit.");
895
896#define UNICODE_SPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200897 {"split", (PyCFunction)(void(*)(void))unicode_split, METH_FASTCALL|METH_KEYWORDS, unicode_split__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900898
899static PyObject *
900unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
901
902static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200903unicode_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +0900904{
905 PyObject *return_value = NULL;
906 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200907 static _PyArg_Parser _parser = {NULL, _keywords, "split", 0};
908 PyObject *argsbuf[2];
909 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
INADA Naoki3ae20562017-01-16 20:41:20 +0900910 PyObject *sep = Py_None;
911 Py_ssize_t maxsplit = -1;
912
Serhiy Storchaka31913912019-03-14 10:32:22 +0200913 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
914 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +0900915 goto exit;
916 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200917 if (!noptargs) {
918 goto skip_optional_pos;
919 }
920 if (args[0]) {
921 sep = args[0];
922 if (!--noptargs) {
923 goto skip_optional_pos;
924 }
925 }
926 if (PyFloat_Check(args[1])) {
927 PyErr_SetString(PyExc_TypeError,
928 "integer argument expected, got float" );
929 goto exit;
930 }
931 {
932 Py_ssize_t ival = -1;
933 PyObject *iobj = PyNumber_Index(args[1]);
934 if (iobj != NULL) {
935 ival = PyLong_AsSsize_t(iobj);
936 Py_DECREF(iobj);
937 }
938 if (ival == -1 && PyErr_Occurred()) {
939 goto exit;
940 }
941 maxsplit = ival;
942 }
943skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +0900944 return_value = unicode_split_impl(self, sep, maxsplit);
945
946exit:
947 return return_value;
948}
949
950PyDoc_STRVAR(unicode_partition__doc__,
951"partition($self, sep, /)\n"
952"--\n"
953"\n"
954"Partition the string into three parts using the given separator.\n"
955"\n"
956"This will search for the separator in the string. If the separator is found,\n"
957"returns a 3-tuple containing the part before the separator, the separator\n"
958"itself, and the part after it.\n"
959"\n"
960"If the separator is not found, returns a 3-tuple containing the original string\n"
961"and two empty strings.");
962
963#define UNICODE_PARTITION_METHODDEF \
964 {"partition", (PyCFunction)unicode_partition, METH_O, unicode_partition__doc__},
965
966PyDoc_STRVAR(unicode_rpartition__doc__,
967"rpartition($self, sep, /)\n"
968"--\n"
969"\n"
970"Partition the string into three parts using the given separator.\n"
971"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300972"This will search for the separator in the string, starting at the end. If\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900973"the separator is found, returns a 3-tuple containing the part before the\n"
974"separator, the separator itself, and the part after it.\n"
975"\n"
976"If the separator is not found, returns a 3-tuple containing two empty strings\n"
977"and the original string.");
978
979#define UNICODE_RPARTITION_METHODDEF \
980 {"rpartition", (PyCFunction)unicode_rpartition, METH_O, unicode_rpartition__doc__},
981
982PyDoc_STRVAR(unicode_rsplit__doc__,
983"rsplit($self, /, sep=None, maxsplit=-1)\n"
984"--\n"
985"\n"
986"Return a list of the words in the string, using sep as the delimiter string.\n"
987"\n"
988" sep\n"
989" The delimiter according which to split the string.\n"
990" None (the default value) means split according to any whitespace,\n"
991" and discard empty strings from the result.\n"
992" maxsplit\n"
993" Maximum number of splits to do.\n"
994" -1 (the default value) means no limit.\n"
995"\n"
996"Splits are done starting at the end of the string and working to the front.");
997
998#define UNICODE_RSPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200999 {"rsplit", (PyCFunction)(void(*)(void))unicode_rsplit, METH_FASTCALL|METH_KEYWORDS, unicode_rsplit__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +09001000
1001static PyObject *
1002unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
1003
1004static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001005unicode_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +09001006{
1007 PyObject *return_value = NULL;
1008 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001009 static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0};
1010 PyObject *argsbuf[2];
1011 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
INADA Naoki3ae20562017-01-16 20:41:20 +09001012 PyObject *sep = Py_None;
1013 Py_ssize_t maxsplit = -1;
1014
Serhiy Storchaka31913912019-03-14 10:32:22 +02001015 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
1016 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +09001017 goto exit;
1018 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001019 if (!noptargs) {
1020 goto skip_optional_pos;
1021 }
1022 if (args[0]) {
1023 sep = args[0];
1024 if (!--noptargs) {
1025 goto skip_optional_pos;
1026 }
1027 }
1028 if (PyFloat_Check(args[1])) {
1029 PyErr_SetString(PyExc_TypeError,
1030 "integer argument expected, got float" );
1031 goto exit;
1032 }
1033 {
1034 Py_ssize_t ival = -1;
1035 PyObject *iobj = PyNumber_Index(args[1]);
1036 if (iobj != NULL) {
1037 ival = PyLong_AsSsize_t(iobj);
1038 Py_DECREF(iobj);
1039 }
1040 if (ival == -1 && PyErr_Occurred()) {
1041 goto exit;
1042 }
1043 maxsplit = ival;
1044 }
1045skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +09001046 return_value = unicode_rsplit_impl(self, sep, maxsplit);
1047
1048exit:
1049 return return_value;
1050}
1051
1052PyDoc_STRVAR(unicode_splitlines__doc__,
1053"splitlines($self, /, keepends=False)\n"
1054"--\n"
1055"\n"
1056"Return a list of the lines in the string, breaking at line boundaries.\n"
1057"\n"
1058"Line breaks are not included in the resulting list unless keepends is given and\n"
1059"true.");
1060
1061#define UNICODE_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001062 {"splitlines", (PyCFunction)(void(*)(void))unicode_splitlines, METH_FASTCALL|METH_KEYWORDS, unicode_splitlines__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +09001063
1064static PyObject *
1065unicode_splitlines_impl(PyObject *self, int keepends);
1066
1067static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001068unicode_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +09001069{
1070 PyObject *return_value = NULL;
1071 static const char * const _keywords[] = {"keepends", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001072 static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0};
1073 PyObject *argsbuf[1];
1074 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
INADA Naoki3ae20562017-01-16 20:41:20 +09001075 int keepends = 0;
1076
Serhiy Storchaka31913912019-03-14 10:32:22 +02001077 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
1078 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +09001079 goto exit;
1080 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001081 if (!noptargs) {
1082 goto skip_optional_pos;
1083 }
1084 if (PyFloat_Check(args[0])) {
1085 PyErr_SetString(PyExc_TypeError,
1086 "integer argument expected, got float" );
1087 goto exit;
1088 }
1089 keepends = _PyLong_AsInt(args[0]);
1090 if (keepends == -1 && PyErr_Occurred()) {
1091 goto exit;
1092 }
1093skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +09001094 return_value = unicode_splitlines_impl(self, keepends);
1095
1096exit:
1097 return return_value;
1098}
1099
1100PyDoc_STRVAR(unicode_swapcase__doc__,
1101"swapcase($self, /)\n"
1102"--\n"
1103"\n"
1104"Convert uppercase characters to lowercase and lowercase characters to uppercase.");
1105
1106#define UNICODE_SWAPCASE_METHODDEF \
1107 {"swapcase", (PyCFunction)unicode_swapcase, METH_NOARGS, unicode_swapcase__doc__},
1108
1109static PyObject *
1110unicode_swapcase_impl(PyObject *self);
1111
1112static PyObject *
1113unicode_swapcase(PyObject *self, PyObject *Py_UNUSED(ignored))
1114{
1115 return unicode_swapcase_impl(self);
1116}
1117
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001118PyDoc_STRVAR(unicode_maketrans__doc__,
Serhiy Storchaka279f4462019-09-14 12:24:05 +03001119"maketrans(x, y=<unrepresentable>, z=<unrepresentable>, /)\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001120"--\n"
1121"\n"
1122"Return a translation table usable for str.translate().\n"
1123"\n"
1124"If there is only one argument, it must be a dictionary mapping Unicode\n"
1125"ordinals (integers) or characters to Unicode ordinals, strings or None.\n"
1126"Character keys will be then converted to ordinals.\n"
1127"If there are two arguments, they must be strings of equal length, and\n"
1128"in the resulting dictionary, each character in x will be mapped to the\n"
1129"character at the same position in y. If there is a third argument, it\n"
1130"must be a string, whose characters will be mapped to None in the result.");
1131
1132#define UNICODE_MAKETRANS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001133 {"maketrans", (PyCFunction)(void(*)(void))unicode_maketrans, METH_FASTCALL|METH_STATIC, unicode_maketrans__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001134
1135static PyObject *
1136unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z);
1137
1138static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001139unicode_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001140{
1141 PyObject *return_value = NULL;
1142 PyObject *x;
1143 PyObject *y = NULL;
1144 PyObject *z = NULL;
1145
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001146 if (!_PyArg_CheckPositional("maketrans", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001147 goto exit;
1148 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001149 x = args[0];
1150 if (nargs < 2) {
1151 goto skip_optional;
1152 }
1153 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001154 _PyArg_BadArgument("maketrans", "argument 2", "str", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001155 goto exit;
1156 }
1157 if (PyUnicode_READY(args[1]) == -1) {
1158 goto exit;
1159 }
1160 y = args[1];
1161 if (nargs < 3) {
1162 goto skip_optional;
1163 }
1164 if (!PyUnicode_Check(args[2])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001165 _PyArg_BadArgument("maketrans", "argument 3", "str", args[2]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001166 goto exit;
1167 }
1168 if (PyUnicode_READY(args[2]) == -1) {
1169 goto exit;
1170 }
1171 z = args[2];
1172skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001173 return_value = unicode_maketrans_impl(x, y, z);
1174
1175exit:
1176 return return_value;
1177}
INADA Naoki3ae20562017-01-16 20:41:20 +09001178
1179PyDoc_STRVAR(unicode_translate__doc__,
1180"translate($self, table, /)\n"
1181"--\n"
1182"\n"
1183"Replace each character in the string using the given translation table.\n"
1184"\n"
1185" table\n"
1186" Translation table, which must be a mapping of Unicode ordinals to\n"
1187" Unicode ordinals, strings, or None.\n"
1188"\n"
1189"The table must implement lookup/indexing via __getitem__, for instance a\n"
1190"dictionary or list. If this operation raises LookupError, the character is\n"
1191"left untouched. Characters mapped to None are deleted.");
1192
1193#define UNICODE_TRANSLATE_METHODDEF \
1194 {"translate", (PyCFunction)unicode_translate, METH_O, unicode_translate__doc__},
1195
1196PyDoc_STRVAR(unicode_upper__doc__,
1197"upper($self, /)\n"
1198"--\n"
1199"\n"
1200"Return a copy of the string converted to uppercase.");
1201
1202#define UNICODE_UPPER_METHODDEF \
1203 {"upper", (PyCFunction)unicode_upper, METH_NOARGS, unicode_upper__doc__},
1204
1205static PyObject *
1206unicode_upper_impl(PyObject *self);
1207
1208static PyObject *
1209unicode_upper(PyObject *self, PyObject *Py_UNUSED(ignored))
1210{
1211 return unicode_upper_impl(self);
1212}
1213
1214PyDoc_STRVAR(unicode_zfill__doc__,
1215"zfill($self, width, /)\n"
1216"--\n"
1217"\n"
1218"Pad a numeric string with zeros on the left, to fill a field of the given width.\n"
1219"\n"
INADA Naoki15f94592017-01-16 21:49:13 +09001220"The string is never truncated.");
INADA Naoki3ae20562017-01-16 20:41:20 +09001221
1222#define UNICODE_ZFILL_METHODDEF \
1223 {"zfill", (PyCFunction)unicode_zfill, METH_O, unicode_zfill__doc__},
1224
1225static PyObject *
1226unicode_zfill_impl(PyObject *self, Py_ssize_t width);
1227
1228static PyObject *
1229unicode_zfill(PyObject *self, PyObject *arg)
1230{
1231 PyObject *return_value = NULL;
1232 Py_ssize_t width;
1233
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001234 if (PyFloat_Check(arg)) {
1235 PyErr_SetString(PyExc_TypeError,
1236 "integer argument expected, got float" );
INADA Naoki3ae20562017-01-16 20:41:20 +09001237 goto exit;
1238 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001239 {
1240 Py_ssize_t ival = -1;
1241 PyObject *iobj = PyNumber_Index(arg);
1242 if (iobj != NULL) {
1243 ival = PyLong_AsSsize_t(iobj);
1244 Py_DECREF(iobj);
1245 }
1246 if (ival == -1 && PyErr_Occurred()) {
1247 goto exit;
1248 }
1249 width = ival;
1250 }
INADA Naoki3ae20562017-01-16 20:41:20 +09001251 return_value = unicode_zfill_impl(self, width);
1252
1253exit:
1254 return return_value;
1255}
1256
1257PyDoc_STRVAR(unicode___format____doc__,
1258"__format__($self, format_spec, /)\n"
1259"--\n"
1260"\n"
INADA Naoki15f94592017-01-16 21:49:13 +09001261"Return a formatted version of the string as described by format_spec.");
INADA Naoki3ae20562017-01-16 20:41:20 +09001262
1263#define UNICODE___FORMAT___METHODDEF \
1264 {"__format__", (PyCFunction)unicode___format__, METH_O, unicode___format____doc__},
1265
1266static PyObject *
1267unicode___format___impl(PyObject *self, PyObject *format_spec);
1268
1269static PyObject *
1270unicode___format__(PyObject *self, PyObject *arg)
1271{
1272 PyObject *return_value = NULL;
1273 PyObject *format_spec;
1274
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001275 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001276 _PyArg_BadArgument("__format__", "argument", "str", arg);
INADA Naoki3ae20562017-01-16 20:41:20 +09001277 goto exit;
1278 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001279 if (PyUnicode_READY(arg) == -1) {
1280 goto exit;
1281 }
1282 format_spec = arg;
INADA Naoki3ae20562017-01-16 20:41:20 +09001283 return_value = unicode___format___impl(self, format_spec);
1284
1285exit:
1286 return return_value;
1287}
1288
1289PyDoc_STRVAR(unicode_sizeof__doc__,
1290"__sizeof__($self, /)\n"
1291"--\n"
1292"\n"
1293"Return the size of the string in memory, in bytes.");
1294
1295#define UNICODE_SIZEOF_METHODDEF \
1296 {"__sizeof__", (PyCFunction)unicode_sizeof, METH_NOARGS, unicode_sizeof__doc__},
1297
1298static PyObject *
1299unicode_sizeof_impl(PyObject *self);
1300
1301static PyObject *
1302unicode_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored))
1303{
1304 return unicode_sizeof_impl(self);
1305}
sweeneydea81849b2020-04-22 17:05:48 -04001306/*[clinic end generated code: output=b91233f3722643be input=a9049054013a1b77]*/