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