Skip to content

Fix f-string padding #4361

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

Merged
merged 3 commits into from
Jan 5, 2023
Merged

Fix f-string padding #4361

merged 3 commits into from
Jan 5, 2023

Conversation

yt2b
Copy link
Contributor

@yt2b yt2b commented Dec 25, 2022

If multibyte character exists in string, f-string padding doesn't work correctly.
For example:

f'{"🐍":4}'

On CPython, The result of this code is below.

'🐍   '

But on RustPython, The Result is different.

'🐍'

I've fixed the character counting method.
With this fix, the following tests now pass.

  • test_non_ascii_identifiers
  • test_unicode

vm/src/format.rs Outdated
@@ -535,8 +535,7 @@ impl FormatSpec {
) -> Result<String, &'static str> {
let align = self.align.unwrap_or(default_align);

// Use the byte length as the string length since we're in ascii
let num_chars = magnitude_string.len();
let num_chars = magnitude_string.chars().count();
Copy link
Member

Choose a reason for hiding this comment

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

ah, this is not the best. probably we'd better to pass StringKind here to not to count characters for ascii strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Code fix completed.

@youknowone
Copy link
Member

I added a commit. I am thinking adding a struct including (&str, PyStrKindData) and use them to propagate the data. this is not the version but proof of concept how it will work.

Co-Authored-By: yt2b <reinventing.the.wheels@gmail.com>
@youknowone youknowone merged commit 2f94c07 into RustPython:main Jan 5, 2023
@yt2b yt2b deleted the fix_fstring branch January 5, 2023 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants