Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypyc/doc/str_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Operators
Methods
-------

* ``s.capitalize()``
* ``s.casefold()``
* ``s.encode()``
* ``s.encode(encoding: str)``
* ``s.encode(encoding: str, errors: str)``
Expand Down Expand Up @@ -80,6 +82,8 @@ Methods
* ``s.splitlines(keepends: bool)``
* ``s1.startswith(s2: str)``
* ``s1.startswith(t: tuple[str, ...])``
* ``s.swapcase()``
* ``s.title()``
* ``s.upper()``
* ``s.strip()``
* ``s.strip(chars: str)``
Expand Down
4 changes: 4 additions & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,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_Capitalize(PyObject *self);
PyObject *CPyStr_Casefold(PyObject *self);
PyObject *CPyStr_Swapcase(PyObject *self);
PyObject *CPyStr_Title(PyObject *self);
bool CPyStr_IsSpace(PyObject *str);
bool CPyStr_IsAlnum(PyObject *str);
bool CPyStr_IsDigit(PyObject *str);
Expand Down
4 changes: 4 additions & 0 deletions mypyc/lib-rt/static_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ intern_strings(void) {
INTERN_STRING(__le__, "__le__");
INTERN_STRING(__lt__, "__lt__");
INTERN_STRING(__ge__, "__ge__");
INTERN_STRING(capitalize, "capitalize");
INTERN_STRING(casefold, "casefold");
INTERN_STRING(clear, "clear");
INTERN_STRING(close_, "close");
INTERN_STRING(copy, "copy");
Expand All @@ -52,7 +54,9 @@ intern_strings(void) {
INTERN_STRING(setdefault, "setdefault");
INTERN_STRING(startswith, "startswith");
INTERN_STRING(super, "super");
INTERN_STRING(swapcase, "swapcase");
INTERN_STRING(throw_, "throw");
INTERN_STRING(title, "title");
INTERN_STRING(translate, "translate");
INTERN_STRING(update, "update");
INTERN_STRING(upper, "upper");
Expand Down
4 changes: 4 additions & 0 deletions mypyc/lib-rt/static_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ typedef struct mypyc_interned_str_struct {
PyObject *__le__;
PyObject *__lt__;
PyObject *__ge__;
PyObject *capitalize;
PyObject *casefold;
PyObject *clear;
PyObject *close_;
PyObject *copy;
Expand All @@ -44,7 +46,9 @@ typedef struct mypyc_interned_str_struct {
PyObject *setdefault;
PyObject *startswith;
PyObject *super;
PyObject *swapcase;
PyObject *throw_;
PyObject *title;
PyObject *translate;
PyObject *update;
PyObject *upper;
Expand Down
67 changes: 67 additions & 0 deletions mypyc/lib-rt/str_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,73 @@ PyObject *CPyStr_Upper(PyObject *self) {
#endif
}

// Apply a case operation to a str object. ASCII strings use ascii_func directly. Other
// strings fall back to the str method, since the full Unicode case mappings aren't
// available through the public C API. The method is looked up on str (not on the type
// of self), so that the result is always an exact str even if a subclass overrides it.
static inline PyObject *CPyStr_CaseOperation(
PyObject *self, void (*ascii_func)(const Py_UCS1 *, Py_UCS1 *, Py_ssize_t),
PyObject *method_name) {
if (!PyUnicode_IS_ASCII(self)) {
return PyObject_CallMethodOneArg((PyObject *)&PyUnicode_Type, method_name, self);
}
Py_ssize_t len = PyUnicode_GET_LENGTH(self);
PyObject *res = PyUnicode_New(len, 127);
if (res == NULL) {
return NULL;
}
ascii_func(PyUnicode_1BYTE_DATA(self), PyUnicode_1BYTE_DATA(res), len);
return res;
}

static void CPy_ASCII_Capitalize(const Py_UCS1 *data, Py_UCS1 *res, Py_ssize_t len) {
if (len > 0) {
res[0] = Py_TOUPPER(data[0]);
}
for (Py_ssize_t i = 1; i < len; i++) {
res[i] = Py_TOLOWER(data[i]);
}
}

static void CPy_ASCII_Casefold(const Py_UCS1 *data, Py_UCS1 *res, Py_ssize_t len) {
for (Py_ssize_t i = 0; i < len; i++) {
res[i] = Py_TOLOWER(data[i]);
}
}

static void CPy_ASCII_Swapcase(const Py_UCS1 *data, Py_UCS1 *res, Py_ssize_t len) {
for (Py_ssize_t i = 0; i < len; i++) {
Py_UCS1 c = data[i];
res[i] = Py_ISUPPER(c) ? Py_TOLOWER(c) : Py_TOUPPER(c);
}
}

static void CPy_ASCII_Title(const Py_UCS1 *data, Py_UCS1 *res, Py_ssize_t len) {
// Only letters are cased characters in ASCII
int previous_is_cased = 0;
for (Py_ssize_t i = 0; i < len; i++) {
Py_UCS1 c = data[i];
res[i] = previous_is_cased ? Py_TOLOWER(c) : Py_TOUPPER(c);
previous_is_cased = Py_ISALPHA(c);
}
}

PyObject *CPyStr_Capitalize(PyObject *self) {
return CPyStr_CaseOperation(self, CPy_ASCII_Capitalize, mypyc_interned_str.capitalize);
}

PyObject *CPyStr_Casefold(PyObject *self) {
return CPyStr_CaseOperation(self, CPy_ASCII_Casefold, mypyc_interned_str.casefold);
}

PyObject *CPyStr_Swapcase(PyObject *self) {
return CPyStr_CaseOperation(self, CPy_ASCII_Swapcase, mypyc_interned_str.swapcase);
}

PyObject *CPyStr_Title(PyObject *self) {
return CPyStr_CaseOperation(self, CPy_ASCII_Title, mypyc_interned_str.title);
}

bool CPyStr_IsDigit(PyObject *str) {
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
if (len == 0) return false;
Expand Down
10 changes: 10 additions & 0 deletions mypyc/primitives/str_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,16 @@
error_kind=ERR_MAGIC,
)

# str.capitalize(), str.casefold(), str.swapcase() and str.title()
for case_method in ["capitalize", "casefold", "swapcase", "title"]:
method_op(
name=case_method,
arg_types=[str_rprimitive],
return_type=str_rprimitive,
c_function_name=f"CPyStr_{case_method.capitalize()}",
error_kind=ERR_MAGIC,
)

method_op(
name="isspace",
arg_types=[str_rprimitive],
Expand Down
4 changes: 4 additions & 0 deletions mypyc/test-data/fixtures/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ def format(self, *args: Any, **kwargs: Any) -> str: ...
def lower(self) -> str: ...
def upper(self) -> str: ...
def expandtabs(self) -> str: ...
def capitalize(self) -> str: ...
def casefold(self) -> str: ...
def swapcase(self) -> str: ...
def title(self) -> 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: ...
Expand Down
31 changes: 31 additions & 0 deletions mypyc/test-data/irbuild-str.test
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,37 @@ L0:
r0 = CPyStr_Upper(s)
return r0

[case testStrCaseMethods]
def capitalize(s: str) -> str:
return s.capitalize()
def casefold(s: str) -> str:
return s.casefold()
def swapcase(s: str) -> str:
return s.swapcase()
def title(s: str) -> str:
return s.title()
[out]
def capitalize(s):
s, r0 :: str
L0:
r0 = CPyStr_Capitalize(s)
return r0
def casefold(s):
s, r0 :: str
L0:
r0 = CPyStr_Casefold(s)
return r0
def swapcase(s):
s, r0 :: str
L0:
r0 = CPyStr_Swapcase(s)
return r0
def title(s):
s, r0 :: str
L0:
r0 = CPyStr_Title(s)
return r0

[case testStrIsSpace]
def is_space(x: str) -> bool:
return x.isspace()
Expand Down
83 changes: 83 additions & 0 deletions mypyc/test-data/run-strings.test
Original file line number Diff line number Diff line change
Expand Up @@ -1776,3 +1776,86 @@ class StrOverride(str):
return 12345678901234567892
def upper(self) -> Any:
return 12345678901234567893

[case testStrCaseMethods]
from typing import Any, Callable, List
from strsub import StrOverride, StrSubclass

def capitalize(s: str) -> str:
return s.capitalize()

def casefold(s: str) -> str:
return s.casefold()

def swapcase(s: str) -> str:
return s.swapcase()

def title(s: str) -> str:
return s.title()

FUNCS: List[Callable[[str], str]] = [capitalize, casefold, swapcase, title]

STRINGS = [
"", "a", "A", "1", " ", "hello world", "HELLO WORLD", "hELLO wORLD", "they're bill's",
"o'neil-smith_jr 2nd", "x1y2z3", "123abc", "a\tb\nc", "__init__", " leading space",
# Non-ASCII strings
"\xe9cole", "\xc9COLE", "stra\xdfe", "\u03a3\u0391\u03a3", "\u01c5a", "\ufb01ne",
"\u0130stanbul", "\u4e2d\u6587 abc", "\U0001f600 hello", "hello \u03a3", "\u1f80",
]

def check(s: str) -> None:
a: Any = s
assert capitalize(s) == a.capitalize(), s
assert casefold(s) == a.casefold(), s
assert swapcase(s) == a.swapcase(), s
assert title(s) == a.title(), s

def test_strings() -> None:
for s in STRINGS:
check(s)

def test_all_ascii_pairs() -> None:
chars = [chr(i) for i in range(128)]
for c in chars:
check(c)
for d in chars:
check(c + d)
check(c + d + c)

def test_result_type() -> None:
sub = StrSubclass("hello World")
empty = StrSubclass("")
for f in FUNCS:
assert type(f(sub)) is str
assert type(f(empty)) is str
assert f(empty) == ""
assert title(sub) == "Hello World"
non_ascii = StrSubclass("\xe9cole \u03a3")
for f in FUNCS:
assert type(f(non_ascii)) is str

def test_overriding_subclass() -> None:
# Overrides aren't used, and the result is always an exact str
for s in ["hello", "\xe9cole", "\u03a3\u0391\u03a3"]:
sub = StrOverride(s)
for f in FUNCS:
res = f(sub)
assert type(res) is str
assert res == f(s)
assert len(res) == len(f(s))

[file strsub.py]
from typing import Any

class StrSubclass(str):
pass

class StrOverride(str):
def capitalize(self) -> Any:
return 1
def casefold(self) -> Any:
return 2
def swapcase(self) -> Any:
return 3
def title(self) -> Any:
return 4
Loading