Repository navigation
[mypyc] Add primitives for str.ljust, rjust, center and zfill - #22412
Open
Jahongir-Qurbonov wants to merge 4 commits into
Open
Jahongir-Qurbonov wants to merge 4 commits into
Jahongir-Qurbonov wants to merge 4 commits into
Conversation
Jahongir-Qurbonov
force-pushed
the
mypyc-str-pad
branch
from
November 10, 2026 18:02
fba9965 to
edfeb67
Compare
13 of 14 tasks
# Conflicts: # mypyc/doc/str_operations.rst # mypyc/test-data/fixtures/ir.py
# Conflicts: # mypyc/doc/str_operations.rst
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Add primitives for
str.ljust(),str.rjust()andstr.center()(with and without thefillcharargument), andstr.zfill().The implementation follows
pad()and the related methods in CPython'sObjects/unicodeobject.c, including:center()for odd padding,+/-sign inzfill(),str(astrsubclass instance is converted to an exactstr),fillchareven when no padding is needed, with the same error messages.Tests compare the results against the generic implementation (through
Any) for strings and fill characters of all string kinds (ASCII, Latin-1, UCS-2, UCS-4) and a range of widths, including negative widths. Error cases and object identity are also tested.Micro-benchmark (compiled code, 3.2M calls, best of 15, before -> after, Python 3.13):
s.ljust(16)s.rjust(16, ".")s.center(16)s.zfill(8)s.ljust(2)(no padding)s.ljust(16)(non-ASCII)Part of mypyc/mypyc#644.