-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
py/obstr: Support tuples as pre/suffix in startswith() and endswith(). #16812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
py/obstr: Support tuples as pre/suffix in startswith() and endswith(). #16812
Conversation
Code size report:
|
Thanks, this is something I've wanted for a while but was afraid it would be a big feature. It's quite impressive how small the code size change is here! You will need to get the CI passing, I think just remove the |
Ta. It got smaller once I realised
Done. I'm still getting failures on |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #16812 +/- ##
=======================================
Coverage 98.54% 98.54%
=======================================
Files 169 169
Lines 21855 21864 +9
=======================================
+ Hits 21536 21545 +9
Misses 319 319 ☔ View full report in Codecov by Sentry. |
CI now passing. |
I just pushed a commit to do this and there is a tiny overall code size reduction (compared to
Let me know if you prefer to back this out. If you'd like to keep it, I can squash the separate commits. |
db2eaf4
to
d81c594
Compare
This is great to see!! This should enable MicroPython code to pass Once this has been merged, you might also consider adding |
|
@glenn20 I like the additional commit which refactors code using |
667d942
to
8ed4384
Compare
Commits split, reordered, squashed and rebased. |
py/objstr.c
Outdated
@@ -748,23 +768,11 @@ static mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, b | |||
|
|||
GET_STR_DATA_LEN(args[0], haystack, haystack_len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haystack_len
is no longer used, so this could be maybe replaced with something slightly smaller/efficient?
Also, is haystack
really needed now? I guess it is to compute the correct offset for return value.
8ed4384
to
a815856
Compare
This change allows tuples to be passed as the prefix/suffix argument to the `str.startswith()` and `str.endswith()` methods. The methods will return `True` if the string starts/ends with any of the prefixes/suffixes in the tuple. Also adds full support for the `start` and `end` arguments to both methods for compatibility with CPython. Tests have been updated for the new behaviour. Signed-off-by: Glenn Moloney <glenn.moloney@gmail.com>
a815856
to
eb45d97
Compare
Very good, nice enhancement, thank you! |
This PR adds support for passing tuples as the prefix/suffix argument to the
str.startswith()
andstr.endswith()
methods. This improves compatibility with CPython.The methods will return
True
if the string starts/ends with any of the prefixes/suffixes in the tuple.The PR also adds uniform support for the optional
start
andend
arguments ofstr.startswith()
andstr.endswith()
.Taken together, these provide full compatibility with CPython
str.startswith()
andstr.endswith()
methods (see python docs).The new signatures of the methods are:
Testing
The tests in
tests/basics/string_{starts,ends}with.py
have been extended to test the new features. These tests are passing on the unix port.Tradeoffs
This PR should be fully backward compatible with existing python code running on micropython.
While improving compliance wih cpython, this PR will provide a small increase in code size. This may be mitigated by reusing the new
objstr.c:get_substring_data()
function to reduce code size elsewhere (eg.objstr.c:str_count()
andobjstr.c:str_finder()
).