Skip to content

Commit 18b2ecf

Browse files
committed
Update bytes_ops.c
1 parent 9380ae2 commit 18b2ecf

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

mypyc/lib-rt/bytes_ops.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,31 @@ CPyTagged CPyBytes_Ord(PyObject *obj) {
164164
return CPY_INT_TAG;
165165
}
166166

167+
168+
PyObject *CPyBytes_RjustDefaultFill(PyObject *self, CPyTagged width) {
169+
if (!PyBytes_Check(self)) {
170+
PyErr_SetString(PyExc_TypeError, "self must be bytes");
171+
return NULL;
172+
}
173+
Py_ssize_t width_size_t = CPyTagged_AsSsize_t(width);
174+
Py_ssize_t len = PyBytes_Size(self);
175+
if (width_size_t <= len) {
176+
return PyBytes_FromStringAndSize(PyBytes_AsString(self), len);
177+
}
178+
// should this be a constant?
179+
PyObject *fillbyte = PyBytes_FromStringAndSize(" ", 1);
180+
// can we optimize out the above line and the below line?
181+
char fill = PyBytes_AsString(fillbyte)[0];
182+
Py_ssize_t pad = width_size_t - len;
183+
PyObject *result = PyBytes_FromStringAndSize(NULL, width_size_t);
184+
if (!result) return NULL;
185+
char *res_buf = PyBytes_AsString(result);
186+
memset(res_buf, fill, pad);
187+
memcpy(res_buf + pad, PyBytes_AsString(self), len);
188+
return result;
189+
}
190+
191+
167192
PyObject *CPyBytes_Rjust(PyObject *self, CPyTagged width, PyObject *fillbyte) {
168193
if (!PyBytes_Check(self)) {
169194
PyErr_SetString(PyExc_TypeError, "self must be bytes");
@@ -188,6 +213,31 @@ PyObject *CPyBytes_Rjust(PyObject *self, CPyTagged width, PyObject *fillbyte) {
188213
return result;
189214
}
190215

216+
217+
PyObject *CPyBytes_LjustDefaultFill(PyObject *self, CPyTagged width) {
218+
if (!PyBytes_Check(self)) {
219+
PyErr_SetString(PyExc_TypeError, "self must be bytes");
220+
return NULL;
221+
}
222+
Py_ssize_t width_size_t = CPyTagged_AsSsize_t(width);
223+
Py_ssize_t len = PyBytes_Size(self);
224+
if (width_size_t <= len) {
225+
return PyBytes_FromStringAndSize(PyBytes_AsString(self), len);
226+
}
227+
// should this be a constant?
228+
PyObject *fillbyte = PyBytes_FromStringAndSize(" ", 1);
229+
// can we optimize out the above line and the below line?
230+
char fill = PyBytes_AsString(fillbyte)[0];
231+
Py_ssize_t pad = width_size_t - len;
232+
PyObject *result = PyBytes_FromStringAndSize(NULL, width_size_t);
233+
if (!result) return NULL;
234+
char *res_buf = PyBytes_AsString(result);
235+
memcpy(res_buf, PyBytes_AsString(self), len);
236+
memset(res_buf + len, fill, pad);
237+
return result;
238+
}
239+
240+
191241
PyObject *CPyBytes_Ljust(PyObject *self, CPyTagged width, PyObject *fillbyte) {
192242
if (!PyBytes_Check(self)) {
193243
PyErr_SetString(PyExc_TypeError, "self must be bytes");

0 commit comments

Comments
 (0)