Skip to content

Commit 98566ea

Browse files
committed
std: Fix formatting flags for chars
This recently regressed in #24689, and this updates the `Display` implementation to take formatting flags into account. Closes #26625
1 parent 0b70378 commit 98566ea

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
@@ -980,7 +980,14 @@ impl Debug for char {
980980
#[stable(feature = "rust1", since = "1.0.0")]
981981
impl Display for char {
982982
fn fmt(&self, f: &mut Formatter) -> Result {
983-
f.write_char(*self)
983+
if f.width.is_none() && f.precision.is_none() {
984+
f.write_char(*self)
985+
} else {
986+
let mut utf8 = [0; 4];
987+
let amt = self.encode_utf8(&mut utf8).unwrap_or(0);
988+
let s: &str = unsafe { mem::transmute(&utf8[..amt]) };
989+
f.pad(s)
990+
}
984991
}
985992
}
986993

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)