blob: 7b7fd58c7e7a85aa42e2032147fd532e8d21ba92 [file] [log] [blame]
Tal Einatc929df32018-07-06 13:17:38 +03001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020014 {"expandtabs", (PyCFunction)(void(*)(void))stringlib_expandtabs, METH_FASTCALL|METH_KEYWORDS, stringlib_expandtabs__doc__},
Tal Einatc929df32018-07-06 13:17:38 +030015
16static PyObject *
17stringlib_expandtabs_impl(PyObject *self, int tabsize);
18
19static PyObject *
20stringlib_expandtabs(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
21{
22 PyObject *return_value = NULL;
23 static const char * const _keywords[] = {"tabsize", NULL};
24 static _PyArg_Parser _parser = {"|i:expandtabs", _keywords, 0};
25 int tabsize = 8;
26
27 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
28 &tabsize)) {
29 goto exit;
30 }
31 return_value = stringlib_expandtabs_impl(self, tabsize);
32
33exit:
34 return return_value;
35}
36
37PyDoc_STRVAR(stringlib_ljust__doc__,
38"ljust($self, width, fillchar=b\' \', /)\n"
39"--\n"
40"\n"
41"Return a left-justified string of length width.\n"
42"\n"
43"Padding is done using the specified fill character.");
44
45#define STRINGLIB_LJUST_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020046 {"ljust", (PyCFunction)(void(*)(void))stringlib_ljust, METH_FASTCALL, stringlib_ljust__doc__},
Tal Einatc929df32018-07-06 13:17:38 +030047
48static PyObject *
49stringlib_ljust_impl(PyObject *self, Py_ssize_t width, char fillchar);
50
51static PyObject *
52stringlib_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
53{
54 PyObject *return_value = NULL;
55 Py_ssize_t width;
56 char fillchar = ' ';
57
58 if (!_PyArg_ParseStack(args, nargs, "n|c:ljust",
59 &width, &fillchar)) {
60 goto exit;
61 }
62 return_value = stringlib_ljust_impl(self, width, fillchar);
63
64exit:
65 return return_value;
66}
67
68PyDoc_STRVAR(stringlib_rjust__doc__,
69"rjust($self, width, fillchar=b\' \', /)\n"
70"--\n"
71"\n"
72"Return a right-justified string of length width.\n"
73"\n"
74"Padding is done using the specified fill character.");
75
76#define STRINGLIB_RJUST_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020077 {"rjust", (PyCFunction)(void(*)(void))stringlib_rjust, METH_FASTCALL, stringlib_rjust__doc__},
Tal Einatc929df32018-07-06 13:17:38 +030078
79static PyObject *
80stringlib_rjust_impl(PyObject *self, Py_ssize_t width, char fillchar);
81
82static PyObject *
83stringlib_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
84{
85 PyObject *return_value = NULL;
86 Py_ssize_t width;
87 char fillchar = ' ';
88
89 if (!_PyArg_ParseStack(args, nargs, "n|c:rjust",
90 &width, &fillchar)) {
91 goto exit;
92 }
93 return_value = stringlib_rjust_impl(self, width, fillchar);
94
95exit:
96 return return_value;
97}
98
99PyDoc_STRVAR(stringlib_center__doc__,
100"center($self, width, fillchar=b\' \', /)\n"
101"--\n"
102"\n"
103"Return a centered string of length width.\n"
104"\n"
105"Padding is done using the specified fill character.");
106
107#define STRINGLIB_CENTER_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200108 {"center", (PyCFunction)(void(*)(void))stringlib_center, METH_FASTCALL, stringlib_center__doc__},
Tal Einatc929df32018-07-06 13:17:38 +0300109
110static PyObject *
111stringlib_center_impl(PyObject *self, Py_ssize_t width, char fillchar);
112
113static PyObject *
114stringlib_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
115{
116 PyObject *return_value = NULL;
117 Py_ssize_t width;
118 char fillchar = ' ';
119
120 if (!_PyArg_ParseStack(args, nargs, "n|c:center",
121 &width, &fillchar)) {
122 goto exit;
123 }
124 return_value = stringlib_center_impl(self, width, fillchar);
125
126exit:
127 return return_value;
128}
129
130PyDoc_STRVAR(stringlib_zfill__doc__,
131"zfill($self, width, /)\n"
132"--\n"
133"\n"
134"Pad a numeric string with zeros on the left, to fill a field of the given width.\n"
135"\n"
136"The original string is never truncated.");
137
138#define STRINGLIB_ZFILL_METHODDEF \
139 {"zfill", (PyCFunction)stringlib_zfill, METH_O, stringlib_zfill__doc__},
140
141static PyObject *
142stringlib_zfill_impl(PyObject *self, Py_ssize_t width);
143
144static PyObject *
145stringlib_zfill(PyObject *self, PyObject *arg)
146{
147 PyObject *return_value = NULL;
148 Py_ssize_t width;
149
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200150 if (PyFloat_Check(arg)) {
151 PyErr_SetString(PyExc_TypeError,
152 "integer argument expected, got float" );
Tal Einatc929df32018-07-06 13:17:38 +0300153 goto exit;
154 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200155 {
156 Py_ssize_t ival = -1;
157 PyObject *iobj = PyNumber_Index(arg);
158 if (iobj != NULL) {
159 ival = PyLong_AsSsize_t(iobj);
160 Py_DECREF(iobj);
161 }
162 if (ival == -1 && PyErr_Occurred()) {
163 goto exit;
164 }
165 width = ival;
166 }
Tal Einatc929df32018-07-06 13:17:38 +0300167 return_value = stringlib_zfill_impl(self, width);
168
169exit:
170 return return_value;
171}
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200172/*[clinic end generated code: output=bf2ef501639e1190 input=a9049054013a1b77]*/