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