Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 5 | PyDoc_STRVAR(unicode_title__doc__, |
| 6 | "title($self, /)\n" |
| 7 | "--\n" |
| 8 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 9 | "Return a version of the string where each word is titlecased.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 10 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 11 | "More specifically, words start with uppercased characters and all remaining\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 12 | "cased characters have lower case."); |
| 13 | |
| 14 | #define UNICODE_TITLE_METHODDEF \ |
| 15 | {"title", (PyCFunction)unicode_title, METH_NOARGS, unicode_title__doc__}, |
| 16 | |
| 17 | static PyObject * |
| 18 | unicode_title_impl(PyObject *self); |
| 19 | |
| 20 | static PyObject * |
| 21 | unicode_title(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 22 | { |
| 23 | return unicode_title_impl(self); |
| 24 | } |
| 25 | |
| 26 | PyDoc_STRVAR(unicode_capitalize__doc__, |
| 27 | "capitalize($self, /)\n" |
| 28 | "--\n" |
| 29 | "\n" |
| 30 | "Return a capitalized version of the string.\n" |
| 31 | "\n" |
| 32 | "More specifically, make the first character have upper case and the rest lower\n" |
| 33 | "case."); |
| 34 | |
| 35 | #define UNICODE_CAPITALIZE_METHODDEF \ |
| 36 | {"capitalize", (PyCFunction)unicode_capitalize, METH_NOARGS, unicode_capitalize__doc__}, |
| 37 | |
| 38 | static PyObject * |
| 39 | unicode_capitalize_impl(PyObject *self); |
| 40 | |
| 41 | static PyObject * |
| 42 | unicode_capitalize(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 43 | { |
| 44 | return unicode_capitalize_impl(self); |
| 45 | } |
| 46 | |
| 47 | PyDoc_STRVAR(unicode_casefold__doc__, |
| 48 | "casefold($self, /)\n" |
| 49 | "--\n" |
| 50 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 51 | "Return a version of the string suitable for caseless comparisons."); |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 52 | |
| 53 | #define UNICODE_CASEFOLD_METHODDEF \ |
| 54 | {"casefold", (PyCFunction)unicode_casefold, METH_NOARGS, unicode_casefold__doc__}, |
| 55 | |
| 56 | static PyObject * |
| 57 | unicode_casefold_impl(PyObject *self); |
| 58 | |
| 59 | static PyObject * |
| 60 | unicode_casefold(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 61 | { |
| 62 | return unicode_casefold_impl(self); |
| 63 | } |
| 64 | |
| 65 | PyDoc_STRVAR(unicode_center__doc__, |
| 66 | "center($self, width, fillchar=\' \', /)\n" |
| 67 | "--\n" |
| 68 | "\n" |
| 69 | "Return a centered string of length width.\n" |
| 70 | "\n" |
| 71 | "Padding is done using the specified fill character (default is a space)."); |
| 72 | |
| 73 | #define UNICODE_CENTER_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 74 | {"center", (PyCFunction)(void(*)(void))unicode_center, METH_FASTCALL, unicode_center__doc__}, |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 75 | |
| 76 | static PyObject * |
| 77 | unicode_center_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar); |
| 78 | |
| 79 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 80 | unicode_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 81 | { |
| 82 | PyObject *return_value = NULL; |
| 83 | Py_ssize_t width; |
| 84 | Py_UCS4 fillchar = ' '; |
| 85 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 86 | if (!_PyArg_CheckPositional("center", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 87 | goto exit; |
| 88 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 89 | { |
| 90 | Py_ssize_t ival = -1; |
| 91 | PyObject *iobj = PyNumber_Index(args[0]); |
| 92 | if (iobj != NULL) { |
| 93 | ival = PyLong_AsSsize_t(iobj); |
| 94 | Py_DECREF(iobj); |
| 95 | } |
| 96 | if (ival == -1 && PyErr_Occurred()) { |
| 97 | goto exit; |
| 98 | } |
| 99 | width = ival; |
| 100 | } |
| 101 | if (nargs < 2) { |
| 102 | goto skip_optional; |
| 103 | } |
| 104 | if (!convert_uc(args[1], &fillchar)) { |
| 105 | goto exit; |
| 106 | } |
| 107 | skip_optional: |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 108 | return_value = unicode_center_impl(self, width, fillchar); |
| 109 | |
| 110 | exit: |
| 111 | return return_value; |
| 112 | } |
| 113 | |
| 114 | PyDoc_STRVAR(unicode_encode__doc__, |
| 115 | "encode($self, /, encoding=\'utf-8\', errors=\'strict\')\n" |
| 116 | "--\n" |
| 117 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 118 | "Encode the string using the codec registered for encoding.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 119 | "\n" |
| 120 | " encoding\n" |
| 121 | " The encoding in which to encode the string.\n" |
| 122 | " errors\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 123 | " The error handling scheme to use for encoding errors.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 124 | " The default is \'strict\' meaning that encoding errors raise a\n" |
| 125 | " UnicodeEncodeError. Other possible values are \'ignore\', \'replace\' and\n" |
| 126 | " \'xmlcharrefreplace\' as well as any other name registered with\n" |
| 127 | " codecs.register_error that can handle UnicodeEncodeErrors."); |
| 128 | |
| 129 | #define UNICODE_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 130 | {"encode", (PyCFunction)(void(*)(void))unicode_encode, METH_FASTCALL|METH_KEYWORDS, unicode_encode__doc__}, |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 131 | |
| 132 | static PyObject * |
| 133 | unicode_encode_impl(PyObject *self, const char *encoding, const char *errors); |
| 134 | |
| 135 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 136 | unicode_encode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 137 | { |
| 138 | PyObject *return_value = NULL; |
| 139 | static const char * const _keywords[] = {"encoding", "errors", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 140 | static _PyArg_Parser _parser = {NULL, _keywords, "encode", 0}; |
| 141 | PyObject *argsbuf[2]; |
| 142 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 143 | const char *encoding = NULL; |
| 144 | const char *errors = NULL; |
| 145 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 146 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
| 147 | if (!args) { |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 148 | goto exit; |
| 149 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 150 | if (!noptargs) { |
| 151 | goto skip_optional_pos; |
| 152 | } |
| 153 | if (args[0]) { |
| 154 | if (!PyUnicode_Check(args[0])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 155 | _PyArg_BadArgument("encode", "argument 'encoding'", "str", args[0]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 156 | goto exit; |
| 157 | } |
| 158 | Py_ssize_t encoding_length; |
| 159 | encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length); |
| 160 | if (encoding == NULL) { |
| 161 | goto exit; |
| 162 | } |
| 163 | if (strlen(encoding) != (size_t)encoding_length) { |
| 164 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 165 | goto exit; |
| 166 | } |
| 167 | if (!--noptargs) { |
| 168 | goto skip_optional_pos; |
| 169 | } |
| 170 | } |
| 171 | if (!PyUnicode_Check(args[1])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 172 | _PyArg_BadArgument("encode", "argument 'errors'", "str", args[1]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 173 | goto exit; |
| 174 | } |
| 175 | Py_ssize_t errors_length; |
| 176 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 177 | if (errors == NULL) { |
| 178 | goto exit; |
| 179 | } |
| 180 | if (strlen(errors) != (size_t)errors_length) { |
| 181 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 182 | goto exit; |
| 183 | } |
| 184 | skip_optional_pos: |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 185 | return_value = unicode_encode_impl(self, encoding, errors); |
| 186 | |
| 187 | exit: |
| 188 | return return_value; |
| 189 | } |
| 190 | |
| 191 | PyDoc_STRVAR(unicode_expandtabs__doc__, |
| 192 | "expandtabs($self, /, tabsize=8)\n" |
| 193 | "--\n" |
| 194 | "\n" |
| 195 | "Return a copy where all tab characters are expanded using spaces.\n" |
| 196 | "\n" |
| 197 | "If tabsize is not given, a tab size of 8 characters is assumed."); |
| 198 | |
| 199 | #define UNICODE_EXPANDTABS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 200 | {"expandtabs", (PyCFunction)(void(*)(void))unicode_expandtabs, METH_FASTCALL|METH_KEYWORDS, unicode_expandtabs__doc__}, |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 201 | |
| 202 | static PyObject * |
| 203 | unicode_expandtabs_impl(PyObject *self, int tabsize); |
| 204 | |
| 205 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 206 | unicode_expandtabs(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 207 | { |
| 208 | PyObject *return_value = NULL; |
| 209 | static const char * const _keywords[] = {"tabsize", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 210 | static _PyArg_Parser _parser = {NULL, _keywords, "expandtabs", 0}; |
| 211 | PyObject *argsbuf[1]; |
| 212 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 213 | int tabsize = 8; |
| 214 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 215 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf); |
| 216 | if (!args) { |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 217 | goto exit; |
| 218 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 219 | if (!noptargs) { |
| 220 | goto skip_optional_pos; |
| 221 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 222 | tabsize = _PyLong_AsInt(args[0]); |
| 223 | if (tabsize == -1 && PyErr_Occurred()) { |
| 224 | goto exit; |
| 225 | } |
| 226 | skip_optional_pos: |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 227 | return_value = unicode_expandtabs_impl(self, tabsize); |
| 228 | |
| 229 | exit: |
| 230 | return return_value; |
| 231 | } |
| 232 | |
INADA Naoki | a49ac99 | 2018-01-27 14:06:21 +0900 | [diff] [blame] | 233 | PyDoc_STRVAR(unicode_isascii__doc__, |
| 234 | "isascii($self, /)\n" |
| 235 | "--\n" |
| 236 | "\n" |
| 237 | "Return True if all characters in the string are ASCII, False otherwise.\n" |
| 238 | "\n" |
| 239 | "ASCII characters have code points in the range U+0000-U+007F.\n" |
| 240 | "Empty string is ASCII too."); |
| 241 | |
| 242 | #define UNICODE_ISASCII_METHODDEF \ |
| 243 | {"isascii", (PyCFunction)unicode_isascii, METH_NOARGS, unicode_isascii__doc__}, |
| 244 | |
| 245 | static PyObject * |
| 246 | unicode_isascii_impl(PyObject *self); |
| 247 | |
| 248 | static PyObject * |
| 249 | unicode_isascii(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 250 | { |
| 251 | return unicode_isascii_impl(self); |
| 252 | } |
| 253 | |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 254 | PyDoc_STRVAR(unicode_islower__doc__, |
| 255 | "islower($self, /)\n" |
| 256 | "--\n" |
| 257 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 258 | "Return True if the string is a lowercase string, False otherwise.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 259 | "\n" |
| 260 | "A string is lowercase if all cased characters in the string are lowercase and\n" |
| 261 | "there is at least one cased character in the string."); |
| 262 | |
| 263 | #define UNICODE_ISLOWER_METHODDEF \ |
| 264 | {"islower", (PyCFunction)unicode_islower, METH_NOARGS, unicode_islower__doc__}, |
| 265 | |
| 266 | static PyObject * |
| 267 | unicode_islower_impl(PyObject *self); |
| 268 | |
| 269 | static PyObject * |
| 270 | unicode_islower(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 271 | { |
| 272 | return unicode_islower_impl(self); |
| 273 | } |
| 274 | |
| 275 | PyDoc_STRVAR(unicode_isupper__doc__, |
| 276 | "isupper($self, /)\n" |
| 277 | "--\n" |
| 278 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 279 | "Return True if the string is an uppercase string, False otherwise.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 280 | "\n" |
| 281 | "A string is uppercase if all cased characters in the string are uppercase and\n" |
| 282 | "there is at least one cased character in the string."); |
| 283 | |
| 284 | #define UNICODE_ISUPPER_METHODDEF \ |
| 285 | {"isupper", (PyCFunction)unicode_isupper, METH_NOARGS, unicode_isupper__doc__}, |
| 286 | |
| 287 | static PyObject * |
| 288 | unicode_isupper_impl(PyObject *self); |
| 289 | |
| 290 | static PyObject * |
| 291 | unicode_isupper(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 292 | { |
| 293 | return unicode_isupper_impl(self); |
| 294 | } |
| 295 | |
| 296 | PyDoc_STRVAR(unicode_istitle__doc__, |
| 297 | "istitle($self, /)\n" |
| 298 | "--\n" |
| 299 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 300 | "Return True if the string is a title-cased string, False otherwise.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 301 | "\n" |
| 302 | "In a title-cased string, upper- and title-case characters may only\n" |
| 303 | "follow uncased characters and lowercase characters only cased ones."); |
| 304 | |
| 305 | #define UNICODE_ISTITLE_METHODDEF \ |
| 306 | {"istitle", (PyCFunction)unicode_istitle, METH_NOARGS, unicode_istitle__doc__}, |
| 307 | |
| 308 | static PyObject * |
| 309 | unicode_istitle_impl(PyObject *self); |
| 310 | |
| 311 | static PyObject * |
| 312 | unicode_istitle(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 313 | { |
| 314 | return unicode_istitle_impl(self); |
| 315 | } |
| 316 | |
| 317 | PyDoc_STRVAR(unicode_isspace__doc__, |
| 318 | "isspace($self, /)\n" |
| 319 | "--\n" |
| 320 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 321 | "Return True if the string is a whitespace string, False otherwise.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 322 | "\n" |
| 323 | "A string is whitespace if all characters in the string are whitespace and there\n" |
| 324 | "is at least one character in the string."); |
| 325 | |
| 326 | #define UNICODE_ISSPACE_METHODDEF \ |
| 327 | {"isspace", (PyCFunction)unicode_isspace, METH_NOARGS, unicode_isspace__doc__}, |
| 328 | |
| 329 | static PyObject * |
| 330 | unicode_isspace_impl(PyObject *self); |
| 331 | |
| 332 | static PyObject * |
| 333 | unicode_isspace(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 334 | { |
| 335 | return unicode_isspace_impl(self); |
| 336 | } |
| 337 | |
| 338 | PyDoc_STRVAR(unicode_isalpha__doc__, |
| 339 | "isalpha($self, /)\n" |
| 340 | "--\n" |
| 341 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 342 | "Return True if the string is an alphabetic string, False otherwise.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 343 | "\n" |
| 344 | "A string is alphabetic if all characters in the string are alphabetic and there\n" |
| 345 | "is at least one character in the string."); |
| 346 | |
| 347 | #define UNICODE_ISALPHA_METHODDEF \ |
| 348 | {"isalpha", (PyCFunction)unicode_isalpha, METH_NOARGS, unicode_isalpha__doc__}, |
| 349 | |
| 350 | static PyObject * |
| 351 | unicode_isalpha_impl(PyObject *self); |
| 352 | |
| 353 | static PyObject * |
| 354 | unicode_isalpha(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 355 | { |
| 356 | return unicode_isalpha_impl(self); |
| 357 | } |
| 358 | |
| 359 | PyDoc_STRVAR(unicode_isalnum__doc__, |
| 360 | "isalnum($self, /)\n" |
| 361 | "--\n" |
| 362 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 363 | "Return True if the string is an alpha-numeric string, False otherwise.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 364 | "\n" |
| 365 | "A string is alpha-numeric if all characters in the string are alpha-numeric and\n" |
| 366 | "there is at least one character in the string."); |
| 367 | |
| 368 | #define UNICODE_ISALNUM_METHODDEF \ |
| 369 | {"isalnum", (PyCFunction)unicode_isalnum, METH_NOARGS, unicode_isalnum__doc__}, |
| 370 | |
| 371 | static PyObject * |
| 372 | unicode_isalnum_impl(PyObject *self); |
| 373 | |
| 374 | static PyObject * |
| 375 | unicode_isalnum(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 376 | { |
| 377 | return unicode_isalnum_impl(self); |
| 378 | } |
| 379 | |
| 380 | PyDoc_STRVAR(unicode_isdecimal__doc__, |
| 381 | "isdecimal($self, /)\n" |
| 382 | "--\n" |
| 383 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 384 | "Return True if the string is a decimal string, False otherwise.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 385 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 386 | "A string is a decimal string if all characters in the string are decimal and\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 387 | "there is at least one character in the string."); |
| 388 | |
| 389 | #define UNICODE_ISDECIMAL_METHODDEF \ |
| 390 | {"isdecimal", (PyCFunction)unicode_isdecimal, METH_NOARGS, unicode_isdecimal__doc__}, |
| 391 | |
| 392 | static PyObject * |
| 393 | unicode_isdecimal_impl(PyObject *self); |
| 394 | |
| 395 | static PyObject * |
| 396 | unicode_isdecimal(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 397 | { |
| 398 | return unicode_isdecimal_impl(self); |
| 399 | } |
| 400 | |
| 401 | PyDoc_STRVAR(unicode_isdigit__doc__, |
| 402 | "isdigit($self, /)\n" |
| 403 | "--\n" |
| 404 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 405 | "Return True if the string is a digit string, False otherwise.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 406 | "\n" |
| 407 | "A string is a digit string if all characters in the string are digits and there\n" |
| 408 | "is at least one character in the string."); |
| 409 | |
| 410 | #define UNICODE_ISDIGIT_METHODDEF \ |
| 411 | {"isdigit", (PyCFunction)unicode_isdigit, METH_NOARGS, unicode_isdigit__doc__}, |
| 412 | |
| 413 | static PyObject * |
| 414 | unicode_isdigit_impl(PyObject *self); |
| 415 | |
| 416 | static PyObject * |
| 417 | unicode_isdigit(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 418 | { |
| 419 | return unicode_isdigit_impl(self); |
| 420 | } |
| 421 | |
| 422 | PyDoc_STRVAR(unicode_isnumeric__doc__, |
| 423 | "isnumeric($self, /)\n" |
| 424 | "--\n" |
| 425 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 426 | "Return True if the string is a numeric string, False otherwise.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 427 | "\n" |
| 428 | "A string is numeric if all characters in the string are numeric and there is at\n" |
| 429 | "least one character in the string."); |
| 430 | |
| 431 | #define UNICODE_ISNUMERIC_METHODDEF \ |
| 432 | {"isnumeric", (PyCFunction)unicode_isnumeric, METH_NOARGS, unicode_isnumeric__doc__}, |
| 433 | |
| 434 | static PyObject * |
| 435 | unicode_isnumeric_impl(PyObject *self); |
| 436 | |
| 437 | static PyObject * |
| 438 | unicode_isnumeric(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 439 | { |
| 440 | return unicode_isnumeric_impl(self); |
| 441 | } |
| 442 | |
| 443 | PyDoc_STRVAR(unicode_isidentifier__doc__, |
| 444 | "isidentifier($self, /)\n" |
| 445 | "--\n" |
| 446 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 447 | "Return True if the string is a valid Python identifier, False otherwise.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 448 | "\n" |
Sanyam Khurana | ffc5a14 | 2018-10-08 12:23:32 +0530 | [diff] [blame] | 449 | "Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n" |
Emanuele Gaifas | fc8205c | 2018-10-08 12:44:47 +0200 | [diff] [blame] | 450 | "such as \"def\" or \"class\"."); |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 451 | |
| 452 | #define UNICODE_ISIDENTIFIER_METHODDEF \ |
| 453 | {"isidentifier", (PyCFunction)unicode_isidentifier, METH_NOARGS, unicode_isidentifier__doc__}, |
| 454 | |
| 455 | static PyObject * |
| 456 | unicode_isidentifier_impl(PyObject *self); |
| 457 | |
| 458 | static PyObject * |
| 459 | unicode_isidentifier(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 460 | { |
| 461 | return unicode_isidentifier_impl(self); |
| 462 | } |
| 463 | |
| 464 | PyDoc_STRVAR(unicode_isprintable__doc__, |
| 465 | "isprintable($self, /)\n" |
| 466 | "--\n" |
| 467 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 468 | "Return True if the string is printable, False otherwise.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 469 | "\n" |
| 470 | "A string is printable if all of its characters are considered printable in\n" |
| 471 | "repr() or if it is empty."); |
| 472 | |
| 473 | #define UNICODE_ISPRINTABLE_METHODDEF \ |
| 474 | {"isprintable", (PyCFunction)unicode_isprintable, METH_NOARGS, unicode_isprintable__doc__}, |
| 475 | |
| 476 | static PyObject * |
| 477 | unicode_isprintable_impl(PyObject *self); |
| 478 | |
| 479 | static PyObject * |
| 480 | unicode_isprintable(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 481 | { |
| 482 | return unicode_isprintable_impl(self); |
| 483 | } |
| 484 | |
| 485 | PyDoc_STRVAR(unicode_join__doc__, |
| 486 | "join($self, iterable, /)\n" |
| 487 | "--\n" |
| 488 | "\n" |
| 489 | "Concatenate any number of strings.\n" |
| 490 | "\n" |
Martin Panter | 91a8866 | 2017-01-24 00:30:06 +0000 | [diff] [blame] | 491 | "The string whose method is called is inserted in between each given string.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 492 | "The result is returned as a new string.\n" |
| 493 | "\n" |
| 494 | "Example: \'.\'.join([\'ab\', \'pq\', \'rs\']) -> \'ab.pq.rs\'"); |
| 495 | |
| 496 | #define UNICODE_JOIN_METHODDEF \ |
| 497 | {"join", (PyCFunction)unicode_join, METH_O, unicode_join__doc__}, |
| 498 | |
| 499 | PyDoc_STRVAR(unicode_ljust__doc__, |
| 500 | "ljust($self, width, fillchar=\' \', /)\n" |
| 501 | "--\n" |
| 502 | "\n" |
| 503 | "Return a left-justified string of length width.\n" |
| 504 | "\n" |
| 505 | "Padding is done using the specified fill character (default is a space)."); |
| 506 | |
| 507 | #define UNICODE_LJUST_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 508 | {"ljust", (PyCFunction)(void(*)(void))unicode_ljust, METH_FASTCALL, unicode_ljust__doc__}, |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 509 | |
| 510 | static PyObject * |
| 511 | unicode_ljust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar); |
| 512 | |
| 513 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 514 | unicode_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 515 | { |
| 516 | PyObject *return_value = NULL; |
| 517 | Py_ssize_t width; |
| 518 | Py_UCS4 fillchar = ' '; |
| 519 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 520 | if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 521 | goto exit; |
| 522 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 523 | { |
| 524 | Py_ssize_t ival = -1; |
| 525 | PyObject *iobj = PyNumber_Index(args[0]); |
| 526 | if (iobj != NULL) { |
| 527 | ival = PyLong_AsSsize_t(iobj); |
| 528 | Py_DECREF(iobj); |
| 529 | } |
| 530 | if (ival == -1 && PyErr_Occurred()) { |
| 531 | goto exit; |
| 532 | } |
| 533 | width = ival; |
| 534 | } |
| 535 | if (nargs < 2) { |
| 536 | goto skip_optional; |
| 537 | } |
| 538 | if (!convert_uc(args[1], &fillchar)) { |
| 539 | goto exit; |
| 540 | } |
| 541 | skip_optional: |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 542 | return_value = unicode_ljust_impl(self, width, fillchar); |
| 543 | |
| 544 | exit: |
| 545 | return return_value; |
| 546 | } |
| 547 | |
| 548 | PyDoc_STRVAR(unicode_lower__doc__, |
| 549 | "lower($self, /)\n" |
| 550 | "--\n" |
| 551 | "\n" |
| 552 | "Return a copy of the string converted to lowercase."); |
| 553 | |
| 554 | #define UNICODE_LOWER_METHODDEF \ |
| 555 | {"lower", (PyCFunction)unicode_lower, METH_NOARGS, unicode_lower__doc__}, |
| 556 | |
| 557 | static PyObject * |
| 558 | unicode_lower_impl(PyObject *self); |
| 559 | |
| 560 | static PyObject * |
| 561 | unicode_lower(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 562 | { |
| 563 | return unicode_lower_impl(self); |
| 564 | } |
| 565 | |
| 566 | PyDoc_STRVAR(unicode_strip__doc__, |
| 567 | "strip($self, chars=None, /)\n" |
| 568 | "--\n" |
| 569 | "\n" |
Zachary Ware | 09895c2 | 2019-10-09 16:09:00 -0500 | [diff] [blame] | 570 | "Return a copy of the string with leading and trailing whitespace removed.\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 571 | "\n" |
| 572 | "If chars is given and not None, remove characters in chars instead."); |
| 573 | |
| 574 | #define UNICODE_STRIP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 575 | {"strip", (PyCFunction)(void(*)(void))unicode_strip, METH_FASTCALL, unicode_strip__doc__}, |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 576 | |
| 577 | static PyObject * |
| 578 | unicode_strip_impl(PyObject *self, PyObject *chars); |
| 579 | |
| 580 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 581 | unicode_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 582 | { |
| 583 | PyObject *return_value = NULL; |
| 584 | PyObject *chars = Py_None; |
| 585 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 586 | if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 587 | goto exit; |
| 588 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 589 | if (nargs < 1) { |
| 590 | goto skip_optional; |
| 591 | } |
| 592 | chars = args[0]; |
| 593 | skip_optional: |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 594 | return_value = unicode_strip_impl(self, chars); |
| 595 | |
| 596 | exit: |
| 597 | return return_value; |
| 598 | } |
| 599 | |
| 600 | PyDoc_STRVAR(unicode_lstrip__doc__, |
| 601 | "lstrip($self, chars=None, /)\n" |
| 602 | "--\n" |
| 603 | "\n" |
| 604 | "Return a copy of the string with leading whitespace removed.\n" |
| 605 | "\n" |
| 606 | "If chars is given and not None, remove characters in chars instead."); |
| 607 | |
| 608 | #define UNICODE_LSTRIP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 609 | {"lstrip", (PyCFunction)(void(*)(void))unicode_lstrip, METH_FASTCALL, unicode_lstrip__doc__}, |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 610 | |
| 611 | static PyObject * |
| 612 | unicode_lstrip_impl(PyObject *self, PyObject *chars); |
| 613 | |
| 614 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 615 | unicode_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 616 | { |
| 617 | PyObject *return_value = NULL; |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 618 | PyObject *chars = Py_None; |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 619 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 620 | if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 621 | goto exit; |
| 622 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 623 | if (nargs < 1) { |
| 624 | goto skip_optional; |
| 625 | } |
| 626 | chars = args[0]; |
| 627 | skip_optional: |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 628 | return_value = unicode_lstrip_impl(self, chars); |
| 629 | |
| 630 | exit: |
| 631 | return return_value; |
| 632 | } |
| 633 | |
| 634 | PyDoc_STRVAR(unicode_rstrip__doc__, |
| 635 | "rstrip($self, chars=None, /)\n" |
| 636 | "--\n" |
| 637 | "\n" |
| 638 | "Return a copy of the string with trailing whitespace removed.\n" |
| 639 | "\n" |
| 640 | "If chars is given and not None, remove characters in chars instead."); |
| 641 | |
| 642 | #define UNICODE_RSTRIP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 643 | {"rstrip", (PyCFunction)(void(*)(void))unicode_rstrip, METH_FASTCALL, unicode_rstrip__doc__}, |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 644 | |
| 645 | static PyObject * |
| 646 | unicode_rstrip_impl(PyObject *self, PyObject *chars); |
| 647 | |
| 648 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 649 | unicode_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 650 | { |
| 651 | PyObject *return_value = NULL; |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 652 | PyObject *chars = Py_None; |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 653 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 654 | if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 655 | goto exit; |
| 656 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 657 | if (nargs < 1) { |
| 658 | goto skip_optional; |
| 659 | } |
| 660 | chars = args[0]; |
| 661 | skip_optional: |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 662 | return_value = unicode_rstrip_impl(self, chars); |
| 663 | |
| 664 | exit: |
| 665 | return return_value; |
| 666 | } |
| 667 | |
| 668 | PyDoc_STRVAR(unicode_replace__doc__, |
| 669 | "replace($self, old, new, count=-1, /)\n" |
| 670 | "--\n" |
| 671 | "\n" |
| 672 | "Return a copy with all occurrences of substring old replaced by new.\n" |
| 673 | "\n" |
| 674 | " count\n" |
| 675 | " Maximum number of occurrences to replace.\n" |
| 676 | " -1 (the default value) means replace all occurrences.\n" |
| 677 | "\n" |
| 678 | "If the optional argument count is given, only the first count occurrences are\n" |
| 679 | "replaced."); |
| 680 | |
| 681 | #define UNICODE_REPLACE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 682 | {"replace", (PyCFunction)(void(*)(void))unicode_replace, METH_FASTCALL, unicode_replace__doc__}, |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 683 | |
| 684 | static PyObject * |
| 685 | unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new, |
| 686 | Py_ssize_t count); |
| 687 | |
| 688 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 689 | unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 690 | { |
| 691 | PyObject *return_value = NULL; |
| 692 | PyObject *old; |
| 693 | PyObject *new; |
| 694 | Py_ssize_t count = -1; |
| 695 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 696 | if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 697 | goto exit; |
| 698 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 699 | if (!PyUnicode_Check(args[0])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 700 | _PyArg_BadArgument("replace", "argument 1", "str", args[0]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 701 | goto exit; |
| 702 | } |
| 703 | if (PyUnicode_READY(args[0]) == -1) { |
| 704 | goto exit; |
| 705 | } |
| 706 | old = args[0]; |
| 707 | if (!PyUnicode_Check(args[1])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 708 | _PyArg_BadArgument("replace", "argument 2", "str", args[1]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 709 | goto exit; |
| 710 | } |
| 711 | if (PyUnicode_READY(args[1]) == -1) { |
| 712 | goto exit; |
| 713 | } |
| 714 | new = args[1]; |
| 715 | if (nargs < 3) { |
| 716 | goto skip_optional; |
| 717 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 718 | { |
| 719 | Py_ssize_t ival = -1; |
| 720 | PyObject *iobj = PyNumber_Index(args[2]); |
| 721 | if (iobj != NULL) { |
| 722 | ival = PyLong_AsSsize_t(iobj); |
| 723 | Py_DECREF(iobj); |
| 724 | } |
| 725 | if (ival == -1 && PyErr_Occurred()) { |
| 726 | goto exit; |
| 727 | } |
| 728 | count = ival; |
| 729 | } |
| 730 | skip_optional: |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 731 | return_value = unicode_replace_impl(self, old, new, count); |
| 732 | |
| 733 | exit: |
| 734 | return return_value; |
| 735 | } |
| 736 | |
sweeneyde | a81849b | 2020-04-22 17:05:48 -0400 | [diff] [blame] | 737 | PyDoc_STRVAR(unicode_removeprefix__doc__, |
| 738 | "removeprefix($self, prefix, /)\n" |
| 739 | "--\n" |
| 740 | "\n" |
| 741 | "Return a str with the given prefix string removed if present.\n" |
| 742 | "\n" |
| 743 | "If the string starts with the prefix string, return string[len(prefix):].\n" |
| 744 | "Otherwise, return a copy of the original string."); |
| 745 | |
| 746 | #define UNICODE_REMOVEPREFIX_METHODDEF \ |
| 747 | {"removeprefix", (PyCFunction)unicode_removeprefix, METH_O, unicode_removeprefix__doc__}, |
| 748 | |
| 749 | static PyObject * |
| 750 | unicode_removeprefix_impl(PyObject *self, PyObject *prefix); |
| 751 | |
| 752 | static PyObject * |
| 753 | unicode_removeprefix(PyObject *self, PyObject *arg) |
| 754 | { |
| 755 | PyObject *return_value = NULL; |
| 756 | PyObject *prefix; |
| 757 | |
| 758 | if (!PyUnicode_Check(arg)) { |
| 759 | _PyArg_BadArgument("removeprefix", "argument", "str", arg); |
| 760 | goto exit; |
| 761 | } |
| 762 | if (PyUnicode_READY(arg) == -1) { |
| 763 | goto exit; |
| 764 | } |
| 765 | prefix = arg; |
| 766 | return_value = unicode_removeprefix_impl(self, prefix); |
| 767 | |
| 768 | exit: |
| 769 | return return_value; |
| 770 | } |
| 771 | |
| 772 | PyDoc_STRVAR(unicode_removesuffix__doc__, |
| 773 | "removesuffix($self, suffix, /)\n" |
| 774 | "--\n" |
| 775 | "\n" |
| 776 | "Return a str with the given suffix string removed if present.\n" |
| 777 | "\n" |
| 778 | "If the string ends with the suffix string and that suffix is not empty,\n" |
| 779 | "return string[:-len(suffix)]. Otherwise, return a copy of the original\n" |
| 780 | "string."); |
| 781 | |
| 782 | #define UNICODE_REMOVESUFFIX_METHODDEF \ |
| 783 | {"removesuffix", (PyCFunction)unicode_removesuffix, METH_O, unicode_removesuffix__doc__}, |
| 784 | |
| 785 | static PyObject * |
| 786 | unicode_removesuffix_impl(PyObject *self, PyObject *suffix); |
| 787 | |
| 788 | static PyObject * |
| 789 | unicode_removesuffix(PyObject *self, PyObject *arg) |
| 790 | { |
| 791 | PyObject *return_value = NULL; |
| 792 | PyObject *suffix; |
| 793 | |
| 794 | if (!PyUnicode_Check(arg)) { |
| 795 | _PyArg_BadArgument("removesuffix", "argument", "str", arg); |
| 796 | goto exit; |
| 797 | } |
| 798 | if (PyUnicode_READY(arg) == -1) { |
| 799 | goto exit; |
| 800 | } |
| 801 | suffix = arg; |
| 802 | return_value = unicode_removesuffix_impl(self, suffix); |
| 803 | |
| 804 | exit: |
| 805 | return return_value; |
| 806 | } |
| 807 | |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 808 | PyDoc_STRVAR(unicode_rjust__doc__, |
| 809 | "rjust($self, width, fillchar=\' \', /)\n" |
| 810 | "--\n" |
| 811 | "\n" |
| 812 | "Return a right-justified string of length width.\n" |
| 813 | "\n" |
| 814 | "Padding is done using the specified fill character (default is a space)."); |
| 815 | |
| 816 | #define UNICODE_RJUST_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 817 | {"rjust", (PyCFunction)(void(*)(void))unicode_rjust, METH_FASTCALL, unicode_rjust__doc__}, |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 818 | |
| 819 | static PyObject * |
| 820 | unicode_rjust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar); |
| 821 | |
| 822 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 823 | unicode_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 824 | { |
| 825 | PyObject *return_value = NULL; |
| 826 | Py_ssize_t width; |
| 827 | Py_UCS4 fillchar = ' '; |
| 828 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 829 | if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 830 | goto exit; |
| 831 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 832 | { |
| 833 | Py_ssize_t ival = -1; |
| 834 | PyObject *iobj = PyNumber_Index(args[0]); |
| 835 | if (iobj != NULL) { |
| 836 | ival = PyLong_AsSsize_t(iobj); |
| 837 | Py_DECREF(iobj); |
| 838 | } |
| 839 | if (ival == -1 && PyErr_Occurred()) { |
| 840 | goto exit; |
| 841 | } |
| 842 | width = ival; |
| 843 | } |
| 844 | if (nargs < 2) { |
| 845 | goto skip_optional; |
| 846 | } |
| 847 | if (!convert_uc(args[1], &fillchar)) { |
| 848 | goto exit; |
| 849 | } |
| 850 | skip_optional: |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 851 | return_value = unicode_rjust_impl(self, width, fillchar); |
| 852 | |
| 853 | exit: |
| 854 | return return_value; |
| 855 | } |
| 856 | |
| 857 | PyDoc_STRVAR(unicode_split__doc__, |
| 858 | "split($self, /, sep=None, maxsplit=-1)\n" |
| 859 | "--\n" |
| 860 | "\n" |
| 861 | "Return a list of the words in the string, using sep as the delimiter string.\n" |
| 862 | "\n" |
| 863 | " sep\n" |
| 864 | " The delimiter according which to split the string.\n" |
| 865 | " None (the default value) means split according to any whitespace,\n" |
| 866 | " and discard empty strings from the result.\n" |
| 867 | " maxsplit\n" |
| 868 | " Maximum number of splits to do.\n" |
| 869 | " -1 (the default value) means no limit."); |
| 870 | |
| 871 | #define UNICODE_SPLIT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 872 | {"split", (PyCFunction)(void(*)(void))unicode_split, METH_FASTCALL|METH_KEYWORDS, unicode_split__doc__}, |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 873 | |
| 874 | static PyObject * |
| 875 | unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit); |
| 876 | |
| 877 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 878 | unicode_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 879 | { |
| 880 | PyObject *return_value = NULL; |
| 881 | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 882 | static _PyArg_Parser _parser = {NULL, _keywords, "split", 0}; |
| 883 | PyObject *argsbuf[2]; |
| 884 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 885 | PyObject *sep = Py_None; |
| 886 | Py_ssize_t maxsplit = -1; |
| 887 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 888 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
| 889 | if (!args) { |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 890 | goto exit; |
| 891 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 892 | if (!noptargs) { |
| 893 | goto skip_optional_pos; |
| 894 | } |
| 895 | if (args[0]) { |
| 896 | sep = args[0]; |
| 897 | if (!--noptargs) { |
| 898 | goto skip_optional_pos; |
| 899 | } |
| 900 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 901 | { |
| 902 | Py_ssize_t ival = -1; |
| 903 | PyObject *iobj = PyNumber_Index(args[1]); |
| 904 | if (iobj != NULL) { |
| 905 | ival = PyLong_AsSsize_t(iobj); |
| 906 | Py_DECREF(iobj); |
| 907 | } |
| 908 | if (ival == -1 && PyErr_Occurred()) { |
| 909 | goto exit; |
| 910 | } |
| 911 | maxsplit = ival; |
| 912 | } |
| 913 | skip_optional_pos: |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 914 | return_value = unicode_split_impl(self, sep, maxsplit); |
| 915 | |
| 916 | exit: |
| 917 | return return_value; |
| 918 | } |
| 919 | |
| 920 | PyDoc_STRVAR(unicode_partition__doc__, |
| 921 | "partition($self, sep, /)\n" |
| 922 | "--\n" |
| 923 | "\n" |
| 924 | "Partition the string into three parts using the given separator.\n" |
| 925 | "\n" |
| 926 | "This will search for the separator in the string. If the separator is found,\n" |
| 927 | "returns a 3-tuple containing the part before the separator, the separator\n" |
| 928 | "itself, and the part after it.\n" |
| 929 | "\n" |
| 930 | "If the separator is not found, returns a 3-tuple containing the original string\n" |
| 931 | "and two empty strings."); |
| 932 | |
| 933 | #define UNICODE_PARTITION_METHODDEF \ |
| 934 | {"partition", (PyCFunction)unicode_partition, METH_O, unicode_partition__doc__}, |
| 935 | |
| 936 | PyDoc_STRVAR(unicode_rpartition__doc__, |
| 937 | "rpartition($self, sep, /)\n" |
| 938 | "--\n" |
| 939 | "\n" |
| 940 | "Partition the string into three parts using the given separator.\n" |
| 941 | "\n" |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 942 | "This will search for the separator in the string, starting at the end. If\n" |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 943 | "the separator is found, returns a 3-tuple containing the part before the\n" |
| 944 | "separator, the separator itself, and the part after it.\n" |
| 945 | "\n" |
| 946 | "If the separator is not found, returns a 3-tuple containing two empty strings\n" |
| 947 | "and the original string."); |
| 948 | |
| 949 | #define UNICODE_RPARTITION_METHODDEF \ |
| 950 | {"rpartition", (PyCFunction)unicode_rpartition, METH_O, unicode_rpartition__doc__}, |
| 951 | |
| 952 | PyDoc_STRVAR(unicode_rsplit__doc__, |
| 953 | "rsplit($self, /, sep=None, maxsplit=-1)\n" |
| 954 | "--\n" |
| 955 | "\n" |
| 956 | "Return a list of the words in the string, using sep as the delimiter string.\n" |
| 957 | "\n" |
| 958 | " sep\n" |
| 959 | " The delimiter according which to split the string.\n" |
| 960 | " None (the default value) means split according to any whitespace,\n" |
| 961 | " and discard empty strings from the result.\n" |
| 962 | " maxsplit\n" |
| 963 | " Maximum number of splits to do.\n" |
| 964 | " -1 (the default value) means no limit.\n" |
| 965 | "\n" |
| 966 | "Splits are done starting at the end of the string and working to the front."); |
| 967 | |
| 968 | #define UNICODE_RSPLIT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 969 | {"rsplit", (PyCFunction)(void(*)(void))unicode_rsplit, METH_FASTCALL|METH_KEYWORDS, unicode_rsplit__doc__}, |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 970 | |
| 971 | static PyObject * |
| 972 | unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit); |
| 973 | |
| 974 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 975 | unicode_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 976 | { |
| 977 | PyObject *return_value = NULL; |
| 978 | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 979 | static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0}; |
| 980 | PyObject *argsbuf[2]; |
| 981 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 982 | PyObject *sep = Py_None; |
| 983 | Py_ssize_t maxsplit = -1; |
| 984 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 985 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
| 986 | if (!args) { |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 987 | goto exit; |
| 988 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 989 | if (!noptargs) { |
| 990 | goto skip_optional_pos; |
| 991 | } |
| 992 | if (args[0]) { |
| 993 | sep = args[0]; |
| 994 | if (!--noptargs) { |
| 995 | goto skip_optional_pos; |
| 996 | } |
| 997 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 998 | { |
| 999 | Py_ssize_t ival = -1; |
| 1000 | PyObject *iobj = PyNumber_Index(args[1]); |
| 1001 | if (iobj != NULL) { |
| 1002 | ival = PyLong_AsSsize_t(iobj); |
| 1003 | Py_DECREF(iobj); |
| 1004 | } |
| 1005 | if (ival == -1 && PyErr_Occurred()) { |
| 1006 | goto exit; |
| 1007 | } |
| 1008 | maxsplit = ival; |
| 1009 | } |
| 1010 | skip_optional_pos: |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 1011 | return_value = unicode_rsplit_impl(self, sep, maxsplit); |
| 1012 | |
| 1013 | exit: |
| 1014 | return return_value; |
| 1015 | } |
| 1016 | |
| 1017 | PyDoc_STRVAR(unicode_splitlines__doc__, |
| 1018 | "splitlines($self, /, keepends=False)\n" |
| 1019 | "--\n" |
| 1020 | "\n" |
| 1021 | "Return a list of the lines in the string, breaking at line boundaries.\n" |
| 1022 | "\n" |
| 1023 | "Line breaks are not included in the resulting list unless keepends is given and\n" |
| 1024 | "true."); |
| 1025 | |
| 1026 | #define UNICODE_SPLITLINES_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1027 | {"splitlines", (PyCFunction)(void(*)(void))unicode_splitlines, METH_FASTCALL|METH_KEYWORDS, unicode_splitlines__doc__}, |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 1028 | |
| 1029 | static PyObject * |
| 1030 | unicode_splitlines_impl(PyObject *self, int keepends); |
| 1031 | |
| 1032 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1033 | unicode_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 1034 | { |
| 1035 | PyObject *return_value = NULL; |
| 1036 | static const char * const _keywords[] = {"keepends", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 1037 | static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0}; |
| 1038 | PyObject *argsbuf[1]; |
| 1039 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 1040 | int keepends = 0; |
| 1041 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 1042 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf); |
| 1043 | if (!args) { |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 1044 | goto exit; |
| 1045 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 1046 | if (!noptargs) { |
| 1047 | goto skip_optional_pos; |
| 1048 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 1049 | keepends = _PyLong_AsInt(args[0]); |
| 1050 | if (keepends == -1 && PyErr_Occurred()) { |
| 1051 | goto exit; |
| 1052 | } |
| 1053 | skip_optional_pos: |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 1054 | return_value = unicode_splitlines_impl(self, keepends); |
| 1055 | |
| 1056 | exit: |
| 1057 | return return_value; |
| 1058 | } |
| 1059 | |
| 1060 | PyDoc_STRVAR(unicode_swapcase__doc__, |
| 1061 | "swapcase($self, /)\n" |
| 1062 | "--\n" |
| 1063 | "\n" |
| 1064 | "Convert uppercase characters to lowercase and lowercase characters to uppercase."); |
| 1065 | |
| 1066 | #define UNICODE_SWAPCASE_METHODDEF \ |
| 1067 | {"swapcase", (PyCFunction)unicode_swapcase, METH_NOARGS, unicode_swapcase__doc__}, |
| 1068 | |
| 1069 | static PyObject * |
| 1070 | unicode_swapcase_impl(PyObject *self); |
| 1071 | |
| 1072 | static PyObject * |
| 1073 | unicode_swapcase(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 1074 | { |
| 1075 | return unicode_swapcase_impl(self); |
| 1076 | } |
| 1077 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1078 | PyDoc_STRVAR(unicode_maketrans__doc__, |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 1079 | "maketrans(x, y=<unrepresentable>, z=<unrepresentable>, /)\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1080 | "--\n" |
| 1081 | "\n" |
| 1082 | "Return a translation table usable for str.translate().\n" |
| 1083 | "\n" |
| 1084 | "If there is only one argument, it must be a dictionary mapping Unicode\n" |
| 1085 | "ordinals (integers) or characters to Unicode ordinals, strings or None.\n" |
| 1086 | "Character keys will be then converted to ordinals.\n" |
| 1087 | "If there are two arguments, they must be strings of equal length, and\n" |
| 1088 | "in the resulting dictionary, each character in x will be mapped to the\n" |
| 1089 | "character at the same position in y. If there is a third argument, it\n" |
| 1090 | "must be a string, whose characters will be mapped to None in the result."); |
| 1091 | |
| 1092 | #define UNICODE_MAKETRANS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1093 | {"maketrans", (PyCFunction)(void(*)(void))unicode_maketrans, METH_FASTCALL|METH_STATIC, unicode_maketrans__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1094 | |
| 1095 | static PyObject * |
| 1096 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z); |
| 1097 | |
| 1098 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1099 | unicode_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1100 | { |
| 1101 | PyObject *return_value = NULL; |
| 1102 | PyObject *x; |
| 1103 | PyObject *y = NULL; |
| 1104 | PyObject *z = NULL; |
| 1105 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1106 | if (!_PyArg_CheckPositional("maketrans", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1107 | goto exit; |
| 1108 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1109 | x = args[0]; |
| 1110 | if (nargs < 2) { |
| 1111 | goto skip_optional; |
| 1112 | } |
| 1113 | if (!PyUnicode_Check(args[1])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 1114 | _PyArg_BadArgument("maketrans", "argument 2", "str", args[1]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1115 | goto exit; |
| 1116 | } |
| 1117 | if (PyUnicode_READY(args[1]) == -1) { |
| 1118 | goto exit; |
| 1119 | } |
| 1120 | y = args[1]; |
| 1121 | if (nargs < 3) { |
| 1122 | goto skip_optional; |
| 1123 | } |
| 1124 | if (!PyUnicode_Check(args[2])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 1125 | _PyArg_BadArgument("maketrans", "argument 3", "str", args[2]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1126 | goto exit; |
| 1127 | } |
| 1128 | if (PyUnicode_READY(args[2]) == -1) { |
| 1129 | goto exit; |
| 1130 | } |
| 1131 | z = args[2]; |
| 1132 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1133 | return_value = unicode_maketrans_impl(x, y, z); |
| 1134 | |
| 1135 | exit: |
| 1136 | return return_value; |
| 1137 | } |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 1138 | |
| 1139 | PyDoc_STRVAR(unicode_translate__doc__, |
| 1140 | "translate($self, table, /)\n" |
| 1141 | "--\n" |
| 1142 | "\n" |
| 1143 | "Replace each character in the string using the given translation table.\n" |
| 1144 | "\n" |
| 1145 | " table\n" |
| 1146 | " Translation table, which must be a mapping of Unicode ordinals to\n" |
| 1147 | " Unicode ordinals, strings, or None.\n" |
| 1148 | "\n" |
| 1149 | "The table must implement lookup/indexing via __getitem__, for instance a\n" |
| 1150 | "dictionary or list. If this operation raises LookupError, the character is\n" |
| 1151 | "left untouched. Characters mapped to None are deleted."); |
| 1152 | |
| 1153 | #define UNICODE_TRANSLATE_METHODDEF \ |
| 1154 | {"translate", (PyCFunction)unicode_translate, METH_O, unicode_translate__doc__}, |
| 1155 | |
| 1156 | PyDoc_STRVAR(unicode_upper__doc__, |
| 1157 | "upper($self, /)\n" |
| 1158 | "--\n" |
| 1159 | "\n" |
| 1160 | "Return a copy of the string converted to uppercase."); |
| 1161 | |
| 1162 | #define UNICODE_UPPER_METHODDEF \ |
| 1163 | {"upper", (PyCFunction)unicode_upper, METH_NOARGS, unicode_upper__doc__}, |
| 1164 | |
| 1165 | static PyObject * |
| 1166 | unicode_upper_impl(PyObject *self); |
| 1167 | |
| 1168 | static PyObject * |
| 1169 | unicode_upper(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 1170 | { |
| 1171 | return unicode_upper_impl(self); |
| 1172 | } |
| 1173 | |
| 1174 | PyDoc_STRVAR(unicode_zfill__doc__, |
| 1175 | "zfill($self, width, /)\n" |
| 1176 | "--\n" |
| 1177 | "\n" |
| 1178 | "Pad a numeric string with zeros on the left, to fill a field of the given width.\n" |
| 1179 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 1180 | "The string is never truncated."); |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 1181 | |
| 1182 | #define UNICODE_ZFILL_METHODDEF \ |
| 1183 | {"zfill", (PyCFunction)unicode_zfill, METH_O, unicode_zfill__doc__}, |
| 1184 | |
| 1185 | static PyObject * |
| 1186 | unicode_zfill_impl(PyObject *self, Py_ssize_t width); |
| 1187 | |
| 1188 | static PyObject * |
| 1189 | unicode_zfill(PyObject *self, PyObject *arg) |
| 1190 | { |
| 1191 | PyObject *return_value = NULL; |
| 1192 | Py_ssize_t width; |
| 1193 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 1194 | { |
| 1195 | Py_ssize_t ival = -1; |
| 1196 | PyObject *iobj = PyNumber_Index(arg); |
| 1197 | if (iobj != NULL) { |
| 1198 | ival = PyLong_AsSsize_t(iobj); |
| 1199 | Py_DECREF(iobj); |
| 1200 | } |
| 1201 | if (ival == -1 && PyErr_Occurred()) { |
| 1202 | goto exit; |
| 1203 | } |
| 1204 | width = ival; |
| 1205 | } |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 1206 | return_value = unicode_zfill_impl(self, width); |
| 1207 | |
| 1208 | exit: |
| 1209 | return return_value; |
| 1210 | } |
| 1211 | |
| 1212 | PyDoc_STRVAR(unicode___format____doc__, |
| 1213 | "__format__($self, format_spec, /)\n" |
| 1214 | "--\n" |
| 1215 | "\n" |
INADA Naoki | 15f9459 | 2017-01-16 21:49:13 +0900 | [diff] [blame] | 1216 | "Return a formatted version of the string as described by format_spec."); |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 1217 | |
| 1218 | #define UNICODE___FORMAT___METHODDEF \ |
| 1219 | {"__format__", (PyCFunction)unicode___format__, METH_O, unicode___format____doc__}, |
| 1220 | |
| 1221 | static PyObject * |
| 1222 | unicode___format___impl(PyObject *self, PyObject *format_spec); |
| 1223 | |
| 1224 | static PyObject * |
| 1225 | unicode___format__(PyObject *self, PyObject *arg) |
| 1226 | { |
| 1227 | PyObject *return_value = NULL; |
| 1228 | PyObject *format_spec; |
| 1229 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 1230 | if (!PyUnicode_Check(arg)) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 1231 | _PyArg_BadArgument("__format__", "argument", "str", arg); |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 1232 | goto exit; |
| 1233 | } |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 1234 | if (PyUnicode_READY(arg) == -1) { |
| 1235 | goto exit; |
| 1236 | } |
| 1237 | format_spec = arg; |
INADA Naoki | 3ae2056 | 2017-01-16 20:41:20 +0900 | [diff] [blame] | 1238 | return_value = unicode___format___impl(self, format_spec); |
| 1239 | |
| 1240 | exit: |
| 1241 | return return_value; |
| 1242 | } |
| 1243 | |
| 1244 | PyDoc_STRVAR(unicode_sizeof__doc__, |
| 1245 | "__sizeof__($self, /)\n" |
| 1246 | "--\n" |
| 1247 | "\n" |
| 1248 | "Return the size of the string in memory, in bytes."); |
| 1249 | |
| 1250 | #define UNICODE_SIZEOF_METHODDEF \ |
| 1251 | {"__sizeof__", (PyCFunction)unicode_sizeof, METH_NOARGS, unicode_sizeof__doc__}, |
| 1252 | |
| 1253 | static PyObject * |
| 1254 | unicode_sizeof_impl(PyObject *self); |
| 1255 | |
| 1256 | static PyObject * |
| 1257 | unicode_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 1258 | { |
| 1259 | return unicode_sizeof_impl(self); |
| 1260 | } |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 1261 | /*[clinic end generated code: output=ea1aff10c743be14 input=a9049054013a1b77]*/ |