Skip to content

Commit edfeb67

Browse files
[mypyc] Add primitives for str.ljust, rjust, center and zfill
1 parent 6349140 commit edfeb67

7 files changed

Lines changed: 309 additions & 0 deletions

File tree

‎mypyc/doc/str_operations.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Operators
3636
Methods
3737
-------
3838

39+
* ``s.center(width: int)``
40+
* ``s.center(width: int, fillchar: str)``
3941
* ``s.encode()``
4042
* ``s.encode(encoding: str)``
4143
* ``s.encode(encoding: str, errors: str)``
@@ -47,6 +49,8 @@ Methods
4749
* ``s.isalnum()``
4850
* ``s.isdigit()``
4951
* ``s.isspace()``
52+
* ``s.ljust(width: int)``
53+
* ``s.ljust(width: int, fillchar: str)``
5054
* ``s.lower()``
5155
* ``s.join(x: Iterable)``
5256
* ``s.lstrip()``
@@ -59,6 +63,8 @@ Methods
5963
* ``s1.rfind(s2: str)``
6064
* ``s1.rfind(s2: str, start: int)``
6165
* ``s1.rfind(s2: str, start: int, end: int)``
66+
* ``s.rjust(width: int)``
67+
* ``s.rjust(width: int, fillchar: str)``
6268
* ``s.rpartition(sep: str)``
6369
* ``s.rsplit()``
6470
* ``s.rsplit(sep: str)``
@@ -75,6 +81,7 @@ Methods
7581
* ``s.upper()``
7682
* ``s.strip()``
7783
* ``s.strip(chars: str)``
84+
* ``s.zfill(width: int)``
7885

7986
.. note::
8087

‎mypyc/lib-rt/CPy.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,10 @@ CPyTagged CPyStr_Ord(PyObject *obj);
11051105
PyObject *CPyStr_Multiply(PyObject *str, CPyTagged count);
11061106
PyObject *CPyStr_Lower(PyObject *str);
11071107
PyObject *CPyStr_Upper(PyObject *str);
1108+
PyObject *CPyStr_Ljust(PyObject *self, CPyTagged width, PyObject *fillchar);
1109+
PyObject *CPyStr_Rjust(PyObject *self, CPyTagged width, PyObject *fillchar);
1110+
PyObject *CPyStr_Center(PyObject *self, CPyTagged width, PyObject *fillchar);
1111+
PyObject *CPyStr_Zfill(PyObject *self, CPyTagged width);
11081112
bool CPyStr_IsSpace(PyObject *str);
11091113
bool CPyStr_IsAlnum(PyObject *str);
11101114
bool CPyStr_IsDigit(PyObject *str);

‎mypyc/lib-rt/str_ops.c‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,138 @@ PyObject *CPyStr_Upper(PyObject *self) {
774774
#endif
775775
}
776776

777+
// Convert the width argument of str.ljust() and friends.
778+
static inline Py_ssize_t CPyStr_PadWidth(CPyTagged width) {
779+
Py_ssize_t result = CPyTagged_AsSsize_t(width);
780+
if (unlikely(result == -1 && PyErr_Occurred())) {
781+
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
782+
}
783+
return result;
784+
}
785+
786+
// Get the fill character argument of str.ljust() and friends (NULL means a space).
787+
// Returns (Py_UCS4)-1 on error.
788+
static inline Py_UCS4 CPyStr_PadFillChar(PyObject *fillchar) {
789+
if (fillchar == NULL) {
790+
return ' ';
791+
}
792+
if (unlikely(PyUnicode_GET_LENGTH(fillchar) != 1)) {
793+
PyErr_SetString(PyExc_TypeError,
794+
"The fill character must be exactly one character long");
795+
return (Py_UCS4)-1;
796+
}
797+
return PyUnicode_READ_CHAR(fillchar, 0);
798+
}
799+
800+
// Based on pad() in cpython.git:Objects/unicodeobject.c. The caller ensures that
801+
// left and right are non-negative and that left + len(self) + right fits in Py_ssize_t.
802+
static PyObject *CPyStr_Pad(PyObject *self, Py_ssize_t left, Py_ssize_t right, Py_UCS4 fill) {
803+
Py_ssize_t len = PyUnicode_GET_LENGTH(self);
804+
if (left == 0 && right == 0) {
805+
// Like CPython, return the object itself if it's an exact str
806+
return PyUnicode_Substring(self, 0, len);
807+
}
808+
Py_UCS4 maxchar = PyUnicode_MAX_CHAR_VALUE(self);
809+
if (fill > maxchar) {
810+
maxchar = fill;
811+
}
812+
PyObject *res = PyUnicode_New(left + len + right, maxchar);
813+
if (res == NULL) {
814+
return NULL;
815+
}
816+
int kind = PyUnicode_KIND(res);
817+
void *data = PyUnicode_DATA(res);
818+
if (kind == PyUnicode_1BYTE_KIND) {
819+
Py_UCS1 *p = (Py_UCS1 *)data;
820+
memset(p, (int)fill, left);
821+
memset(p + left + len, (int)fill, right);
822+
} else {
823+
for (Py_ssize_t i = 0; i < left; i++) {
824+
PyUnicode_WRITE(kind, data, i, fill);
825+
}
826+
for (Py_ssize_t i = left + len; i < left + len + right; i++) {
827+
PyUnicode_WRITE(kind, data, i, fill);
828+
}
829+
}
830+
if (PyUnicode_KIND(self) == kind) {
831+
memcpy((char *)data + kind * left, PyUnicode_DATA(self), kind * len);
832+
} else if (PyUnicode_CopyCharacters(res, left, self, 0, len) < 0) {
833+
Py_DECREF(res);
834+
return NULL;
835+
}
836+
return res;
837+
}
838+
839+
PyObject *CPyStr_Ljust(PyObject *self, CPyTagged width, PyObject *fillchar) {
840+
Py_ssize_t w = CPyStr_PadWidth(width);
841+
if (unlikely(w == -1 && PyErr_Occurred())) {
842+
return NULL;
843+
}
844+
Py_UCS4 fill = CPyStr_PadFillChar(fillchar);
845+
if (unlikely(fill == (Py_UCS4)-1)) {
846+
return NULL;
847+
}
848+
Py_ssize_t len = PyUnicode_GET_LENGTH(self);
849+
return CPyStr_Pad(self, 0, w > len ? w - len : 0, fill);
850+
}
851+
852+
PyObject *CPyStr_Rjust(PyObject *self, CPyTagged width, PyObject *fillchar) {
853+
Py_ssize_t w = CPyStr_PadWidth(width);
854+
if (unlikely(w == -1 && PyErr_Occurred())) {
855+
return NULL;
856+
}
857+
Py_UCS4 fill = CPyStr_PadFillChar(fillchar);
858+
if (unlikely(fill == (Py_UCS4)-1)) {
859+
return NULL;
860+
}
861+
Py_ssize_t len = PyUnicode_GET_LENGTH(self);
862+
return CPyStr_Pad(self, w > len ? w - len : 0, 0, fill);
863+
}
864+
865+
PyObject *CPyStr_Center(PyObject *self, CPyTagged width, PyObject *fillchar) {
866+
Py_ssize_t w = CPyStr_PadWidth(width);
867+
if (unlikely(w == -1 && PyErr_Occurred())) {
868+
return NULL;
869+
}
870+
Py_UCS4 fill = CPyStr_PadFillChar(fillchar);
871+
if (unlikely(fill == (Py_UCS4)-1)) {
872+
return NULL;
873+
}
874+
Py_ssize_t len = PyUnicode_GET_LENGTH(self);
875+
if (len >= w) {
876+
return CPyStr_Pad(self, 0, 0, fill);
877+
}
878+
Py_ssize_t marg = w - len;
879+
// This matches the rounding used by CPython
880+
Py_ssize_t left = marg / 2 + (marg & w & 1);
881+
return CPyStr_Pad(self, left, marg - left, fill);
882+
}
883+
884+
PyObject *CPyStr_Zfill(PyObject *self, CPyTagged width) {
885+
Py_ssize_t w = CPyStr_PadWidth(width);
886+
if (unlikely(w == -1 && PyErr_Occurred())) {
887+
return NULL;
888+
}
889+
Py_ssize_t len = PyUnicode_GET_LENGTH(self);
890+
if (len >= w) {
891+
return CPyStr_Pad(self, 0, 0, '0');
892+
}
893+
Py_ssize_t fill = w - len;
894+
PyObject *res = CPyStr_Pad(self, fill, 0, '0');
895+
if (res == NULL) {
896+
return NULL;
897+
}
898+
int kind = PyUnicode_KIND(res);
899+
void *data = PyUnicode_DATA(res);
900+
Py_UCS4 ch = PyUnicode_READ(kind, data, fill);
901+
if (ch == '+' || ch == '-') {
902+
// Move the sign to the beginning
903+
PyUnicode_WRITE(kind, data, 0, ch);
904+
PyUnicode_WRITE(kind, data, fill, '0');
905+
}
906+
return res;
907+
}
908+
777909
bool CPyStr_IsDigit(PyObject *str) {
778910
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
779911
if (len == 0) return false;

‎mypyc/primitives/str_ops.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,35 @@
415415
error_kind=ERR_MAGIC,
416416
)
417417

418+
# str.ljust(width), str.rjust(width), str.center(width) and
419+
# variants with the fillchar argument
420+
for pad_method in ["ljust", "rjust", "center"]:
421+
method_op(
422+
name=pad_method,
423+
arg_types=[str_rprimitive, int_rprimitive],
424+
return_type=str_rprimitive,
425+
c_function_name=f"CPyStr_{pad_method.capitalize()}",
426+
# This 0 below is implicitly treated as NULL in C.
427+
extra_int_constants=[(0, c_int_rprimitive)],
428+
error_kind=ERR_MAGIC,
429+
)
430+
method_op(
431+
name=pad_method,
432+
arg_types=[str_rprimitive, int_rprimitive, str_rprimitive],
433+
return_type=str_rprimitive,
434+
c_function_name=f"CPyStr_{pad_method.capitalize()}",
435+
error_kind=ERR_MAGIC,
436+
)
437+
438+
# str.zfill(width)
439+
method_op(
440+
name="zfill",
441+
arg_types=[str_rprimitive, int_rprimitive],
442+
return_type=str_rprimitive,
443+
c_function_name="CPyStr_Zfill",
444+
error_kind=ERR_MAGIC,
445+
)
446+
418447
method_op(
419448
name="isspace",
420449
arg_types=[str_rprimitive],

‎mypyc/test-data/fixtures/ir.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ def join(self, x: Iterable[str]) -> str: pass
127127
def format(self, *args: Any, **kwargs: Any) -> str: ...
128128
def lower(self) -> str: ...
129129
def upper(self) -> str: ...
130+
def ljust(self, width: int, fillchar: str = ' ', /) -> str: ...
131+
def rjust(self, width: int, fillchar: str = ' ', /) -> str: ...
132+
def center(self, width: int, fillchar: str = ' ', /) -> str: ...
133+
def zfill(self, width: int, /) -> str: ...
130134
def startswith(self, x: Union[str, Tuple[str, ...]], start: int=..., end: int=...) -> bool: ...
131135
def endswith(self, x: Union[str, Tuple[str, ...]], start: int=..., end: int=...) -> bool: ...
132136
def replace(self, old: str, new: str, maxcount: int=...) -> str: ...

‎mypyc/test-data/irbuild-str.test‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,51 @@ L0:
993993
r0 = CPyStr_Upper(s)
994994
return r0
995995

996+
[case testStrPadding]
997+
def ljust(s: str, n: int, c: str) -> str:
998+
return s.ljust(n) + s.ljust(n, c)
999+
def rjust(s: str, n: int, c: str) -> str:
1000+
return s.rjust(n) + s.rjust(n, c)
1001+
def center(s: str, n: int, c: str) -> str:
1002+
return s.center(n) + s.center(n, c)
1003+
def zfill(s: str, n: int) -> str:
1004+
return s.zfill(n)
1005+
[out]
1006+
def ljust(s, n, c):
1007+
s :: str
1008+
n :: int
1009+
c, r0, r1, r2 :: str
1010+
L0:
1011+
r0 = CPyStr_Ljust(s, n, 0)
1012+
r1 = CPyStr_Ljust(s, n, c)
1013+
r2 = PyUnicode_Concat(r0, r1)
1014+
return r2
1015+
def rjust(s, n, c):
1016+
s :: str
1017+
n :: int
1018+
c, r0, r1, r2 :: str
1019+
L0:
1020+
r0 = CPyStr_Rjust(s, n, 0)
1021+
r1 = CPyStr_Rjust(s, n, c)
1022+
r2 = PyUnicode_Concat(r0, r1)
1023+
return r2
1024+
def center(s, n, c):
1025+
s :: str
1026+
n :: int
1027+
c, r0, r1, r2 :: str
1028+
L0:
1029+
r0 = CPyStr_Center(s, n, 0)
1030+
r1 = CPyStr_Center(s, n, c)
1031+
r2 = PyUnicode_Concat(r0, r1)
1032+
return r2
1033+
def zfill(s, n):
1034+
s :: str
1035+
n :: int
1036+
r0 :: str
1037+
L0:
1038+
r0 = CPyStr_Zfill(s, n)
1039+
return r0
1040+
9961041
[case testStrIsSpace]
9971042
def is_space(x: str) -> bool:
9981043
return x.isspace()

‎mypyc/test-data/run-strings.test‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,3 +1518,91 @@ class StrOverride(str):
15181518
return 12345678901234567892
15191519
def upper(self) -> Any:
15201520
return 12345678901234567893
1521+
1522+
[case testStrPadding]
1523+
from typing import Any, Callable, Optional
1524+
from testutil import assertRaises
1525+
from strsub import StrSubclass
1526+
1527+
def ljust(s: str, width: int, fillchar: Optional[str] = None) -> str:
1528+
if fillchar is None:
1529+
return s.ljust(width)
1530+
return s.ljust(width, fillchar)
1531+
1532+
def rjust(s: str, width: int, fillchar: Optional[str] = None) -> str:
1533+
if fillchar is None:
1534+
return s.rjust(width)
1535+
return s.rjust(width, fillchar)
1536+
1537+
def center(s: str, width: int, fillchar: Optional[str] = None) -> str:
1538+
if fillchar is None:
1539+
return s.center(width)
1540+
return s.center(width, fillchar)
1541+
1542+
def zfill(s: str, width: int) -> str:
1543+
return s.zfill(width)
1544+
1545+
STRINGS = ["", "a", "ab", "abc", "\xe9t\xe9", "\u4e2d\u6587", "\U0001f600x", "-", "+", "-42",
1546+
"+42", "--1", "42-", "-\u4e2d", "+\U0001f600"]
1547+
FILLCHARS = [None, " ", "*", "0", "\xe9", "\u4e2d", "\U0001f600"]
1548+
1549+
def check(f: Callable[[str, int, Optional[str]], str], name: str) -> None:
1550+
for s in STRINGS:
1551+
a: Any = s
1552+
for width in range(-3, 12):
1553+
for fillchar in FILLCHARS:
1554+
if fillchar is None:
1555+
expected = getattr(a, name)(width)
1556+
else:
1557+
expected = getattr(a, name)(width, fillchar)
1558+
assert f(s, width, fillchar) == expected, (name, s, width, fillchar)
1559+
1560+
def test_ljust() -> None:
1561+
check(ljust, "ljust")
1562+
1563+
def test_rjust() -> None:
1564+
check(rjust, "rjust")
1565+
1566+
def test_center() -> None:
1567+
check(center, "center")
1568+
1569+
def test_zfill() -> None:
1570+
for s in STRINGS:
1571+
a: Any = s
1572+
for width in range(-3, 12):
1573+
assert zfill(s, width) == a.zfill(width), (s, width)
1574+
assert zfill("-1", 4) == "-001"
1575+
assert zfill("+\u4e2d", 3) == "+0\u4e2d"
1576+
assert zfill("1-", 4) == "001-"
1577+
1578+
def test_unchanged() -> None:
1579+
s = "".join(["a", "bc"])
1580+
assert ljust(s, 3) is s
1581+
assert rjust(s, 2, "*") is s
1582+
assert center(s, -1) is s
1583+
assert zfill(s, 0) is s
1584+
sub = StrSubclass("abc")
1585+
for res in [ljust(sub, 3), rjust(sub, 2, "*"), center(sub, -1), zfill(sub, 0)]:
1586+
assert type(res) is str
1587+
assert res == "abc"
1588+
assert type(ljust(sub, 5)) is str
1589+
assert ljust(sub, 5) == "abc "
1590+
1591+
def test_errors() -> None:
1592+
for f in (ljust, rjust, center):
1593+
for fillchar in ["", "ab", "\U0001f600\U0001f600"]:
1594+
with assertRaises(TypeError, "The fill character must be exactly one character long"):
1595+
f("abc", 5, fillchar)
1596+
# The fill character is checked even if no padding is needed
1597+
with assertRaises(TypeError, "The fill character must be exactly one character long"):
1598+
f("abc", 1, "ab")
1599+
with assertRaises(OverflowError, "Python int too large to convert to C ssize_t"):
1600+
f("abc", 2**100)
1601+
with assertRaises(OverflowError, "Python int too large to convert to C ssize_t"):
1602+
f("abc", -2**100)
1603+
with assertRaises(OverflowError, "Python int too large to convert to C ssize_t"):
1604+
zfill("abc", 2**100)
1605+
1606+
[file strsub.py]
1607+
class StrSubclass(str):
1608+
pass

0 commit comments

Comments
 (0)