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

Closed
wants to merge 14 commits into 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  

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
@msaelices msaelices requested a review from a team as a code owner August 31, 2024 15:35
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
@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
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
@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
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
@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
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>
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 merged-internally Indicates that this pull request has been merged internally merged-externally Merged externally in public mojo repo labels Sep 19, 2024
modularbot pushed a commit that referenced this pull request Sep 21, 2024
…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
@modularbot
Copy link
Collaborator

Landed in f3c225f! Thank you for your contribution 🎉

@modularbot modularbot closed this Sep 21, 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-externally Merged externally in public mojo repo 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