/src/cpython/Objects/stringlib/clinic/transmogrify.h.h
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
29 | 0 | PyObject *return_value = NULL; |
30 | 0 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
31 | |
|
32 | 0 | #define NUM_KEYWORDS 1 |
33 | 0 | static struct { |
34 | 0 | PyGC_Head _this_is_not_used; |
35 | 0 | PyObject_VAR_HEAD |
36 | 0 | Py_hash_t ob_hash; |
37 | 0 | PyObject *ob_item[NUM_KEYWORDS]; |
38 | 0 | } _kwtuple = { |
39 | 0 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
40 | 0 | .ob_hash = -1, |
41 | 0 | .ob_item = { &_Py_ID(tabsize), }, |
42 | 0 | }; |
43 | 0 | #undef NUM_KEYWORDS |
44 | 0 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
45 | |
|
46 | | #else // !Py_BUILD_CORE |
47 | | # define KWTUPLE NULL |
48 | | #endif // !Py_BUILD_CORE |
49 | |
|
50 | 0 | static const char * const _keywords[] = {"tabsize", NULL}; |
51 | 0 | static _PyArg_Parser _parser = { |
52 | 0 | .keywords = _keywords, |
53 | 0 | .fname = "expandtabs", |
54 | 0 | .kwtuple = KWTUPLE, |
55 | 0 | }; |
56 | 0 | #undef KWTUPLE |
57 | 0 | PyObject *argsbuf[1]; |
58 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
59 | 0 | int tabsize = 8; |
60 | |
|
61 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
62 | 0 | /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
63 | 0 | if (!args) { |
64 | 0 | goto exit; |
65 | 0 | } |
66 | 0 | if (!noptargs) { |
67 | 0 | goto skip_optional_pos; |
68 | 0 | } |
69 | 0 | tabsize = PyLong_AsInt(args[0]); |
70 | 0 | if (tabsize == -1 && PyErr_Occurred()) { |
71 | 0 | goto exit; |
72 | 0 | } |
73 | 0 | skip_optional_pos: |
74 | 0 | return_value = stringlib_expandtabs_impl(self, tabsize); |
75 | |
|
76 | 0 | exit: |
77 | 0 | return return_value; |
78 | 0 | } Unexecuted instantiation: bytesobject.c:stringlib_expandtabs Unexecuted instantiation: bytearrayobject.c:stringlib_expandtabs |
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 | 0 | { |
97 | 0 | PyObject *return_value = NULL; |
98 | 0 | Py_ssize_t width; |
99 | 0 | char fillchar = ' '; |
100 | |
|
101 | 0 | if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) { |
102 | 0 | goto exit; |
103 | 0 | } |
104 | 0 | { |
105 | 0 | Py_ssize_t ival = -1; |
106 | 0 | PyObject *iobj = _PyNumber_Index(args[0]); |
107 | 0 | if (iobj != NULL) { |
108 | 0 | ival = PyLong_AsSsize_t(iobj); |
109 | 0 | Py_DECREF(iobj); |
110 | 0 | } |
111 | 0 | if (ival == -1 && PyErr_Occurred()) { |
112 | 0 | goto exit; |
113 | 0 | } |
114 | 0 | width = ival; |
115 | 0 | } |
116 | 0 | if (nargs < 2) { |
117 | 0 | goto skip_optional; |
118 | 0 | } |
119 | 0 | if (PyBytes_Check(args[1])) { |
120 | 0 | if (PyBytes_GET_SIZE(args[1]) != 1) { |
121 | 0 | PyErr_Format(PyExc_TypeError, |
122 | 0 | "ljust(): argument 2 must be a byte string of length 1, " |
123 | 0 | "not a bytes object of length %zd", |
124 | 0 | PyBytes_GET_SIZE(args[1])); |
125 | 0 | goto exit; |
126 | 0 | } |
127 | 0 | fillchar = PyBytes_AS_STRING(args[1])[0]; |
128 | 0 | } |
129 | 0 | else if (PyByteArray_Check(args[1])) { |
130 | 0 | if (PyByteArray_GET_SIZE(args[1]) != 1) { |
131 | 0 | PyErr_Format(PyExc_TypeError, |
132 | 0 | "ljust(): argument 2 must be a byte string of length 1, " |
133 | 0 | "not a bytearray object of length %zd", |
134 | 0 | PyByteArray_GET_SIZE(args[1])); |
135 | 0 | goto exit; |
136 | 0 | } |
137 | 0 | fillchar = PyByteArray_AS_STRING(args[1])[0]; |
138 | 0 | } |
139 | 0 | else { |
140 | 0 | _PyArg_BadArgument("ljust", "argument 2", "a byte string of length 1", args[1]); |
141 | 0 | goto exit; |
142 | 0 | } |
143 | 0 | skip_optional: |
144 | 0 | return_value = stringlib_ljust_impl(self, width, fillchar); |
145 | |
|
146 | 0 | exit: |
147 | 0 | return return_value; |
148 | 0 | } Unexecuted instantiation: bytesobject.c:stringlib_ljust Unexecuted instantiation: bytearrayobject.c:stringlib_ljust |
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 | 0 | { |
167 | 0 | PyObject *return_value = NULL; |
168 | 0 | Py_ssize_t width; |
169 | 0 | char fillchar = ' '; |
170 | |
|
171 | 0 | if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) { |
172 | 0 | goto exit; |
173 | 0 | } |
174 | 0 | { |
175 | 0 | Py_ssize_t ival = -1; |
176 | 0 | PyObject *iobj = _PyNumber_Index(args[0]); |
177 | 0 | if (iobj != NULL) { |
178 | 0 | ival = PyLong_AsSsize_t(iobj); |
179 | 0 | Py_DECREF(iobj); |
180 | 0 | } |
181 | 0 | if (ival == -1 && PyErr_Occurred()) { |
182 | 0 | goto exit; |
183 | 0 | } |
184 | 0 | width = ival; |
185 | 0 | } |
186 | 0 | if (nargs < 2) { |
187 | 0 | goto skip_optional; |
188 | 0 | } |
189 | 0 | if (PyBytes_Check(args[1])) { |
190 | 0 | if (PyBytes_GET_SIZE(args[1]) != 1) { |
191 | 0 | PyErr_Format(PyExc_TypeError, |
192 | 0 | "rjust(): argument 2 must be a byte string of length 1, " |
193 | 0 | "not a bytes object of length %zd", |
194 | 0 | PyBytes_GET_SIZE(args[1])); |
195 | 0 | goto exit; |
196 | 0 | } |
197 | 0 | fillchar = PyBytes_AS_STRING(args[1])[0]; |
198 | 0 | } |
199 | 0 | else if (PyByteArray_Check(args[1])) { |
200 | 0 | if (PyByteArray_GET_SIZE(args[1]) != 1) { |
201 | 0 | PyErr_Format(PyExc_TypeError, |
202 | 0 | "rjust(): argument 2 must be a byte string of length 1, " |
203 | 0 | "not a bytearray object of length %zd", |
204 | 0 | PyByteArray_GET_SIZE(args[1])); |
205 | 0 | goto exit; |
206 | 0 | } |
207 | 0 | fillchar = PyByteArray_AS_STRING(args[1])[0]; |
208 | 0 | } |
209 | 0 | else { |
210 | 0 | _PyArg_BadArgument("rjust", "argument 2", "a byte string of length 1", args[1]); |
211 | 0 | goto exit; |
212 | 0 | } |
213 | 0 | skip_optional: |
214 | 0 | return_value = stringlib_rjust_impl(self, width, fillchar); |
215 | |
|
216 | 0 | exit: |
217 | 0 | return return_value; |
218 | 0 | } Unexecuted instantiation: bytesobject.c:stringlib_rjust Unexecuted instantiation: bytearrayobject.c:stringlib_rjust |
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 | 0 | { |
237 | 0 | PyObject *return_value = NULL; |
238 | 0 | Py_ssize_t width; |
239 | 0 | char fillchar = ' '; |
240 | |
|
241 | 0 | if (!_PyArg_CheckPositional("center", nargs, 1, 2)) { |
242 | 0 | goto exit; |
243 | 0 | } |
244 | 0 | { |
245 | 0 | Py_ssize_t ival = -1; |
246 | 0 | PyObject *iobj = _PyNumber_Index(args[0]); |
247 | 0 | if (iobj != NULL) { |
248 | 0 | ival = PyLong_AsSsize_t(iobj); |
249 | 0 | Py_DECREF(iobj); |
250 | 0 | } |
251 | 0 | if (ival == -1 && PyErr_Occurred()) { |
252 | 0 | goto exit; |
253 | 0 | } |
254 | 0 | width = ival; |
255 | 0 | } |
256 | 0 | if (nargs < 2) { |
257 | 0 | goto skip_optional; |
258 | 0 | } |
259 | 0 | if (PyBytes_Check(args[1])) { |
260 | 0 | if (PyBytes_GET_SIZE(args[1]) != 1) { |
261 | 0 | PyErr_Format(PyExc_TypeError, |
262 | 0 | "center(): argument 2 must be a byte string of length 1, " |
263 | 0 | "not a bytes object of length %zd", |
264 | 0 | PyBytes_GET_SIZE(args[1])); |
265 | 0 | goto exit; |
266 | 0 | } |
267 | 0 | fillchar = PyBytes_AS_STRING(args[1])[0]; |
268 | 0 | } |
269 | 0 | else if (PyByteArray_Check(args[1])) { |
270 | 0 | if (PyByteArray_GET_SIZE(args[1]) != 1) { |
271 | 0 | PyErr_Format(PyExc_TypeError, |
272 | 0 | "center(): argument 2 must be a byte string of length 1, " |
273 | 0 | "not a bytearray object of length %zd", |
274 | 0 | PyByteArray_GET_SIZE(args[1])); |
275 | 0 | goto exit; |
276 | 0 | } |
277 | 0 | fillchar = PyByteArray_AS_STRING(args[1])[0]; |
278 | 0 | } |
279 | 0 | else { |
280 | 0 | _PyArg_BadArgument("center", "argument 2", "a byte string of length 1", args[1]); |
281 | 0 | goto exit; |
282 | 0 | } |
283 | 0 | skip_optional: |
284 | 0 | return_value = stringlib_center_impl(self, width, fillchar); |
285 | |
|
286 | 0 | exit: |
287 | 0 | return return_value; |
288 | 0 | } Unexecuted instantiation: bytesobject.c:stringlib_center Unexecuted instantiation: bytearrayobject.c:stringlib_center |
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 | 0 | { |
307 | 0 | PyObject *return_value = NULL; |
308 | 0 | Py_ssize_t width; |
309 | |
|
310 | 0 | { |
311 | 0 | Py_ssize_t ival = -1; |
312 | 0 | PyObject *iobj = _PyNumber_Index(arg); |
313 | 0 | if (iobj != NULL) { |
314 | 0 | ival = PyLong_AsSsize_t(iobj); |
315 | 0 | Py_DECREF(iobj); |
316 | 0 | } |
317 | 0 | if (ival == -1 && PyErr_Occurred()) { |
318 | 0 | goto exit; |
319 | 0 | } |
320 | 0 | width = ival; |
321 | 0 | } |
322 | 0 | return_value = stringlib_zfill_impl(self, width); |
323 | |
|
324 | 0 | exit: |
325 | 0 | return return_value; |
326 | 0 | } Unexecuted instantiation: bytesobject.c:stringlib_zfill Unexecuted instantiation: bytearrayobject.c:stringlib_zfill |
327 | | /*[clinic end generated code: output=8363b4b6cdaad556 input=a9049054013a1b77]*/ |