diff --git a/mypyc/doc/str_operations.rst b/mypyc/doc/str_operations.rst index 34f1553c9d21..8d8de77ce723 100644 --- a/mypyc/doc/str_operations.rst +++ b/mypyc/doc/str_operations.rst @@ -36,6 +36,8 @@ Operators Methods ------- +* ``s.center(width: int)`` +* ``s.center(width: int, fillchar: str)`` * ``s.encode()`` * ``s.encode(encoding: str)`` * ``s.encode(encoding: str, errors: str)`` @@ -59,6 +61,8 @@ Methods * ``s.isspace()`` * ``s.istitle()`` * ``s.isupper()`` +* ``s.ljust(width: int)`` +* ``s.ljust(width: int, fillchar: str)`` * ``s.lower()`` * ``s.join(x: Iterable)`` * ``s.lstrip()`` @@ -74,6 +78,8 @@ Methods * ``s1.rindex(s2: str)`` * ``s1.rindex(s2: str, start: int)`` * ``s1.rindex(s2: str, start: int, end: int)`` +* ``s.rjust(width: int)`` +* ``s.rjust(width: int, fillchar: str)`` * ``s.rpartition(sep: str)`` * ``s.rsplit()`` * ``s.rsplit(sep: str)`` @@ -90,6 +96,7 @@ Methods * ``s.upper()`` * ``s.strip()`` * ``s.strip(chars: str)`` +* ``s.zfill(width: int)`` .. note:: diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index d9f33959492f..f24b92f975f3 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -1149,6 +1149,10 @@ PyObject *CPyStr_Chr(CPyTagged n); PyObject *CPyStr_Multiply(PyObject *str, CPyTagged count); PyObject *CPyStr_Lower(PyObject *str); PyObject *CPyStr_Upper(PyObject *str); +PyObject *CPyStr_Ljust(PyObject *self, CPyTagged width, PyObject *fillchar); +PyObject *CPyStr_Rjust(PyObject *self, CPyTagged width, PyObject *fillchar); +PyObject *CPyStr_Center(PyObject *self, CPyTagged width, PyObject *fillchar); +PyObject *CPyStr_Zfill(PyObject *self, CPyTagged width); bool CPyStr_IsSpace(PyObject *str); bool CPyStr_IsAlnum(PyObject *str); bool CPyStr_IsDigit(PyObject *str); diff --git a/mypyc/lib-rt/str_ops.c b/mypyc/lib-rt/str_ops.c index c9552b6b87bf..45dec509a9e7 100644 --- a/mypyc/lib-rt/str_ops.c +++ b/mypyc/lib-rt/str_ops.c @@ -809,6 +809,129 @@ PyObject *CPyStr_Upper(PyObject *self) { #endif } +// Get the fill character argument of str.ljust() and friends (NULL means a space). +// Returns (Py_UCS4)-1 on error. +static inline Py_UCS4 CPyStr_PadFillChar(PyObject *fillchar) { + if (fillchar == NULL) { + return ' '; + } + if (unlikely(PyUnicode_GET_LENGTH(fillchar) != 1)) { + PyErr_SetString(PyExc_TypeError, + "The fill character must be exactly one character long"); + return (Py_UCS4)-1; + } + return PyUnicode_READ_CHAR(fillchar, 0); +} + +// Based on pad() in cpython.git:Objects/unicodeobject.c. The caller ensures that +// left and right are non-negative and that left + len(self) + right fits in Py_ssize_t. +static PyObject *CPyStr_Pad(PyObject *self, Py_ssize_t left, Py_ssize_t right, Py_UCS4 fill) { + Py_ssize_t len = PyUnicode_GET_LENGTH(self); + if (left == 0 && right == 0) { + // Like CPython, return the object itself if it's an exact str + return PyUnicode_Substring(self, 0, len); + } + Py_UCS4 maxchar = PyUnicode_MAX_CHAR_VALUE(self); + if (fill > maxchar) { + maxchar = fill; + } + PyObject *res = PyUnicode_New(left + len + right, maxchar); + if (res == NULL) { + return NULL; + } + int kind = PyUnicode_KIND(res); + void *data = PyUnicode_DATA(res); + if (kind == PyUnicode_1BYTE_KIND) { + Py_UCS1 *p = (Py_UCS1 *)data; + memset(p, (int)fill, left); + memset(p + left + len, (int)fill, right); + } else { + for (Py_ssize_t i = 0; i < left; i++) { + PyUnicode_WRITE(kind, data, i, fill); + } + for (Py_ssize_t i = left + len; i < left + len + right; i++) { + PyUnicode_WRITE(kind, data, i, fill); + } + } + if (PyUnicode_KIND(self) == kind) { + memcpy((char *)data + kind * left, PyUnicode_DATA(self), kind * len); + } else if (PyUnicode_CopyCharacters(res, left, self, 0, len) < 0) { + Py_DECREF(res); + return NULL; + } + return res; +} + +PyObject *CPyStr_Ljust(PyObject *self, CPyTagged width, PyObject *fillchar) { + Py_ssize_t w = CPyTagged_AsSsize_t(width); + if (unlikely(w == -1 && PyErr_Occurred())) { + return NULL; + } + Py_UCS4 fill = CPyStr_PadFillChar(fillchar); + if (unlikely(fill == (Py_UCS4)-1)) { + return NULL; + } + Py_ssize_t len = PyUnicode_GET_LENGTH(self); + return CPyStr_Pad(self, 0, w > len ? w - len : 0, fill); +} + +PyObject *CPyStr_Rjust(PyObject *self, CPyTagged width, PyObject *fillchar) { + Py_ssize_t w = CPyTagged_AsSsize_t(width); + if (unlikely(w == -1 && PyErr_Occurred())) { + return NULL; + } + Py_UCS4 fill = CPyStr_PadFillChar(fillchar); + if (unlikely(fill == (Py_UCS4)-1)) { + return NULL; + } + Py_ssize_t len = PyUnicode_GET_LENGTH(self); + return CPyStr_Pad(self, w > len ? w - len : 0, 0, fill); +} + +PyObject *CPyStr_Center(PyObject *self, CPyTagged width, PyObject *fillchar) { + Py_ssize_t w = CPyTagged_AsSsize_t(width); + if (unlikely(w == -1 && PyErr_Occurred())) { + return NULL; + } + Py_UCS4 fill = CPyStr_PadFillChar(fillchar); + if (unlikely(fill == (Py_UCS4)-1)) { + return NULL; + } + Py_ssize_t len = PyUnicode_GET_LENGTH(self); + if (len >= w) { + return CPyStr_Pad(self, 0, 0, fill); + } + Py_ssize_t marg = w - len; + // This matches the rounding used by CPython + Py_ssize_t left = marg / 2 + (marg & w & 1); + return CPyStr_Pad(self, left, marg - left, fill); +} + +PyObject *CPyStr_Zfill(PyObject *self, CPyTagged width) { + Py_ssize_t w = CPyTagged_AsSsize_t(width); + if (unlikely(w == -1 && PyErr_Occurred())) { + return NULL; + } + Py_ssize_t len = PyUnicode_GET_LENGTH(self); + if (len >= w) { + return CPyStr_Pad(self, 0, 0, '0'); + } + Py_ssize_t fill = w - len; + PyObject *res = CPyStr_Pad(self, fill, 0, '0'); + if (res == NULL) { + return NULL; + } + int kind = PyUnicode_KIND(res); + void *data = PyUnicode_DATA(res); + Py_UCS4 ch = PyUnicode_READ(kind, data, fill); + if (ch == '+' || ch == '-') { + // Move the sign to the beginning + PyUnicode_WRITE(kind, data, 0, ch); + PyUnicode_WRITE(kind, data, fill, '0'); + } + return res; +} + bool CPyStr_IsDigit(PyObject *str) { Py_ssize_t len = PyUnicode_GET_LENGTH(str); if (len == 0) return false; diff --git a/mypyc/primitives/str_ops.py b/mypyc/primitives/str_ops.py index 4e600a3bdc8f..73c4cc16090a 100644 --- a/mypyc/primitives/str_ops.py +++ b/mypyc/primitives/str_ops.py @@ -435,6 +435,35 @@ error_kind=ERR_MAGIC, ) +# str.ljust(width), str.rjust(width), str.center(width) and +# variants with the fillchar argument +for pad_method in ["ljust", "rjust", "center"]: + method_op( + name=pad_method, + arg_types=[str_rprimitive, int_rprimitive], + return_type=str_rprimitive, + c_function_name=f"CPyStr_{pad_method.capitalize()}", + # This 0 below is implicitly treated as NULL in C. + extra_int_constants=[(0, c_int_rprimitive)], + error_kind=ERR_MAGIC, + ) + method_op( + name=pad_method, + arg_types=[str_rprimitive, int_rprimitive, str_rprimitive], + return_type=str_rprimitive, + c_function_name=f"CPyStr_{pad_method.capitalize()}", + error_kind=ERR_MAGIC, + ) + +# str.zfill(width) +method_op( + name="zfill", + arg_types=[str_rprimitive, int_rprimitive], + return_type=str_rprimitive, + c_function_name="CPyStr_Zfill", + error_kind=ERR_MAGIC, +) + method_op( name="isspace", arg_types=[str_rprimitive], diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index b0808b60bdd2..4f3045064457 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -130,6 +130,10 @@ def format(self, *args: Any, **kwargs: Any) -> str: ... def lower(self) -> str: ... def upper(self) -> str: ... def expandtabs(self) -> str: ... + def ljust(self, width: int, fillchar: str = ' ', /) -> str: ... + def rjust(self, width: int, fillchar: str = ' ', /) -> str: ... + def center(self, width: int, fillchar: str = ' ', /) -> str: ... + def zfill(self, width: int, /) -> str: ... def startswith(self, x: Union[str, Tuple[str, ...]], start: int=..., end: int=...) -> bool: ... def endswith(self, x: Union[str, Tuple[str, ...]], start: int=..., end: int=...) -> bool: ... def replace(self, old: str, new: str, maxcount: int=...) -> str: ... diff --git a/mypyc/test-data/irbuild-str.test b/mypyc/test-data/irbuild-str.test index af006abe72c8..0434ae6c3b47 100644 --- a/mypyc/test-data/irbuild-str.test +++ b/mypyc/test-data/irbuild-str.test @@ -1055,6 +1055,51 @@ L0: r0 = CPyStr_Upper(s) return r0 +[case testStrPadding] +def ljust(s: str, n: int, c: str) -> str: + return s.ljust(n) + s.ljust(n, c) +def rjust(s: str, n: int, c: str) -> str: + return s.rjust(n) + s.rjust(n, c) +def center(s: str, n: int, c: str) -> str: + return s.center(n) + s.center(n, c) +def zfill(s: str, n: int) -> str: + return s.zfill(n) +[out] +def ljust(s, n, c): + s :: str + n :: int + c, r0, r1, r2 :: str +L0: + r0 = CPyStr_Ljust(s, n, 0) + r1 = CPyStr_Ljust(s, n, c) + r2 = PyUnicode_Concat(r0, r1) + return r2 +def rjust(s, n, c): + s :: str + n :: int + c, r0, r1, r2 :: str +L0: + r0 = CPyStr_Rjust(s, n, 0) + r1 = CPyStr_Rjust(s, n, c) + r2 = PyUnicode_Concat(r0, r1) + return r2 +def center(s, n, c): + s :: str + n :: int + c, r0, r1, r2 :: str +L0: + r0 = CPyStr_Center(s, n, 0) + r1 = CPyStr_Center(s, n, c) + r2 = PyUnicode_Concat(r0, r1) + return r2 +def zfill(s, n): + s :: str + n :: int + r0 :: str +L0: + r0 = CPyStr_Zfill(s, n) + return r0 + [case testStrIsSpace] def is_space(x: str) -> bool: return x.isspace() diff --git a/mypyc/test-data/run-strings.test b/mypyc/test-data/run-strings.test index aaf2cb39b127..1f50701bb849 100644 --- a/mypyc/test-data/run-strings.test +++ b/mypyc/test-data/run-strings.test @@ -1916,3 +1916,101 @@ class StrOverride(str): return 12345678901234567892 def upper(self) -> Any: return 12345678901234567893 + +[case testStrPadding] +from typing import Any, Callable, Optional +from testutil import assertRaises +from strsub import StrSubclass, MAXSIZE + +def ljust(s: str, width: int, fillchar: Optional[str] = None) -> str: + if fillchar is None: + return s.ljust(width) + return s.ljust(width, fillchar) + +def rjust(s: str, width: int, fillchar: Optional[str] = None) -> str: + if fillchar is None: + return s.rjust(width) + return s.rjust(width, fillchar) + +def center(s: str, width: int, fillchar: Optional[str] = None) -> str: + if fillchar is None: + return s.center(width) + return s.center(width, fillchar) + +def zfill(s: str, width: int) -> str: + return s.zfill(width) + +STRINGS = ["", "a", "ab", "abc", "\xe9t\xe9", "\u4e2d\u6587", "\U0001f600x", "-", "+", "-42", + "+42", "--1", "42-", "-\u4e2d", "+\U0001f600"] +FILLCHARS = [None, " ", "*", "0", "\xe9", "\u4e2d", "\U0001f600"] + +def check(f: Callable[[str, int, Optional[str]], str], name: str) -> None: + for s in STRINGS: + a: Any = s + for width in range(-3, 12): + for fillchar in FILLCHARS: + if fillchar is None: + expected = getattr(a, name)(width) + else: + expected = getattr(a, name)(width, fillchar) + assert f(s, width, fillchar) == expected, (name, s, width, fillchar) + +def test_ljust() -> None: + check(ljust, "ljust") + +def test_rjust() -> None: + check(rjust, "rjust") + +def test_center() -> None: + check(center, "center") + +def test_zfill() -> None: + for s in STRINGS: + a: Any = s + for width in range(-3, 12): + assert zfill(s, width) == a.zfill(width), (s, width) + assert zfill("-1", 4) == "-001" + assert zfill("+\u4e2d", 3) == "+0\u4e2d" + assert zfill("1-", 4) == "001-" + +def test_unchanged() -> None: + s = "".join(["a", "bc"]) + assert ljust(s, 3) is s + assert rjust(s, 2, "*") is s + assert center(s, -1) is s + assert zfill(s, 0) is s + sub = StrSubclass("abc") + for res in [ljust(sub, 3), rjust(sub, 2, "*"), center(sub, -1), zfill(sub, 0)]: + assert type(res) is str + assert res == "abc" + assert type(ljust(sub, 5)) is str + assert ljust(sub, 5) == "abc " + +def test_errors() -> None: + for f in (ljust, rjust, center): + for fillchar in ["", "ab", "\U0001f600\U0001f600"]: + with assertRaises(TypeError, "The fill character must be exactly one character long"): + f("abc", 5, fillchar) + # The fill character is checked even if no padding is needed + with assertRaises(TypeError, "The fill character must be exactly one character long"): + f("abc", 1, "ab") + with assertRaises(OverflowError, "Python int too large to convert to C ssize_t"): + f("abc", 2**100) + with assertRaises(OverflowError, "Python int too large to convert to C ssize_t"): + f("abc", -2**100) + with assertRaises(OverflowError, "Python int too large to convert to C ssize_t"): + zfill("abc", 2**100) + for s in ["a", "\u4e2d", "\U0001f600"]: + for f in (ljust, rjust, center): + with assertRaises(MemoryError): + f(s, MAXSIZE) + with assertRaises(MemoryError): + zfill(s, MAXSIZE) + +[file strsub.py] +import sys + +MAXSIZE: int = getattr(sys, "maxsize") + +class StrSubclass(str): + pass