Skip to content

[mypyc] Add primitives for str.index and str.rindex - #22411

Merged
p-sawicki merged 3 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-str-index
Nov 16, 2026
Merged

p-sawicki merged 3 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-str-index

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

Add primitives for str.index() and str.rindex() with optional start and end arguments.

These reuse the existing str.find() / str.rfind() implementation (PyUnicode_Find) and raise ValueError("substring not found") if the substring is not found, matching CPython.

Tests cover all argument combinations, negative indices, empty substrings, non-ASCII strings, the error cases, and compare the results against the generic implementation (through Any) for many start/end combinations.

Micro-benchmark (compiled code, 3.2M calls, best of 15, before -> after, Python 3.13):

Case Before After
s.index(sub) 0.102s 0.052s
s.rindex(sub) 0.102s 0.057s
s.index(sub, start) 0.118s 0.058s
s.index(sub, start, end) 0.131s 0.058s

The not found case is dominated by exception handling and is roughly unchanged.

Part of mypyc/mypyc#644.

@Jahongir-Qurbonov Jahongir-Qurbonov mentioned this pull request Nov 10, 2026
13 of 14 tasks

@p-sawicki p-sawicki left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@p-sawicki
p-sawicki merged commit 17f7a85 into python:master Nov 16, 2026
19 checks passed
@Jahongir-Qurbonov
Jahongir-Qurbonov deleted the mypyc-str-index branch November 16, 2026 18:01
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.

2 participants