Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_STRVAR(stringlib_expandtabs__doc__, |
| 6 | "expandtabs($self, /, tabsize=8)\n" |
| 7 | "--\n" |
| 8 | "\n" |
| 9 | "Return a copy where all tab characters are expanded using spaces.\n" |
| 10 | "\n" |
| 11 | "If tabsize is not given, a tab size of 8 characters is assumed."); |
| 12 | |
| 13 | #define STRINGLIB_EXPANDTABS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 14 | {"expandtabs", (PyCFunction)(void(*)(void))stringlib_expandtabs, METH_FASTCALL|METH_KEYWORDS, stringlib_expandtabs__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 15 | |
| 16 | static PyObject * |
| 17 | stringlib_expandtabs_impl(PyObject *self, int tabsize); |
| 18 | |
| 19 | static PyObject * |
| 20 | stringlib_expandtabs(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 21 | { |
| 22 | PyObject *return_value = NULL; |
| 23 | static const char * const _keywords[] = {"tabsize", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 24 | static _PyArg_Parser _parser = {NULL, _keywords, "expandtabs", 0}; |
| 25 | PyObject *argsbuf[1]; |
| 26 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 27 | int tabsize = 8; |
| 28 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 29 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf); |
| 30 | if (!args) { |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 31 | goto exit; |
| 32 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 33 | if (!noptargs) { |
| 34 | goto skip_optional_pos; |
| 35 | } |
| 36 | if (PyFloat_Check(args[0])) { |
| 37 | PyErr_SetString(PyExc_TypeError, |
| 38 | "integer argument expected, got float" ); |
| 39 | goto exit; |
| 40 | } |
| 41 | tabsize = _PyLong_AsInt(args[0]); |
| 42 | if (tabsize == -1 && PyErr_Occurred()) { |
| 43 | goto exit; |
| 44 | } |
| 45 | skip_optional_pos: |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 46 | return_value = stringlib_expandtabs_impl(self, tabsize); |
| 47 | |
| 48 | exit: |
| 49 | return return_value; |
| 50 | } |
| 51 | |
| 52 | PyDoc_STRVAR(stringlib_ljust__doc__, |
| 53 | "ljust($self, width, fillchar=b\' \', /)\n" |
| 54 | "--\n" |
| 55 | "\n" |
| 56 | "Return a left-justified string of length width.\n" |
| 57 | "\n" |
| 58 | "Padding is done using the specified fill character."); |
| 59 | |
| 60 | #define STRINGLIB_LJUST_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 61 | {"ljust", (PyCFunction)(void(*)(void))stringlib_ljust, METH_FASTCALL, stringlib_ljust__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 62 | |
| 63 | static PyObject * |
| 64 | stringlib_ljust_impl(PyObject *self, Py_ssize_t width, char fillchar); |
| 65 | |
| 66 | static PyObject * |
| 67 | stringlib_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
| 68 | { |
| 69 | PyObject *return_value = NULL; |
| 70 | Py_ssize_t width; |
| 71 | char fillchar = ' '; |
| 72 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 73 | if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) { |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 74 | goto exit; |
| 75 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 76 | if (PyFloat_Check(args[0])) { |
| 77 | PyErr_SetString(PyExc_TypeError, |
| 78 | "integer argument expected, got float" ); |
| 79 | goto exit; |
| 80 | } |
| 81 | { |
| 82 | Py_ssize_t ival = -1; |
| 83 | PyObject *iobj = PyNumber_Index(args[0]); |
| 84 | if (iobj != NULL) { |
| 85 | ival = PyLong_AsSsize_t(iobj); |
| 86 | Py_DECREF(iobj); |
| 87 | } |
| 88 | if (ival == -1 && PyErr_Occurred()) { |
| 89 | goto exit; |
| 90 | } |
| 91 | width = ival; |
| 92 | } |
| 93 | if (nargs < 2) { |
| 94 | goto skip_optional; |
| 95 | } |
| 96 | if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) { |
| 97 | fillchar = PyBytes_AS_STRING(args[1])[0]; |
| 98 | } |
| 99 | else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) { |
| 100 | fillchar = PyByteArray_AS_STRING(args[1])[0]; |
| 101 | } |
| 102 | else { |
| 103 | _PyArg_BadArgument("ljust", 2, "a byte string of length 1", args[1]); |
| 104 | goto exit; |
| 105 | } |
| 106 | skip_optional: |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 107 | return_value = stringlib_ljust_impl(self, width, fillchar); |
| 108 | |
| 109 | exit: |
| 110 | return return_value; |
| 111 | } |
| 112 | |
| 113 | PyDoc_STRVAR(stringlib_rjust__doc__, |
| 114 | "rjust($self, width, fillchar=b\' \', /)\n" |
| 115 | "--\n" |
| 116 | "\n" |
| 117 | "Return a right-justified string of length width.\n" |
| 118 | "\n" |
| 119 | "Padding is done using the specified fill character."); |
| 120 | |
| 121 | #define STRINGLIB_RJUST_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 122 | {"rjust", (PyCFunction)(void(*)(void))stringlib_rjust, METH_FASTCALL, stringlib_rjust__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 123 | |
| 124 | static PyObject * |
| 125 | stringlib_rjust_impl(PyObject *self, Py_ssize_t width, char fillchar); |
| 126 | |
| 127 | static PyObject * |
| 128 | stringlib_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
| 129 | { |
| 130 | PyObject *return_value = NULL; |
| 131 | Py_ssize_t width; |
| 132 | char fillchar = ' '; |
| 133 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 134 | if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) { |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 135 | goto exit; |
| 136 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 137 | if (PyFloat_Check(args[0])) { |
| 138 | PyErr_SetString(PyExc_TypeError, |
| 139 | "integer argument expected, got float" ); |
| 140 | goto exit; |
| 141 | } |
| 142 | { |
| 143 | Py_ssize_t ival = -1; |
| 144 | PyObject *iobj = PyNumber_Index(args[0]); |
| 145 | if (iobj != NULL) { |
| 146 | ival = PyLong_AsSsize_t(iobj); |
| 147 | Py_DECREF(iobj); |
| 148 | } |
| 149 | if (ival == -1 && PyErr_Occurred()) { |
| 150 | goto exit; |
| 151 | } |
| 152 | width = ival; |
| 153 | } |
| 154 | if (nargs < 2) { |
| 155 | goto skip_optional; |
| 156 | } |
| 157 | if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) { |
| 158 | fillchar = PyBytes_AS_STRING(args[1])[0]; |
| 159 | } |
| 160 | else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) { |
| 161 | fillchar = PyByteArray_AS_STRING(args[1])[0]; |
| 162 | } |
| 163 | else { |
| 164 | _PyArg_BadArgument("rjust", 2, "a byte string of length 1", args[1]); |
| 165 | goto exit; |
| 166 | } |
| 167 | skip_optional: |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 168 | return_value = stringlib_rjust_impl(self, width, fillchar); |
| 169 | |
| 170 | exit: |
| 171 | return return_value; |
| 172 | } |
| 173 | |
| 174 | PyDoc_STRVAR(stringlib_center__doc__, |
| 175 | "center($self, width, fillchar=b\' \', /)\n" |
| 176 | "--\n" |
| 177 | "\n" |
| 178 | "Return a centered string of length width.\n" |
| 179 | "\n" |
| 180 | "Padding is done using the specified fill character."); |
| 181 | |
| 182 | #define STRINGLIB_CENTER_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 183 | {"center", (PyCFunction)(void(*)(void))stringlib_center, METH_FASTCALL, stringlib_center__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 184 | |
| 185 | static PyObject * |
| 186 | stringlib_center_impl(PyObject *self, Py_ssize_t width, char fillchar); |
| 187 | |
| 188 | static PyObject * |
| 189 | stringlib_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
| 190 | { |
| 191 | PyObject *return_value = NULL; |
| 192 | Py_ssize_t width; |
| 193 | char fillchar = ' '; |
| 194 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 195 | if (!_PyArg_CheckPositional("center", nargs, 1, 2)) { |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 196 | goto exit; |
| 197 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 198 | if (PyFloat_Check(args[0])) { |
| 199 | PyErr_SetString(PyExc_TypeError, |
| 200 | "integer argument expected, got float" ); |
| 201 | goto exit; |
| 202 | } |
| 203 | { |
| 204 | Py_ssize_t ival = -1; |
| 205 | PyObject *iobj = PyNumber_Index(args[0]); |
| 206 | if (iobj != NULL) { |
| 207 | ival = PyLong_AsSsize_t(iobj); |
| 208 | Py_DECREF(iobj); |
| 209 | } |
| 210 | if (ival == -1 && PyErr_Occurred()) { |
| 211 | goto exit; |
| 212 | } |
| 213 | width = ival; |
| 214 | } |
| 215 | if (nargs < 2) { |
| 216 | goto skip_optional; |
| 217 | } |
| 218 | if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) { |
| 219 | fillchar = PyBytes_AS_STRING(args[1])[0]; |
| 220 | } |
| 221 | else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) { |
| 222 | fillchar = PyByteArray_AS_STRING(args[1])[0]; |
| 223 | } |
| 224 | else { |
| 225 | _PyArg_BadArgument("center", 2, "a byte string of length 1", args[1]); |
| 226 | goto exit; |
| 227 | } |
| 228 | skip_optional: |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 229 | return_value = stringlib_center_impl(self, width, fillchar); |
| 230 | |
| 231 | exit: |
| 232 | return return_value; |
| 233 | } |
| 234 | |
| 235 | PyDoc_STRVAR(stringlib_zfill__doc__, |
| 236 | "zfill($self, width, /)\n" |
| 237 | "--\n" |
| 238 | "\n" |
| 239 | "Pad a numeric string with zeros on the left, to fill a field of the given width.\n" |
| 240 | "\n" |
| 241 | "The original string is never truncated."); |
| 242 | |
| 243 | #define STRINGLIB_ZFILL_METHODDEF \ |
| 244 | {"zfill", (PyCFunction)stringlib_zfill, METH_O, stringlib_zfill__doc__}, |
| 245 | |
| 246 | static PyObject * |
| 247 | stringlib_zfill_impl(PyObject *self, Py_ssize_t width); |
| 248 | |
| 249 | static PyObject * |
| 250 | stringlib_zfill(PyObject *self, PyObject *arg) |
| 251 | { |
| 252 | PyObject *return_value = NULL; |
| 253 | Py_ssize_t width; |
| 254 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 255 | if (PyFloat_Check(arg)) { |
| 256 | PyErr_SetString(PyExc_TypeError, |
| 257 | "integer argument expected, got float" ); |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 258 | goto exit; |
| 259 | } |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 260 | { |
| 261 | Py_ssize_t ival = -1; |
| 262 | PyObject *iobj = PyNumber_Index(arg); |
| 263 | if (iobj != NULL) { |
| 264 | ival = PyLong_AsSsize_t(iobj); |
| 265 | Py_DECREF(iobj); |
| 266 | } |
| 267 | if (ival == -1 && PyErr_Occurred()) { |
| 268 | goto exit; |
| 269 | } |
| 270 | width = ival; |
| 271 | } |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 272 | return_value = stringlib_zfill_impl(self, width); |
| 273 | |
| 274 | exit: |
| 275 | return return_value; |
| 276 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 277 | /*[clinic end generated code: output=96cbb19b238d0e84 input=a9049054013a1b77]*/ |