Debian Codegraph @

Objects/stringlib/clinic/transmogrify.h.h

raw
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 *
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 Py_hash_t ob_hash;
37 PyObject *ob_item[NUM_KEYWORDS];
38 } _kwtuple = {
39 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
40 .ob_hash = -1,
41 .ob_item = { &_Py_ID(tabsize), },
42 };
43 #undef NUM_KEYWORDS
44 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
45
46 #else // !Py_BUILD_CORE
47 # define KWTUPLE NULL
48 #endif // !Py_BUILD_CORE
49
50 static const char * const _keywords[] = {"tabsize", NULL};
51 static _PyArg_Parser _parser = {
52 .keywords = _keywords,
53 .fname = "expandtabs",
54 .kwtuple = KWTUPLE,
55 };
56 #undef KWTUPLE
57 PyObject *argsbuf[1];
58 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
59 int tabsize = 8;
60
61 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
62 /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
63 if (!args) {
64 goto exit;
65 }
66 if (!noptargs) {
67 goto skip_optional_pos;
68 }
69 tabsize = PyLong_AsInt(args[0]);
70 if (tabsize == -1 && PyErr_Occurred()) {
71 goto exit;
72 }
73 skip_optional_pos:
74 return_value = stringlib_expandtabs_impl(self, tabsize);
75
76 exit:
77 return return_value;
78 }
79
80 PyDoc_STRVAR(stringlib_ljust__doc__,
81 "ljust($self, width, fillchar=b\' \', /)\n"
82 "--\n"
83 "\n"
84 "Return a left-justified string of length width.\n"
85 "\n"
86 "Padding is done using the specified fill character.");
87
88 #define STRINGLIB_LJUST_METHODDEF \
89 {"ljust", _PyCFunction_CAST(stringlib_ljust), METH_FASTCALL, stringlib_ljust__doc__},
90
91 static PyObject *
92 stringlib_ljust_impl(PyObject *self, Py_ssize_t width, char fillchar);
93
94 static PyObject *
95 stringlib_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
96 {
97 PyObject *return_value = NULL;
98 Py_ssize_t width;
99 char fillchar = ' ';
100
101 if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) {
102 goto exit;
103 }
104 {
105 Py_ssize_t ival = -1;
106 PyObject *iobj = _PyNumber_Index(args[0]);
107 if (iobj != NULL) {
108 ival = PyLong_AsSsize_t(iobj);
109 Py_DECREF(iobj);
110 }
111 if (ival == -1 && PyErr_Occurred()) {
112 goto exit;
113 }
114 width = ival;
115 }
116 if (nargs < 2) {
117 goto skip_optional;
118 }
119 if (PyBytes_Check(args[1])) {
120 if (PyBytes_GET_SIZE(args[1]) != 1) {
121 PyErr_Format(PyExc_TypeError,
122 "ljust(): argument 2 must be a byte string of length 1, "
123 "not a bytes object of length %zd",
124 PyBytes_GET_SIZE(args[1]));
125 goto exit;
126 }
127 fillchar = PyBytes_AS_STRING(args[1])[0];
128 }
129 else if (PyByteArray_Check(args[1])) {
130 if (PyByteArray_GET_SIZE(args[1]) != 1) {
131 PyErr_Format(PyExc_TypeError,
132 "ljust(): argument 2 must be a byte string of length 1, "
133 "not a bytearray object of length %zd",
134 PyByteArray_GET_SIZE(args[1]));
135 goto exit;
136 }
137 fillchar = PyByteArray_AS_STRING(args[1])[0];
138 }
139 else {
140 _PyArg_BadArgument("ljust", "argument 2", "a byte string of length 1", args[1]);
141 goto exit;
142 }
143 skip_optional:
144 return_value = stringlib_ljust_impl(self, width, fillchar);
145
146 exit:
147 return return_value;
148 }
149
150 PyDoc_STRVAR(stringlib_rjust__doc__,
151 "rjust($self, width, fillchar=b\' \', /)\n"
152 "--\n"
153 "\n"
154 "Return a right-justified string of length width.\n"
155 "\n"
156 "Padding is done using the specified fill character.");
157
158 #define STRINGLIB_RJUST_METHODDEF \
159 {"rjust", _PyCFunction_CAST(stringlib_rjust), METH_FASTCALL, stringlib_rjust__doc__},
160
161 static PyObject *
162 stringlib_rjust_impl(PyObject *self, Py_ssize_t width, char fillchar);
163
164 static PyObject *
165 stringlib_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
166 {
167 PyObject *return_value = NULL;
168 Py_ssize_t width;
169 char fillchar = ' ';
170
171 if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) {
172 goto exit;
173 }
174 {
175 Py_ssize_t ival = -1;
176 PyObject *iobj = _PyNumber_Index(args[0]);
177 if (iobj != NULL) {
178 ival = PyLong_AsSsize_t(iobj);
179 Py_DECREF(iobj);
180 }
181 if (ival == -1 && PyErr_Occurred()) {
182 goto exit;
183 }
184 width = ival;
185 }
186 if (nargs < 2) {
187 goto skip_optional;
188 }
189 if (PyBytes_Check(args[1])) {
190 if (PyBytes_GET_SIZE(args[1]) != 1) {
191 PyErr_Format(PyExc_TypeError,
192 "rjust(): argument 2 must be a byte string of length 1, "
193 "not a bytes object of length %zd",
194 PyBytes_GET_SIZE(args[1]));
195 goto exit;
196 }
197 fillchar = PyBytes_AS_STRING(args[1])[0];
198 }
199 else if (PyByteArray_Check(args[1])) {
200 if (PyByteArray_GET_SIZE(args[1]) != 1) {
201 PyErr_Format(PyExc_TypeError,
202 "rjust(): argument 2 must be a byte string of length 1, "
203 "not a bytearray object of length %zd",
204 PyByteArray_GET_SIZE(args[1]));
205 goto exit;
206 }
207 fillchar = PyByteArray_AS_STRING(args[1])[0];
208 }
209 else {
210 _PyArg_BadArgument("rjust", "argument 2", "a byte string of length 1", args[1]);
211 goto exit;
212 }
213 skip_optional:
214 return_value = stringlib_rjust_impl(self, width, fillchar);
215
216 exit:
217 return return_value;
218 }
219
220 PyDoc_STRVAR(stringlib_center__doc__,
221 "center($self, width, fillchar=b\' \', /)\n"
222 "--\n"
223 "\n"
224 "Return a centered string of length width.\n"
225 "\n"
226 "Padding is done using the specified fill character.");
227
228 #define STRINGLIB_CENTER_METHODDEF \
229 {"center", _PyCFunction_CAST(stringlib_center), METH_FASTCALL, stringlib_center__doc__},
230
231 static PyObject *
232 stringlib_center_impl(PyObject *self, Py_ssize_t width, char fillchar);
233
234 static PyObject *
235 stringlib_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
236 {
237 PyObject *return_value = NULL;
238 Py_ssize_t width;
239 char fillchar = ' ';
240
241 if (!_PyArg_CheckPositional("center", nargs, 1, 2)) {
242 goto exit;
243 }
244 {
245 Py_ssize_t ival = -1;
246 PyObject *iobj = _PyNumber_Index(args[0]);
247 if (iobj != NULL) {
248 ival = PyLong_AsSsize_t(iobj);
249 Py_DECREF(iobj);
250 }
251 if (ival == -1 && PyErr_Occurred()) {
252 goto exit;
253 }
254 width = ival;
255 }
256 if (nargs < 2) {
257 goto skip_optional;
258 }
259 if (PyBytes_Check(args[1])) {
260 if (PyBytes_GET_SIZE(args[1]) != 1) {
261 PyErr_Format(PyExc_TypeError,
262 "center(): argument 2 must be a byte string of length 1, "
263 "not a bytes object of length %zd",
264 PyBytes_GET_SIZE(args[1]));
265 goto exit;
266 }
267 fillchar = PyBytes_AS_STRING(args[1])[0];
268 }
269 else if (PyByteArray_Check(args[1])) {
270 if (PyByteArray_GET_SIZE(args[1]) != 1) {
271 PyErr_Format(PyExc_TypeError,
272 "center(): argument 2 must be a byte string of length 1, "
273 "not a bytearray object of length %zd",
274 PyByteArray_GET_SIZE(args[1]));
275 goto exit;
276 }
277 fillchar = PyByteArray_AS_STRING(args[1])[0];
278 }
279 else {
280 _PyArg_BadArgument("center", "argument 2", "a byte string of length 1", args[1]);
281 goto exit;
282 }
283 skip_optional:
284 return_value = stringlib_center_impl(self, width, fillchar);
285
286 exit:
287 return return_value;
288 }
289
290 PyDoc_STRVAR(stringlib_zfill__doc__,
291 "zfill($self, width, /)\n"
292 "--\n"
293 "\n"
294 "Pad a numeric string with zeros on the left, to fill a field of the given width.\n"
295 "\n"
296 "The original string is never truncated.");
297
298 #define STRINGLIB_ZFILL_METHODDEF \
299 {"zfill", (PyCFunction)stringlib_zfill, METH_O, stringlib_zfill__doc__},
300
301 static PyObject *
302 stringlib_zfill_impl(PyObject *self, Py_ssize_t width);
303
304 static PyObject *
305 stringlib_zfill(PyObject *self, PyObject *arg)
306 {
307 PyObject *return_value = NULL;
308 Py_ssize_t width;
309
310 {
311 Py_ssize_t ival = -1;
312 PyObject *iobj = _PyNumber_Index(arg);
313 if (iobj != NULL) {
314 ival = PyLong_AsSsize_t(iobj);
315 Py_DECREF(iobj);
316 }
317 if (ival == -1 && PyErr_Occurred()) {
318 goto exit;
319 }
320 width = ival;
321 }
322 return_value = stringlib_zfill_impl(self, width);
323
324 exit:
325 return return_value;
326 }
327 /*[clinic end generated code: output=8363b4b6cdaad556 input=a9049054013a1b77]*/