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}; |
| 24 | static _PyArg_Parser _parser = {"|i:expandtabs", _keywords, 0}; |
| 25 | int tabsize = 8; |
| 26 | |
| 27 | if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, |
| 28 | &tabsize)) { |
| 29 | goto exit; |
| 30 | } |
| 31 | return_value = stringlib_expandtabs_impl(self, tabsize); |
| 32 | |
| 33 | exit: |
| 34 | return return_value; |
| 35 | } |
| 36 | |
| 37 | PyDoc_STRVAR(stringlib_ljust__doc__, |
| 38 | "ljust($self, width, fillchar=b\' \', /)\n" |
| 39 | "--\n" |
| 40 | "\n" |
| 41 | "Return a left-justified string of length width.\n" |
| 42 | "\n" |
| 43 | "Padding is done using the specified fill character."); |
| 44 | |
| 45 | #define STRINGLIB_LJUST_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 46 | {"ljust", (PyCFunction)(void(*)(void))stringlib_ljust, METH_FASTCALL, stringlib_ljust__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 47 | |
| 48 | static PyObject * |
| 49 | stringlib_ljust_impl(PyObject *self, Py_ssize_t width, char fillchar); |
| 50 | |
| 51 | static PyObject * |
| 52 | stringlib_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
| 53 | { |
| 54 | PyObject *return_value = NULL; |
| 55 | Py_ssize_t width; |
| 56 | char fillchar = ' '; |
| 57 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame^] | 58 | if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) { |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 59 | goto exit; |
| 60 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame^] | 61 | if (PyFloat_Check(args[0])) { |
| 62 | PyErr_SetString(PyExc_TypeError, |
| 63 | "integer argument expected, got float" ); |
| 64 | goto exit; |
| 65 | } |
| 66 | { |
| 67 | Py_ssize_t ival = -1; |
| 68 | PyObject *iobj = PyNumber_Index(args[0]); |
| 69 | if (iobj != NULL) { |
| 70 | ival = PyLong_AsSsize_t(iobj); |
| 71 | Py_DECREF(iobj); |
| 72 | } |
| 73 | if (ival == -1 && PyErr_Occurred()) { |
| 74 | goto exit; |
| 75 | } |
| 76 | width = ival; |
| 77 | } |
| 78 | if (nargs < 2) { |
| 79 | goto skip_optional; |
| 80 | } |
| 81 | if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) { |
| 82 | fillchar = PyBytes_AS_STRING(args[1])[0]; |
| 83 | } |
| 84 | else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) { |
| 85 | fillchar = PyByteArray_AS_STRING(args[1])[0]; |
| 86 | } |
| 87 | else { |
| 88 | _PyArg_BadArgument("ljust", 2, "a byte string of length 1", args[1]); |
| 89 | goto exit; |
| 90 | } |
| 91 | skip_optional: |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 92 | return_value = stringlib_ljust_impl(self, width, fillchar); |
| 93 | |
| 94 | exit: |
| 95 | return return_value; |
| 96 | } |
| 97 | |
| 98 | PyDoc_STRVAR(stringlib_rjust__doc__, |
| 99 | "rjust($self, width, fillchar=b\' \', /)\n" |
| 100 | "--\n" |
| 101 | "\n" |
| 102 | "Return a right-justified string of length width.\n" |
| 103 | "\n" |
| 104 | "Padding is done using the specified fill character."); |
| 105 | |
| 106 | #define STRINGLIB_RJUST_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 107 | {"rjust", (PyCFunction)(void(*)(void))stringlib_rjust, METH_FASTCALL, stringlib_rjust__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 108 | |
| 109 | static PyObject * |
| 110 | stringlib_rjust_impl(PyObject *self, Py_ssize_t width, char fillchar); |
| 111 | |
| 112 | static PyObject * |
| 113 | stringlib_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
| 114 | { |
| 115 | PyObject *return_value = NULL; |
| 116 | Py_ssize_t width; |
| 117 | char fillchar = ' '; |
| 118 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame^] | 119 | if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) { |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 120 | goto exit; |
| 121 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame^] | 122 | if (PyFloat_Check(args[0])) { |
| 123 | PyErr_SetString(PyExc_TypeError, |
| 124 | "integer argument expected, got float" ); |
| 125 | goto exit; |
| 126 | } |
| 127 | { |
| 128 | Py_ssize_t ival = -1; |
| 129 | PyObject *iobj = PyNumber_Index(args[0]); |
| 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 { |
| 149 | _PyArg_BadArgument("rjust", 2, "a byte string of length 1", args[1]); |
| 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 | if (PyFloat_Check(args[0])) { |
| 184 | PyErr_SetString(PyExc_TypeError, |
| 185 | "integer argument expected, got float" ); |
| 186 | goto exit; |
| 187 | } |
| 188 | { |
| 189 | Py_ssize_t ival = -1; |
| 190 | PyObject *iobj = PyNumber_Index(args[0]); |
| 191 | if (iobj != NULL) { |
| 192 | ival = PyLong_AsSsize_t(iobj); |
| 193 | Py_DECREF(iobj); |
| 194 | } |
| 195 | if (ival == -1 && PyErr_Occurred()) { |
| 196 | goto exit; |
| 197 | } |
| 198 | width = ival; |
| 199 | } |
| 200 | if (nargs < 2) { |
| 201 | goto skip_optional; |
| 202 | } |
| 203 | if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) { |
| 204 | fillchar = PyBytes_AS_STRING(args[1])[0]; |
| 205 | } |
| 206 | else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) { |
| 207 | fillchar = PyByteArray_AS_STRING(args[1])[0]; |
| 208 | } |
| 209 | else { |
| 210 | _PyArg_BadArgument("center", 2, "a byte string of length 1", args[1]); |
| 211 | goto exit; |
| 212 | } |
| 213 | skip_optional: |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 214 | return_value = stringlib_center_impl(self, width, fillchar); |
| 215 | |
| 216 | exit: |
| 217 | return return_value; |
| 218 | } |
| 219 | |
| 220 | PyDoc_STRVAR(stringlib_zfill__doc__, |
| 221 | "zfill($self, width, /)\n" |
| 222 | "--\n" |
| 223 | "\n" |
| 224 | "Pad a numeric string with zeros on the left, to fill a field of the given width.\n" |
| 225 | "\n" |
| 226 | "The original string is never truncated."); |
| 227 | |
| 228 | #define STRINGLIB_ZFILL_METHODDEF \ |
| 229 | {"zfill", (PyCFunction)stringlib_zfill, METH_O, stringlib_zfill__doc__}, |
| 230 | |
| 231 | static PyObject * |
| 232 | stringlib_zfill_impl(PyObject *self, Py_ssize_t width); |
| 233 | |
| 234 | static PyObject * |
| 235 | stringlib_zfill(PyObject *self, PyObject *arg) |
| 236 | { |
| 237 | PyObject *return_value = NULL; |
| 238 | Py_ssize_t width; |
| 239 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 240 | if (PyFloat_Check(arg)) { |
| 241 | PyErr_SetString(PyExc_TypeError, |
| 242 | "integer argument expected, got float" ); |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 243 | goto exit; |
| 244 | } |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 245 | { |
| 246 | Py_ssize_t ival = -1; |
| 247 | PyObject *iobj = PyNumber_Index(arg); |
| 248 | if (iobj != NULL) { |
| 249 | ival = PyLong_AsSsize_t(iobj); |
| 250 | Py_DECREF(iobj); |
| 251 | } |
| 252 | if (ival == -1 && PyErr_Occurred()) { |
| 253 | goto exit; |
| 254 | } |
| 255 | width = ival; |
| 256 | } |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 257 | return_value = stringlib_zfill_impl(self, width); |
| 258 | |
| 259 | exit: |
| 260 | return return_value; |
| 261 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame^] | 262 | /*[clinic end generated code: output=787248a980f6a00e input=a9049054013a1b77]*/ |