Skip to content

Commit a0329ee

Browse files
[mypyc] Add primitives for str.islower, isupper and istitle (#22405)
Add primitives for `str.islower()`, `str.isupper()` and `str.istitle()`. The implementations mirror CPython's and have a fast path for ASCII strings (which have no titlecase characters). `str.islower()` and `str.isupper()` share a helper. Tests compare the results against CPython for every code point (alone and combined with cased ASCII characters), and for a set of multi-character strings including titlecase characters such as `Dž`. The annotate test that used `str.islower()` as an example of a non-native method call now uses `str.expandtabs()` instead. Micro-benchmark (2M iterations over 11 short strings, compiled code, generic method call through `Any` vs the new primitive, Python 3.13): | Method | Before | After | |---|---|---| | `islower` | 0.362s | 0.129s | | `isupper` | 0.337s | 0.104s | | `istitle` | 0.375s | 0.159s | Part of mypyc/mypyc#644.
1 parent 91643be commit a0329ee

8 files changed

Lines changed: 263 additions & 2 deletions

File tree

‎mypyc/doc/str_operations.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ Methods
4949
* ``s.isascii()``
5050
* ``s.isdecimal()``
5151
* ``s.isdigit()``
52+
* ``s.islower()``
5253
* ``s.isnumeric()``
5354
* ``s.isprintable()``
5455
* ``s.isspace()``
56+
* ``s.istitle()``
57+
* ``s.isupper()``
5558
* ``s.lower()``
5659
* ``s.join(x: Iterable)``
5760
* ``s.lstrip()``

‎mypyc/lib-rt/CPy.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,9 @@ static inline bool CPyStr_IsAscii(PyObject *str) {
11541154
bool CPyStr_IsDecimal(PyObject *str);
11551155
bool CPyStr_IsNumeric(PyObject *str);
11561156
bool CPyStr_IsPrintable(PyObject *str);
1157+
bool CPyStr_IsLower(PyObject *str);
1158+
bool CPyStr_IsTitle(PyObject *str);
1159+
bool CPyStr_IsUpper(PyObject *str);
11571160

11581161
// Bytes operations
11591162

‎mypyc/lib-rt/str_ops.c‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,3 +890,98 @@ bool CPyStr_IsPrintable(PyObject *str) {
890890
#undef CPY_ASCII_ISPRINTABLE
891891
#undef CPY_STR_RETURN_ALL_CHARS
892892
#undef CPY_STR_CHECK_CHARS
893+
894+
// Shared implementation of str.islower() and str.isupper(), mirroring CPython. There
895+
// must be at least one cased character, and no character may be titlecase or have
896+
// the opposite case.
897+
static inline bool CPyStr_IsCase(PyObject *str, bool lower) {
898+
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
899+
bool cased = false;
900+
901+
// ASCII fast path (there are no titlecase characters in ASCII)
902+
if (PyUnicode_IS_ASCII(str)) {
903+
const Py_UCS1 *data = PyUnicode_1BYTE_DATA(str);
904+
for (Py_ssize_t i = 0; i < len; i++) {
905+
Py_UCS1 ch = data[i];
906+
if (lower ? Py_ISUPPER(ch) : Py_ISLOWER(ch))
907+
return false;
908+
if (lower ? Py_ISLOWER(ch) : Py_ISUPPER(ch))
909+
cased = true;
910+
}
911+
return cased;
912+
}
913+
914+
int kind = PyUnicode_KIND(str);
915+
const void *data = PyUnicode_DATA(str);
916+
if (len == 1) {
917+
Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
918+
return lower ? Py_UNICODE_ISLOWER(ch) : Py_UNICODE_ISUPPER(ch);
919+
}
920+
for (Py_ssize_t i = 0; i < len; i++) {
921+
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
922+
if (Py_UNICODE_ISTITLE(ch) || (lower ? Py_UNICODE_ISUPPER(ch) : Py_UNICODE_ISLOWER(ch)))
923+
return false;
924+
if (!cased && (lower ? Py_UNICODE_ISLOWER(ch) : Py_UNICODE_ISUPPER(ch)))
925+
cased = true;
926+
}
927+
return cased;
928+
}
929+
930+
bool CPyStr_IsLower(PyObject *str) {
931+
return CPyStr_IsCase(str, true);
932+
}
933+
934+
bool CPyStr_IsUpper(PyObject *str) {
935+
return CPyStr_IsCase(str, false);
936+
}
937+
938+
// Mirrors CPython's str.istitle(). Uppercase and titlecase characters may only follow
939+
// uncased characters, lowercase characters may only follow cased characters, and
940+
// there must be at least one cased character.
941+
bool CPyStr_IsTitle(PyObject *str) {
942+
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
943+
bool cased = false;
944+
bool previous_is_cased = false;
945+
946+
// ASCII fast path (there are no titlecase characters in ASCII)
947+
if (PyUnicode_IS_ASCII(str)) {
948+
const Py_UCS1 *data = PyUnicode_1BYTE_DATA(str);
949+
for (Py_ssize_t i = 0; i < len; i++) {
950+
Py_UCS1 ch = data[i];
951+
if (Py_ISUPPER(ch)) {
952+
if (previous_is_cased)
953+
return false;
954+
previous_is_cased = cased = true;
955+
} else if (Py_ISLOWER(ch)) {
956+
if (!previous_is_cased)
957+
return false;
958+
previous_is_cased = cased = true;
959+
} else {
960+
previous_is_cased = false;
961+
}
962+
}
963+
return cased;
964+
}
965+
966+
int kind = PyUnicode_KIND(str);
967+
const void *data = PyUnicode_DATA(str);
968+
if (len == 1) {
969+
Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
970+
return Py_UNICODE_ISTITLE(ch) || Py_UNICODE_ISUPPER(ch);
971+
}
972+
for (Py_ssize_t i = 0; i < len; i++) {
973+
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
974+
if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) {
975+
if (previous_is_cased)
976+
return false;
977+
previous_is_cased = cased = true;
978+
} else if (Py_UNICODE_ISLOWER(ch)) {
979+
if (!previous_is_cased)
980+
return false;
981+
previous_is_cased = cased = true;
982+
} else {
983+
previous_is_cased = false;
984+
}
985+
}
986+
return cased;
987+
}

‎mypyc/primitives/str_ops.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,33 @@
484484
error_kind=ERR_NEVER,
485485
)
486486

487+
# str.islower()
488+
method_op(
489+
name="islower",
490+
arg_types=[str_rprimitive],
491+
return_type=bool_rprimitive,
492+
c_function_name="CPyStr_IsLower",
493+
error_kind=ERR_NEVER,
494+
)
495+
496+
# str.istitle()
497+
method_op(
498+
name="istitle",
499+
arg_types=[str_rprimitive],
500+
return_type=bool_rprimitive,
501+
c_function_name="CPyStr_IsTitle",
502+
error_kind=ERR_NEVER,
503+
)
504+
505+
# str.isupper()
506+
method_op(
507+
name="isupper",
508+
arg_types=[str_rprimitive],
509+
return_type=bool_rprimitive,
510+
c_function_name="CPyStr_IsUpper",
511+
error_kind=ERR_NEVER,
512+
)
513+
487514

488515
# obj.decode()
489516
method_op(

‎mypyc/test-data/annotate-basic.test‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ def dict_set_item(d: Dict[str, str]) -> None:
185185
def startswith(x: str) -> bool:
186186
return x.startswith('foo')
187187

188-
def islower(x: str) -> bool:
189-
return x.islower() # A: Call non-native method "islower" (it may be defined in a non-native class, or decorated).
188+
def expandtabs(x: str) -> str:
189+
return x.expandtabs() # A: Call non-native method "expandtabs" (it may be defined in a non-native class, or decorated).
190190

191191
[case testAnnotateSpecificStdlibFeatures]
192192
import functools

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ 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 expandtabs(self) -> str: ...
130131
def startswith(self, x: Union[str, Tuple[str, ...]], start: int=..., end: int=...) -> bool: ...
131132
def endswith(self, x: Union[str, Tuple[str, ...]], start: int=..., end: int=...) -> bool: ...
132133
def replace(self, old: str, new: str, maxcount: int=...) -> str: ...
@@ -145,6 +146,8 @@ def isascii(self) -> bool: ...
145146
def isdecimal(self) -> bool: ...
146147
def isnumeric(self) -> bool: ...
147148
def isprintable(self) -> bool: ...
149+
def istitle(self) -> bool: ...
150+
def isupper(self) -> bool: ...
148151

149152
class float:
150153
def __init__(self, x: object) -> None: pass

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,39 @@ L0:
11031103
r0 = CPyStr_IsPrintable(x)
11041104
return r0
11051105

1106+
[case testStrIsLower]
1107+
def is_lower(x: str) -> bool:
1108+
return x.islower()
1109+
[out]
1110+
def is_lower(x):
1111+
x :: str
1112+
r0 :: bool
1113+
L0:
1114+
r0 = CPyStr_IsLower(x)
1115+
return r0
1116+
1117+
[case testStrIsTitle]
1118+
def is_title(x: str) -> bool:
1119+
return x.istitle()
1120+
[out]
1121+
def is_title(x):
1122+
x :: str
1123+
r0 :: bool
1124+
L0:
1125+
r0 = CPyStr_IsTitle(x)
1126+
return r0
1127+
1128+
[case testStrIsUpper]
1129+
def is_upper(x: str) -> bool:
1130+
return x.isupper()
1131+
[out]
1132+
def is_upper(x):
1133+
x :: str
1134+
r0 :: bool
1135+
L0:
1136+
r0 = CPyStr_IsUpper(x)
1137+
return r0
1138+
11061139
[case testStrIndexEqLiteral_64bit]
11071140
def is_comma(s: str, i: int) -> bool:
11081141
return s[i] == ","

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,103 @@ def test_str_subclass() -> None:
15711571
class S(str):
15721572
pass
15731573

1574+
[case testStrIsCased]
1575+
from typing import Any
1576+
1577+
def check(s: str) -> None:
1578+
# Any forces generic dispatch, so we compare our primitives against CPython's
1579+
a: Any = s
1580+
assert s.islower() == a.islower(), f"islower mismatch for {s!r}"
1581+
assert s.isupper() == a.isupper(), f"isupper mismatch for {s!r}"
1582+
assert s.istitle() == a.istitle(), f"istitle mismatch for {s!r}"
1583+
1584+
def test_all_code_points() -> None:
1585+
# Covers the ASCII, UCS-1, UCS-2 and UCS-4 string kinds. Check all BMP code points,
1586+
# and a sample of the others (checking all of them is slow).
1587+
for i in [*range(0x10000), *range(0x10000, 0x110000, 97), 0x10FFFF]:
1588+
c = chr(i)
1589+
check(c)
1590+
check(c + "a")
1591+
check("A" + c)
1592+
1593+
def test_strings() -> None:
1594+
samples = [
1595+
"", " ", "a", "A", "abc", "ABC", "Abc", "aBC", "abC", "123", "abc 123!", "ABC 123!",
1596+
"Hello World", "Hello world", "hello World", "HEllo", "Hello-World", "Hello World 123!",
1597+
"été", "Été", "ÉTÉ", "ÉtÉ",
1598+
"абв", "АБВ", "Абв Гд",
1599+
"DŽ", "Dž", "dž", "Dža", "DžA", "DŽDž", "aDž",
1600+
"中文", "abc中", "ABC中", "Abc中",
1601+
"\U00010400", "\U00010428", "abc\U00010428", "ABC\U00010400", "abc\U00010400",
1602+
"\U00010400\U00010428", "\U0001f600", "A\U0001f600",
1603+
]
1604+
for s in samples:
1605+
check(s)
1606+
check(s + " " + s)
1607+
1608+
def test_islower() -> None:
1609+
assert "abc".islower()
1610+
assert "abc 123!".islower()
1611+
assert not "".islower()
1612+
assert not "123".islower()
1613+
assert not "Abc".islower()
1614+
assert not "abC".islower()
1615+
assert "été".islower()
1616+
assert not "Été".islower()
1617+
assert "абв".islower()
1618+
assert not "Dža".islower() # Titlecase character
1619+
assert "abc\U00010428".islower()
1620+
assert not "abc\U00010400".islower()
1621+
assert not "中文".islower()
1622+
1623+
def test_isupper() -> None:
1624+
assert "ABC".isupper()
1625+
assert "ABC 123!".isupper()
1626+
assert not "".isupper()
1627+
assert not "123".isupper()
1628+
assert not "aBC".isupper()
1629+
assert not "ABc".isupper()
1630+
assert "ÉTÉ".isupper()
1631+
assert not "ÉtÉ".isupper()
1632+
assert "АБВ".isupper()
1633+
assert not "DžA".isupper() # Titlecase character
1634+
assert "ABC\U00010400".isupper()
1635+
assert not "ABC\U00010428".isupper()
1636+
assert not "中文".isupper()
1637+
1638+
def test_istitle() -> None:
1639+
assert "Hello".istitle()
1640+
assert "Hello World".istitle()
1641+
assert "Hello World 123!".istitle()
1642+
assert "A".istitle()
1643+
assert "Hello-World".istitle()
1644+
assert not "".istitle()
1645+
assert not "123".istitle()
1646+
assert not "hello".istitle()
1647+
assert not "Hello world".istitle()
1648+
assert not "HEllo".istitle()
1649+
assert not "HELLO".istitle()
1650+
assert "Été".istitle()
1651+
assert not "été".istitle()
1652+
assert "Абв Гд".istitle()
1653+
assert "Dž".istitle() # Titlecase character
1654+
assert "Dža".istitle()
1655+
assert not "DŽDž".istitle()
1656+
assert "\U00010400\U00010428".istitle()
1657+
assert not "\U00010428".istitle()
1658+
assert not "中文".istitle()
1659+
1660+
def test_str_subclass() -> None:
1661+
from strsubclass import S
1662+
assert S("abc").islower()
1663+
assert not S("abc").isupper()
1664+
assert not S("abc").istitle()
1665+
assert S("Abc").istitle()
1666+
1667+
[file strsubclass.py]
1668+
class S(str):
1669+
pass
1670+
15741671
[case testStrIndexEqLiteralSpecialize]
15751672
from testutil import assertRaises
15761673

0 commit comments

Comments
 (0)