Skip to content

Commit 1fc0f68

Browse files
committed
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
2 parents bf3c979 + 98566ea commit 1fc0f68

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/libcore/fmt/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,14 @@ impl Debug for char {
983983
#[stable(feature = "rust1", since = "1.0.0")]
984984
impl Display for char {
985985
fn fmt(&self, f: &mut Formatter) -> Result {
986-
f.write_char(*self)
986+
if f.width.is_none() && f.precision.is_none() {
987+
f.write_char(*self)
988+
} else {
989+
let mut utf8 = [0; 4];
990+
let amt = self.encode_utf8(&mut utf8).unwrap_or(0);
991+
let s: &str = unsafe { mem::transmute(&utf8[..amt]) };
992+
f.pad(s)
993+
}
987994
}
988995
}
989996

src/libcoretest/fmt/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ fn test_format_flags() {
1616
// No residual flags left by pointer formatting
1717
let p = "".as_ptr();
1818
assert_eq!(format!("{:p} {:x}", p, 16), format!("{:p} 10", p));
19+
20+
assert_eq!(format!("{: >3}", 'a'), " a");
1921
}

0 commit comments

Comments
 (0)