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