[stdlib] Require explicit byte= keyword for String/StringSlice span indexing#6152
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
There was a problem hiding this comment.
Pull request overview
This PR addresses #6024 by making byte-based slicing of String / StringSlice explicit, preventing accidental UTF-8-invalid slicing via implicit byte indexing.
Changes:
- Make
StringSlice.__getitem__span slicing keyword-only ([byte=...]) to disallow implicit byte slicing. - Update
String.__getitem__span slicing to forward toStringSliceusing the explicitbyte=keyword. - Update stdlib call sites and tests to use
[byte=...]where byte-based slicing is intended.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| mojo/stdlib/test/collections/string/test_string_unicode_panic.mojo | Updates a unicode boundary panic test to use explicit byte slicing. |
| mojo/stdlib/test/collections/string/test_string_slice.mojo | Updates StringSlice byte-slicing tests to use [byte=...]. |
| mojo/stdlib/test/collections/string/test_string.mojo | Updates String slicing/indexing tests to use explicit byte slicing. |
| mojo/stdlib/test/builtin/test_string_literal.mojo | Updates comparisons that slice StringSlice inputs to use [byte=...]. |
| mojo/stdlib/test/builtin/test_print.mojo | Updates output trimming/prefix checks to use explicit byte slicing. |
| mojo/stdlib/test/builtin/test_file.mojo | Updates exception-message prefix extraction to use explicit byte slicing. |
| mojo/stdlib/std/python/python_object.mojo | Updates inplace-method name construction to slice via [byte=2:]. |
| mojo/stdlib/std/pathlib/path.mojo | Updates path substring extraction to use explicit byte slicing. |
| mojo/stdlib/std/os/path/path.mojo | Updates root/suffix splitting logic to use explicit byte slicing. |
| mojo/stdlib/std/collections/string/string_slice.mojo | Makes span slicing keyword-only and updates docs/examples accordingly. |
| mojo/stdlib/std/collections/string/string.mojo | Makes span slicing keyword-only and forwards to StringSlice via byte=. |
| mojo/stdlib/std/collections/string/_parsing_numbers/parsing_floats.mojo | Updates sign stripping to use explicit byte slicing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
…ndexing Fixes modular#6024 — String/StringSlice slice syntax like s[0:1] silently did byte-level indexing. Now requires s[byte=0:1] to make byte-based slicing explicit. Updated all stdlib callers and tests.
f82cb02 to
e535b76
Compare
barcharcraz
left a comment
There was a problem hiding this comment.
This looks good, this is the second PR to do this (actually the third, I had one internally). It's about time we got one merged. I'll have to migrate the rest of max internally so expect some additional changes.
|
!sync |
|
✅🟣 This contribution has been merged 🟣✅ Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the main branch during the next Mojo nightly release, typically within the next 24-48 hours. We use Copybara to merge external contributions, click here to learn more. |
|
Landed in b90e36f! Thank you for your contribution 🎉 |
…an indexing
Fixes #6024 — String/StringSlice slice syntax like s[0:1] silently did byte-level indexing. Now requires s[byte=0:1] to make byte-based slicing explicit. Updated all stdlib callers and tests.