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