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