-
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] Allow !r
conversion flag in String.format
#3279
Conversation
Signed-off-by: Joshua James Venter <[email protected]>
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.
Great work! Thanks for working on this. FYI @bethebunny as you were asking about this.
Signed-off-by: Joe Loser <[email protected]>
!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. |
Landed in f13ddcf! Thank you for your contribution 🎉 |
…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
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'") |
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.
Realised this is duplicate before the merge. See #3302
[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
…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
[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
This addresses Issue #3267.
!s
and!r
conversion flags in format strings._FormatCurlyEntry
struct to include aconversion_flag
field.create_entries
method to correctly parse and store conversion flags.str
orrepr
) based on the flag.Additionally, I was conscious of potential future support of
!a
flag andformat_spec
, which is not currently supported. The additions of this PR should be direct to modify for this support. Hence whysupported_conversion_flags
was added, and a TODO was left in the code for the latter.For reference, see Python's Format String Syntax: