-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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] Complete the string literals signature to match the String
one
#3438
Conversation
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
strip()
, lstrip()
, and rstrip()
for string literalsstrip()
, lstrip()
, and rstrip()
, isdigit()
for string literals
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
strip()
, lstrip()
, and rstrip()
, isdigit()
for string literalsstrip()
, lstrip()
, and rstrip()
, isdigit()
, isupper()
and islower()
for string literals
strip()
, lstrip()
, and rstrip()
, isdigit()
, isupper()
and islower()
for string literalsstrip()
, lstrip()
, and rstrip()
, isdigit()
, isupper()
, __iter__()
and islower()
for string literals
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
strip()
, lstrip()
, and rstrip()
, isdigit()
, isupper()
, __iter__()
and islower()
for string literalsString
one
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
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.
!sync
!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 nightly 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. |
…he `String` one (#47461) [External] [stdlib] Complete the string literals signature to match the `String` one To match the existing methods in both Mojo and Python strings. This could help bit a little with the transition of Python programmers playing with the REPL and small mojo examples that handle strings, finding it more familiar with Python E.g. it's annoying for them to start playing with Mojo with a file like this one: ```mojo fn main(): s = "Hello" for c in s: print(c) ``` And encounter an error like this one: ``` ❯ mojo t.mojo /tmp/t.mojo:3:12: error: 'StringLiteral' does not implement the '__iter__' method for c in s: ^ mojo: error: failed to parse the provided Mojo source module ``` ### New methods and features implemented in String literals - Implemented `.strip()`, `.lstrip()`, and `.rstrip()` - Implement `.isdigit()` - Implement `.islower()` and `.isupper()` - Implement `.__iter__()` - Implement indexing - Implement `.startswith()` and `.endswith()` - Implement `.rjust()`, `.ljust()` and `.center()` - Implement `.count()` - Implement `.split()` and `.splitlines()` ### Example ```mojo > mojo 1> x = "123".isdigit() (Bool) x = True 2> y = " hello ".strip() (String) y = "hello" 2> z = " hello ".lstrip() (String) z = "hello " 3> t = " hello ".rstrip() (String) t = " hello" 4> b1 = "HELLO".isupper() (Bool) b1 = True 5> b2 = "HELLO".islower() (Bool) b2 = False 6> s = "Hello" (StringLiteral) s = "Hello" │ 7> h = s[0] (String) x = "H" 8> b3 = s.startswith("He") (Bool) b3 = True 9> sl = s.ljust(10) 10> c = s.count("l") (Int) c = 2 11> first = "Hello world".split()[0] (String) first = "Hello" 12> for c in "HELLO": 12. print(c) 13. H E L L O ``` Co-authored-by: Manuel Saelices <msaelices@gmail.com> Closes #3438 MODULAR_ORIG_COMMIT_REV_ID: 0b1d12be1b0d7ea489c22343055f3b586ad6e0f4
Landed in f3c225f! Thank you for your contribution 🎉 |
To match the existing methods in both Mojo and Python strings.
This could help bit a little with the transition of Python programmers playing with the REPL and small mojo examples that handle strings, finding it more familiar with Python
E.g. it's annoying for them to start playing with Mojo with a file like this one:
And encounter an error like this one:
New methods and features implemented in String literals
.strip()
,.lstrip()
, and.rstrip()
.isdigit()
.islower()
and.isupper()
.__iter__()
.startswith()
and.endswith()
.rjust()
,.ljust()
and.center()
.count()
.split()
and.splitlines()
Example