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