blob: 0c53a75b1bf834139273457843be702507187ded [file] [log] [blame]
Tal Einatc929df32018-07-06 13:17:38 +03001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_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 Storchaka4a934d42018-11-27 11:27:36 +020014 {"expandtabs", (PyCFunction)(void(*)(void))stringlib_expandtabs, METH_FASTCALL|METH_KEYWORDS, stringlib_expandtabs__doc__},
Tal Einatc929df32018-07-06 13:17:38 +030015
16static PyObject *
17stringlib_expandtabs_impl(PyObject *self, int tabsize);
18
19static PyObject *
20stringlib_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 Storchaka31913912019-03-14 10:32:22 +020024 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 Einatc929df32018-07-06 13:17:38 +030027 int tabsize = 8;
28
Serhiy Storchaka31913912019-03-14 10:32:22 +020029 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
30 if (!args) {
Tal Einatc929df32018-07-06 13:17:38 +030031 goto exit;
32 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020033 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 }
45skip_optional_pos:
Tal Einatc929df32018-07-06 13:17:38 +030046 return_value = stringlib_expandtabs_impl(self, tabsize);
47
48exit:
49 return return_value;
50}
51
52PyDoc_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 Storchaka4a934d42018-11-27 11:27:36 +020061 {"ljust", (PyCFunction)(void(*)(void))stringlib_ljust, METH_FASTCALL, stringlib_ljust__doc__},
Tal Einatc929df32018-07-06 13:17:38 +030062
63static PyObject *
64stringlib_ljust_impl(PyObject *self, Py_ssize_t width, char fillchar);
65
66static PyObject *
67stringlib_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 Storchaka4fa95912019-01-11 16:01:14 +020073 if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) {
Tal Einatc929df32018-07-06 13:17:38 +030074 goto exit;
75 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020076 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 }
106skip_optional:
Tal Einatc929df32018-07-06 13:17:38 +0300107 return_value = stringlib_ljust_impl(self, width, fillchar);
108
109exit:
110 return return_value;
111}
112
113PyDoc_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 Storchaka4a934d42018-11-27 11:27:36 +0200122 {"rjust", (PyCFunction)(void(*)(void))stringlib_rjust, METH_FASTCALL, stringlib_rjust__doc__},
Tal Einatc929df32018-07-06 13:17:38 +0300123
124static PyObject *
125stringlib_rjust_impl(PyObject *self, Py_ssize_t width, char fillchar);
126
127static PyObject *
128stringlib_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 Storchaka4fa95912019-01-11 16:01:14 +0200134 if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) {
Tal Einatc929df32018-07-06 13:17:38 +0300135 goto exit;
136 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200137 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 }
167skip_optional:
Tal Einatc929df32018-07-06 13:17:38 +0300168 return_value = stringlib_rjust_impl(self, width, fillchar);
169
170exit:
171 return return_value;
172}
173
174PyDoc_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 Storchaka4a934d42018-11-27 11:27:36 +0200183 {"center", (PyCFunction)(void(*)(void))stringlib_center, METH_FASTCALL, stringlib_center__doc__},
Tal Einatc929df32018-07-06 13:17:38 +0300184
185static PyObject *
186stringlib_center_impl(PyObject *self, Py_ssize_t width, char fillchar);
187
188static PyObject *
189stringlib_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 Storchaka4fa95912019-01-11 16:01:14 +0200195 if (!_PyArg_CheckPositional("center", nargs, 1, 2)) {
Tal Einatc929df32018-07-06 13:17:38 +0300196 goto exit;
197 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200198 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 }
228skip_optional:
Tal Einatc929df32018-07-06 13:17:38 +0300229 return_value = stringlib_center_impl(self, width, fillchar);
230
231exit:
232 return return_value;
233}
234
235PyDoc_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
246static PyObject *
247stringlib_zfill_impl(PyObject *self, Py_ssize_t width);
248
249static PyObject *
250stringlib_zfill(PyObject *self, PyObject *arg)
251{
252 PyObject *return_value = NULL;
253 Py_ssize_t width;
254
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200255 if (PyFloat_Check(arg)) {
256 PyErr_SetString(PyExc_TypeError,
257 "integer argument expected, got float" );
Tal Einatc929df32018-07-06 13:17:38 +0300258 goto exit;
259 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200260 {
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 Einatc929df32018-07-06 13:17:38 +0300272 return_value = stringlib_zfill_impl(self, width);
273
274exit:
275 return return_value;
276}
Serhiy Storchaka31913912019-03-14 10:32:22 +0200277/*[clinic end generated code: output=96cbb19b238d0e84 input=a9049054013a1b77]*/