-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[mypyc] feat: new primitives for bytes.rjust
and bytes.ljust
#19672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BobTheBuidler
wants to merge
26
commits into
python:master
Choose a base branch
from
BobTheBuidler:just
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
ab75317
[mypyc] feat: new primitives for `bytes.rjust` and `bytes.ljust`
BobTheBuidler 02d76a5
fix: tests
BobTheBuidler 6453373
Update bytes_ops.c
BobTheBuidler 0c33019
fix: tests
BobTheBuidler 2baf425
add headers
BobTheBuidler 350db60
deatil asserts
BobTheBuidler ee1a76e
fix ir
BobTheBuidler e5d6e43
fix run tests
BobTheBuidler b936c3c
Update bytes_ops.c
BobTheBuidler c5ed8a4
Update bytes_ops.py
BobTheBuidler 3db102b
Update bytes_ops.c
BobTheBuidler 4fd876f
fix pyssizet
BobTheBuidler b82c603
fix headers
BobTheBuidler a106445
fix: ;
BobTheBuidler a44e9b5
fix ir
BobTheBuidler 10e5093
drop dupe tests
BobTheBuidler 600b276
Update bytes_ops.c
BobTheBuidler eaee990
Update CPy.h
BobTheBuidler 3e66f36
Update bytes_ops.py
BobTheBuidler 0edcb0d
Update bytes_ops.c
BobTheBuidler 1be3982
Update irbuild-bytes.test
BobTheBuidler c1caaa2
optimize c funcs
BobTheBuidler b28b6d1
fix ir
BobTheBuidler b63c06d
Merge branch 'master' into just
BobTheBuidler 5aee840
Merge branch 'master' into just
BobTheBuidler 51fe159
Merge branch 'master' into just
BobTheBuidler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
#include <Python.h> | ||
#include "CPy.h" | ||
#include <string.h> | ||
|
||
// Returns -1 on error, 0 on inequality, 1 on equality. | ||
// | ||
|
@@ -162,3 +163,97 @@ CPyTagged CPyBytes_Ord(PyObject *obj) { | |
PyErr_SetString(PyExc_TypeError, "ord() expects a character"); | ||
return CPY_INT_TAG; | ||
} | ||
|
||
|
||
PyObject *CPyBytes_RjustDefaultFill(PyObject *self, CPyTagged width) { | ||
if (!PyBytes_Check(self)) { | ||
PyErr_SetString(PyExc_TypeError, "self must be bytes"); | ||
return NULL; | ||
} | ||
Py_ssize_t width_size_t = CPyTagged_AsSsize_t(width); | ||
Py_ssize_t len = PyBytes_Size(self); | ||
if (width_size_t <= len) { | ||
Py_INCREF(self); | ||
return self; | ||
} | ||
Py_ssize_t pad = width_size_t - len; | ||
PyObject *result = PyBytes_FromStringAndSize(NULL, width_size_t); | ||
if (!result) return NULL; | ||
char *res_buf = PyBytes_AsString(result); | ||
memset(res_buf, ' ', pad); | ||
memcpy(res_buf + pad, PyBytes_AsString(self), len); | ||
return result; | ||
} | ||
|
||
|
||
PyObject *CPyBytes_RjustCustomFill(PyObject *self, CPyTagged width, PyObject *fillbyte) { | ||
if (!PyBytes_Check(self)) { | ||
PyErr_SetString(PyExc_TypeError, "self must be bytes"); | ||
return NULL; | ||
} | ||
if (!PyBytes_Check(fillbyte) || PyBytes_Size(fillbyte) != 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get rid of these type checks? |
||
PyErr_SetString(PyExc_TypeError, "fillbyte must be a single byte"); | ||
return NULL; | ||
} | ||
Py_ssize_t width_size_t = CPyTagged_AsSsize_t(width); | ||
Py_ssize_t len = PyBytes_Size(self); | ||
if (width_size_t <= len) { | ||
Py_INCREF(self); | ||
return self; | ||
} | ||
char fill = PyBytes_AsString(fillbyte)[0]; | ||
Py_ssize_t pad = width_size_t - len; | ||
PyObject *result = PyBytes_FromStringAndSize(NULL, width_size_t); | ||
if (!result) return NULL; | ||
char *res_buf = PyBytes_AsString(result); | ||
memset(res_buf, fill, pad); | ||
memcpy(res_buf + pad, PyBytes_AsString(self), len); | ||
return result; | ||
} | ||
|
||
|
||
PyObject *CPyBytes_LjustDefaultFill(PyObject *self, CPyTagged width) { | ||
if (!PyBytes_Check(self)) { | ||
PyErr_SetString(PyExc_TypeError, "self must be bytes"); | ||
return NULL; | ||
} | ||
Py_ssize_t width_size_t = CPyTagged_AsSsize_t(width); | ||
Py_ssize_t len = PyBytes_Size(self); | ||
if (width_size_t <= len) { | ||
Py_INCREF(self); | ||
return self; | ||
} | ||
Py_ssize_t pad = width_size_t - len; | ||
PyObject *result = PyBytes_FromStringAndSize(NULL, width_size_t); | ||
if (!result) return NULL; | ||
char *res_buf = PyBytes_AsString(result); | ||
memcpy(res_buf, PyBytes_AsString(self), len); | ||
memset(res_buf + len, ' ', pad); | ||
return result; | ||
} | ||
|
||
|
||
PyObject *CPyBytes_LjustCustomFill(PyObject *self, CPyTagged width, PyObject *fillbyte) { | ||
if (!PyBytes_Check(self)) { | ||
PyErr_SetString(PyExc_TypeError, "self must be bytes"); | ||
return NULL; | ||
} | ||
if (!PyBytes_Check(fillbyte) || PyBytes_Size(fillbyte) != 1) { | ||
PyErr_SetString(PyExc_TypeError, "fillbyte must be a single byte"); | ||
return NULL; | ||
} | ||
Py_ssize_t width_size_t = CPyTagged_AsSsize_t(width); | ||
Py_ssize_t len = PyBytes_Size(self); | ||
if (width_size_t <= len) { | ||
Py_INCREF(self); | ||
return self; | ||
} | ||
char fill = PyBytes_AsString(fillbyte)[0]; | ||
Py_ssize_t pad = width_size_t - len; | ||
PyObject *result = PyBytes_FromStringAndSize(NULL, width_size_t); | ||
if (!result) return NULL; | ||
char *res_buf = PyBytes_AsString(result); | ||
memcpy(res_buf, PyBytes_AsString(self), len); | ||
memset(res_buf + len, fill, pad); | ||
return result; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get rid of these type checks?