blob: 0d134064bab0917aaff72bc5dd8353487c4e988d [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])) {
Serhiy Storchaka96631dc2019-08-29 18:29:59 +0300160 _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])) {
Serhiy Storchaka96631dc2019-08-29 18:29:59 +0300177 _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"
Miss Islington (bot)0baa6b32019-10-09 14:55:39 -0700585"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 Storchakad322abb2019-09-14 13:31:50 +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 Storchakad322abb2019-09-14 13:31:50 +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])) {
Serhiy Storchaka96631dc2019-08-29 18:29:59 +0300715 _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])) {
Serhiy Storchaka96631dc2019-08-29 18:29:59 +0300723 _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
757PyDoc_STRVAR(unicode_rjust__doc__,
758"rjust($self, width, fillchar=\' \', /)\n"
759"--\n"
760"\n"
761"Return a right-justified string of length width.\n"
762"\n"
763"Padding is done using the specified fill character (default is a space).");
764
765#define UNICODE_RJUST_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200766 {"rjust", (PyCFunction)(void(*)(void))unicode_rjust, METH_FASTCALL, unicode_rjust__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900767
768static PyObject *
769unicode_rjust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
770
771static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200772unicode_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
INADA Naoki3ae20562017-01-16 20:41:20 +0900773{
774 PyObject *return_value = NULL;
775 Py_ssize_t width;
776 Py_UCS4 fillchar = ' ';
777
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200778 if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100779 goto exit;
780 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200781 if (PyFloat_Check(args[0])) {
782 PyErr_SetString(PyExc_TypeError,
783 "integer argument expected, got float" );
784 goto exit;
785 }
786 {
787 Py_ssize_t ival = -1;
788 PyObject *iobj = PyNumber_Index(args[0]);
789 if (iobj != NULL) {
790 ival = PyLong_AsSsize_t(iobj);
791 Py_DECREF(iobj);
792 }
793 if (ival == -1 && PyErr_Occurred()) {
794 goto exit;
795 }
796 width = ival;
797 }
798 if (nargs < 2) {
799 goto skip_optional;
800 }
801 if (!convert_uc(args[1], &fillchar)) {
802 goto exit;
803 }
804skip_optional:
INADA Naoki3ae20562017-01-16 20:41:20 +0900805 return_value = unicode_rjust_impl(self, width, fillchar);
806
807exit:
808 return return_value;
809}
810
811PyDoc_STRVAR(unicode_split__doc__,
812"split($self, /, sep=None, maxsplit=-1)\n"
813"--\n"
814"\n"
815"Return a list of the words in the string, using sep as the delimiter string.\n"
816"\n"
817" sep\n"
818" The delimiter according which to split the string.\n"
819" None (the default value) means split according to any whitespace,\n"
820" and discard empty strings from the result.\n"
821" maxsplit\n"
822" Maximum number of splits to do.\n"
823" -1 (the default value) means no limit.");
824
825#define UNICODE_SPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200826 {"split", (PyCFunction)(void(*)(void))unicode_split, METH_FASTCALL|METH_KEYWORDS, unicode_split__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900827
828static PyObject *
829unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
830
831static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200832unicode_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +0900833{
834 PyObject *return_value = NULL;
835 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200836 static _PyArg_Parser _parser = {NULL, _keywords, "split", 0};
837 PyObject *argsbuf[2];
838 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
INADA Naoki3ae20562017-01-16 20:41:20 +0900839 PyObject *sep = Py_None;
840 Py_ssize_t maxsplit = -1;
841
Serhiy Storchaka31913912019-03-14 10:32:22 +0200842 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
843 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +0900844 goto exit;
845 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200846 if (!noptargs) {
847 goto skip_optional_pos;
848 }
849 if (args[0]) {
850 sep = args[0];
851 if (!--noptargs) {
852 goto skip_optional_pos;
853 }
854 }
855 if (PyFloat_Check(args[1])) {
856 PyErr_SetString(PyExc_TypeError,
857 "integer argument expected, got float" );
858 goto exit;
859 }
860 {
861 Py_ssize_t ival = -1;
862 PyObject *iobj = PyNumber_Index(args[1]);
863 if (iobj != NULL) {
864 ival = PyLong_AsSsize_t(iobj);
865 Py_DECREF(iobj);
866 }
867 if (ival == -1 && PyErr_Occurred()) {
868 goto exit;
869 }
870 maxsplit = ival;
871 }
872skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +0900873 return_value = unicode_split_impl(self, sep, maxsplit);
874
875exit:
876 return return_value;
877}
878
879PyDoc_STRVAR(unicode_partition__doc__,
880"partition($self, sep, /)\n"
881"--\n"
882"\n"
883"Partition the string into three parts using the given separator.\n"
884"\n"
885"This will search for the separator in the string. If the separator is found,\n"
886"returns a 3-tuple containing the part before the separator, the separator\n"
887"itself, and the part after it.\n"
888"\n"
889"If the separator is not found, returns a 3-tuple containing the original string\n"
890"and two empty strings.");
891
892#define UNICODE_PARTITION_METHODDEF \
893 {"partition", (PyCFunction)unicode_partition, METH_O, unicode_partition__doc__},
894
895PyDoc_STRVAR(unicode_rpartition__doc__,
896"rpartition($self, sep, /)\n"
897"--\n"
898"\n"
899"Partition the string into three parts using the given separator.\n"
900"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300901"This will search for the separator in the string, starting at the end. If\n"
INADA Naoki3ae20562017-01-16 20:41:20 +0900902"the separator is found, returns a 3-tuple containing the part before the\n"
903"separator, the separator itself, and the part after it.\n"
904"\n"
905"If the separator is not found, returns a 3-tuple containing two empty strings\n"
906"and the original string.");
907
908#define UNICODE_RPARTITION_METHODDEF \
909 {"rpartition", (PyCFunction)unicode_rpartition, METH_O, unicode_rpartition__doc__},
910
911PyDoc_STRVAR(unicode_rsplit__doc__,
912"rsplit($self, /, sep=None, maxsplit=-1)\n"
913"--\n"
914"\n"
915"Return a list of the words in the string, using sep as the delimiter string.\n"
916"\n"
917" sep\n"
918" The delimiter according which to split the string.\n"
919" None (the default value) means split according to any whitespace,\n"
920" and discard empty strings from the result.\n"
921" maxsplit\n"
922" Maximum number of splits to do.\n"
923" -1 (the default value) means no limit.\n"
924"\n"
925"Splits are done starting at the end of the string and working to the front.");
926
927#define UNICODE_RSPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200928 {"rsplit", (PyCFunction)(void(*)(void))unicode_rsplit, METH_FASTCALL|METH_KEYWORDS, unicode_rsplit__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900929
930static PyObject *
931unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
932
933static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200934unicode_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +0900935{
936 PyObject *return_value = NULL;
937 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200938 static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0};
939 PyObject *argsbuf[2];
940 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
INADA Naoki3ae20562017-01-16 20:41:20 +0900941 PyObject *sep = Py_None;
942 Py_ssize_t maxsplit = -1;
943
Serhiy Storchaka31913912019-03-14 10:32:22 +0200944 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
945 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +0900946 goto exit;
947 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200948 if (!noptargs) {
949 goto skip_optional_pos;
950 }
951 if (args[0]) {
952 sep = args[0];
953 if (!--noptargs) {
954 goto skip_optional_pos;
955 }
956 }
957 if (PyFloat_Check(args[1])) {
958 PyErr_SetString(PyExc_TypeError,
959 "integer argument expected, got float" );
960 goto exit;
961 }
962 {
963 Py_ssize_t ival = -1;
964 PyObject *iobj = PyNumber_Index(args[1]);
965 if (iobj != NULL) {
966 ival = PyLong_AsSsize_t(iobj);
967 Py_DECREF(iobj);
968 }
969 if (ival == -1 && PyErr_Occurred()) {
970 goto exit;
971 }
972 maxsplit = ival;
973 }
974skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +0900975 return_value = unicode_rsplit_impl(self, sep, maxsplit);
976
977exit:
978 return return_value;
979}
980
981PyDoc_STRVAR(unicode_splitlines__doc__,
982"splitlines($self, /, keepends=False)\n"
983"--\n"
984"\n"
985"Return a list of the lines in the string, breaking at line boundaries.\n"
986"\n"
987"Line breaks are not included in the resulting list unless keepends is given and\n"
988"true.");
989
990#define UNICODE_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200991 {"splitlines", (PyCFunction)(void(*)(void))unicode_splitlines, METH_FASTCALL|METH_KEYWORDS, unicode_splitlines__doc__},
INADA Naoki3ae20562017-01-16 20:41:20 +0900992
993static PyObject *
994unicode_splitlines_impl(PyObject *self, int keepends);
995
996static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200997unicode_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
INADA Naoki3ae20562017-01-16 20:41:20 +0900998{
999 PyObject *return_value = NULL;
1000 static const char * const _keywords[] = {"keepends", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001001 static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0};
1002 PyObject *argsbuf[1];
1003 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
INADA Naoki3ae20562017-01-16 20:41:20 +09001004 int keepends = 0;
1005
Serhiy Storchaka31913912019-03-14 10:32:22 +02001006 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
1007 if (!args) {
INADA Naoki3ae20562017-01-16 20:41:20 +09001008 goto exit;
1009 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001010 if (!noptargs) {
1011 goto skip_optional_pos;
1012 }
1013 if (PyFloat_Check(args[0])) {
1014 PyErr_SetString(PyExc_TypeError,
1015 "integer argument expected, got float" );
1016 goto exit;
1017 }
1018 keepends = _PyLong_AsInt(args[0]);
1019 if (keepends == -1 && PyErr_Occurred()) {
1020 goto exit;
1021 }
1022skip_optional_pos:
INADA Naoki3ae20562017-01-16 20:41:20 +09001023 return_value = unicode_splitlines_impl(self, keepends);
1024
1025exit:
1026 return return_value;
1027}
1028
1029PyDoc_STRVAR(unicode_swapcase__doc__,
1030"swapcase($self, /)\n"
1031"--\n"
1032"\n"
1033"Convert uppercase characters to lowercase and lowercase characters to uppercase.");
1034
1035#define UNICODE_SWAPCASE_METHODDEF \
1036 {"swapcase", (PyCFunction)unicode_swapcase, METH_NOARGS, unicode_swapcase__doc__},
1037
1038static PyObject *
1039unicode_swapcase_impl(PyObject *self);
1040
1041static PyObject *
1042unicode_swapcase(PyObject *self, PyObject *Py_UNUSED(ignored))
1043{
1044 return unicode_swapcase_impl(self);
1045}
1046
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001047PyDoc_STRVAR(unicode_maketrans__doc__,
Serhiy Storchakad322abb2019-09-14 13:31:50 +03001048"maketrans(x, y=<unrepresentable>, z=<unrepresentable>, /)\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001049"--\n"
1050"\n"
1051"Return a translation table usable for str.translate().\n"
1052"\n"
1053"If there is only one argument, it must be a dictionary mapping Unicode\n"
1054"ordinals (integers) or characters to Unicode ordinals, strings or None.\n"
1055"Character keys will be then converted to ordinals.\n"
1056"If there are two arguments, they must be strings of equal length, and\n"
1057"in the resulting dictionary, each character in x will be mapped to the\n"
1058"character at the same position in y. If there is a third argument, it\n"
1059"must be a string, whose characters will be mapped to None in the result.");
1060
1061#define UNICODE_MAKETRANS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001062 {"maketrans", (PyCFunction)(void(*)(void))unicode_maketrans, METH_FASTCALL|METH_STATIC, unicode_maketrans__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001063
1064static PyObject *
1065unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z);
1066
1067static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001068unicode_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001069{
1070 PyObject *return_value = NULL;
1071 PyObject *x;
1072 PyObject *y = NULL;
1073 PyObject *z = NULL;
1074
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001075 if (!_PyArg_CheckPositional("maketrans", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001076 goto exit;
1077 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001078 x = args[0];
1079 if (nargs < 2) {
1080 goto skip_optional;
1081 }
1082 if (!PyUnicode_Check(args[1])) {
Serhiy Storchaka96631dc2019-08-29 18:29:59 +03001083 _PyArg_BadArgument("maketrans", "argument 2", "str", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001084 goto exit;
1085 }
1086 if (PyUnicode_READY(args[1]) == -1) {
1087 goto exit;
1088 }
1089 y = args[1];
1090 if (nargs < 3) {
1091 goto skip_optional;
1092 }
1093 if (!PyUnicode_Check(args[2])) {
Serhiy Storchaka96631dc2019-08-29 18:29:59 +03001094 _PyArg_BadArgument("maketrans", "argument 3", "str", args[2]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001095 goto exit;
1096 }
1097 if (PyUnicode_READY(args[2]) == -1) {
1098 goto exit;
1099 }
1100 z = args[2];
1101skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001102 return_value = unicode_maketrans_impl(x, y, z);
1103
1104exit:
1105 return return_value;
1106}
INADA Naoki3ae20562017-01-16 20:41:20 +09001107
1108PyDoc_STRVAR(unicode_translate__doc__,
1109"translate($self, table, /)\n"
1110"--\n"
1111"\n"
1112"Replace each character in the string using the given translation table.\n"
1113"\n"
1114" table\n"
1115" Translation table, which must be a mapping of Unicode ordinals to\n"
1116" Unicode ordinals, strings, or None.\n"
1117"\n"
1118"The table must implement lookup/indexing via __getitem__, for instance a\n"
1119"dictionary or list. If this operation raises LookupError, the character is\n"
1120"left untouched. Characters mapped to None are deleted.");
1121
1122#define UNICODE_TRANSLATE_METHODDEF \
1123 {"translate", (PyCFunction)unicode_translate, METH_O, unicode_translate__doc__},
1124
1125PyDoc_STRVAR(unicode_upper__doc__,
1126"upper($self, /)\n"
1127"--\n"
1128"\n"
1129"Return a copy of the string converted to uppercase.");
1130
1131#define UNICODE_UPPER_METHODDEF \
1132 {"upper", (PyCFunction)unicode_upper, METH_NOARGS, unicode_upper__doc__},
1133
1134static PyObject *
1135unicode_upper_impl(PyObject *self);
1136
1137static PyObject *
1138unicode_upper(PyObject *self, PyObject *Py_UNUSED(ignored))
1139{
1140 return unicode_upper_impl(self);
1141}
1142
1143PyDoc_STRVAR(unicode_zfill__doc__,
1144"zfill($self, width, /)\n"
1145"--\n"
1146"\n"
1147"Pad a numeric string with zeros on the left, to fill a field of the given width.\n"
1148"\n"
INADA Naoki15f94592017-01-16 21:49:13 +09001149"The string is never truncated.");
INADA Naoki3ae20562017-01-16 20:41:20 +09001150
1151#define UNICODE_ZFILL_METHODDEF \
1152 {"zfill", (PyCFunction)unicode_zfill, METH_O, unicode_zfill__doc__},
1153
1154static PyObject *
1155unicode_zfill_impl(PyObject *self, Py_ssize_t width);
1156
1157static PyObject *
1158unicode_zfill(PyObject *self, PyObject *arg)
1159{
1160 PyObject *return_value = NULL;
1161 Py_ssize_t width;
1162
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001163 if (PyFloat_Check(arg)) {
1164 PyErr_SetString(PyExc_TypeError,
1165 "integer argument expected, got float" );
INADA Naoki3ae20562017-01-16 20:41:20 +09001166 goto exit;
1167 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001168 {
1169 Py_ssize_t ival = -1;
1170 PyObject *iobj = PyNumber_Index(arg);
1171 if (iobj != NULL) {
1172 ival = PyLong_AsSsize_t(iobj);
1173 Py_DECREF(iobj);
1174 }
1175 if (ival == -1 && PyErr_Occurred()) {
1176 goto exit;
1177 }
1178 width = ival;
1179 }
INADA Naoki3ae20562017-01-16 20:41:20 +09001180 return_value = unicode_zfill_impl(self, width);
1181
1182exit:
1183 return return_value;
1184}
1185
1186PyDoc_STRVAR(unicode___format____doc__,
1187"__format__($self, format_spec, /)\n"
1188"--\n"
1189"\n"
INADA Naoki15f94592017-01-16 21:49:13 +09001190"Return a formatted version of the string as described by format_spec.");
INADA Naoki3ae20562017-01-16 20:41:20 +09001191
1192#define UNICODE___FORMAT___METHODDEF \
1193 {"__format__", (PyCFunction)unicode___format__, METH_O, unicode___format____doc__},
1194
1195static PyObject *
1196unicode___format___impl(PyObject *self, PyObject *format_spec);
1197
1198static PyObject *
1199unicode___format__(PyObject *self, PyObject *arg)
1200{
1201 PyObject *return_value = NULL;
1202 PyObject *format_spec;
1203
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001204 if (!PyUnicode_Check(arg)) {
Serhiy Storchaka96631dc2019-08-29 18:29:59 +03001205 _PyArg_BadArgument("__format__", "argument", "str", arg);
INADA Naoki3ae20562017-01-16 20:41:20 +09001206 goto exit;
1207 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001208 if (PyUnicode_READY(arg) == -1) {
1209 goto exit;
1210 }
1211 format_spec = arg;
INADA Naoki3ae20562017-01-16 20:41:20 +09001212 return_value = unicode___format___impl(self, format_spec);
1213
1214exit:
1215 return return_value;
1216}
1217
1218PyDoc_STRVAR(unicode_sizeof__doc__,
1219"__sizeof__($self, /)\n"
1220"--\n"
1221"\n"
1222"Return the size of the string in memory, in bytes.");
1223
1224#define UNICODE_SIZEOF_METHODDEF \
1225 {"__sizeof__", (PyCFunction)unicode_sizeof, METH_NOARGS, unicode_sizeof__doc__},
1226
1227static PyObject *
1228unicode_sizeof_impl(PyObject *self);
1229
1230static PyObject *
1231unicode_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored))
1232{
1233 return unicode_sizeof_impl(self);
1234}
Miss Islington (bot)0baa6b32019-10-09 14:55:39 -07001235/*[clinic end generated code: output=e4ed33400979c7e8 input=a9049054013a1b77]*/