blob: 15c43af61a284e20828b7795144d0b8880bc04d0 [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};
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
33exit:
34 return return_value;
35}
36
37PyDoc_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 Storchaka4a934d42018-11-27 11:27:36 +020046 {"ljust", (PyCFunction)(void(*)(void))stringlib_ljust, METH_FASTCALL, stringlib_ljust__doc__},
Tal Einatc929df32018-07-06 13:17:38 +030047
48static PyObject *
49stringlib_ljust_impl(PyObject *self, Py_ssize_t width, char fillchar);
50
51static PyObject *
52stringlib_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 Storchaka4fa95912019-01-11 16:01:14 +020058 if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) {
Tal Einatc929df32018-07-06 13:17:38 +030059 goto exit;
60 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020061 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 }
91skip_optional:
Tal Einatc929df32018-07-06 13:17:38 +030092 return_value = stringlib_ljust_impl(self, width, fillchar);
93
94exit:
95 return return_value;
96}
97
98PyDoc_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 Storchaka4a934d42018-11-27 11:27:36 +0200107 {"rjust", (PyCFunction)(void(*)(void))stringlib_rjust, METH_FASTCALL, stringlib_rjust__doc__},
Tal Einatc929df32018-07-06 13:17:38 +0300108
109static PyObject *
110stringlib_rjust_impl(PyObject *self, Py_ssize_t width, char fillchar);
111
112static PyObject *
113stringlib_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 Storchaka4fa95912019-01-11 16:01:14 +0200119 if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) {
Tal Einatc929df32018-07-06 13:17:38 +0300120 goto exit;
121 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200122 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 }
152skip_optional:
Tal Einatc929df32018-07-06 13:17:38 +0300153 return_value = stringlib_rjust_impl(self, width, fillchar);
154
155exit:
156 return return_value;
157}
158
159PyDoc_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 Storchaka4a934d42018-11-27 11:27:36 +0200168 {"center", (PyCFunction)(void(*)(void))stringlib_center, METH_FASTCALL, stringlib_center__doc__},
Tal Einatc929df32018-07-06 13:17:38 +0300169
170static PyObject *
171stringlib_center_impl(PyObject *self, Py_ssize_t width, char fillchar);
172
173static PyObject *
174stringlib_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 Storchaka4fa95912019-01-11 16:01:14 +0200180 if (!_PyArg_CheckPositional("center", nargs, 1, 2)) {
Tal Einatc929df32018-07-06 13:17:38 +0300181 goto exit;
182 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200183 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 }
213skip_optional:
Tal Einatc929df32018-07-06 13:17:38 +0300214 return_value = stringlib_center_impl(self, width, fillchar);
215
216exit:
217 return return_value;
218}
219
220PyDoc_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
231static PyObject *
232stringlib_zfill_impl(PyObject *self, Py_ssize_t width);
233
234static PyObject *
235stringlib_zfill(PyObject *self, PyObject *arg)
236{
237 PyObject *return_value = NULL;
238 Py_ssize_t width;
239
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200240 if (PyFloat_Check(arg)) {
241 PyErr_SetString(PyExc_TypeError,
242 "integer argument expected, got float" );
Tal Einatc929df32018-07-06 13:17:38 +0300243 goto exit;
244 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200245 {
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 Einatc929df32018-07-06 13:17:38 +0300257 return_value = stringlib_zfill_impl(self, width);
258
259exit:
260 return return_value;
261}
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200262/*[clinic end generated code: output=787248a980f6a00e input=a9049054013a1b77]*/