]> git.lizzy.rs Git - rust.git/commitdiff
std: Fix formatting flags for chars
authorAlex Crichton <alex@alexcrichton.com>
Wed, 1 Jul 2015 02:26:03 +0000 (19:26 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 1 Jul 2015 02:26:03 +0000 (19:26 -0700)
This recently regressed in #24689, and this updates the `Display` implementation
to take formatting flags into account.

Closes #26625

src/libcore/fmt/mod.rs
src/libcoretest/fmt/mod.rs

index cbbb186af7609fc7cc431e12d9e0c83065d9a1ad..343772c764f817b242c7856dd8b025c35cc2c949 100644 (file)
@@ -980,7 +980,14 @@ fn fmt(&self, f: &mut Formatter) -> Result {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl Display for char {
     fn fmt(&self, f: &mut Formatter) -> Result {
-        f.write_char(*self)
+        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)
+        }
     }
 }
 
index cdb9c38f027f729d83c99f7c8d44e1170859c34c..42872589bb01fa296dbe0d76ffb8e2575a6ef5e7 100644 (file)
@@ -16,4 +16,6 @@ fn test_format_flags() {
     // No residual flags left by pointer formatting
     let p = "".as_ptr();
     assert_eq!(format!("{:p} {:x}", p, 16), format!("{:p} 10", p));
+
+    assert_eq!(format!("{: >3}", 'a'), "  a");
 }