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

Formatting of chars with fixed width was broken? #26625

Closed
askobara opened this Issue Jun 27, 2015 · 4 comments

Comments

Projects
None yet
5 participants
@askobara
Copy link

askobara commented Jun 27, 2015

Current code gives different results on stable and nightly branches

fn main() {
    println!("{: >5} | {: >5}", 112, 0);
    println!("{: >5} | {: >5}", '.', 0);
}

Here is the stable:

  112 |     0
    . |     0

And here is the nightly:

  112 |     0
. |     0
@eefriedman

This comment has been minimized.

Copy link
Contributor

eefriedman commented Jun 27, 2015

Ooh, that's nasty... first guess is a regression from https://github.com/rust-lang/rust/pull/24689/files . @SimonSapin?

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented Jun 29, 2015

Untested fix:

impl Display for char {
    fn fmt(&self, f: &mut Formatter) -> Result {
        if f.width.is_none() && f.precision.is_none() {
            f.write_char(*self)
        } else {
            let mut utf8 = [0; 4];
            let amt = self.encode_utf8(&mut utf8).unwrap_or(0);
            let s: &str = unsafe { mem::transmute(&utf8[..amt]) };
            f.pad(s)
        }
    }
}
@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Jun 30, 2015

triage: P-high

We're also likely to backport the fix to beta. Note that this is not broken on 1.1.0 stable but it is broken on 1.2.0.

@rust-highfive rust-highfive added P-high and removed I-nominated labels Jun 30, 2015

alexcrichton added a commit to alexcrichton/rust that referenced this issue Jul 1, 2015

std: Fix formatting flags for chars
This recently regressed in rust-lang#24689, and this updates the `Display` implementation
to take formatting flags into account.

Closes rust-lang#26625

bors added a commit that referenced this issue Jul 1, 2015

Auto merge of #26698 - alexcrichton:char-fmt, r=huonw
This recently regressed in #24689, and this updates the `Display` implementation
to take formatting flags into account.

Closes #26625

@bors bors closed this in #26698 Jul 1, 2015

alexcrichton added a commit to alexcrichton/rust that referenced this issue Jul 8, 2015

std: Fix formatting flags for chars
This recently regressed in rust-lang#24689, and this updates the `Display` implementation
to take formatting flags into account.

Closes rust-lang#26625

jroesch added a commit to jroesch/rust that referenced this issue Jul 21, 2015

std: Fix formatting flags for chars
This recently regressed in rust-lang#24689, and this updates the `Display` implementation
to take formatting flags into account.

Closes rust-lang#26625

thepowersgang added a commit to thepowersgang/rust that referenced this issue Jul 25, 2015

std: Fix formatting flags for chars
This recently regressed in rust-lang#24689, and this updates the `Display` implementation
to take formatting flags into account.

Closes rust-lang#26625
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.