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] Allow !r conversion flag in String.format #3279

Closed
wants to merge 2 commits into from

Conversation

jjvraw
Copy link
Contributor

@jjvraw jjvraw commented Jul 21, 2024

This addresses Issue #3267.

  • Implemented parsing and handling of !s and !r conversion flags in format strings.
  • Updated the _FormatCurlyEntry struct to include a conversion_flag field.
  • Modified the create_entries method to correctly parse and store conversion flags.
  • Updated the string formatting logic to apply the appropriate conversion (str or repr) based on the flag.

Additionally, I was conscious of potential future support of !a flag and format_spec , which is not currently supported. The additions of this PR should be direct to modify for this support. Hence why supported_conversion_flags was added, and a TODO was left in the code for the latter.

For reference, see Python's Format String Syntax:

replacement_field ::=  "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name        ::=  arg_name ("." attribute_name | "[" element_index "]")*
arg_name          ::=  [identifier | digit ]
attribute_name    ::=  identifier
element_index     ::=  digit  | index_string
index_string      ::=  <any source character except "]">  
conversion        ::=  "r" | "s" | "a"
format_spec       ::=  <see Python docs for details>

@jjvraw jjvraw requested a review from a team as a code owner July 21, 2024 18:19
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.

Great work! Thanks for working on this. FYI @bethebunny as you were asking about this.

docs/changelog.md Outdated Show resolved Hide resolved
Signed-off-by: Joe Loser <[email protected]>
@JoeLoser
Copy link
Collaborator

!sync

@modularbot modularbot added the imported-internally Signals that a given pull request has been imported internally. label Jul 23, 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 Jul 23, 2024
@modularbot
Copy link
Collaborator

Landed in f13ddcf! Thank you for your contribution 🎉

modularbot added a commit that referenced this pull request Jul 24, 2024
…3914)

[External] [stdlib] Allow `!r` conversion flag in `String.format`

This addresses #3267.

- Implemented parsing and handling of `!s` and `!r` conversion flags in
format strings.
- Updated the `_FormatCurlyEntry` struct to include a `conversion_flag`
field.
- Modified the `create_entries` method to correctly parse and store
conversion flags.
- Updated the string formatting logic to apply the appropriate
conversion (`str` or `repr`) based on the flag.

Additionally, I was conscious of potential future support of `!a` flag
and `format_spec` , which is not currently supported. The additions of
this PR should be direct to modify for this support. Hence why
`supported_conversion_flags` was added, and a TODO was left in the code
for the latter.

For reference, see [Python's Format String
Syntax](https://docs.python.org/3/library/string.html#format-string-syntax):
```
replacement_field ::=  "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name        ::=  arg_name ("." attribute_name | "[" element_index "]")*
arg_name          ::=  [identifier | digit ]
attribute_name    ::=  identifier
element_index     ::=  digit  | index_string
index_string      ::=  <any source character except "]">  
conversion        ::=  "r" | "s" | "a"
format_spec       ::=  <see Python docs for details>
```

ORIGINAL_AUTHOR=Joshua James Venter
<67124214 [email protected]>
PUBLIC_PR_LINK=#3279

Co-authored-by: Joshua James Venter <67124214 [email protected]>
Closes #3279
MODULAR_ORIG_COMMIT_REV_ID: 9ee92a55ac683be22f7b93012f37c6980f7110b6
@modularbot modularbot closed this Jul 24, 2024
jjvraw added a commit to jjvraw/mojo that referenced this pull request Jul 24, 2024
Signed-off-by: Joshua James Venter <[email protected]>
in String("{0!r} {3} {1!r}").format(a, b, c, d)
)

assert_equal(String("{0!s} {0!r}").format(a), "Mojo 'Mojo'")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Realised this is duplicate before the merge. See #3302

modularbot pushed a commit that referenced this pull request Jul 25, 2024
[External] [stdlib] Remove duplicate test from PR
#3279

Also on line `1468`.

Co-authored-by: Joshua James Venter <[email protected]>
Closes #3302
MODULAR_ORIG_COMMIT_REV_ID: 88a7c8b72c6688b55a481f7135a783446b2815a3
modularbot added a commit that referenced this pull request Sep 13, 2024
…3914)

[External] [stdlib] Allow `!r` conversion flag in `String.format`

This addresses #3267.

- Implemented parsing and handling of `!s` and `!r` conversion flags in
format strings.
- Updated the `_FormatCurlyEntry` struct to include a `conversion_flag`
field.
- Modified the `create_entries` method to correctly parse and store
conversion flags.
- Updated the string formatting logic to apply the appropriate
conversion (`str` or `repr`) based on the flag.

Additionally, I was conscious of potential future support of `!a` flag
and `format_spec` , which is not currently supported. The additions of
this PR should be direct to modify for this support. Hence why
`supported_conversion_flags` was added, and a TODO was left in the code
for the latter.

For reference, see [Python's Format String
Syntax](https://docs.python.org/3/library/string.html#format-string-syntax):
```
replacement_field ::=  "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name        ::=  arg_name ("." attribute_name | "[" element_index "]")*
arg_name          ::=  [identifier | digit ]
attribute_name    ::=  identifier
element_index     ::=  digit  | index_string
index_string      ::=  <any source character except "]">  
conversion        ::=  "r" | "s" | "a"
format_spec       ::=  <see Python docs for details>
```

ORIGINAL_AUTHOR=Joshua James Venter
<67124214 [email protected]>
PUBLIC_PR_LINK=#3279

Co-authored-by: Joshua James Venter <67124214 [email protected]>
Closes #3279
MODULAR_ORIG_COMMIT_REV_ID: 9ee92a55ac683be22f7b93012f37c6980f7110b6
modularbot pushed a commit that referenced this pull request Sep 13, 2024
[External] [stdlib] Remove duplicate test from PR
#3279

Also on line `1468`.

Co-authored-by: Joshua James Venter <[email protected]>
Closes #3302
MODULAR_ORIG_COMMIT_REV_ID: 88a7c8b72c6688b55a481f7135a783446b2815a3
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