Skip to content
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

Open
wants to merge 14 commits into
base: nightly
Choose a base branch
from

Conversation

msaelices
Copy link
Contributor

@msaelices msaelices commented Aug 31, 2024

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:

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
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  

@msaelices msaelices requested a review from a team as a code owner August 31, 2024 15:35
@msaelices msaelices changed the title [stdlib] Implemented strip(), lstrip(), and rstrip() for string literals [stdlib] Implemented strip(), lstrip(), and rstrip(), isdigit() for string literals Sep 1, 2024
@msaelices msaelices changed the title [stdlib] Implemented strip(), lstrip(), and rstrip(), isdigit() for string literals [stdlib] Implemented strip(), lstrip(), and rstrip(), isdigit(), isupper() and islower() for string literals Sep 1, 2024
@msaelices msaelices changed the title [stdlib] Implemented strip(), lstrip(), and rstrip(), isdigit(), isupper() and islower() for string literals [stdlib] Implemented strip(), lstrip(), and rstrip(), isdigit(), isupper(), __iter__() and islower() for string literals Sep 1, 2024
@msaelices msaelices changed the title [stdlib] Implemented strip(), lstrip(), and rstrip(), isdigit(), isupper(), __iter__() and islower() for string literals [stdlib] Complete the string literals signature to match the string one Sep 1, 2024
@msaelices msaelices changed the title [stdlib] Complete the string literals signature to match the string one [stdlib] Complete the string literals signature to match the String one Sep 1, 2024
Copy link
Collaborator

@JoeLoser JoeLoser left a comment

Choose a reason for hiding this comment

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

!sync

@JoeLoser
Copy link
Collaborator

!sync

@modularbot modularbot added the imported-internally Signals that a given pull request has been imported internally. label Sep 18, 2024
@modularbot
Copy link
Collaborator

✅🟣 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.

@modularbot modularbot added the merged-internally Indicates that this pull request has been merged internally label Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported-internally Signals that a given pull request has been imported internally. merged-internally Indicates that this pull request has been merged internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants