-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[stdlib] [Feature Request] Refactor String
to not have so many raises and use Optional
#2686
Comments
String
to not have so many raises and use OptionalString
to not have so many raises and use Optional
For Python interop |
understood. how does this look ? fn split(self, owned delimiter: String = " ") -> List[String]:
"""Split the string by a delimiter.
Args:
delimiter: The string to split on.
Returns:
A List of Strings containing the input split by the delimiter.
"""
if not delimiter:
delimiter = " "
... |
Whitespace could be more than just spaces. I think all of these count as whitespace:
Also, note that, for example, EDIT: There's also File Separator, Group Separator, Record Separator, & Unit Separator. |
wow I had no idea. The current implementation doesn't take that into account, will try to imitate python. But this will absolutely kill performance lol. |
@martinvuyk Sorry I didn't notice you already had a PR making changes to the I've tried to address these differences and added a missing |
please close that PR I'm currently working on that also there are some characters missing still in String.WHITESPACE to have Python's behaviour which I'm also adressing. And I'm also trying to document this thoroughly and add more tests. Also what do you think of putting the maxsplit in params? at least that way the compiled code wont have so many branches. Iit'll be less pythonic but I think python devs will manage (?). fn split[
maxsplit: Int = -1
](self, owned delimiter: String = "") -> List[String]:
... |
… behavior to be pythonic (#40714) [External] [stdlib] Make `String.split()` default to whitespace & fix behavior to be pythonic Closes #2686 `String.split()` now defaults to whitespace and has pythonic behavior in that it removes all adjacent whitespaces by default. ORIGINAL_AUTHOR=martinvuyk <110240700+martinvuyk@users.noreply.github.com> PUBLIC_PR_LINK=#2711 --------- Co-authored-by: martinvuyk <110240700+martinvuyk@users.noreply.github.com> Closes #2711 MODULAR_ORIG_COMMIT_REV_ID: b6a05e97f8de09a2c77b272b6cd8b96da3c5c782
Review Mojo's priorities
What is your request?
As title.
What is your motivation for this change?
Could have a
fn () raising -> Int
and anfn () -> Optional[Int]
This could just return a list with one item, it being the whole String.
if not delimiter: return List[String](self)
This will most certainly always be checked against anyway with
if index == -1: ...
so it could also be made an Optional[Int]Any other details?
No response
The text was updated successfully, but these errors were encountered: