Skip to content

[mypyc] Add primitives for str.isalpha, isascii, isdecimal, isnumeric and isprintable - #22404

Open
Jahongir-Qurbonov wants to merge 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-str-is-charclass
Open

Jahongir-Qurbonov wants to merge 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-str-is-charclass

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

Add primitives for str.isalpha(), str.isascii(), str.isdecimal(), str.isnumeric() and str.isprintable().

These all check whether every character of a string belongs to a character class, so they share a macro that has a fast path for ASCII strings and dispatches on the string kind otherwise (similar to the existing str.isdigit() primitive). str.isascii() is O(1) and is implemented as an inline function.

Tests compare the results against CPython for every code point, and for a set of multi-character strings covering all string kinds.

Micro-benchmark (2M iterations over 10 short strings, compiled code, generic method call through Any vs the new primitive, Python 3.13):

Method Before After
isalpha 0.373s 0.140s
isascii 0.334s 0.103s
isdecimal 0.350s 0.120s
isnumeric 0.368s 0.122s
isprintable 0.420s 0.170s

Part of mypyc/mypyc#644.

… and isprintable

These check whether every character of a string belongs to a character class.
They share a macro that uses a fast path for ASCII strings and dispatches on the
string kind otherwise. str.isascii() is O(1) and is implemented as an inline
function.

Part of mypyc/mypyc#644.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant